• Create Account

### #ActualTrienco

Posted 05 September 2012 - 10:29 AM

I'm scaling the projection matrix, not the modelview matrix.

My bad, I was just quickly scrolling back up and saw your first post, where it was the model view.

Have I done this completely wonky?

Well, I don't think adding a scale offset instead of multiplying with a scale factor makes much sense, especially since I would expect that to screw up your aspect ratio.

Let me dig out some old code..
void zoomBy( float factor )
{
scroll += ( factor < 1.f ? mousePos : Vector3(worldWindow.w/2, worldWindow.h/2,0) ) * invZoom*(1.f-factor);
invZoom *= factor;
}

void zoomIn() { zoomBy(1.25f); }
void zoomOut() { zoomBy(.8f); }

//When setting up the frame for rendering...

glViewport(worldWindow.x, worldWindow.y + (screenHeight-worldWindow.h), worldWindow.w, worldWindow.h);

glOrtho(scroll.x, scroll.x + (worldWindow.w*invZoom), scroll.y + (worldWindow.h*invZoom), scroll.y, -1000000, 1000000);


This is probably confusing more than helping. It will always zoom towards the location of the mouse pointer, but when zooming out it uses the center of the view. I find it a lot more convenient than zooming towards the center of the screen and it even lets you quickly zip all over the map without actually having to scroll, just by zooming in and out.

It's also (ab)using the projection to scroll, though one could argue that the "camera position" should be in the modelview, not the projection. The reason is simply so I won't have to consider the current zoom factor when scrolling (you will want to scroll faster when zoomed out and slower when zoomed in).

Note that zooming back out would be easier if the projection was centered around the origin, so I wouldn't have to correct the scroll offset. But since I need a function that will zoom towards/from any arbitrary point anyway, it wouldn't really make much difference and I find it very tedious to think of the screen as going from -width/2 to +width/2 instead of 0 to width.

In this case the "worldWindow" is the area where the world is drawn. I set the viewport to leave out the area at the bottom where the GUI stuff will go. Unless you want to limit the viewport, you can pretend that worldWindow.x/worldWindow.y are 0 and worldWindow.w/worldWindow.h are your screen or window size. So basically looking at my old code it's not even required to have your view centered around the origin.

### #2Trienco

Posted 05 September 2012 - 10:29 AM

I'm scaling the projection matrix, not the modelview matrix.

My bad, I was just quickly scrolling back up and saw your first post, where it was the model view.

Have I done this completely wonky?

Well, I don't think adding a scale offset instead of multiplying with a scale factor makes much sense, especially since I would expect that to screw up your aspect ratio.

Let me dig out some old code..
void zoomBy( float factor )
{
scroll += ( factor < 1.f ? mousePos : Vector3(worldWindow.w/2, worldWindow.h/2,0) ) * invZoom*(1.f-factor);
invZoom *= factor;
}

void zoomIn() { zoomBy(1.25f); }
void zoomOut() { zoomBy(.8f); }

//When setting up the frame for rendering...

glViewport(worldWindow.x, worldWindow.y + (screenHeight-worldWindow.h), worldWindow.w, worldWindow.h);

glOrtho(scroll.x, scroll.x + (worldWindow.w*invZoom), scroll.y + (worldWindow.h*invZoom), scroll.y, -1000000, 1000000);


This is probably confusing more than helping. It will always zoom towards the location of the mouse pointer, but when zooming out it uses the center of the view. I find it a lot more convenient than zooming towards the center of the screen and it even lets you quickly zip all over the map without actually having to scroll, just by zooming in and out.

It's also (ab)using the projection to scroll, though one could argue that the "camera position" should be in the modelview, not the projection. The reason is simply so I won't have to consider the current zoom factor when scrolling (you will want to scroll faster when zoomed out and slower when zoomed in).

Note that zooming back out would be easier if the projection was centered around the origin, so I wouldn't have to correct the scroll offset. But since I need a function that will zoom towards/from any arbitrary point anyway, it wouldn't really make much difference and I find it very tedious to think of the screen as going from -width/2 to +width/2 instead of 0 to width.

In this case the "worldWindow" is the area where the world is drawn. I set the viewport to leave out the area at the bottom where the GUI stuff will go. Unless you want to limit the viewport, you can pretend that worldWindow.x/worldWindow.y are 0 and worldWindow.w/worldWindow.h are your screen or window size. So basically looking at my old code it's not even required to have your view centered around the origin.

### #1Trienco

Posted 05 September 2012 - 10:28 AM

I'm scaling the projection matrix, not the modelview matrix.

My bad, I was just quickly scrolling back up and saw your first post, where it was the model view.

Have I done this completely wonky?

Well, I don't think adding a scale offset instead of multiplying with a scale factor makes much sense, especially since I would expect that to screw up your aspect ratio.

Let me dig out some old code..
void zoomBy( float factor )
{
scroll += ( factor < 1.f ? mousePos : Vector3(worldWindow.w/2, worldWindow.h/2,0) ) * invZoom*(1.f-factor);
invZoom *= factor;
}

void zoomIn() { zoomBy(1.25f); }
void zoomOut() { zoomBy(.8f); }

When setting up the frame for rendering...

glViewport(worldWindow.x, worldWindow.y + (screenHeight-worldWindow.h), worldWindow.w, worldWindow.h);

glOrtho(scroll.x, scroll.x + (worldWindow.w*invZoom), scroll.y + (worldWindow.h*invZoom), scroll.y, -1000000, 1000000);


This is probably confusing more than helping. It will always zoom towards the location of the mouse pointer, but when zooming out it uses the center of the view. I find it a lot more convenient than zooming towards the center of the screen and it even lets you quickly zip all over the map without actually having to scroll, just by zooming in and out.

It's also (ab)using the projection to scroll, though one could argue that the "camera position" should be in the modelview, not the projection. The reason is simply so I won't have to consider the current zoom factor when scrolling (you will want to scroll faster when zoomed out and slower when zoomed in).

Note that zooming back out would be easier if the projection was centered around the origin, so I wouldn't have to correct the scroll offset. But since I need a function that will zoom towards/from any arbitrary point anyway, it wouldn't really make much difference and I find it very tedious to think of the screen as going from -width/2 to +width/2 instead of 0 to width.

In this case the "worldWindow" is the area where the world is drawn. I set the viewport to leave out the area at the bottom where the GUI stuff will go. Unless you want to limit the viewport, you can pretend that worldWindow.x/worldWindow.y are 0 and worldWindow.w/worldWindow.h are your screen or window size. So basically looking at my old code it's not even required to have your view centered around the origin.

PARTNERS