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

Kevin Fishburne kevinfishburne at ...1887...
Tue May 22 06:14:02 CEST 2012


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.

-- 
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