我在OpenGL中显示简单屏幕时遇到问题

时间:2020-04-02 04:31:36

标签: c++

基本上,我在YouTube上关注有关使用OpenGL的教程。 我在那里写了作者的代码,但是当我尝试运行它时,出现了如下错误:

线程1:glfwGetFramebufferSize( window, &screenWidth, &screenHeight ); 处的EXC_BAD_ACCESS(代码= 1,地址= 0x350) 这也给了我目标完整性的错误。

我该如何解决?

这是代码

#include <iostream>
//GLEW
#define GLEW_STATIC
#include <GL/glew.h>

//GLFW
#include <GLFW/glfw3.h>

const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
    glfwInit();

    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint( GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow( WIDTH, HEIGHT, "Learn OPENGL", nullptr, nullptr );
    int screenWidth, screenHeight;
    glfwGetFramebufferSize( window, &screenWidth, &screenHeight );

    if ( nullptr  == window )
    {
        std::cout<<"Failed to create GLWwindow"<<std::endl;
        glfwTerminate( );

        return 0;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if ( GLEW_OK != glewInit() )
    {
        std::cout<<"Failed to initialize GLEW"<<std::endl;

        return 0;
    }

    glViewport( 0,0, screenWidth, screenHeight );

    while ( !glfwWindowShouldClose( window ) )
    {
        glfwPollEvents();

        glClearColor(0.2f, 0.3f, 0.3, 1.0f);
        glClear( GL_COLOR_BUFFER_BIT );

        glfwSwapBuffers( window );

    }

    glfwTerminate();

    return 0;

}
`

1 个答案:

答案 0 :(得分:0)

glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3);

尝试将第二行更改为:

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);