c - 在同一个 OpenGL 窗口中绘制 2D 和 3D

标签 c opengl 3d 2d sdl

我正在使用 OpenGL 创建 3D 游戏,我想在窗口顶部创建一个工具栏。为此,我尝试使用 SDL 来绘制按钮,并使用 OpenGL 来绘制实际的游戏。这是我的代码的相关部分:

void openMainWindow(){
    SDL_Surface *screen;
    SDL_Event event;
    SDL_Rect position;
    SDL_Init(SDL_INIT_VIDEO);
    putenv("SDL_VIDEO_CENTERED=center");
    SDL_WM_SetCaption("Example",NULL);
    SDL_WM_SetIcon(IMG_Load("icon.png"),NULL);
    screen = SDL_SetVideoMode(832,487,32,SDL_HWSURFACE | SDL_OPENGL);
    glLoadIdentity();
    gluPerspective(70,(double)832/487,1,1000);
    //Some things to initialize the window
    int continue = 1;
    while(continue){
        SDL_PollEvent(&event);
        switch(event.type){
            //Events
        }
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
            //Draw a square
        glEnd();
        //Draw more things the same way
        glFlush();
        SDL_GL_SwapBuffers();
        SDL_Surface *button1 = SDL_CreateRGBSurface(SDL_HWSURFACE,50,50,32,0,0,0,0);
        SDL_FillRect(button1,NULL,SDL_MapRGB(screen->format,50,50,50);
        position.x = 8;
        position.y = 8;
        SDL_BlitSurface(button1,NULL,screen,&position);
        SDL_Flip(screen);
    }
    SDL_Quit();
}

这样做的问题是,当调用这个函数时,进程结束并返回3(这意味着有错误)。所以我尝试用 OpenGL 来绘制按钮,如下所示:

void openMainWindow(){
    //Everything before the while loop is the same as in the other code
    int continue = 1;
    while(continue){
        SDL_PollEvent(&event);
        switch(event.type){
            //Events
        }
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
            //Draw a square
        glEnd();
        //Draw more things the same way
        glDisable(GL_DEPTH_TEST);
        glLoadIdentity();
        glBegin(GL_QUADS);    //Draw the button
            glColor3ub(50,50,50);
            glVertex2d(-0.5,-0.5);
            glVertex2d(-0.5,0.5);
            glVertex2d(0.5,0.5);
            glVertex2d(0.5,-0.5);
        glEnd();
        glEnable(GL_DEPTH_TEST);
        glFlush();
        SDL_GL_SwapBuffers();
    }
    SDL_Quit();
}

我知道第二个代码应该使按钮在窗口中居中,但我使用此代码只是为了测试它是否有效(事实并非如此,这就是我发布此问题的原因)。

使用第二个代码,3D 内容按其应有的方式出现在窗口中,但我看不到任何按钮。如何将 2D 按钮放入 3D OpenGL 窗口中?

最佳答案

第二个代码的工作原理是在绘制 2D 按钮之前添加以下代码:

glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();

以及绘制 2D 按钮后的以下代码:

glEnable(GL_DEPTH_TEST);
gluPerspective(70,(double)640/480,0.5,INFINITE);   //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
gluLookAt(0,0,3,1,0,3,0,0,0.01);    //Where you want to position the camera and where you want to look at
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

这是完整的代码:

void openMainWindow(){
    //Everything before the while loop is the same as in the other code
    int continue = 1;
    while(continue){
        SDL_PollEvent(&event);
        switch(event.type){
            //Events
        }
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
            //Draw a square
        glEnd();
        //Draw more things the same way
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glDisable(GL_DEPTH_TEST);
        glLoadIdentity();
        glBegin(GL_QUADS);    //Draw the button
            glColor3ub(50,50,50);
            glVertex2d(-0.5,-0.5);
            glVertex2d(-0.5,0.5);
            glVertex2d(0.5,0.5);
            glVertex2d(0.5,-0.5);
        glEnd();
        glEnable(GL_DEPTH_TEST);
        gluPerspective(70,(double)640/480,0.5,INFINITE);   //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
        gluLookAt(0,0,3,1,0,3,0,0,0.01);    //Where you want to position the camera and where you want to look at
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glFlush();
        SDL_GL_SwapBuffers();
    }
    SDL_Quit();
}

关于c - 在同一个 OpenGL 窗口中绘制 2D 和 3D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611423/

相关文章:

c - _通用 : multiple types to a single value?

c - 在 C、Linux 中递归列出目录和文件时出现段错误(核心已转储)

linux - 当 GLEW_ARB_framebuffer_object 不为零时,为什么 glFramebufferTexture 是 NULL 指针?

python - 在 python 中交互式地旋转 3D 图 - matplotlib - Jupyter Notebook

3d - 四元数 - 如何限制轴?

c++ - 每轴网格属性(颜色),它是如何工作的?

c - 为什么我的 int 变量值突然跳跃?

c - 为什么下面的函数没有被内联?

c++ - 让火浮起来

c++ - 如何在 glsl 中编写基本着色器?