user11427886
user11427886

Reputation:

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

I'm trying to write a code in C ++ using OpenGL (I use GLFW and GLEW libraries). Here is the code:

#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;
}

Unfortunately when I run the code I get an error code:

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

It should appear a black screen with a dot in the center.

Upvotes: 2

Views: 1348

Answers (1)

genpfault
genpfault

Reputation: 52084

Multiple issues:

  1. You're calling GL functions before you have a current GL context:

    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);
    

    Move those calls to after glfwMakeContextCurrent() (and your GL loader init) so they have a GL context to operate on (and check if window is NULL before using it in 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. You should also initialize renderingProgram before glUseProgram()ing it, perhaps with createShaderProgram()?

  3. gl_Position isn't valid in a fragment shader. You're thinking of gl_Color or a user-defined output like your color output.

  4. If you're requesting a GL 3.2 context then #version 430 isn't valid either. Either upgrade to GL 4.3 or downgrade your shaders to #version 150.

  5. GL_POINT isn't a valid input to glDrawArrays(). You're thinking of GL_POINTS.

  6. Call glDrawArrays() each frame instead of once at the beginning of your draw-loop so you have a chance to actually see your point.

  7. GLSL statements require a semicolon after them.

    Invalid:

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

    Valid:

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

All together:

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;
}

Upvotes: 5

Related Questions