Sign in to follow this  

multiple glViewports

This topic is 3598 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

If this has been addressed elsewhere in recent forum or article history, please post me on but I couldn't find anything so I'm posting here... I'm creating two VPs, which now I've got the model-view matrices clearing and setting up properly, do work in terms of their existence. What I'm having trouble with is setting the background colour. Is it even possible to have have different background for each viewport? This produces a white background:
//---------------------------------------------------
// For whole Window: 

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//---------------------------------------------------
// Main ViewPort:  _vp1
//

_vp1.Prepare(true);              // Size = screen-width x (screen-height -64px)
glClearColor(0.0, 0.0, 0.0, 1.0);
DrawScene();

//---------------------------------------------------
// Toolbar ViewPort: _vp2
//

_vp2.Prepare(true);             // Size = screen-width x 64px
glClearColor(1.0, 1.0, 1.0, 1.0);
DrawUI();

//
// End of _vp2
//---------------------------------------------------
_vp1 and _vp2 are of type ViewPort. The contents of ViewPort::Prepare are below:
void ViewPort::Prepare(bool is2d) const
{		
	glViewport(_left, _bottom, _width, _height);		// set its position & size
	glMatrixMode (GL_PROJECTION);				// Select The Projection Matrix
	glLoadIdentity ();					// Reset The Projection Matrix	

	if(is2d==true)
		{gluOrtho2D(-_width/2,+_width/2, -_height/2, +_height/2);}  //gives a 1:1 screen-space to world space ratio

	else
                {return;//actually - put in the 3d stuff here!}

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
//---------------------------------------------------------
//
// End of code



What I want to achive is for _vp1 to appear with a black background and _vp2 to appear with a white one. Any ideas? [Edited by - Will-O on February 9, 2008 12:29:44 PM]

Share this post


Link to post
Share on other sites
Just to clarify...
Where should I be calling what?
Writing it as below means the screen flickers in a most disturbing way and I don't think it's supposed to, when doing this correctly!

//---------------------------------------------------
// For whole Window: DrawWin

glEnable(GL_SCISSOR_TEST);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//---------------------------------------------------
//
// Main ViewPort: _vp1
//

_vp1.Prepare(true);
glClearColor(0.0, 0.0, 0.0, 1.0);
DrawScene();

//---------------------------------------------------
//
// Toolbar ViewPort: _vp2
//

_vp2.Prepare(true);
glClearColor(1.0, 1.0, 1.0, 1.0);
DrawUI();
glDisable(GL_SCISSOR_TEST);


Then viewport::prepare is

void ViewPort::Prepare(bool is2d) const
{
glViewport(_left, _bottom, _width, _height); // set its position & size
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

if(is2d==true)
{gluOrtho2D(-_width/2, +_width/2, -_height/2, +_height/2);}

else
{return;/*actually - put in the 3d stuff here!*/}

glMatrixMode(GL_MODELVIEW);
glScissor(-_width/2, +_width/2, _width/2, _height/2); // do the SCISSOR test!
glLoadIdentity();
}



[Edited by - Will-O on February 9, 2008 12:34:15 PM]

Share this post


Link to post
Share on other sites
Thanks, that's a bit better - ie view-ports actually work but still not great because I'm trying to give then a different background colour.

Draw function

_vp1.Prepare(true,0.0, 0.0, 0.0, 1.0);
DrawScene();

_vp2.Prepare(true,1.0, 1.0, 1.0, 1.0);
DrawUI();


void ViewPort::Prepare(bool is2d, const float R, const float G, const float B, const float A) const
{
glViewport(_left, _bottom, _width, _height); // set its position & size
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

if(is2d==true)
{gluOrtho2D(-_width/2, +_width/2, -_height/2, +_height/2);} // 2D projection
else
{return;/*actually - put in the 3d stuff here!*/} // 3D projection

glEnable(GL_SCISSOR_TEST);
glScissor(_left, _bottom, _width, _height); // do the SCISSOR test!
glDisable(GL_SCISSOR_TEST);

//glClear/*Color*/(GL_COLOR_BUFFER_BIT)/*(R, G, B, A)*/;
//glColor(R, G, B, A);

}



The last bits - with glClear / glClearColor - how do I get them to work so I have different backgrounds in different view ports?

Share this post


Link to post
Share on other sites

glEnable(GL_SCISSOR_TEST)

glScissor(a,b,c,d) // one viewport (window coordinates)
glClearColor(... // one color
glClear(GL_COLOR_BUFFER_BIT) // clears just the area set by scissor

glScissor(e,f,g,h) // another viewport (window coordinates)
glClearColor(... // another color
glClear(GL_COLOR_BUFFER_BIT)

.. and so on

glDisable(GL_SCISSOR_TEST)


Share this post


Link to post
Share on other sites
Why are you disabling the scissor test as soon as you enable it? Of course the scissor test won't do anything if you disable it as soon as you enable it.

If you need to deal with multiple viewports and have a good reason not to to it like this, always follow a call to glViewport by a corresponding call to glScissor, and have the scissor test enabled at all time.


glViewport(viewport 1)
glScissor(viewport 1)
glClear(...)
Draw viewport 1 here

glViewport(viewport 2)
glScissor(viewport 2)
glClear(...)
Draw viewport 2 here

... and so on, for each viewport.

Share this post


Link to post
Share on other sites
That's excellent. Thank you very much, indeed!

Code now reads...
A. In DrawWin.cpp
void DrawWin::OnDisplay()
{
//---------------------------------------------------
// For whole Window: DrawWin
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);

//---------------------------------------------------
// Main ViewPort: _vp1
_vp1.Prepare(true,0.0, 0.0, 0.0, 1.0);
DrawScene();

//---------------------------------------------------
// UI ViewPort: _vp2
_vp2.Prepare(true,1.0, 1.0, 1.0, 1.0);
DrawUI();

glDisable(GL_SCISSOR_TEST);
SwapBuffers();
}



B. In ViewPort.cpp...
void ViewPort::Prepare(bool is2d, const float R, const float G, const float B, const float A) const
{
glViewport(_left, _bottom, _width, _height); // set its position & size
glMatrixMode(GL_MODELVIEW); // modelling & viewing matrix
glLoadIdentity(); // reset the MV matrix
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

if(is2d==true)
{gluOrtho2D(-_width/2, +_width/2, -_height/2, +_height/2);} // 2D projection
else
{return;/*actually - put in the 3d stuff here!*/} // 3D projection

glScissor(+_left, +_bottom, _width, _height); // do the SCISSOR test!
glClearColor(R, G, B, A); // Clear with colour RGBA
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // clear the buffers...
}

Share this post


Link to post
Share on other sites

This topic is 3598 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.

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