|
Post by Supermonkey on Aug 5, 2003 8:24:51 GMT -5
I have this little thing where your in a box and you can use the arrow keys to move the camera inside the box.
textmode(TEXT_OVERLAID) dim ay#,i,z# z#=0 while true locate 0,6:print "Use the I+O KEYS TO MOVE "+z# glclear(GL_DEPTH_BUFFER_BIT OR GL_COLOR_BUFFER_BIT) glloadidentity() gltranslatef(0,-1,z#) glrotatef(ay#,0,1,0) glbegin(GL_QUADS) ' Top
glBegin (GL_QUADS) glcolor3f(1,0,1): glVertex3f (-15,2, -15) glcolor3f(1,1,0): glVertex3f (15,2, -15) glcolor3f(0,1,1): glVertex3f (15,2, 15) glcolor3f(0,1,0): glVertex3f (-15,2, 15) glEnd ()
' Sides for i = 1 to 4 glBegin (GL_QUADS) glcolor3f(1,0,1):glVertex3f (-15, 2.05, -15) glcolor3f(1,1,0):glVertex3f (15, 2.05, -15) glcolor3f(0,1,1):glVertex3f (15,-0.05, -15) glcolor3f(1,1,1):glVertex3f (-15,-0.05, -15) glEnd () glRotatef (90, 0, 1, 0) next 'Bottom glBegin (GL_QUADS) glcolor3f(1,0,1): glVertex3f (-15, 0, -15) glcolor3f(1,1,0): glVertex3f (15, 0, -15) glcolor3f(0,1,1): glVertex3f (15, 0, 15) glcolor3f(0,1,0): glVertex3f (-15, 0, 15) glEnd ()
if keydown("A") then ay#=ay#+1:endif if keydown("D") then ay#=ay#-1:endif if keydown("W") then z#=z#+0.1:ENDIF if keydown("S") then z#=z#-0.1:endif drawtext() swapbuffers() wend
I found the best way to rotate the camera is to rotate the scene but just using glrotatef doesnt work because it rotates around the central point of the box. How do I get it so the scene rotates around the camera position?
Thanks Jonny
|
|
|
Post by Tom Mulgrew on Aug 6, 2003 4:30:30 GMT -5
glRotatef and glTranslatef don't actually affect the camera as such. Instead they affect everything you draw after that.
So really instead of moving the camera 7 units down and 4 units left, you move the universe 7 units up and 4 units right. Which gives you the same effect. Likewise instead of turning the camera 12 degrees anticlockwise you turn the universe 12 degrees clockwise.
To get the rotation around the correct point you need to rearrange your transformations. OpenGL actually applies transformations in reverse order, so if you glTranslatef() to some point P then glRotatef(), your vertices will all be rotated around the origin first, then translated to P. This gives the result of rotating around P.
If you reverse the order, the points will all be translated to P, then rotated around the origin. Which effectively rotates the camera.
There's also some work when moving the camera, as you need to calculate in which way to move based on the direction it is facing.
-Tom
textmode(TEXT_OVERLAID) dim ay#,i,x#,y#,z#,xd#,zd# z#=0 while true locate 0,6:print "Use the I+O KEYS TO MOVE "+z# glclear(GL_DEPTH_BUFFER_BIT OR GL_COLOR_BUFFER_BIT)
' Position the camera glloadidentity() glrotatef(-ay#,0,1,0) gltranslatef(-x#,-y#-1,-z#)
glbegin(GL_QUADS) ' Top
glBegin (GL_QUADS) glcolor3f(1,0,1): glVertex3f (-15,2, -15) glcolor3f(1,1,0): glVertex3f (15,2, -15) glcolor3f(0,1,1): glVertex3f (15,2, 15) glcolor3f(0,1,0): glVertex3f (-15,2, 15) glEnd ()
' Sides for i = 1 to 4
glBegin (GL_QUADS) glcolor3f(1,0,1):glVertex3f (-15, 2.05, -15) glcolor3f(1,1,0):glVertex3f (15, 2.05, -15) glcolor3f(0,1,1):glVertex3f (15,-0.05, -15) glcolor3f(1,1,1):glVertex3f (-15,-0.05, -15) glEnd () glRotatef (90, 0, 1, 0) next 'Bottom glBegin (GL_QUADS) glcolor3f(1,0,1): glVertex3f (-15, 0, -15) glcolor3f(1,1,0): glVertex3f (15, 0, -15) glcolor3f(0,1,1): glVertex3f (15, 0, 15) glcolor3f(0,1,0): glVertex3f (-15, 0, 15) glEnd ()
if keydown("A") then ay#=ay#+1:endif if keydown("D") then ay#=ay#-1:endif if keydown("W") then ' Note: OpenGL rotations operate in degrees (360 degrees = 1 full rotation) ' Sine and Cosine functions operate in radians (2*PI radians = 1 full rotation) ' We convert degrees to radians by multiplying by 2*PI/360 ' which equals PI/180.
' This calculates a vector of length 1 in the direction the camera is facing xd# = -sin(ay#*m_pi/180) zd# = -cos(ay#*m_pi/180)
' Add vector to camera position x# = x# + xd# * 0.1 z# = z# + zd# * 0.1 ENDIF if keydown("S") then
' This calculates a vector of length 1 in the direction the camera is facing xd# = -sin(ay#*m_pi/180) zd# = -cos(ay#*m_pi/180)
' Add vector to camera position x# = x# + xd# * -0.1 z# = z# + zd# * -0.1 endif drawtext() swapbuffers() wend
Btw, you can also do keyboard input like this: if scanKeyDown (VK_UP) then ... (up arrow) if scanKeyDown (VK_DOWN) then ... (down arrow) if scanKeyDown (VK_LEFT) then ... (left arrow) if scanKeyDown (VK_RIGHT) then ... (right arrow)
-Tom
|
|