我無法渲染一個簡單的三角形。下面的代碼編譯並運行,除了沒有任何三角形;只有一個黑色的背景。三角形不渲染
GLuint VBO;
static void RenderSceneCB()
{
//glClear sets the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil.
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
//swaps the buffers of the current window if double buffered.
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 02");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
我對OpenGL本人相當陌生,但看起來像需要着色器的「新」OpenGL。對於一個非常好的教程,我推薦[這一個](http://arcsynthesis.org/gltut/index.html),你可能想閱讀[「你好三角形」](http://arcsynthesis.org/gltut /Basics/Tutorial%2001.html)一章。 –
也許嘗試改變整個屏幕的顏色使用'glClearColor()'並在glClear'glLoadIdentity'後調用這個函數' – mr5
@ReetoKoradi - 謝謝....這工作! – SINGULARITY