【问题标题】:simple opengl triangle program with glfw renders black screen带有glfw的简单opengl三角形程序呈现黑屏
【发布时间】:2018-11-15 15:04:00
【问题描述】:

我有一个简单的程序,它使用 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;
}

【问题讨论】:

    标签: glfw opengl-3


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-25
      • 2018-07-08
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      相关资源
      最近更新 更多