尝试使用 SDL 时出现分段错误 +格鲁夫
我正在尝试学习如何将 SDL 与 OpenGL 结合使用,并使用 GLEW 作为扩展方法。据我从 Using OpenGL with SDL 从 SDL 角落来看,以下代码应该可以工作
#include <glew.h>
#include <SDL.h>
#include <cstdlib>
int main(int argc, char *argv[]) {
if (SDL_Init( SDL_INIT_EVERYTHING ) != 0) exit(EXIT_FAILURE);
if (SDL_GL_LoadLibrary( NULL ) != 0) exit(EXIT_FAILURE);
if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) exit(EXIT_FAILURE);
if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
glViewport(0, 0, 640, 480);
while (1) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_QUADS);
glColor3f(1, 0, 0); glVertex3f(0, 0, 0);
glColor3f(1, 1, 0); glVertex3f(3, 0, 0);
glColor3f(1, 0, 1); glVertex3f(3, 3, 0);
glColor3f(1, 1, 1); glVertex3f(0, 3, 0);
glEnd();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
,但是当尝试调用 glViewport
时,它只是在第 12 行出现段错误。这是在 OS X 10.7 上编译的:
clang++ -g $(pkg-config --cflags sdl gl glu glew) -o test test.cpp $(pkg-config --libs sdl gl glu glew)
SDL 版本为 1.2.14,GLEW 版本为 1.7.0。
I'm attempting to learn how to use SDL with OpenGL using GLEW for the extension methods. As far as I can tell from pages such as Using OpenGL with SDL from SDL corner the following code should work
#include <glew.h>
#include <SDL.h>
#include <cstdlib>
int main(int argc, char *argv[]) {
if (SDL_Init( SDL_INIT_EVERYTHING ) != 0) exit(EXIT_FAILURE);
if (SDL_GL_LoadLibrary( NULL ) != 0) exit(EXIT_FAILURE);
if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) exit(EXIT_FAILURE);
if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
glViewport(0, 0, 640, 480);
while (1) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_QUADS);
glColor3f(1, 0, 0); glVertex3f(0, 0, 0);
glColor3f(1, 1, 0); glVertex3f(3, 0, 0);
glColor3f(1, 0, 1); glVertex3f(3, 3, 0);
glColor3f(1, 1, 1); glVertex3f(0, 3, 0);
glEnd();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
However it just segfaults on line 12 when trying to call glViewport
. This is being compiled on OS X 10.7 with:
clang++ -g $(pkg-config --cflags sdl gl glu glew) -o test test.cpp $(pkg-config --libs sdl gl glu glew)
SDL is version 1.2.14 and GLEW is version 1.7.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将投影和模型视图部分放在 while 循环之外,并将 glViewport 放在投影之后:
Try putting the projection and modelview sections outside the while loop and the glViewport after the projection: