opengl 多个对象,纹理和非纹理,伪影

Posted

技术标签:

【中文标题】opengl 多个对象,纹理和非纹理,伪影【英文标题】:opengl multiple objects, textured and non textured, artifacting 【发布时间】:2013-10-27 07:26:28 【问题描述】:

我正在绘制一个带有钟摆的框架,钟摆应用了纹理,但框架没有纹理。当我同时显示我得到

但是当我只渲染钟摆时,它们会正确绘制并且我得到了

我不确定这是为什么。我炸毁了钟摆,纹理似乎映射到框架中的顶点。这是vao decleration

    // Create a vertex array object
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

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

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

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

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

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

glBindVertexArray(0);



glGenVertexArrays( 2, &pVao );
glBindVertexArray( pVao );

// Create and initialize two buffer objects
glGenBuffers( 1, pBuffers);

//glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes, numPVertexColourBytes, pCols);

glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pBuffers[0]);
glEnableVertexAttribArray(0);
glBufferData( GL_ELEMENT_ARRAY_BUFFER,numPVertexPositionBytes+ numPVertexColourBytes+numTexCoordBytes, NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, numPVertexPositionBytes, pendulumVertexPos );
glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes, numPVertexColourBytes, pCols);
glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes+numPVertexColourBytes, numTexCoordBytes, texCoords);

// set up vertex arrays
  GLuint pVPosition = glGetAttribLocation( program, "vPosition" );
  glEnableVertexAttribArray( pVPosition );
  glVertexAttribPointer( pVPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
  GLuint pVColor = glGetAttribLocation( program, "vColor" );
  glEnableVertexAttribArray( pVColor );
  glVertexAttribPointer( pVColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numPVertexPositionBytes) );
  GLuint vTexCoord = glGetAttribLocation( program, "vTexCoord" );
  glEnableVertexAttribArray(vTexCoord);
  glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numPVertexPositionBytes+numPVertexColourBytes) );

和显示

void
display( void )

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


modelViewStack.loadIdentity();
modelViewStack.pushMatrix();
glDisable(GL_TEXTURE_2D);


modelViewStack.lookAt(radius*sin(theta)*cos(phi),
                        radius*sin(theta)*sin(phi),
                        radius*cos(theta),
                        0.0,0.0,0.0,
                        0.0,1.0,0.0);
modelViewStack.rotatef(rotate,0.0,1.0,0.0);

glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertexPositionBytes, frameVertexPositions );
glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, frameVertexColours);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
//Indexing into vertices we need to use glDrawElements
glDrawElements(GL_TRIANGLES, NumFIndices, GL_UNSIGNED_BYTE, 0);

modelViewStack.popMatrix();

lineScale = 1.0/8.0;
pendulumScale = 1.0/8.0;


for(int i = 0; i<NumPendulums; i++)
    if(!active[i])
        currentTheta[i] = 0.0;
    
    modelViewStack.loadIdentity();
    modelViewStack.pushMatrix();
    glEnable(GL_TEXTURE_2D);

    modelViewStack.lookAt(radius*sin(theta)*cos(phi),
                            radius*sin(theta)*sin(phi),
                            radius*cos(theta),
                            0.0,0.0,0.0,
                            0.0,1.0,0.0);

    modelViewStack.scalef(pendulumScale,pendulumScale,pendulumScale);
    modelViewStack.rotatef(currentTheta[i],0.0,0.0,1.0);
    modelViewStack.rotatef(rotate,0.0,1.0,0.0);
    modelViewStack.translatef(dhVals[i][1],dhVals[i][4],0.0);

    glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());


    glBindVertexArray(pVao);
    glBindBuffer(GL_ARRAY_BUFFER, pBuffers[0]);
    glDrawArrays(GL_TRIANGLES,0,NumPVertices);


    glBindVertexArray(0);
    glDisable(GL_TEXTURE_2D);
    modelViewStack.popMatrix();


    modelViewStack.loadIdentity();
    modelViewStack.pushMatrix();
    modelViewStack.lookAt(radius*sin(theta)*cos(phi),
                            radius*sin(theta)*sin(phi),
                            radius*cos(theta),
                            0.0,0.0,0.0,
                            0.0,1.0,0.0);
    modelViewStack.rotatef(currentTheta[i],0.0,0.0,1.0);
    modelViewStack.rotatef(rotate,0.0,1.0,0.0);
    modelViewStack.scalef(pendulumScale,lineScale,pendulumScale);
    modelViewStack.translatef(dhVals[i][1],0.0,0.0);


    glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());

    glEnableVertexAttribArray(0);
    glBindVertexArray(lineVao);
    glDrawArrays(GL_LINES,0,NumLVertices);

    glBindVertexArray(0);
    modelViewStack.popMatrix();

    float temp = changeAngle(currentTheta[i], dhVals[i][5], dhVals[i][6], dhVals[i][7], i);
    currentTheta[i] = temp;
    lineScale+=0.09;




    projectionStack.loadIdentity();
    projectionStack.ortho(-20.0,20.0,-20.0,10.0,-20.0,20.0);

    calculateLighting(fPoints,frameVertexColours,NumFSides,6);
    calculateLighting(pPoints,pCols,NumPSides,6);


    glUniformMatrix4fv(projection, 1, GL_FALSE, projectionStack.getMatrixf());

glutSwapBuffers();

这是维护顶点的问题还是其他问题?

【问题讨论】:

【参考方案1】:

我没有在您的代码中看到 glDisableVertexAttribArray 调用。这意味着,仍然有从 texcoord 缓冲区读取的纹理坐标。此外,您必须取消绑定纹理或至少使用不从纹理读取的着色器。

【讨论】:

以上是关于opengl 多个对象,纹理和非纹理,伪影的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL - 非常奇怪的伪影。只有当有很多相同类型的对象实例时才会发生

OpenGL ES 顶点数组对象和奇怪的伪影

openGL之API学习(一九九)纹理单元和纹理对象的关系

OpenGL中多个纹理的问题

在 OpenGL 中将深度渲染到纹理时出现奇怪的结果

使用帧缓冲区对象作为纹理的视觉问题