I'm currently trying to import a Mesh using Assimp. For some reason, it's displayed incorrectly. I'm trying to load Suzanne:
This is a screenshot from Open3D Model Viewer - which also uses Assimp. That means the model file is correct. Now when I try to load it, it looks like this (rendered in Wireframe):
I've been trying lots of things which is why I reduced the code to the minimum (no normals, texcoords, etc.) and why I'm not using any of the classes I have (except for the Shader classes, which are extensively tested and fine):
auto pScene = importer.ReadFile("../assets/models/suzanne/suzanne.obj",
aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType
);
//aiProcess_GenNormals);
// Meshes
for (size_t stMeshIndex = 0; stMeshIndex < pScene->mNumMeshes; stMeshIndex++)
{
auto pMeshData = pScene->mMeshes[stMeshIndex];
// Vertices
std::vector<float> vertices;
for (size_t stIndex = 0; stIndex < pMeshData->mNumVertices; stIndex ++)
{
vertices.push_back(pMeshData->mVertices[stIndex].x);
vertices.push_back(pMeshData->mVertices[stIndex].y);
vertices.push_back(pMeshData->mVertices[stIndex].z);
}
// Indices
std::vector<uint32_t> indices;
for (size_t stIndex = 0; stIndex < pMeshData->mNumFaces; stIndex++)
{
indices.push_back(pMeshData->mFaces[stIndex].mIndices[0]);
indices.push_back(pMeshData->mFaces[stIndex].mIndices[1]);
indices.push_back(pMeshData->mFaces[stIndex].mIndices[2]);
}
GLuint gl_vao;
GLuint gl_vbo;
GLuint gl_ibo;
// Vertex Array
glGenVertexArrays(1, &gl_vao);
glGenBuffers(1, &gl_vbo);
glGenBuffers(1, &gl_ibo);
// Shader
auto pVertexShader = pRenderer->CreateShader(ShaderType::Vertex);
if (!pVertexShader->CompileFromFile("shader/default_simple.vs"))
return;
auto pFragmentShader = pRenderer->CreateShader(ShaderType::Fragment);
if (!pFragmentShader->CompileFromFile("shader/default.fs"))
return;
auto pShaderProgram = pRenderer->CreateShaderProgram();
pShaderProgram->AttachShader(pVertexShader);
pShaderProgram->AttachShader(pFragmentShader);
if (!pShaderProgram->Link())
return;
glm::vec2 viewportSize = { 800,600 };// pRenderer->GetContext()->GetViewportSize();
auto matProjection = glm::perspective(
glm::radians(45.0f),
viewportSize.x / viewportSize.y,
0.1f,
1000.0f);
auto matView = glm::lookAt(glm::vec3(3, 3, 0),
glm::vec3(),
glm::vec3(0, 1, 0));
auto matModel = glm::mat4(1.0f);
auto matMVP = matProjection * matView * matModel;
pShaderProgram->Use();
pShaderProgram->GetUniform(ShaderUniformRole::TransformMVP)->SetValue(matMVP);
glBindBuffer(GL_ARRAY_BUFFER, gl_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * pMeshData->mNumVertices, &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * pMeshData->mNumFaces * 3, &indices[0], GL_STATIC_DRAW);
glBindVertexArray(gl_vao);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (GLvoid*)0);
while (!pRenderer->GetContext()->GetWindow()->ShouldClose())
{
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pShaderProgram->Use();
glBindVertexArray(gl_vao);
glBindBuffer(GL_ARRAY_BUFFER, gl_vbo);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Wireframe
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_ibo);
glDrawElements(GL_TRIANGLES, pMeshData->mNumFaces * 3, GL_UNSIGNED_INT, 0);
pRenderer->GetContext()->GetWindow()->SwapBuffers();
glfwPollEvents();
}
}
The shaders are minimalized too:
#version 410
layout(location = 0) in vec3 vp;
uniform mat4 MVP;
void main() {
gl_Position = MVP * vec4(vp, 1.0);
}
#version 410
out vec4 color;
void main()
{
color = vec4(1,0,0,1);
}
This is Assimp's log output:
Didn't test it. But I'm assuming it's because of this:
glBufferData(... sizeof(float) * pMeshData->mNumVertices ...);
You're forgetting to multiply mNumVertices
by 3
:
glBufferData(... sizeof(float) * pMeshData->mNumVertices * 3 ...);
Since you already have vertices
, you could also do:
glBufferData(... sizeof(float) * vertices.size() ...);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.