Most likely glGenBuffers is not a valid function pointer, so the call fails. Possibly glew is not correctly initialized/encounters an error while initializing (do you set glewExperimental=TRUE before glewInit?) or your context creation does not work correctly.
You’ll have to post your code where you create the OpenGL context and initialize glew to say anything more specific.
[QUOTE=carsten neumann;1253550]Most likely glGenBuffers is not a valid function pointer, so the call fails. Possibly glew is not correctly initialized/encounters an error while initializing (do you set glewExperimental=TRUE before glewInit?) or your context creation does not work correctly.
You’ll have to post your code where you create the OpenGL context and initialize glew to say anything more specific.[/QUOTE]
Thanks for the reply,
Yes I have set glewExperimental to true before it is init…
[QUOTE=Alfonse Reinheart;1253560]You have to initialize GLFW first. You need an active OpenGL context before you can load OpenGL functions or make any OpenGL calls.
This means you need to initialize GLFW, set up your window properties, and then create a window. All before touching GLEW or anything in OpenGL.[/QUOTE]
Correct me if I am wrong (Noob alert with opengl here) but isnt glfw inialized with glfwinit() and the window created with glfwCreateGraphicsWindow?
I can render basic shapes by specifying the coords using glBegin and glEnd and these render fine when entered in manually (I can render basic cubes and etc, etc…
Correct me if I am wrong (Noob alert with opengl here) but isnt glfw inialized with glfwinit() and the window created with glfwCreateGraphicsWindow?
Yes. And your call to “glewInit” comes before all of that. You have to initialize GLEW after creating the window, not before.
I can render basic shapes by specifying the coords using glBegin and glEnd and these render fine when entered in manually (I can render basic cubes and etc, etc…
That’s because GLEW’s macros pipe the GL 1.1 stuff through non-GLEW functions. Any functions from later GL versions will fail because GLEW has not been properly initialized yet.
[QUOTE=Alfonse Reinheart;1253563]Yes. And your call to “glewInit” comes before all of that. You have to initialize GLEW after creating the window, not before.
That’s because GLEW’s macros pipe the GL 1.1 stuff through non-GLEW functions. Any functions from later GL versions will fail because GLEW has not been properly initialized yet.[/QUOTE]
Ok that worked thanks!
However I cannot see anything being rendered so will have a look into this!
One more question, can someone please explain to me why this doesn’t work (causes another access violation at a memory address, alsopTR->GetVertexes() returns a std::vector<float>)?
Nothing to do with OpenGL directly. You are passing the address of a std::vector<float> to glVertexPointer, which is the address of the vector not the data it stores (which is what you want to pass to OpenGL) - you’d want &(pTR->GetVertexes().front()) for example. Additionally, your function returns the std::vector<float> by value (i.e. it makes a copy), so you are taking the address of a temporary that is deleted immediately after the glVertexPointer call - it’s also inefficient because you keep copying the potentially large vertex data. Either assign the return value from GetVertexes() to a local variable (which still makes the copy, but keeps it alive until the end of scope):
std::vector<float> v = pTR->GetVertexes();
glVertexPointer(3, GL_FLOAT, 0, &v.front());
Or change your function to return a const reference (but make sure you understand what that does!):
If any of this seems confusing, I’d recommend reading up on C++ object live time and memory management, you are likely to keep running into similar problems otherwise.
Luckily it all seems to make sense , I take it we obviously don’t want it to create a new vector with a different memory address (as we want it to point to the start of the vector) so it is declared as constant. Also we don’t want duplicated data in the memory.!
The above returns a copy of the first element in the vector. You want to return (and store) either a reference or pointer to the first element in the vector. The fact that this isn’t immediately obvious to you indicates that you’re still at a very early stage in learning C++. You need to learn C++ first before you start trying to learn how to use OpenGL (or anything else) in a C++ program.
Hmm, this is moving off-topic and into how pointers, memory, and object life time in C++ work and I really think you’d make your life easier if you’d review those issues outside of your concrete problem. Graphics programming is to a good extent about managing the memory for your assets.
The front() function for std::vector returns (a reference to) the first element in the vector which you then assign to the local variable verteciesFront. You then pass the address of that local variable to glVertexPointer, which is very different from passing the address of the first element in the vector.
[QUOTE=carsten neumann;1253665]Hmm, this is moving off-topic and into how pointers, memory, and object life time in C++ work and I really think you’d make your life easier if you’d review those issues outside of your concrete problem. Graphics programming is to a good extent about managing the memory for your assets.
The front() function for std::vector returns (a reference to) the first element in the vector which you then assign to the local variable verteciesFront. You then pass the address of that local variable to glVertexPointer, which is very different from passing the address of the first element in the vector.[/QUOTE]
I have been reviewing it since you suggested it, it has been a while since I have had to do memory management properly…
Hi Alfonse Reinheart…could you help me please! I’ve been stuck on this for hours…I’m having the same access violation when calling glGenbuffers. I made sure I followed these steps exactly (initializing glfw, setting window properties, then creating the window before glew calls) and what I’ve done is in line with everything else I’ve found online. I’m rather new to openGL so I’m really lost as to what could be going wrong.
This is how I’ve written it:
glfwInit();
glewExperimental = GL_TRUE;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(1200, 800, "Game", nullptr, nullptr); // Windowed
glewInit();
typedef void(*GENBUFFERS) (GLsizei, GLuint*);
// Load address of function and assign it to a function pointer
GENBUFFERS glGenBuffers = (GENBUFFERS)wglGetProcAddress("glGenBuffers");
// Call function as normal
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u