Selection mode with multiple viewports

I have just added a second viewport to my openGL window, and now the selection (picking) process for objects drawn in the first viewport no longer works. The reason is simple: my call to glGetIntegerv(GL_VIEWPORT, viewport) keeps returning the SECOND VIEWPORT, but I need it to return the FIRST viewport. The reason this function keeps returning the SECOND viewport is because the second viewport was the last viewport that I drew. FYI, I must draw the second viewport last. It cannot be drawn first. So my question is, how do I get the FIRST viewport when handling the selection process?

Here’s my code in OnLeftButtonDown(…):
{

  //if picking mode...l	
if(m_bMouseControlState == NODEPICKING)
{
	GLint hits;
	GLint viewport[4];

	GLint iDepth;

	glGetIntegerv(GL_MAX_NAME_STACK_DEPTH, &iDepth);

           //fetch the viewport (this is the line that gets the second viewport;  I NEED THE FIRST VIEWPORT to be returned in "viewport")
	glGetIntegerv(GL_VIEWPORT, viewport);

           ................
          .................
          ................
           ...............

    }

}

Thanks very much.

Hardly an advanced OpenGL question.
OpenGL is a state machine, whatever is set last in an OpenGL context is returned on glGet calls.
The viewport is just one of the transformations in the OpenGL pipeline. If you want to pick in the first viewport, you need to set it up the exact same way as during rendering, except for the additional pick matrix.

Use wglMakeCurrent hdc,hrc to make a rendering context the current one. You should also use wglShareLists Mainhrc,Newhrc on every rendering context you create after the first one.

Just to ensure this point: To my point of view, I really don’t see why he would need GL context manipulation tools… Viewports are not context sensitive, you can use multiple viewports within the same context without any problem at all.

Of course, you can harden things: make a multithreading program in which each thread renders to a specific context and a specific viewport… But I’m pretty sure he doesn’t need it.

actually you don’t really need glGetIntegerv(GL_VIEWPORT,…), because it will return the same values you are using with glViewport before you draw the first viewport.

Great suggestions, all. I’m very impressed. Thank you. But alas, I overlooked the obvious. Since my first viewport is the entire window (the second viewport is a little corner within this window), all I need to do to “fetch” the dimensions of the first viewport is to call GetClientRect(&rectForFirstViewport); This gets me the width and height of the window. Then I stick these dimensions into my viewport variable viewport[4], which I use to define my picking region for selections in my first viewport. It looks something like this:

CRect rectViewport;
GetClientRect( &rectViewport );
int nWndWidth = rectViewport.Width();
int nWndHeight = rectViewport.Height();

GLint viewport[4];
viewport[0] = 0; //x starting coord
viewport[1] = 0; //y starting coord
viewport[2] = nWndWidth; //viewport width
viewport[3] = nWndHeight; //viewport height

//assign buffer size for clickable elements
glSelectBuffer(2*nClickableElementCount,selectBuf);
glRenderMode (GL_SELECT);

glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();

glLoadIdentity();

//define the picking region, for selection
//(THIS IS WHAT I NEEDED THE FIRST VIEWPORT FOR)
gluPickMatrix((GLdouble)point.x, (GLdouble)(viewport[3] - point.y), 1.0, 1.0, viewport);



Thanks again, to all.