Skip to content

Commit

Permalink
Correct vertexCount to indexCount, implement an actual vertexCount
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Jun 11, 2024
1 parent 3070799 commit 79a7136
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ammonite/graphics/renderCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ namespace ammonite {
glBindVertexArray(drawObjectData->meshes[i].vertexArrayId);

//Draw the triangles
glDrawElements(mode, drawObjectData->meshes[i].vertexCount, GL_UNSIGNED_INT, nullptr);
glDrawElements(mode, drawObjectData->meshes[i].indexCount, GL_UNSIGNED_INT, nullptr);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ammonite/models/internal/modelTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace ammonite {

struct MeshData {
VertexData* meshData = nullptr;
int meshDataLength = 0;
unsigned int* indices = nullptr;
int vertexCount = 0;
unsigned int* indices = nullptr;
int indexCount = 0;
GLuint vertexBufferId = 0; //vertexBufferId and elementBufferId must stay in this memory layout
GLuint elementBufferId = 0;
GLuint vertexArrayId = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/ammonite/models/modelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ammonite {

//Add a new empty mesh to the mesh vector
models::internal::MeshData* newMesh = &modelObjectData->meshes.emplace_back();
newMesh->meshDataLength = meshPtr->mNumVertices;
newMesh->vertexCount = meshPtr->mNumVertices;
newMesh->meshData = new VertexData[meshPtr->mNumVertices];

//Fill the mesh with vertex data
Expand Down Expand Up @@ -53,11 +53,11 @@ namespace ammonite {

//Count mesh indices
for (unsigned int i = 0; i < meshPtr->mNumFaces; i++) {
newMesh->vertexCount += meshPtr->mFaces[i].mNumIndices;
newMesh->indexCount += meshPtr->mFaces[i].mNumIndices;
}

//Allocate and fill mesh indices
newMesh->indices = new unsigned int[newMesh->vertexCount];
newMesh->indices = new unsigned int[newMesh->indexCount];
int index = 0;
for (unsigned int i = 0; i < meshPtr->mNumFaces; i++) {
aiFace face = meshPtr->mFaces[i];
Expand Down
1 change: 1 addition & 0 deletions src/ammonite/models/modelStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace ammonite {
bool* externalSuccess);
void applyTexture(int modelId, AmmoniteEnum textureType, const char* texturePath,
bool srgbTexture, bool* externalSuccess);
int getIndexCount(int modelId);
int getVertexCount(int modelId);
void setDrawMode(int modelId, AmmoniteEnum drawMode);
}
Expand Down
20 changes: 18 additions & 2 deletions src/ammonite/models/models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ namespace ammonite {

//Fill interleaved vertex + normal + texture buffer and index buffer
glNamedBufferData(meshData->vertexBufferId,
meshData->meshDataLength * sizeof(models::internal::VertexData),
meshData->vertexCount * sizeof(models::internal::VertexData),
&meshData->meshData[0], GL_STATIC_DRAW);
glNamedBufferData(meshData->elementBufferId,
meshData->vertexCount * sizeof(unsigned int),
meshData->indexCount * sizeof(unsigned int),
&meshData->indices[0], GL_STATIC_DRAW);

//Destroy mesh data early
Expand Down Expand Up @@ -456,6 +456,22 @@ namespace ammonite {
applyTexture(modelId, textureType, texturePath, ASSUME_SRGB_TEXTURES, externalSuccess);
}

//Return the number of indices on a model
int getIndexCount(int modelId) {
internal::ModelInfo* modelPtr = modelIdPtrMap[modelId];
if (modelPtr == nullptr) {
return 0;
}

int indexCount = 0;
models::internal::ModelData* modelData = modelPtr->modelData;
for (unsigned int i = 0; i < modelData->meshes.size(); i++) {
indexCount += modelData->meshes[i].indexCount;
}

return indexCount;
}

//Return the number of vertices on a model
int getVertexCount(int modelId) {
internal::ModelInfo* modelPtr = modelIdPtrMap[modelId];
Expand Down

0 comments on commit 79a7136

Please sign in to comment.