【发布时间】:2015-02-18 17:10:29
【问题描述】:
这个 OpenGL 程序是为了绘制一个三角形而写的,结果崩溃了。
基本上将一组三角形顶点定义为名为坐标的数组,然后将此数组传递给缓冲区,glDrawArrays方法将根据GL_TRIANGLES模式从顶点0开始绘制三角形,总共3个顶点。
我说的对吗?有人可以告诉我错误在哪里吗?代码如下:
// Open an OpenGL window
GLFWwindow* window;
int k = 0;
/****Step 1: define vertices in (x, y, z) form****/
const GLfloat coordinates[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
/************************/
/**Step 2: send this triangle vertices to OpenGL through a buffer**/
GLuint vertexBuffer; // identify vertex buffer
void Render(void){
/************************/
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3 /*size*/, GL_FLOAT /*type*/, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDisableVertexAttribArray(0);
/************************/
// glClearColor(0., 1., 1., 1.); // blue colour
glClear( GL_COLOR_BUFFER_BIT );
// Swap front and back rendering buffers
glfwSwapBuffers(window);
//Poll for and process events
glfwPollEvents();
}
int main( void ) {
/*Initializing steps here*/
// Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(700, 500, "Hello World", NULL, NULL);
// Make the window's context current
glfwMakeContextCurrent(window);
/**Step 2: send this triangle vertices to OpenGL through a buffer**/
glGenBuffers(1, &vertexBuffer); // generating 1 buffer, put resulting identifier in this buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);
/************************/
// Main loop
while( glfwWindowShouldClose(window) == 0) {
// OpenGL rendering goes here...
Render();
}
// Close window and terminate GLFW
glfwDestroyWindow(window);
glfwTerminate();
// Exit program
exit( EXIT_SUCCESS );
}
编辑 这是输出的截图:
更新 我发现了错误,这是因为我在初始化 GLEW 后创建了 OpenGL 上下文。它会导致程序崩溃。
【问题讨论】:
-
它在哪里崩溃?你检查过调试器吗?