C++ 中的原始重启索引错误与OpenGL
我正在学习 OpenGL 并尝试对原始重启索引的工作原理进行简单的测试。无论我尝试什么,g++ 都会给出错误“对`__glewPrimitiveRestartIndex'的未定义引用”。
这是有问题的代码:
#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);
}
我承认我对使用 g++ 有点陌生,并且不完全理解它的 switch 和 include 机制。我用来编译它的 g++ 命令如下: g++ -o test test.cpp -lGL -lglut
在将“-lGLEW”附加到命令后,程序可以编译,但会出现段错误。删除行“glPrimitiveRestartIndex(0xffff);”当 -lGLEW 附加到编译命令时,从代码中可以使其编译和运行没有错误(当然,没有原始重新启动索引工作),但在尝试不添加 -lGLEW 时会出现相同的错误。
这让我相信最初的问题是由于没有包含 -lGLEW 造成的 - 所以唯一剩下的问题是弄清楚为什么我会遇到段错误。我尝试过使用 0xffff 以外的其他值,但问题仍然存在。
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 the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于您传递的值,而是 GLEW 未初始化,因此
glPrimitiveRestartIndex
是一个无效的指针,尝试取消引用/调用它将导致未定义的行为。创建 OpenGL 上下文并将其设置为当前上下文后,必须初始化 GLEW。在使用 GLUT 的情况下,这是在调用glutCreateWindow
之后,因此您的代码应该类似于这样:此外,您还必须检查您想要的扩展是否确实存在。就像 @Nicol Bolas 已经告诉你的那样,原始起始索引进入传递给
glDrawElements
的索引数组。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 callingglutCreateWindow
, so your code should read somewhat like this: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
.在实际加载 OpenGL 函数之前,不应调用它们。由于您使用的是 GLEW,因此应该在调用 GL 函数之前使用 GLEW 的初始化例程。有关详细信息,请参阅 GLEW 的文档。
哦,这不是原始重启的工作原理。重新启动索引是一个索引,而不是顶点位置。它应该位于您的索引列表中,即您为
glDrawElements
提供的内容。如果您没有索引列表并且使用glDrawArrays
进行绘制,则无法使用原始重新启动。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 withglDrawArrays
, you cannot use primitive restart.