Donald Duck Donald Duck - 4 months ago 15
C Question

Draw 2D and 3D in the same OpenGL window

I'm creating a 3D game with OpenGL and I would like to make a toolbar in the top of the window. For that, I tried to use SDL to draw the buttons and OpenGL to draw the actual game. Here is the relevant part of my code:

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


The problem with this is that when this function is called, the process ends and returns 3 (which means that there is an error). So I tried to draw the buttons with OpenGL like this:

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


I know that the second code should center the button in the window, but I use this code just to test if it works (and it doesn't, that's why I'm posting this question).

With the second code, the 3D things appear in the window as they should, but I can't see any button. How do I put 2D buttons in a 3D OpenGL window?

Answer

The second code works by adding the following right before drawing the 2D button:

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

and the following code right after drawing the 2D button:

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

Here is the complete code that works:

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