[Gambas-user] gb3: OpenGL Rotate and Translate logic
tommyline at ...2340...
tommyline at ...2340...
Thu May 24 11:00:56 CEST 2012
Hi Kevin.
I think you should concider using Glu.LookAt function, which should solve all your problems.
I used it in collision project (attached) to follow the ball.
You just set the camera with few parameters, and that's it! Please check the command help in Gambas.
Simply speaking, you define eye point's (x,y,z position - no need for scale or translate), point you look at and point where the top of camera is (by changing it, you rotate the world in front of you), so I think that would solve it. See the attached example to see how I did it. Press F3 to see how it works.
>From glu man pages:
NAME
gluLookAt - define a viewing transformation
C SPECIFICATION
void gluLookAt( GLdouble eyeX,
GLdouble eyeY,
GLdouble eyeZ,
GLdouble centerX,
GLdouble centerY,
GLdouble centerZ,
GLdouble upX,
GLdouble upY,
GLdouble upZ )
PARAMETERS
eyeX, eyeY, eyeZ
Specifies the position of the eye point.
centerX, centerY, centerZ
Specifies the position of the reference
point.
upX, upY, upZ Specifies the direction of the up vector.
DESCRIPTION
gluLookAt creates a viewing matrix derived from an eye
point, a reference point indicating the center of the scene,
and an UP vector.
The matrix maps the reference point to the negative z axis
and the eye point to the origin. When a typical projection
matrix is used, the center of the scene therefore maps to
the center of the viewport. Similarly, the direction
described by the UP vector projected onto the viewing plane
is mapped to the positive y axis so that it points upward in
the viewport. The UP vector must not be parallel to the
line of sight from the eye point to the reference point.
Let
( centerX - eyeX )
F = | |
| centerY - eyeY |
( centerZ - eyeZ )
Let UP be the vector (upX,upY,upZ).
Then normalize as follows: f = _____
||F||
UP' = ______
||UP||
Finally, let s = f x UP', and u = s x f.
M is then constructed as follows:
( s[0] s[1] s[2] 0 )
| u[0] u[1] u[2] 0 |
M = | |
|-f[0] -f[1] -f[2] 0 |
| 0 0 0 1 |
( )
and gluLookAt is equivalent to glMultMatrixf(M);
glTranslated (-eyex, -eyey, -eyez);
I hope I did help.
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
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user
-------------- next part --------------
A non-text attachment was scrubbed...
Name: col1-0.0.3.tar.gz
Type: application/x-compressed-tar
Size: 164859 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20120524/5212e17d/attachment.bin>
More information about the User
mailing list