OpenGL 在渲染多个对象时闪烁

opengl flickering while rendering multiple objects

本文关键字:对象 闪烁 OpenGL      更新时间:2023-10-16

我对opengl很陌生,我真的不明白这里发生了什么。我正在尝试使用两个 VAO 来创建多个对象,并使用自定义矩阵来旋转/平移它们。当我加载一个图像时,图像很好,但是当我加载两个图像时,它们都会闪烁。我的初始化是这样的,我为每个缓冲区、顶点位置、顶点索引和顶点颜色都有一个不同的数组。

void init()
{
 readFile();
 //glEnable(GL_DEPTH);
 glEnable( GL_DEPTH_TEST );
 //make background yerpul in colour
 glClearColor( 0.235,  0.194,  0.314, 1.0 );

    // Load shaders and use the resulting shader program
    program = InitShader( "aVertexShader61.glsl", "aFragShader61.glsl" );
    glUseProgram( program );
    // Create a vertex array object
    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,vertexIndicies, 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) );
    // Second object
    glGenVertexArrays( 1, &vao2 );
    glBindVertexArray( vao2 );
    glGenBuffers( 2, buffers2);
    //one buffer for the vertexPositions and colours
    glBindBuffer( GL_ARRAY_BUFFER, buffers2[0]);
    glBufferData( GL_ARRAY_BUFFER, numVertexPositionBytes + numVertexColourBytes,NULL, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions2 );
    glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, vertexColours2);
    //one buffer for the indices
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers2[1]);
    glBufferData( GL_ELEMENT_ARRAY_BUFFER, numVertexIndexBytes,vertexIndicies2, GL_STATIC_DRAW );
    // set up vertex arrays
    GLuint vPosition2 = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition2 );
    glVertexAttribPointer( vPosition2, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
    GLuint vColor2 = glGetAttribLocation( program, "vColor" );
    glEnableVertexAttribArray( vColor2 );
    glVertexAttribPointer( vColor2, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertexPositionBytes) );

    glBindVertexArray(0);
}

这是我的显示器,它被 glutPostRedisplay() 调用;在我的空闲函数中,没有从空闲处对任何东西进行其他调用。 mStack 是从外部文件创建的矩阵堆栈对象

    void
    display( void )
{
    //clear for first object, generate matrix and apply to object
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    mStack.loadIdentity();
    mStack.translatef(0,yDisplace,0);
    mStack.rotatef(Theta[Yaxis], 0.0,1.0,0.0);
    for (unsigned char i=0; i<NumVertices; i++){
        mStack.transformf(&vertexPositionsInit[i*4],&vertexPositions[i*4]);
    }
    //Apply to second object
    for (unsigned char i=0; i<NumVertices; i++){
        mStack.transformf(&vertexPositionsInit2[i*4],&vertexPositions2[i*4]);
    }

    //Draw first object
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBufferSubData(GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions );
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    //Indexing into vertices we need to use glDrawElements
    glDrawElements(GL_TRIANGLES, NumIndicies, GL_UNSIGNED_BYTE, 0);
    glutSwapBuffers();

    //Clear and draw second object
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glutSwapBuffers();
    glBindVertexArray(vao2);
    glBindBuffer(GL_ARRAY_BUFFER, buffers2[0]);
    glBufferSubData(GL_ARRAY_BUFFER, 0, numVertexPositionBytes, vertexPositions2 );
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers2[1]);
    //Indexing into vertices we need to use glDrawElements
    glDrawElements(GL_TRIANGLES, NumIndicies, GL_UNSIGNED_BYTE, 0);
    glutSwapBuffers();

}

我正在使用简单的顶点和片段着色器。顶点着色器,

in  vec4 vPosition;
in  vec4 vColor;
out vec4 color;
void main() 
{
  gl_Position = vPosition;
  color = vColor;
} 

和片段着色器,

in  vec4  color;
out vec4  fColor;
void main() 
{ 
    fColor = color;
} 

任何帮助将不胜感激,如果需要,我可以发布矩阵文件。谢谢

不要在对象之间调用glutSwapBuffers()glClear()。 交换缓冲区是一种告诉 GLUT "好的,我完成了这一帧,让我们从下一帧开始"的一种方式。

通常,您需要将设置和完成每个帧的代码(例如对glClear()glutSwapBuffers()的调用)与呈现每个对象的代码分开,因为OpenGL基本上是一个巨大的全局变量盒子,如果不将方法分解成小块,就很难编写好的OpenGL代码。