-1
我正在嘗試使用Assimp導入網格。出於某種原因,它顯示不正確。我試圖加載蘇珊:OpenGL:Assimp網格不正確
這是Open3D模型瀏覽器截圖 - 它也使用Assimp。這意味着模型文件是正確的。現在,當我嘗試加載它,它看起來像這樣(以線框渲染):
我一直想很多東西這就是爲什麼我的代碼減少到最低限度(沒有法線,texcoords等),爲什麼我沒有使用任何我有(除着色器類的類,這是廣泛的測試和罰款)的:
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();
}
}
的着色器minimalized太:
#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的日誌輸出:
多麼愚蠢的錯誤。我有一個誤解,即mNumVertices是總點數。 – Freakyy