Reputation: 2563
i have a simple program that creates a triangle on the screen using OpenGL & glfw
here is the code.on running this with g++ -std=c++11 main.cpp src/glad.c -o out -Iinclude -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl
i get a window which opens up & then gets closed with core dumped message on screen. what could be wrong? .
NOTE: i managed to remove that error but now i have a black empty screen . here is my new code.what could be the reason for this
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Upvotes: 1
Views: 735
Reputation: 210878
First you render the scene:
glDrawArrays(GL_TRIANGLES, 0, 3);
But then the framebuffer and so the rendering is cleared immediately:
glClear(GL_COLOR_BUFFER_BIT);
Clear the color plane of the default framebuffer, before you do any rendering:
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
....
Since GLFW uses double buffering, it would also work, if you clear the default framebuffer immediately after swapping the buffers:
glfwSwapBuffers(window);
glClear(GL_COLOR_BUFFER_BIT);
Upvotes: 1