Results 1 to 3 of 3

Thread: glDrawArrays bad access

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    5

    glDrawArrays bad access

    Casual/hobby programmer here. Probably something super basic going on here (my understanding is basic). When I call glDrawArrays I get a BAD_ACCESS message. For the life of me I can't figure out what it is. Here is the smallest working code that reproduces the error:

    Code :
    #define GLFW_INCLUDE_GLCOREARB //to make glfw3.h include gl3.h instead of gl.h
    #include <GLFW/glfw3.h>
    #include "ShaderUtils.h"
     
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    //struct point3 {
    //    GLfloat x;
    //    GLfloat y;
    //    GLfloat z;
    //};
     
    int main(){
     
    //Create Window and start glfw
    GLint glfwStatus=glfwInit();
    if(!glfwStatus){
        std::cout<<"window did not load"<<std::endl;
        glfwTerminate();
        }
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);//3
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);//2
        GLFWwindow* window=glfwCreateWindow(640, 360, "OpenGL practice", NULL, NULL);
        if (window==NULL) {
            std::cout<<"did not create window"<<std::endl;
            glfwTerminate();
            return 1;
        }
        glfwMakeContextCurrent(window);
        std::cout<<glGetString(GL_VERSION)<<std::endl;
     
     
    //Create Shaders and Program
    GLuint vertexShader=shaderUtils::createShaderFromFile("/Users/christophersnyder/Documents/cppprojects/OpenGlTutorial2/vertShader.vs", GL_VERTEX_SHADER);
    GLuint fragmentShader=shaderUtils::createShaderFromFile("/Users/christophersnyder/Documents/cppprojects/OpenGlTutorial2/fragsShader.fs", GL_FRAGMENT_SHADER);
    GLuint shaderProgram=glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    GLint linkStatus;
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
    if (linkStatus!=GL_TRUE) {
        std::cout<<"Shader program Failed to link"<<std::endl;
        GLint infoLogLength;
        glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar* infoLog=new GLchar[infoLogLength+1];
        glGetProgramInfoLog(shaderProgram, infoLogLength+1, NULL, infoLog);
        delete infoLog;
        return 0;
        }
        glUseProgram(shaderProgram);
     
    //create buffer for axes
    const float ticksize=.5;
    const int nticks=21;
    float tickspacing=.1;
     
    GLfloat ticks[6*nticks];
    //xticks
    float x_pos;
    for (int i=1;i<nticks;  i++) {
        x_pos= -1 + i * tickspacing;
        ticks[i * 6] = x_pos;
        ticks[i * 6+1] = -1*ticksize;
        ticks[i * 6+2]=0;
        ticks[i * 6+3] = x_pos;
        ticks[i * 6+4] =ticksize;
        ticks[i * 6+5]=0;
    }
     
    GLuint axesVBO;
    glGenBuffers(1, &axesVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, axesVBO);
    glBufferData(axesVBO, sizeof(ticks), ticks, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
     
    //create VAO for axes
    GLuint axesVAO;
    glGenVertexArrays(1, &axesVAO);
    glBindVertexArray(axesVAO);
    GLint positionLoc=glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(positionLoc);
    glBindBuffer(GL_ARRAY_BUFFER, axesVBO);
    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), 0);
    glBindBuffer(GL_ARRAY_BUFFER, NULL);
    glBindVertexArray(NULL);
     
    //make background black
    glClearColor(0.0, 0.0, 0.0, 0.0);
     
    //plotstuff
    while( !glfwWindowShouldClose(window)){
        GLint windowWidth,windowHeight;
        glfwGetWindowSize(window,&windowWidth,&windowHeight);
        glViewport(0, 0, windowWidth, windowHeight);
        glClear(GL_COLOR_BUFFER_BIT);
     
    glBindVertexArray(axesVAO);
    glDrawArrays(GL_LINES, 0,  2*nticks);
    //glDrawArrays(GL_LINE_STRIP, 0,  2*nticks);
    glBindVertexArray(NULL);
     
     
    glfwPollEvents();
    glfwSwapBuffers(window);
    }
     
    glDeleteProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
    }

    //Shaders
    Code :
    #version 410 core
     
    in vec3 position;
     
    void main(){
        gl_Position=vec4(position,1.0);
    }

    #version 410 core

    Code :
    out vec4 fragData;
    void main(){
        fragData=vec4(1.0,1.0,1.0,1.0);
    }

  2. #2
    Senior Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    891
    You previously mentioned you're using Xcode. Let me save you a lot of time and point you at an example of setting up the debugger.

    If your case, if you use the debugger you'll find you're calling glBufferData incorrectly, so your buffer object is never created. Then when you try to draw with it, you are dereferencing unallocated memory and you crash. (You could also determine this by introspecting glGetBufferParameteriv(...GL_BUFFER_SIZE...) just before drawing.)

    That's another useful debugging technique, by the way. Learn to introspect state.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    5
    I never got back to you.
    Thank you for your help. I got OpenGL profiler up and running. It's not really clear yet how to make use of all of the tonnage it gives me back in the trace, but I'm optimistic. You other link was helpful as well, but probably not helpful enough that you won't be hearing from me ever again

Similar Threads

  1. Access violation in GLDrawArrays()
    By swtsvn in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-24-2014, 01:48 PM
  2. glDrawArrays access violation, multiple objects
    By stigersh in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 02-20-2012, 10:33 AM
  3. glDrawArrays & memory access weirdness
    By remdul in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 07-11-2011, 09:13 AM
  4. glDrawArrays access violation (Python/PyOpenGL)
    By ChrisAWFUL in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-30-2010, 05:54 PM
  5. glDrawArrays triggers Access Violation
    By OGLBeginner in forum OpenGL: Basic Coding
    Replies: 22
    Last Post: 04-09-2010, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean