Sign in to follow this  
MartinOlsen

OpenGL GUI with 3D inside 2D window.

Recommended Posts

Hi gamedev! I'm trying to do a GUI (buttons, menus, windows, etc.) in OpenGL, but have run into some trouble. First i start out in 2D (orthographic) view where i create a window (draw four white lines). Then, I would like to have a 3D (projection) view within this "window" I've created. My idea is that this "window" should display my 3D game-world. How do I accomplish this? I would suspect a view port should do the trick, but I cannot make it work. Google has failed me. Can you guys point me in the right direction, or at least slip me some clues? Thank you.

Share this post


Link to post
Share on other sites
Usually you'd just render your GUI in orthographic projection mode on top of the scene, but I guess you're going for something else...

I think "render to texture" would be the way to go for something like this. (NeHe Lesson)

Share this post


Link to post
Share on other sites
Quote:
Original post by kloffy
Usually you'd just render your GUI in orthographic projection mode on top of the scene, but I guess you're going for something else...


I know. I'll probably do it like that in the future, but right now I just want to know how to do it.

Quote:
Original post by kloffy
I think "render to texture" would be the way to go for something like this. (NeHe Lesson)


I've been looking into this method, but put it off as I thought the performance impact would be to great. Does anyone know how expensive this would be, performance wise?


I went over my code again and found a way to do it with view ports. If anyone is interested, this is how I did it:

As I said earlier, I initialize OpenGL in ortho-mode.

SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_OPENGL);

glViewport(0, 0, 640, 480);

glClearColor(0, 0, 0, 0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 640, 480, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


In my render I translate to the windows position and draw a beautiful frame :-).

glTranslatef(x, y, 0);

glBegin(GL_LINE_LOOP);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);

glVertex3f(0, 0, 0);
glVertex3f(width, 0, 0);
glVertex3f(width, height, 0);
glVertex3f(0, height, 0);
glEnd();


Then, I set the view port and change from orthographic to perspective view.

glViewport(
static_cast<GLint>(x),
480 - (static_cast<GLint>(y) + static_cast<GLsizei>(height)), // 480 MINUS (y PLUS height)
static_cast<GLsizei>(width),
static_cast<GLsizei>(height));

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(
45.0f,
static_cast<GLdouble>(width / height),
0.1f,
100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


Now I can render all my cool 3D models and and they will appear inside the view port in my window. For completion here's the two triangles I draw right now.

glTranslatef(0.0f, 0.0f, -20.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 10.0f, 0.0f);
glVertex3f(-10.0f, -10.0f, 0.0f);
glVertex3f(10.0f, -10.0f, 0.0f);
glEnd();

glTranslatef(-1.5f, 0.0f, -6.0f);
glColor4f(1.0f, 0.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();


Finally I change back to orthographic view.

glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 640, 480, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();



That seems to work for me. Any comments or suggestions?

Share this post


Link to post
Share on other sites
glViewport is indeed the key element. However, you may run into problems later on because it doesn't guarantee that the rest of the screen won't be affected by what you're drawing. For example, glClear will clear the whole screen instead of just the rectangle you defined. So you should combine the glViewport with glScissor and glEnable(GL_SCISSOR_TEST).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this