0

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

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;
} 
4

1 回答 1

0

据我所知,你的着色器还不错。但是你没有#version在那里使用标签,这可能会导致一些问题,比如着色器编译错误。

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

您的资源中有一些重要的部分被遗漏了。例如,我不知道是什么InitShader(...),也没有看到任何调用glUniform(..)(我想,它们隐藏在pStack对象中?)。而且我看不到一些重要变量的初始化代码,例如numVertexPositionBytes,或者BUFFER_OFFSET(numVertexPositionBytes)甚至应该是什么意思。

于 2013-10-22T11:59:19.963 回答