开发者

Primitive Restart Index error in C++ with OpenGL

I'm just learning OpenGl and trying to implement a simple test of how the Primitive Restart Index works. No matter what I try, g++ gives me the error "undefined reference to `__glewPrimitiveRestartIndex'."

Here's th开发者_如何转开发e code in question:

#include <GL/glew.h>
#include <GL/freeglut.h>

static GLfloat vertices[] = {0.0, 0.0, 0.0, 5.0, -5.0, 0.0,
0xffff, 0.0, -10.0, 5.0, -15.0, 6.0, -8.0};

void init(void)
{
    glEnable(GL_PRIMITIVE_RESTART);
    glPrimitiveRestartIndex(0xffff);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, vertices);
}

I admit that I'm a bit new to using g++, and don't fully understand its switch and include mechanisms. The g++ command I'm using to compile it is as follows: g++ -o test test.cpp -lGL -lglut

Upon appending "-lGLEW" to the command, the program compiles but hands me a segfault. Removing the line "glPrimitiveRestartIndex(0xffff);" from the code makes it compile and run without fault (of course, without the primitive restart index working) when -lGLEW is appended to the compile command, but hands the same error when trying without.

This leads me to believe that the initial problem results from not having included -lGLEW - so the only remaining issue is figuring out why I'm being handed a segfault. I've tried with different values other than 0xffff, but the problem remains.


You should not call OpenGL functions until you have actually loaded them. Since you're using GLEW, you should use GLEW's initialization routines before calling GL functions. See GLEW's documentation for details.


Oh, and that's not how primitive restart works. The restart index is an index, not a vertex position. It should go in your list of indices, what you give to glDrawElements. If you don't have a list of indices and are drawing with glDrawArrays, you cannot use primitive restart.


This leads me to believe that the initial problem results from not having included -lGLEW - so the only remaining issue is figuring out why I'm being handed a segfault. I've tried with different values other than 0xffff, but the problem remains.

The problem is not the value you pass, but that GLEW is not initialized, thus glPrimitiveRestartIndex an invalid pointer and trying to dereference/call it will result in undefined behaviour. You must initialize GLEW after creating and making current a OpenGL context. In the case of GLUT being used this is after calling glutCreateWindow, so your code should read somewhat like this:

glutCreateWindow("...");
glewInit();

Also you must check if your desired extensions are actually present. And like @Nicol Bolas already told you, the primitive start index goes into the index array passed to glDrawElements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜