[Gambas-user] gb3: OpenGL Rotate and Translate logic

Kevin Fishburne kevinfishburne at ...1887...
Thu May 24 04:14:40 CEST 2012


> ----- Original Message -----
> From: "Kevin Fishburne"<kevinfishburne at ...1887...>
> To: gambas-user at lists.sourceforge.net
> Sent: Tuesday, 22 May, 2012 5:14:02 AM
> Subject: [Gambas-user] gb3: OpenGL Rotate and Translate logic
>
> Disclaimer: I've been working on this for days and neither Google nor
> trial and error is adequate, apparently.
>
> I'm trying to render an overhead, 2D scene with the "camera" at the
> center of the screen and the world objects rotated around it. Here are
> my variables:
>
> Camera.WorldX As Single      ' World coordinates of camera.
> Camera.WorldYAs Single      ' World coordinates of camera.
> Camera.Orientation As Single ' Angle of camera (2D overhead view,
> rotates screen like Contra III for SNES).
> Camera.Zoom As Single        ' Scale of matrix for zooming in and out.
> sWidth As Short              ' Width in pixels of screen.
> sHeight As Short             ' Height in pixels of screen.
>
> I need to rotate the matrix so that rendered objects will appear in the
> correct positions with regard to the camera's orientation. I need to
> translate the matrix so that the camera coordinates are always at the
> center of the rendering window. I need to scale the matrix so that the
> camera can zoom in and out (always centered at the middle of the
> rendering window).
>
> I suspect that to center the camera on the rendering window after
> rotating the matrix, I need to "unrotate" the x and y offsets required
> to center it, which I can do if necessary using some functions I created
> to rotate an arbitrary point about an arbitrary origin.
>
> I think I can successfully rotate the matrix around the camera with code
> like this:
>
>     Gl.Translatef(Camera.WorldX, Camera.WorldY, 0)
>     Gl.Rotatef(- Camera.Orientation, 0, 0, 1)
>     Gl.Translatef(- Camera.WorldX, - Camera.WorldY, 0)
>
> But then I need to translate the scene so that the camera is in the
> center of the rendering window. I also can't figure out how to
> arbitrarily scale the matrix to zoom in and out on the camera in this
> sequence. I have this:
>
> Gl.Scalef(128 * Zoom.Current, 128 * Zoom.Current, 1)
>
> but its effects vary depending on where I place it. The reason it
> multiplies by 128 is because each "tile" is 128 pixels, and each world
> coordinate is one tile. I couldn't be more confused if I'd just been
> shot in the head, so any help is greatly appreciated.
>

On 05/22/2012 03:02 AM, tommyline at ...2340... wrote:
> Hi Kevin.
>
> There's plenty of issues you post, but first thing I would do would be:
>
> instead of:
>
>   Gl.Translatef(Camera.WorldX, Camera.WorldY, 0)
>   Gl.Rotatef(- Camera.Orientation, 0, 0, 1)
>   Gl.Translatef(- Camera.WorldX, - Camera.WorldY, 0)  '<<-- you come back along different path here because
>                                                       'the world is turned along z-axis
>
>
> try this:
>
>   Gl.Translatef(Camera.WorldX, Camera.WorldY, 0)
>   gl.PushMatrix()                                    '<-you save the world's coordinate here
>
>   do all the drawing here
>
>   gl.PopMatrix()                                     '<-restore camera's original position.
>
>
>
> Doing gl.Rotatef() you turn GLOBAL coordinates, so if you do gl.Rotatef(90,0,0,1), your x an y axes are swapped, y becomes x, becomes -y, thus doing gl.Translatef(1,1,0) next in fact moves your object by (-1,1,0...I think:)) ).
> That's for the moment, the rest we can discuss if you want.
>
> regards
>
> Tomek
>

Hi Tomek. I'm not sure that saving and loading the matrix will do the 
trick, but I don't really understand the implications of doing so 
either. I basically want to translate, scale and rotate the matrix so 
that the camera is always at the center of the render window and I can 
draw objects using their native world coordinates. The camera is 
pointing straight down at the ground (perpendicular to the landscape). 
Rotating the camera/matrix will spin the landscape with the camera being 
the origin, just like spinning a globe of Earth if the camera is 
pointing down at the north pole. It would look something like this:

http://www.youtube.com/watch?v=mee_FiIQoEY

If I do this below everything is perfect (including zoom) but it has no 
rotation:

   ' Rotate, translate and scale matrix.
   ScreenOffsetX = sWidth / 2 / 128 / Zoom.Current
   ScreenOffsetY = sHeight / 2 / 128 / Zoom.Current
   Gl.LoadIdentity()
   Gl.Scalef(128 * Zoom.Current, 128 * Zoom.Current, 1)
   Gl.Translatef(- Camera.WorldX + ScreenOffsetX, - Camera.WorldY + 
ScreenOffsetY, 0)

Incorporating rotation rotates the scene but not around the camera. 
Using the above code and then rendering everything at its native world 
coordinates looks like this:

http://eightvirtues.com/sanctimonia/misc/Everything%20But%20Rotation.ogv

If necessary I can translate the matrix by (x,y) units which have been 
pre-rotated opposite the angle the matrix was rotated at. I can 
basically manually "undo" the matrix rotation if it needs to be 
translated post rotation, if that makes any sense. Some instinct inside 
me keeps nagging that I might need to do a trick like that to get it to 
work.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list