[英]OpenGL: Assimp mesh incorrect
我目前正在嘗試使用Assimp導入網格。 由於某種原因,它顯示不正確。 我正在嘗試加載Suzanne:
這是Open3D Model Viewer(也使用Assimp)的屏幕截圖。 這意味着模型文件是正確的。 現在,當我嘗試加載它時,它看起來像這樣(在Wireframe中渲染):
我一直在嘗試很多事情,這就是為什么我將代碼減少到最低限度(沒有法線,texcoords等)以及為什么我不使用我擁有的任何類(除了Shader類,它們廣泛經過測試並且很好):
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();
}
}
着色器也被最小化:
#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);
}
這是Assimp的日志輸出:
沒有測試。 但我想是因為這樣的:
glBufferData(... sizeof(float) * pMeshData->mNumVertices ...);
您忘記將mNumVertices
乘以3
:
glBufferData(... sizeof(float) * pMeshData->mNumVertices * 3 ...);
由於您已經有了vertices
,因此您也可以執行以下操作:
glBufferData(... sizeof(float) * vertices.size() ...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.