用OpenGL渲染意外的三角形

埃拉特

我想渲染Blender的默认立方体和Suzanne。我把它们都导出为OBJ文件。加载和渲染工作正常,但是当我尝试在同一场景中渲染它们时,事情变得混乱(对我而言)。

“相关”代码在第48至56行中。

(I)屏幕截图:代码+渲染场景

  1. 刚开始时只有model1(第51行,Suzanne或立方体或其他任何东西),并且场景按预期渲染。

  2. 然后我添加了另一个model2(第54行),它恰好是多维数据集。意外的三角形(请参见渲染的场景,屏幕快照(I))绘制在多维数据集的顶部,此时尚未平移。

  3. Don't know my plans or thoughts of when I moved the cube into the back, but was surprised to find the garbage triangles stay where they are. Since I had no idea where they came from, I started flipping lines around to get my hands on it:

    • It is of no importance whether model1 is rendered first, or model2.
    • When I load two default_cubes instead of one suzanne, one cube is perfectly fine, and the other one just like in the first screenshot.

The result is seen in Screenshot (II). I'm constructing model2 before model1 and the garbage is now rendered differently in a different place.

(二)截图:代码+渲染场景

The code below shows the last part of the Model-constructor, where the VBA is configured. The render() function is the one being seen in the screenshots.

   [...]
   glGenVertexArrays( 1, &VBA );
   glBindVertexArray( VBA );

   glGenBuffers( 1, &VBO[0] );
   glBindBuffer( GL_ARRAY_BUFFER, VBO[0] );
   glBufferData( GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), vertices.data(), GL_STATIC_DRAW );
   glEnableVertexAttribArray( 0 );
   glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );
}


void Model::render( void )
{
  glUniformMatrix4fv( model_location, 1, GL_FALSE, &model_matrix[0][0] );
  glBindVertexArray( VBA );
  // 'vertices' is of type std::vector<glm::vec3>
  glDrawArrays( GL_TRIANGLES, 0, sizeof(glm::vec3) * vertices.size() );
}
BDL

您将错误的数据传递给glDrawArrays第三个参数必须包含顶点数,而不是字节数。正确的调用是:

glDrawArrays( GL_TRIANGLES, 0, vertices.size() );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章