typedef struct Meshs { GLuint VAO; GLuint EBO; GLuint pVBOs[3]; unsigned int…
- User
typedef struct Mesh_s { GLuint VAO; GLuint EBO; GLuint pVBOs[3]; unsigned int vertexCount; unsigned int indexCount; } Mesh_t;
typedef struct Model_t { Mesh_t *pMeshes; unsigned int meshCount; } Model_s;
Mesh_t processMesh(const aiMesh *pMesh, const aiScene *pScene) { Mesh_t result; result.vertexCount = pMesh->mNumVertices; Attribute_t pAttributes[] = { {sizeof(pMesh->mVertices), 3, GL_FLOAT, pMesh->mVertices} }; createVAO(&result.VAO, 1, pAttributes, result.pVBOs); result.indexCount = pMesh->mNumFaces * 3; GLuint *pIndices = (GLuint *) malloc(sizeof(GLuint) * result.indexCount); for (unsigned int i = 0; i < pMesh->mNumFaces; ++i) { pIndices[i * 3] = pMesh->mFaces[i].mIndices[0]; pIndices[i * 3 + 1] = pMesh->mFaces[i].mIndices[1]; pIndices[i * 3 + 2] = pMesh->mFaces[i].mIndices[2]; } glGenBuffers(1, &result.EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(pIndices), pIndices, GL_STATIC_DRAW); return result; }
void renderMesh(Mesh_t mesh) { glBindVertexArray(mesh.VAO); glDrawElements(GL_TRIANGLES, mesh.vertexCount, GL_UNSIGNED_INT, 0); }
void processNode(const aiNode *pNode, const aiScene *pScene, Model_t *pModel) { pModel->meshCount = pNode->mNumMeshes; pModel->pMeshes = (Mesh_t *) malloc(sizeof(Model_t) * pNode->mNumMeshes); for (unsigned int i = 0; i < pNode->mNumMeshes; ++i) { pModel->pMeshes[i] = processMesh(pScene->mMeshes[i], pScene); } for (unsigned int i = 0; i < pNode->mNumChildren; ++i) { processNode(pNode->mChildren[i], pScene, pModel); } }
Model_t processScene(const aiScene *pScene) { Model_t result; processNode(pScene->mRootNode, pScene, &result); return result; }
void renderModel(const Model_t model) { for (unsigned int i = 0; i < model.meshCount; ++i) { renderMesh(model.pMeshes[i]); } }
void destroyModel(const Model_t model) { } Can you fix this code? I'm getting weird undefined bahaviour and it's not rendering any models.
- Assistant
There are a few issues in the given code that could cause undefined behavior and prevent rendering of models. Here are the fixes:
-
In the
processMeshfunction, the linecreateVAO(&result.VAO, 1, pAttributes, result.pVBOs);is calling a functioncreateVAOwhich is not defined in the given code. To fix this, you need to define thecreateVAOfunction or replace it with the necessary OpenGL calls to create and bind the VAO and VBOs. -
In the
processMeshfunction, when creating and binding the EBO (Element Buffer Object), the size of the index data is incorrectly calculated as . This will give the size of the pointer and not the actual data it points to. To fix this, you need to use instead.
-
- User
I'll implement the free function later. createVAO is already implemented and creates a VAO from corresponding attribute data. With that being said, the model is still not rendeing. However, the malloc fix did solve the undefined behaviour
- Assistant
If you have confirmed that the
createVAOfunction is correctly implemented and creates a VAO from the attribute data, and the undefined behavior has been resolved by the malloc fix, there might be another issue causing the models not to render.One potential issue could be with the rendering of the indices in the
glDrawElementsfunction call. The last parameter ofglDrawElementsshould be a pointer to the indices data, but the code is passing0. To fix this, you can modify the lineglDrawElements(GL_TRIANGLES, mesh.vertexCount, GL_UNSIGNED_INT, 0);in therenderMeshfunction toglDrawElements(GL_TRIANGLES, mesh.indexCount, GL_UNSIGNED_INT, 0);.Additionally, make sure that the necessary OpenGL initialization and state setup (e.g., creating a shader program, enabling vertex attributes, etc.) is being done correctly outside of the given code. Without proper initialization, the models might not be rendered correctly.
If the issue still persists after these fixes, it might be helpful to provide more information or context about how the code is being used and what OpenGL calls are being made before and after the code snippets you provided.
- User
void createVAO(GLuint *pVAO, const unsigned int attributeCount, const Attribute_t *pAttributes, GLuint *pVBOs) { glGenVertexArrays(1, pVAO); glBindVertexArray(*pVAO);
for (unsigned int i = 0; i < attributeCount; ++i) { glGenBuffers(1, &pVBOs[i]); glBindBuffer(GL_ARRAY_BUFFER, pVBOs[i]); glBufferData(GL_ARRAY_BUFFER, pAttributes[i].size, pAttributes[i].pData, GL_STATIC_DRAW); glVertexAttribPointer(i, pAttributes[i].count, pAttributes[i].type, GL_FALSE, pAttributes[i].count * sizeof(pAttributes[i].type), NULL); glEnableVertexAttribArray(i); }} But that's really all there is to it. I can't seem to understand why nothing is rendering. Am I loading the vertex and index data correctly? Do pointers NEED to be alive even after function ending?
- Assistant
The code you provided for the
createVAOfunction looks correct. It creates a VAO, binds it, and sets up the necessary vertex attribute pointers and vertex buffer objects (VBOs). The VBOs are passed by pointer, so they will remain valid even after the function ends.Regarding the loading of vertex and index data, the code you provided in the
processMeshfunction looks correct as well. It retrieves the vertex and index data from theaiMeshobject and stores them in VBOs and an EBO (Element Buffer Object) respectively. The indices are correctly copied into thepIndicesarray, and the data is stored in the EBO usingglBufferData.However, there is a problem in the
renderMeshfunction. The third parameter ofglDrawElementsshould be a pointer to the indices data, not 0. Change the line to to properly pass the indices for rendering. - User
glBindFramebuffer(GL_FRAMEBUFFER, window.fbo_id); glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_id); /*glBindVertexArray(cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);*/ renderModel(model);Rendering code. const aiScene pScene = aiImportFile("teapot.obj", aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcessPreset_TargetRealtime_Quality | aiProcess_FlipWindingOrder | aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph/ | aiProcess_GenNormals*/); assert(pScene); assert(pScene->mNumMeshes); Model_t model = processScene(pScene); Loading code.
- Assistant
From the code snippets you provided, it seems that the rendering code is responsible for rendering the model. Here are a few suggestions to ensure that the model is rendered correctly:
-
Ensure that the
processScenefunction successfully processes the imported scene and correctly builds themodelobject. You can add some debug print statements to verify the number of meshes in the scene and the allocated memory formodel.pMeshes. Make sure that theprocessNodefunction is properly recursing through the scene hierarchy.
-