This OpenGL program is written to draw a triangle and it got crashed.
Basically a set of triangle vertices is define as array named coordinate, then this array is passed to a buffer and the method glDrawArrays will draw the triangle based on the mode GL_TRIANGLES start from vertex 0, 3 vertices in total.
Am I right? And could someone show me where is the error? Here is the code:
// 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 );
}
EDIT Here is a screenshot of the output:
UPDATE I found the error, this is because I create the OpenGL context after initializing GLEW. It causes program to crash.