代码错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0)

时间:2019-04-29 15:58:23

标签: c++ opengl glsl glfw glew

我正在尝试使用OpenGL在C ++中编写代码(我使用GLFW和GLEW库)。这是代码:

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

#define numVAOs 1

GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram(){
    const char*vshaderSource =
    "#version 430 \n"
    "void main(void) \n"
    "{gl_Position = vec4(0.0,0.0,0.0,1.0)};";
    const char*fshaderSource =
    "#version 430 \n"
    "out vec4 color; \n"
    "void main(void) \n"
    "{gl_Position = vec4(0.0,0.0,1.0,1.0)};";

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vShader,1,&vshaderSource,NULL);
    glShaderSource(fShader,1,&fshaderSource,NULL);
    glCompileShader(vShader);
    glCompileShader(fShader);

    GLuint vfProgram = glCreateProgram();
    glAttachShader(vfProgram,vShader);
    glAttachShader(vfProgram,fShader);
    glLinkProgram(vfProgram);
    return vfProgram;

}



int main()
{

    glfwInit();

    // Define version and compatibility settings
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    glGenVertexArrays(numVAOs,vao);
    glBindVertexArray(vao[0]);
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINT,0,1);
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    glfwMakeContextCurrent(window);

    // Check for window creation failure
    if (!window)
    {
        // Terminate GLFW
        glfwTerminate();
        return 0;
    }


    glewExperimental = GL_TRUE; glewInit();

    // Event loop
    while(!glfwWindowShouldClose(window))
    {
        // Clear the screen to black
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Terminate GLFW
    glfwTerminate(); return 0;
}

不幸的是,当我运行代码时,我得到一个错误代码:

Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x0).

它应该出现一个黑屏,中间有一个点。

1 个答案:

答案 0 :(得分:2)

多个问题:

  1. 在拥有当前GL上下文之前,您正在调用GL函数:

    glGenVertexArrays(numVAOs,vao);  // nope
    glBindVertexArray(vao[0]);       // nope
    glUseProgram(renderingProgram);  // nope
    glDrawArrays(GL_POINT,0,1);      // nope
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    glfwMakeContextCurrent(window);
    

    将那些调用移至之后 glfwMakeContextCurrent()(和您的GL加载程序init),以便它们具有GL上下文可操作(并检查window是否为NULL glfwMakeContextCurrent()中使用它)

    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow(1430, 800, "Davide", NULL, NULL);
    // Check for window creation failure
    if (!window)
    {
        // Terminate GLFW
        glfwTerminate();
        return 0;
    }
    glfwMakeContextCurrent(window);
    
    glewExperimental = GL_TRUE; glewInit();
    
    glGenVertexArrays(numVAOs,vao);
    glBindVertexArray(vao[0]);
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINT,0,1);
    
  2. 您还应该在renderingProgram之前初始化glUseProgram(),也许用createShaderProgram()

  3. gl_Position在片段着色器中无效。您正在考虑使用gl_Color或类似color的用户定义输出。

  4. 如果您请求GL 3.2上下文,则#version 430也不有效。升级到GL 4.3或将着色器降级到#version 150

  5. GL_POINTglDrawArrays()无效。您正在考虑GL_POINTS

  6. 每帧调用glDrawArrays()而不是在绘制循环开始时调用一次,这样您就有机会真正看到您的观点。

  7. GLSL语句后面需要分号。

    无效:

    void main(void)
    {
        gl_Position = vec4(0.0,0.0,0.0,1.0)
    };
    

    有效:

    void main(void)
    {
        gl_Position = vec4(0.0,0.0,0.0,1.0);  // note the semicolon
    } // note the lack of semicolon
    

一起:

screenshot of blue dot in middle of black window

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

void CheckStatus( GLuint obj, bool isShader )
{
    GLint status = GL_FALSE, log[ 1 << 11 ] = { 0 };
    ( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
    if( status == GL_TRUE ) return;
    ( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
    std::cerr << (GLchar*)log << "\n";
    std::exit( EXIT_FAILURE );
}

void AttachShader( GLuint program, GLenum type, const char* src )
{
    GLuint shader = glCreateShader( type );
    glShaderSource( shader, 1, &src, NULL );
    glCompileShader( shader );
    CheckStatus( shader, true );
    glAttachShader( program, shader );
    glDeleteShader( shader );
}

const char* const vert = 1 + R"GLSL(
#version 150
void main()
{
    gl_Position = vec4(0.0,0.0,0.0,1.0);
}
)GLSL";

const char* const frag = 1 + R"GLSL(
#version 150
out vec4 color;
void main()
{
    color = vec4(0.0,0.0,1.0,1.0);
}
)GLSL";

#define numVAOs 1

GLuint renderingProgram;
GLuint vao[ numVAOs ];

int main()
{
    glfwInit();

    // Define version and compatibility settings
    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
    glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
    // Create OpenGL window and context
    GLFWwindow* window = glfwCreateWindow( 320, 240, "Davide", NULL, NULL );
    // Check for window creation failure
    if( !window )
    {
        // Terminate GLFW
        glfwTerminate();
        return 0;
    }
    glfwMakeContextCurrent( window );

    glewExperimental = GL_TRUE;
    glewInit();

    glGenVertexArrays( numVAOs, vao );
    glBindVertexArray( vao[ 0 ] );

    GLuint renderingProgram = glCreateProgram();
    AttachShader( renderingProgram, GL_VERTEX_SHADER, vert );
    AttachShader( renderingProgram, GL_FRAGMENT_SHADER, frag );
    glLinkProgram( renderingProgram );
    CheckStatus( renderingProgram, false );
    glUseProgram( renderingProgram );

    // Event loop
    while( !glfwWindowShouldClose( window ) )
    {
        // Clear the screen to black
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT );
        glDrawArrays( GL_POINTS, 0, 1 );
        glfwSwapBuffers( window );
        glfwPollEvents();
    }

    // Terminate GLFW
    glfwTerminate();
    return 0;
}