我有一个简单的程序,可以使用OpenGL和glfw在屏幕上创建一个三角形
这是代码。使用g++ -std=c++11 main.cpp src/glad.c -o out -Iinclude -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl
运行此代码时,我得到一个打开的窗口,然后在屏幕上关闭核心转储消息。有什么问题吗?
注意: 我设法删除了该错误,但是现在我的屏幕黑屏了 。这是我的新代码。这可能是什么原因
#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;
}
答案 0 :(得分:1)
首先渲染场景:
glDrawArrays(GL_TRIANGLES, 0, 3);
但是随后是帧缓冲区,因此渲染立即被清除:
glClear(GL_COLOR_BUFFER_BIT);
在进行任何渲染之前,请清除默认帧缓冲区的颜色平面:
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
....
由于GLFW使用双缓冲,因此,如果在交换缓冲区后立即清除默认帧缓冲区,它也将起作用:
glfwSwapBuffers(window);
glClear(GL_COLOR_BUFFER_BIT);