How to apply multiple viewports?

Is there any solution to display multiple viewports on a GL window?
For an exampler, I’d like to make a racing game, which allows two player on just one PC. How can I render 2 different part of the same scene, which are specified by two different viewpoint?

I think you have to store the render area for each player and maybe a camera object too.

Then when you want to render the view for a player, you define the viewport to the player’s area and setup the current projection and model view matrix to the current player’s.

I think that should work, but be carefull with the glClear function that clear the entire OpenGL context.

Otherwise, you could use two rendering contexts.

glViewport can be used to select a portion of the context to work in.

Be careful to reset your projections when you switch which one you are rendering in, and judicious use of the matrix stacks could be in order.

Good luck.

So, I must alternatively switch between viewports, right? Doesn’t it sound a little bit slow? Is there any, say, simultaneous way to apply that situation?

Nope, you clear the screen - then set your viewport A, decide what projection to use, switch back to the modelview matrix and render the scene.

Then, set your viewport B, decide what projection to use, switch back to the modelview matrix and render the scene again (maybe from a different angle or something).

The calculations for the extra transformations are tiny compared to the stuff for the actual filling of the viewports…

Thanks guys, now I did it :slight_smile:
Anyway, there’s no way to create multiple viewpoints at the same time, right!
I thought I could draw the scene once, then select viewpoints for each viewport, and render all of them! But that’s impossoble, isn’t it?