【发布时间】:2013-12-14 03:12:18
【问题描述】:
我在显示使用 Assimp 加载的模型时遇到问题。模型已正确加载并显示较早,但我只使用没有法线等的顶点数据。
现在,当我什至删除着色器的整个代码时,它会为我绘制一个白色模型。谁能给我解释一下?
这里是网格加载辅助方法:
Model * MeshHelper::loadModelFromObjFile( char * filePath )
{
Model * model = new Model();
const aiScene* scene = aiImportFile ( filePath, aiProcess_Triangulate);
if (!scene) {
fprintf (stderr, "ERROR: reading mesh %s\n", filePath);
return false;
}
printf ("%i animations\n", scene->mNumAnimations);
printf ("%i cameras\n", scene->mNumCameras);
printf ("%i lights\n", scene->mNumLights);
printf ("%i materials\n", scene->mNumMaterials);
printf ("%i meshes\n", scene->mNumMeshes);
printf ("%i textures\n", scene->mNumTextures);
for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++) {
const aiMesh* mesh = scene->mMeshes[m_i];
GLuint vertexBuffer, normalBuffer, textureBuffer;
Mesh * modelMesh = new Mesh();
modelMesh->vertexCount = mesh->mNumVertices;
glGenVertexArrays( 1, &modelMesh->vertexArray );
glBindVertexArray( modelMesh->vertexArray );
for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++)
{
if (mesh->HasPositions ()) {
const aiVector3D* vp = &(mesh->mVertices[v_i]);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mVertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glDisableVertexAttribArray(0);
}
// buffer for vertex normals
if (mesh->HasNormals()) {
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->mNumVertices, mesh->mNormals, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glDisableVertexAttribArray(1);
}
// buffer for vertex texture coordinates
if (mesh->HasTextureCoords(0)) {
float *texCoords = (float *)malloc(sizeof(float) * 2 * mesh->mNumVertices);
for (unsigned int k = 0; k < mesh->mNumVertices; ++k) {
texCoords[k*2] = mesh->mTextureCoords[0][k].x;
texCoords[k*2+1] = mesh->mTextureCoords[0][k].y;
}
glGenBuffers(1, &textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * mesh->mNumVertices, texCoords, GL_STATIC_DRAW);
//glEnableVertexAttribArray(2);
//glVertexAttribPointer(2, 2, GL_FLOAT, 0, 0, 0);
}
if (mesh->HasTangentsAndBitangents ()) {
// NB: could store/print tangents here
}
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
model->meshCollection->push_back( modelMesh );
}
aiReleaseImport (scene);
return model;
}
模型类包含:
std::vector<Mesh*> * meshCollection;
Mesh 有 VAO 索引和要绘制的点数 Mesh的绘制方法
void Mesh::draw()
{
glBindVertexArray( vertexArray );
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glBindVertexArray(0);
}
这里是顶点着色器和片段
顶点
#version 400
layout( location = 0 ) in vec3 aPosition;
layout( location = 1 ) in vec3 aNormal;
uniform mat4 uMVPMatrix;
out vec3 vNormal;
void main()
{
vNormal = aNormal;
gl_Position = uMVPMatrix * vec4 ( aPosition, 1.0);
};
片段
#version 400
out vec4 frag_colour;
in vec3 vNormal;
void main()
{
frag_colour = vec4 ( vNormal, 1.0) * vec4( 0.5 );
};
着色器加载
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertexShader, NULL );
glCompileShader (vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragmentShader, NULL);
glCompileShader (fs);
program = glCreateProgram();
glAttachShader (program, fs);
glAttachShader (program, vs);
glLinkProgram (program);
【问题讨论】:
-
您需要在调用
glDraw*函数时保持glEnableVertexAttribArray处于活动状态 -
我删除了 glDisableVertexAttribArray();对于顶点和法线,它出现了,但着色器似乎对模型没有任何影响