OpenGL GLFW 窗口一打开就关闭

Mos*_*ebi 1 c++ opengl glew glfw

我无法在黑色窗口中看到我的三角形。窗口只是在打开时关闭,并且不允许我看到里面发生了什么。我在网上的某个地方看到我与次要版本控制有关,我不知道如何检查我的 VGA 卡。

这是我的完整代码:

#define GLEW_STATIC
#include <stdio.h>
#include <GL\glew.h>
#include <GL\GLU.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\gl.h>
#include <GLFW\glfw3.h>

using namespace glm;
using namespace std;




int main()
{
    glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
    glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL

    GLFWwindow* window;

    window = glfwCreateWindow(1024, 768, "First Window in OpenGL", NULL, NULL);

    if (window == NULL)
    {
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0,
        0.0f, 1.0f, 0.0f
    };

    // identifying our vertex buffer
    GLuint 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);



    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    do{
        // 1rst 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
            );

        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
    glfwWindowShouldClose(window) == 0);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我保持窗口打开并按照代码的预期使用 ESC 键关闭它吗?

swa*_*log 5

窗口立即关闭的原因是您遇到了分段错误。这很可能是由于未能以正确的顺序初始化事物。

GLFW在运行任何 glfw 函数调用之前进行初始化,如下所示:

// Initialise GLFW
if( !glfwInit() ) {
    fprintf( stderr, "Failed to initialize GLFW\n" );
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL

GLFWwindow* window;
window = gl
Run Code Online (Sandbox Code Playgroud)

然后,glew在创建和设置 GL 上下文后将的初始化移动到右边:

if (window == NULL) {
  fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
  glfwTerminate();
  return -1;
}
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE; // Needed in core profile 
const GLenum err = glewInit(); 
if (err != GLEW_OK) {
  fprintf(stderr, "Failed to initialize GLEW\n");
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  return -1;
}

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

// ... rest of code follows
Run Code Online (Sandbox Code Playgroud)

我已经对其进行了测试,您的代码现在应该可以正常工作,并且在您点击 之前窗口不会关闭ESC