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

唐老鸭

我正在使用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事物将按原样出现在窗口中,但是我看不到任何按钮。如何在3D OpenGL窗口中放置2D按钮?

唐老鸭

第二个代码通过在绘制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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python在同一窗口中渲染3D,2D图像

如何将多个图形(由循环加载)绘制到同一个 3D 图中?

R-在一个窗口中有多个3D散点图

Python-一个窗口中有多个3D图

在一个3d图形中绘制多个2d等高线图[Matlab]

一个人如何在2D模式下绘制3D矩阵?

通过索引一个2D矩阵和另一个2D矩阵来向量化创建3D矩阵

无论如何要在qt的同一个窗口中放置一个QTreeView和一个QChartView?

如何在同一个2D数组中使用两个构造函数?爪哇

使用scipy / voronoi_plot_2d在同一窗口中重新绘制图

在具有重力形式的 Magnific 弹出窗口中的同一个弹出窗口中显示“谢谢消息”

更好地使用一个3d数组或两个2d?

如何在同一个弹出窗口中显示多个不同的 div?

pyQt5 在同一个窗口中添加多张图片

python 不在同一个 cmd 窗口中运行

在一个3D中绘制多个直方图

如何在openGL c ++中的同一窗口中绘制两个对象?

沿着2D数组的最后一个轴滑动窗口以使用NumPy步幅生成3D数组

将2d矩阵转换为3d一个热矩阵numpy

总结3D矩阵的第一个2D

用重复的2D位置递增一个numpy的3D数组

将2D地图坐标绘制到OpenGL 3D投影

如何在3D OpenGL场景上绘制SDL 2D矩形?

opengl 2d游戏只绘制第二个精灵纹理而不是第一个

构建一个表示 3D 平面的 2D 数组(存储其 Z 值),由 3 个点和所需的数组大小定义

在3d阵列中找到与另一个3d阵列中的2d元素相似的2d元素

Electron:如何在同一个窗口中打开一个 url 并在用户执行某些操作后返回同一个窗口中的应用程序

使用plot3d在单个绘图窗口中绘制多个3D图像

cv2-在同一窗口中显示图像,一个接一个