gpt4 book ai didi

opengl - 尝试使用 SDL + GLEW 时出现段错误

转载 作者:行者123 更新时间:2023-12-01 23:06:53 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 GLEW 作为扩展方法将 SDL 与 OpenGL 结合使用。据我从 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;
}

然而,它只是在第 12 行尝试调用 glViewport 时出现段错误。这是在 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 版。

最佳答案

尝试将投影和模型 View 部分放在 while 循环和投影之后的 glViewport 之外:

#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);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, 640, 480);
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);

while (1) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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;
}

关于opengl - 尝试使用 SDL + GLEW 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7789072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com