Older Version Newer Version

robmcal robmcal Jun 29, 2009

=OpenGL 3D Graphics in Liberty BASIC= ==Lesson Three: Rotation and scaling== //by Robert McAllister// Rotation and scaling are two separate operations but since both are fairly simple they will be combined into the same lesson. ==Rotation:== To rotate the entire scene you can use a call to glRotate which has four parameters: [[code format="vbnet"]] CALL glRotate amt , X , Y , Z [[code]] The first parameter "amt" is the degrees of rotation you want to apply. The remaining three are used to specify which axis you want to rotate the scene around. Only one axis can be rotated at a time. Typically you will have three variables to keep track of each axis and then use a block of code such as this in your drawing routine: [[code format="vbnet"]] CALL glRotate xrot , 1 , 0 , 0 CALL glRotate yrot , 0 , 1 , 0 CALL glRotate zrot , 0 , 0 , 1 [[code]] In the following snippet uncomment one rotation variable at a time to see the triangle rotate around each of the axis’s. Or, uncomment all of them to see it go crazy. [[code format="vbnet"]] 'Rotating an object with glRotate FOR a = 1 TO 360 xrot = a 'yrot = a 'zrot = a CALL ClearView eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ CALL glRotate xrot , 1 , 0 , 0 CALL glRotate yrot , 0 , 1 , 0 CALL glRotate zrot , 0 , 0 , 1 CALL glBegin GL.TRIANGLES CALL glColor4fv 1 , 0 , 0 , 1 CALL glVertex -1 , -1 , 0 CALL glColor4fv 0 , 1 , 0 , 1 CALL glVertex 0 , 1 , 0 CALL glColor4fv 0 , 0 , 1 , 1 CALL glVertex 1 , -1 , 0 CALL glEnd CALL RefreshView CALL Pause 10 NEXT a WAIT [[code]] You can also rotate an individual object using a little trigonometry. [[code format="vbnet"]] 'Rotating an object FOR a = 1 TO 360 angle1 = ((a+90)/57.2957) angle2 = ((a+270)/57.2957) xAdjustment1 = 0 + (sin(angle1)) xAdjustment2 = 0 + (sin(angle2)) CALL ClearView eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ CALL glBegin GL.TRIANGLES CALL glColor4fv 1 , 0 , 0 , 1 CALL glVertex 0 + xAdjustment1 , -1 , 0 CALL glColor4fv 0 , 1 , 0 , 1 CALL glVertex 0 , 1 , 0 CALL glColor4fv 0 , 0 , 1 , 1 CALL glVertex 0 + xAdjustment2 , -1 , 0 CALL glEnd CALL RefreshView CALL Pause 10 NEXT a WAIT [[code]] ==Scaling:== OpenGL has a handy function for scaling the entire scene, glScalef. [[code format="vbnet"]] CALL glScalef X , Y , Z [[code]] With this function you can expand or shrink the entire scene (actually the 3D matrix) along any or all of the three axis’s. If a value is set to 1, that axis will not be changed. Here we have the same old triangle but it has been expanded along the X axis and shrunk along the Y axis. [[code format="vbnet"]] 'Scaling with glScalef CALL ClearView eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ CALL glScalef 2 , .5 , 1 CALL glBegin GL.TRIANGLES CALL glColor4fv 1 , 0 , 0 , 1 CALL glVertex -1 , -1 , 0 CALL glColor4fv 0 , 1 , 0 , 1 CALL glVertex 0 , 1 , 0 CALL glColor4fv 0 , 0 , 1 , 1 CALL glVertex 1 , -1 , 0 CALL glEnd CALL RefreshView WAIT [[code]] Scaling can also be accomplished with a scale factor. This can come in handy if you only want to change the size of one object in a scene. [[code format="vbnet"]] 'Scaling with a scale factor FOR a = .5 TO 1.5 step .1 CALL ClearView eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ ScaleFactor = a CALL glBegin GL.TRIANGLES CALL glColor4fv 1 , 0 , 0 , 1 CALL glVertex -1 * ScaleFactor , -1 * ScaleFactor , 0 CALL glColor4fv 0 , 1 , 0 , 1 CALL glVertex 0 * ScaleFactor , 1 * ScaleFactor , 0 CALL glColor4fv 0 , 0 , 1 , 1 CALL glVertex 1 * ScaleFactor , -1 * ScaleFactor , 0 CALL glEnd CALL RefreshView CALL Pause 250 NEXT a WAIT [[code]] In the next lesson we will learn what "Display lists" are.