-1
我想在OpenGL中顯示2個簡單的三角形,但它們不會出現,儘管檢查了很多網站。我忘記了代碼中的任何內容嗎?OpenGl不顯示使用VBO的三角形
#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))
// Prepare:
GLfloat vertices[]={0,0,1,
0.5,0,-0.5,
0,0,0,
0,0,1,
0,0,0,
-0.5,0,-0.5};
GLushort indices[]={0,1,2,3,4,5};
GLfloat *generatedVertices=new GLfloat[18];
GLushort *generatedIndices=new GLushort[6];
GLfloat *colors=new GLfloat[18];
glGenBuffers(3, triangleBuffers); // triangleBuffers = Global GLuint
for (int p=0;p<6;p++)
{
MVector V(vertices[p*3],vertices[p*3+1],vertices[p*3+2]); // Maya type
generatedVertices[p*3]=V.x;
generatedVertices[p*3+1]=V.y;
generatedVertices[p*3+2]=V.z;
generatedIndices[p]=p;
colors[p*3]=0.8f;
colors[p*3+1]=0.6f;
colors[p*3+2]=0.0f;
}
// vertices
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*18, generatedVertices, GL_STATIC_DRAW);
// indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data->triangleBuffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*6, generatedIndices, GL_STATIC_DRAW);
// couloirs
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*18*data->totalSize, colors, GL_STATIC_DRAW);
// Render:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[0]);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data->triangleBuffers[1]);
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[2]);
glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, totalSize*6, GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
您以'GLushort'格式上傳索引,但是在繪製調用中您聲稱它的格式爲'GL_UNSIGNED_INT'。 – BDL
相機的位置是多少?如果它在'y軸'上具有'0'的位置,那麼這意味着,根據您的頂點。你不會看到三角形,因爲你正在畫兩個三角形。嘗試以下頂點; '0,1,0 - 0,0,0 - 0.5,-0.5,0'對於三角形 – marko5049
BDL:良好的捕捉,糾正但仍然不顯示任何東西 –