tags:

views:

59

answers:

3

I've got a screen-aligned quad, and I'd like to zoom into an arbitrary rectangle within that quad, but I'm not getting my math right.

I think I've got the translate worked out, just not the scaling. Basically, my code is the following:

//
// render once zoomed in
glPushMatrix();
glTranslatef(offX, offY, 0);
glScalef(?wtf?, ?wtf?, 1.0f);

RenderQuad();

glPopMatrix();

//
// render PIP display
glPushMatrix();
glTranslatef(0.7f, 0.7f, 0);
glScalef(0.175f, 0.175f, 1.0f);

RenderQuad();

glPopMatrix();

Anyone have any tips? The user selects a rect area, and then those values are passed to my rendering object as [x, y, w, h], where those values are percentages of the viewport's width and height.

A: 

When I've needed to do this, I've always just changed the parameters I passed to glOrtho, glFrustrum, gluPerspective, or whatever (whichever I was using).

Jerry Coffin
I'm using glOrtho(0, 1, 0, 1, 0, 1). I tried changing it to glOrtho(0, 0.5, 0, 0.5, 0, 1), to simulate zooming in on the bottom-left rect, but I just got a black screen of death.
mos
@mos:I'm a bit lost as to how that could happen, unless you changed something else as well (or, of course, just hadn't drawn anything in that quadrant). Presumably you're calling it following `glMatrixMode(GL_Projection); glLoadIdentity();` ?
Jerry Coffin
Ack! Sorry, when I did my test, I commented out my current code. ...Including the call to RenderQuad. Putting that part back in made it work. </sheepish>
mos
@mos: ah, that's a relief (I'm sure to you as well). Of course, *I've* never done anything like that (and if you believe that, I've got a **great** deal on some real estate that might interest you!)
Jerry Coffin
A: 

From your comment it looks like you want to draw the same quad (RenderQuad()) as full image and in PIP mode.

Assuming you have widthPIP and heightPIP and startXY position of PIP window then use widthPIP/totalWidth, heightPIP/totalHeight to scale original quad and re-render at given startXY.

Is this what you are looking for?

Ketan
+1  A: 

Given that your values are passed as [x, y, w, h] I think you want to first translate in the negative direction to get the upper left hand corner at 0,0, then scale by 1/w and 1/h to fill it to the screen. Like this:

//
// render once zoomed in
glPushMatrix();
glTranslatef(-x, -y, 0);
glScalef(1.0/w, 1.0/h, 1.0f);

RenderQuad();

glPopMatrix();

Does this work?

Rich