OpenGl对象不显示,着色器相关

时间:2013-10-22 10:40:51

标签: c++ opengl buffer shader vao

我正在尝试使用着色器在opengl中渲染,生成背景颜色并且不显示任何错误消息,但我正在尝试渲染的对象不显示。我一整天都在努力解决这个问题但却一无所获。我正在从.txt文件中读取顶点和索引,但这似乎不是问题,因为所有数字都应该是它们应该是的。这是我的 init

void init() {
    readFile("pendulum.txt", pVert, pIndices, pCols);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable( GL_DEPTH_TEST );

    glClearColor(0.5,0.5,0.5,1.0);

    program = InitShader( "aVertexShader64.glsl", "aFragShader63.glsl" );
    glUseProgram( program );

    modelView = glGetUniformLocation( program, "model_view" );
    projection = glGetUniformLocation( program, "projection" );


    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize two buffer objects
    glGenBuffers( 2, buffers);

    //one buffer for the vertexPositions and colours
    glBindBuffer( GL_ARRAY_BUFFER, buffers[0]);
    glBufferData( GL_ARRAY_BUFFER, numVertexPositionBytes + numVertexColourBytes,NULL, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions );
    glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, vertexColours);

    //one buffer for the indices
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, numVertexIndexBytes,vertexIndices, GL_STATIC_DRAW );

    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

    GLuint vColor = glGetAttribLocation( program, "vColor" );
    glEnableVertexAttribArray( vColor );
    glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertexPositionBytes) );
}

主要

int main( int argc, char **argv ){
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow( "pendulum" );

    glewInit();
    init();
    //initialiseBoxCoordinates();
    //glutTimerFunc(1,timer,0);
    glutReshapeFunc(reshape);
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutIdleFunc( idle );

    glutMainLoop();

    return 0;
}

显示:

void display( void ) {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    pStack.pushMatrix();
    pStack.loadIdentity();
    pStack.rotated(theta,0.0,1.0,0.0);
    pStack.translated(0.0,0.0,-1.0);

    for(int i = 0; i<NumPVerts; i++){
        pStack.transformd(&pVert[i*4],&pVertActual[i*4]);
    }

    glBindVertexArray(lineVao);
    glDrawArrays(GL_LINES,0, lineVertSize/3);

    glBindVertexArray(pVao);

    glBindBuffer(GL_ARRAY_BUFFER, pBuffers[0]);
    glBufferSubData(GL_ARRAY_BUFFER, 0, numPVertexBytes, pVertActual);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pBuffers[1]);

    glDrawElements(GL_TRIANGLES, NumPIndices, GL_UNSIGNED_BYTE, 0);

    pStack.popMatrix();

    //switch buffers
    glutSwapBuffers();

}

在矩阵变换之后,顶点都是它们应该存在的位置,我之前使用过相同的矩阵类,没有任何问题,所以我不认为是这样。我认为这与着色器有关,但我还是刚开始使用opengl,我真的不确定。以下是顶点着色器

in  vec4 vPosition;
in  vec4 vColor;
out vec4 color;

uniform mat4 model_view;
uniform mat4 projection;

void main()  {
    gl_Position = projection*model_view*vPosition/vPosition.w;
    color = vColor;
} 

片段着色器

in  vec4 color;
out vec4 fColor;

void main() {
    fColor = color;
} 

1 个答案:

答案 0 :(得分:0)

据我所知,你的着色器有点好。但是你没有使用 #version 标记,这可能会导致一些问题,例如着色器编译错误。

我不明白为什么你的顶点着色器中有vPosition/vPosition.w分区。这仍然由opengl管道自动完成,您不需要这样做。

你的消息来源中有一些特殊的部分。例如,我不知道InitShader(...)做了什么,我没有看到对glUniform(..)的任何调用(我想,它们隐藏在pStack对象中?)。我没有看到一些重要变量的初始化代码,例如numVertexPositionBytes,或者BUFFER_OFFSET(numVertexPositionBytes)甚至意味着什么。