• Advertisement
Sign in to follow this  

Viewports Viewports

This topic is 4274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to render multiple viewports onto a window and have viewports of different size but I can't get it to work with glViewport: glViewport( 0, 0, WinWidth, WinHeight ); glClearColor(1.0f, 1.0f, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glViewport( 0, 0, WinWidth * 0.5f, WinHeight * 0.5f ); glClearColor(1.0f, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glutSwapBuffers(); What gets clear is the second one, but to the whole window.

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what you're trying to do. But glViewport will only set the size of your viewport...

Share this post


Link to post
Share on other sites
Looks like glClear() clears the whole window instead of just the viewport.

edit: The docs say it should just clear the viewport.

[Edited by - Boder on May 8, 2006 2:05:07 AM]

Share this post


Link to post
Share on other sites
So how to clear only the viewport? If you've every use D3D, setting the viewport only clear a portion associated with the viewport.

Share this post


Link to post
Share on other sites
My only guess from the code is that somehow the second call to glViewport() takes up the whole window.

Share this post


Link to post
Share on other sites
As far as I am aware, glClear(GL_COLOR_BUFFER_BIT) clears the _whole_ color buffer. glViewport will only affect the normalised device coord to window space transformation. To clear a single viewport just draw a quad of the required colour in the viewport - it isn't ideal but it's quick and it works.

Share this post


Link to post
Share on other sites
I guess the documentation means "window" when they say "the viewport"

I guess you'll have to get out your scissors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kuladus
As far as I am aware, glClear(GL_COLOR_BUFFER_BIT) clears the _whole_ color buffer. glViewport will only affect the normalised device coord to window space transformation. To clear a single viewport just draw a quad of the required colour in the viewport - it isn't ideal but it's quick and it works.


Just to notice that with this method, others buffers won't be cleared (depth, accum...).

Share this post


Link to post
Share on other sites
Hi, Simplicity!
Things are not ok in your code, so I'll point them:


// Your first example
glViewport( 0, 0, WinWidth, WinHeight );
glClearColor(1.0f, 1.0f, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport( 0, 0, WinWidth * 0.5f, WinHeight * 0.5f );
glClearColor(1.0f, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();



What you are doing here is telling your OpenGL program to (supposing WinWidth == 800 and WinHwight == 600):
1- Set a viewport with bottom-left at (0,0) and top-right at (800,600);
2- Clear ALL the screen to yellow (glClear will not distinguish between viewports);
3- Set a viewport with bottom-left at (0,0) and top-right at (400,300);
4- Clear ALL the screen (again?!) to red;
5- Swap the front and back buffers

This is wrong for multiple reasons. You should try something like this simplified example (I can't test it right now, sorry):


////////////////////
// FIRST VIEWPORT //
////////////////////

// Be sure you are handling the projection matrix
glMatrixMode( GL_PROJECTION );

// Reset the projection matrix
glLoadIdentity();

// Set the desired viewport
glViewport( 0, 0, WinWidth, WinHeight );

// Set the type of the projection matrix. [tt]glOrtho[/tt] is for orthogonal
// projection (for perspective, use [tt]gluPerspective[/tt] or [tt]glFrustum[/tt]). Remember that the bottom-left coordinate (in this case, "0,0", is RELATIVE to the bottom-left of the viewport)
glOrtho( 0, WinWidth, 0, WinHeight, -1, 1 );

// From now on, use the modelview matrix to animate my scene
glMatrixMode( GL_MODELVIEW );

// Reset the modelview matrix
glLoadIdentity();

// Draw a square in the (0, 0, 800, 600) viewport
square.draw();

// Remember to clear the depth buffer (if you use it, of course) or strange
// things will happen to your scene. DON'T CLEAR the color buffer (only at the
// END OF ALL THE DRAWING PROCESS)
glClear( GL_DEPTH_BUFFER_BIT );

/////////////////////
// SECOND VIEWPORT //
/////////////////////

// Be sure you are handling the projection matrix
glMatrixMode( GL_PROJECTION );

// Reset the projection matrix
glLoadIdentity();

// Set the desired viewport (it will be drawn OVER the first viewport. Is this
// what you really want? Whathever...
glViewport( 0, 0, WinWidth * 0.5f, WinHeight* 0.5f );

// Set the type of the projection matrix. [tt]glOrtho[/tt] is for orthogonal
// projection (for perspective, use [tt]gluPerspective[/tt] or
// [tt]glFrustum[/tt]). Remember that the bottom-left coordinate (in this case,
// "0,0", is RELATIVE to the bottom-left of the viewport).
// WARNING WARNING: if your viewport has 400x300 pixels, this will PAST the
// bounds of it
glOrtho( 0, WinWidth, 0, WinHeight, -1, 1 );

// From now on, use the modelview matrix to animate my scene
glMatrixMode( GL_MODELVIEW );

// Reset the modelview matrix
glLoadIdentity();

// Draw a diamond in the (0, 0, 400, 300) viewport
diamond.draw();

// Remember to clear the depth buffer (if you use it, of course) or strange
// things will happen to your scene. DON'T CLEAR the color buffer (only at the
// END OF ALL THE DRAWING PROCESS)
glClear( GL_DEPTH_BUFFER_BIT );

///////////////
// FINISHING //
///////////////

// Clear the color buffer
glClear( GL_COLOR_BUFFER_BIT );

// Swap the front and back buffers
glutSwapBuffers();



Hope it helps! Goodbye!

Share this post


Link to post
Share on other sites
Quote:
Original post by iliak
Try to cheat with glScissor()...


I don't think its a cheat, I'm pretty sure its the right way todo.. infact, I seem to recall a FAQ entry on opengl.org which yelled 'glViewport doesnt clip', and that the scissor test is required to only clear certain areas of the screen.

Share this post


Link to post
Share on other sites
So the what does glViewport actually do? And interm of comparison to D3D viewport. I'm trying to write an engine that needs to have the same behavior for D3D and OpenGL incase you're wondering.

Thanks for the help by the way.

Share this post


Link to post
Share on other sites
When the geometry have passed the perspective division, the coordinates are in normalized device coordinates. That is, in the range [-1, 1] along all axes. glViewport only defines the transformation from normalized device coordinates to window coordinates in pixel units so you can decide where on the window you want the normalized device coordinates to go. That's all it does.

If you want to stop drawing outside the viewport completely (it is possible to draw outside it actually), or restrict the area cleared by glClear, you have to use the scissor box.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement