Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to follow a tutorial of glew, but i can't run this glGenVertexArrays, it always leads to a memmory access violation!...

I tried glExperimental = GL_TRUE too, also updated my video card driver 2 times, but it doesn't run.

Btw, there's my code (to TRY to generate a simple triangle):

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

#include <glew.h>
#include <glfw.h>
#include <glm/glm.hpp>
using namespace glm;

#define GLFW_DLL
//#pragma comment(linker,  "/SUBSYSTEM:WINDOWS")
//#pragma comment(linker, "/ENTRY:mainCRTStartup")

int main()
{

    //glewExperimental = GL_TRUE;

    if(!glfwInit())
    {
        cout << "Failed to load graphics... " << endl; 
        return -1;
    }



    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.0f,
         0.0f,  1.0f,  0.0f,
    };

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);


    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    glfwOpenWindowHint(GLFW_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_VERSION_MINOR, 1);
    //glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, 0);

    if(!glfwOpenWindow(1024, 768, 0, 0, 0, 0, 32, 0, GLFW_WINDOW))
    {
        cout << "Failed to open window";
        glfwTerminate();
        return -2;
    }

    glfwSetWindowTitle("IfUSeeingThisMsgTheShitWorked");

    glfwEnable(GLFW_STICKY_KEYS);

    //Main loop
    do
    {
        cout << "happaned";
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 0, (void*) 0);

        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers();

    }while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS && glfwGetWindowParam(GLFW_OPENED));



    return 0;
}

Also, I want to point out that this function doesn't work:

glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

so I had to change it to:

glfwOpenWindowHint(GLFW_OPENGL_PROFILE, 0);

Does it have anything to do with my error? hm.

I'm posting it here, just to make sure that there's no other thing I can do, other than buy a new video card. I really want to use the modern OpenGL, it is pointless to learn the old one.

share|improve this question

closed as too localized by Byte56, Sam Hocevar, Trevor Powell, Sean Middleditch, Josh Petrie Feb 22 '13 at 16:17

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
AND my card is nvidia 9500, so it should support glew 3.0 but it doesn't... –  Dhiego Magalhães Feb 21 '13 at 23:26
    
Try using a known good example. It's far more likely there's a problem with your code than a hardware problem. Once you've confirmed that it's your code, proceed with debugging it. As it is, I think this question is too localized for the site. –  Byte56 Feb 21 '13 at 23:26
    
I would love if its my code, but im following the tutorial... and it compiles. an this problem with GenVertexArrays seems to be very common... –  Dhiego Magalhães Feb 21 '13 at 23:30
2  
Download and run the GL extensions viewer (Google for it) & confirm that your hardware/driver actually supports VAOs first. –  Darth Satan Feb 21 '13 at 23:34
    
:) Just because it compiles doesn't mean much. It's a common problem because it's so easy to write past the bounds of your array. For example, people will often use sizeof and get the size of a pointer instead of the size of an array. (not saying that's the case here). You may want to look for similar errors. –  Byte56 Feb 21 '13 at 23:39

2 Answers 2

up vote 1 down vote accepted

You're not actually calling glewInit in your code, so you're not even loading the GL entry points. You should add a call to glewInit after your glfwInit call, like so:

if(!glfwInit())
{
    cout << "Failed to load graphics... " << endl; 
    return -1;
}

if (glewInit () != GLEW_NO_ERROR)
{
    cout << "Failed to initialize GLEW... " << endl; 
    return -1;
}

// check for required GL_VERSION and extensions here and error out if requirements not met

// and everything else should follow the same as before from here...
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
share|improve this answer
    
...and scooped by Sam! –  Darth Satan Feb 21 '13 at 23:53
    
Yeah, following the tutorial i didn't notice that there was glfwinit AND glewinit. Very bad mistake, thank you –  Dhiego Magalhães Feb 22 '13 at 0:02
    
Wait, i keep getting the same error, GenVertexArray <- memmory access violation –  Dhiego Magalhães Feb 22 '13 at 0:08
    
Try a glGetString (GL_VERSION) and tell us what it gives you. I suggest this because if everything else is correct, then the only remaining explanation is that you are getting a downlevel context (possibly Microsoft's software GL1.1 or the equivalent if on another OS); if this is the case then I recommend closing this and opening another question about not getting the correct context level for your hardware. –  Darth Satan Feb 22 '13 at 0:20

You're apparently not using Glew at all: you don't initialise the library. It looks like you haven’t even read the first sentence of the first paragraph of the GLEW documentation.

Another thing you do before even creating the OpenGL context is create a vertex buffer. That cannot work either: OpenGL doesn’t exist until you create an OpenGL context.

This is the order in which you need to do things:

  • Create an OpenGL context (using glfw)
  • Initialise GLEW
  • Use OpenGL functions
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.