OpenGL顶点属性指针或着色器无法正常工作

时间:2013-12-13 16:29:46

标签: c++ opengl glsl shader

我在显示我加载的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;

网格具有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);

1 个答案:

答案 0 :(得分:0)

使用法线作为颜色值。 “法线”用于计算照明过程的最终颜色。 例如,DiffuseLighting。法线表示来自给定面(三角形)的正交方向矢量。它用于计算光线方向和曲面之间的角度 确定亮度强度。

您需要从网格中加载材质信息并传递此信息 片段着色器的信息。

由于评论更新:

在draw方法中启用normalBuffer和texturebuffer。

这是我的渲染方法的摘录: 您可以看到我启用了所有必需的缓冲区以及所有VertexAttributeArrays 所以将正确的VertexAttributes传递给着色器程序。 这个代码是为我拥有的每个模型(Mesh)执行的。

void VEModel :: render(VEEnvironment&amp; environment,glm :: mat4&amp; parentModel){

glUseProgram( shaderProgram );

glPolygonMode( GL_FRONT_AND_BACK, polygonModel);

glm::mat4 proj = environment.getProjection();
VECamera* cam = &environment.getActiveCamera();

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo );

GLint refModel    = getUniformLocation( shaderProgram, "model" );
GLint refView     = getUniformLocation( shaderProgram, "view" );
GLint refProj     = getUniformLocation( shaderProgram, "proj" );

glUniformMatrix4fv( refView, 1, GL_FALSE, glm::value_ptr( cam->getCamera() ) );
glUniformMatrix4fv( refProj, 1, GL_FALSE, glm::value_ptr( proj ) );
glUniformMatrix4fv( refModel, 1, GL_FALSE, glm::value_ptr( modelMatrix ) );

applyLights(environment);


GLint posAttrib = getAttributeLocation(shaderProgram, "vertPosition");
GLint normalAttrib = getAttributeLocation(shaderProgram, "vertNormal");
GLint colAttrib = getAttributeLocation(shaderProgram, "vertColor");

glEnableVertexAttribArray( posAttrib );
glVertexAttribPointer( posAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 0 * sizeof(float) ) );


glEnableVertexAttribArray( normalAttrib );
glVertexAttribPointer( normalAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 3 * sizeof(float) ));


glEnableVertexAttribArray( colAttrib );
glVertexAttribPointer( colAttrib, 3, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*) ( 6 * sizeof(float) ) );

int offset = 0;
int texUnitOffset = 0;
//glDrawElements(GL_TRIANGLES, maxIndices, GL_UNSIGNED_INT, (void*)0);


for(std::vector<VEGeometryObject *>::iterator geometry = geometryObjects.begin(); geometry != geometryObjects.end(); ++geometry) {
    if ((*geometry)->hasTexture()) {

        for (int i=0; i<(*geometry)->getTextureCount(); i++) {
            glActiveTexture( i +  texUnitOffset);
            glBindTexture( GL_TEXTURE_2D, textures[i+texUnitOffset] );
            //glActiveTexture(GL_TEXTURE0); // i + texUnitOffset);

            (*geometry)->enableTextureStates();
            /*
            fprintf(stderr, "Enabled texture buffer TBO %i\n", textures[i+texUnitOffset]);
            */

            GLint texAttrib = getAttributeLocation( shaderProgram, "vertUV" );
            glEnableVertexAttribArray( texAttrib );
            glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, vertexAttrLen * sizeof(float), (void*)( 9 * sizeof(float) ) );

            /*GLint uniTime = getUniformLocation(shaderProgram, "time");
            glUniform1f(uniTime, (GLfloat)clock() / (GLfloat)CLOCKS_PER_SEC);*/
            glUniform1i( glGetUniformLocation( shaderProgram, "sampler" ), i + texUnitOffset );
        }

        texUnitOffset++;
    }
    //glDrawArrays(GL_TRIANGLES, 0, (*geometry)->getVertexCount());
    glDrawElements(GL_TRIANGLES, maxIndices, GL_UNSIGNED_INT, (void*) offset) ;
    offset += (*geometry)->getFaceCount();
}

glDisableVertexAttribArray(posAttrib);
glDisableVertexAttribArray(normalAttrib);
glDisableVertexAttribArray(colAttrib);

}

这是一个真正的sophosticated教程,解释了 以非常好的方式缓冲概念:http://open.gl/introduction