Skip to main content
GameDev.net gamedev.net
🔒 Locked

Multiple cameras in OpenGL?

Started by MrSandman666 Sep 28, 2000 at 8:18 PM 3 replies 11.6k views
Original Post
MrSandman666
MrSandman666
Is it possible to have more than one camera and have them render at different areas on the screen (but in the same window) at the same time? And if so, how can it be done?
-----------------------------"Mr Sandman bring me a dream"
fifty1
fifty1
Ya, it''s possible. You render the view from each camera sequentially. To switch to a new camera, change your viewport, projection and modelview matrices to whatever''s appropriate. The viewport is not guaranteed to clip pixels, so if any of your rendering might fall outside the viewport, be sure to use glScissor().

Example:

// prelims
glEnable(GL_SCISSOR_TEST);

// camera 1
glViewport(...);
glScissor(...); // match viewport extents
glMatrixMode(GL_PROJECTION);
gluOrtho(...);
glMatrixMode(GL_MODELVIEW);
glTranslatef(...);
glRotatef(...);
...(rendering for camera 1)

// camera 2
glViewport(...);
glScissor(...); // match viewport extents
glMatrixMode(GL_PROJECTION);
gluOrtho(...);
glMatrixMode(GL_MODELVIEW);
glTranslatef(...);
glRotatef(...);
...(rendering for camera 2)

etc.

Hope this helps.

MrSandman666
MrSandman666
Ok, Thanx a lot!
I''ll try that out.
-----------------------------"Mr Sandman bring me a dream"
BlinksTale
BlinksTale
Bumping old post because this is a really nice easy way to do splitscreen, but I ran into some problems that I'd like others to not have to repeat:

glEnable(GL_SCISSOR_TEST) can be in an initialization stage, but it must come after the window is already created. I found this by having it not work in initialization, then having it work if called every loop - so of course I'd like no one else to discover it that way :)

glScissors and glViewport must be called (in that order) once for every screen BEFORE glClear or glMatrixMode(GL_MODELVIEW) (which also must both come in that order).

Then glLoadIdentity and glClearColor can be called.

glutFlush/glutSwapBuffers must come only once at the very end of all screens being drawn.

And those were my findings! I hope that helps someone else to cut down on their time on this, because it's a really cool feature and I love that it can be implemented this simply!

Thanks again fifty1!











Brother Bob
Brother Bob
[quote name='BlinksTale' timestamp='1333226929' post='4927014']
glScissors and glViewport must be called (in that order) once for every screen BEFORE glClear or glMatrixMode(GL_MODELVIEW) (which also must both come in that order).

Then glLoadIdentity and glClearColor can be called.
[/quote]
Many of your statements about in what order commands have to be called are wrong. The scissor box and the viewport box can be configured in any order; matrix operations can be performed anywhere and has no effect on, or are not affected by, neither the viewport or scissor boxes nor the buffer clear. Also, calling glClearColor after clearing the buffer is not wrong by itself, but may not be what you intend if you want to clear the box with a specific color. You need to set the color before clearing.

Otherwise, dependent commands of course have to appear in order (if you want to reset the modelview matrix, then activate the modelview matrix and then reset it). But independent commands like glViewport, glScissor and glMatrixMode can all be called in any relative order.

What you have to look out for though is glClear when you use a scissor box. The scissor box is one of very few states that affect glClear, so you have to be sure to set the box before clearing the designated area. But you can clear the entire window with one scissor box; you may not have to clear each viewport individually. The corner cases you need to look out for are overlapping viewports and if the intersection of all viewports don't cover the whole window.

[quote name='BlinksTale' timestamp='1333226929' post='4927014']
glutFlush/glutSwapBuffers must come only once at the very end of all screens being drawn.
[/quote]
This point is important though. No matter how many viewports you have, there is only one buffer that has to be swapped. So swap once when all viewports are rendered.













Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.