尝试使用 SDL 时出现分段错误 +格鲁夫

发布于 2024-12-10 10:00:50 字数 1561 浏览 2 评论 0原文

我正在尝试学习如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-12-17 10:00:50

尝试将投影和模型视图部分放在 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;
}

Try putting the projection and modelview sections outside the while loop and the glViewport after the projection:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文