This section covers how to rotate 3D objects.
For continuously rotating models (e.g. during animation), it is generally best to use coordinate systems, which will be discussed in a later chapter.
However, if you just want to rotate a model once, you can easily do so using functions like rotModel introduced in this section.
Unlike translation, multiple rotation functions are available. Use the appropriate one depending on your use case.
To rotate a model around one of the coordinate axes, use the rotXModel, rotYModel, or rotZModel function.
- Function Format -
These three functions rotate the model around the X-, Y-, and Z-axis respectively.
Arguments:
A radian is a unit of angle frequently used in science and engineering. One full half-turn (180 degrees) is equal to π radians, so 90 degrees is π/2 radians, and 45 degrees is π/4 radians.
The value of π is provided as the float constant "PI" in the Math library.
To rotate around an axis in an arbitrary direction, use the rotModel(...) function.
- Function Format -
Arguments:
To rotate around an axis that passes through a specific point and has an arbitrary direction, use the extended version of the rotModel(...) function.
- Function Format -
Arguments:
Just like with models, multiple functions are available for rotating individual polygons.
To rotate a polygon around one of the coordinate axes, use the rotXPolygon(...), rotYPolygon(...), or rotZPolygon(...) function.
- Function Format -
These three functions rotate the polygon around the X-, Y-, and Z-axis respectively.
Arguments:
To rotate around an axis in an arbitrary direction, use the rotPolygon(...) function.
- Function Format -
Arguments:
To rotate around an axis that passes through a specific point and has an arbitrary direction, use the extended version of the rotPolygon(...) function.
- Function Format -
Arguments:
Let's try placing a box-shaped model and rotating it 45 degrees (π/4 radians) around the Z-axis. Write and run the following code:
import graphics3d.Graphics3DFramework;
import Graphics3D;
import Math; // For the value of PI
// Function called at the start of the program
void onStart ( int rendererID ) {
// Optional: Set window size and background color
setWindowSize( 800, 600 );
setBackgroundColor( 0, 0, 0, 255 );
// Create and place an axis model
int axis = newAxisModel( 3.0, 3.0, 3.0 );
mountModel( axis, rendererID );
// Create and place a box-shaped model
int box = newBoxModel( 2.0, 2.0, 2.0 );
mountModel( box, rendererID );
// Rotate the box 45 degrees (pi/4 radians) around the Z-axis
rotModelZ( box, PI/4.0 );
}
Rot.vcssl
When you run this program, a white box will appear on a black background. The box is rotated 45 degrees (π/4 radians) around the Z-axis.
To rotate a model continuously in an animation, you can use the onUpdate(...) function, which is automatically called several dozen times per second by the framework:
import graphics3d.Graphics3DFramework;
import Graphics3D;
import Math; // For the value of PI
// Variables to store the model IDs
int axis, box;
// Function called at the start of the program
void onStart ( int rendererID ) {
// Optional: Set window size and background color
setWindowSize( 800, 600 );
setBackgroundColor( 0, 0, 0, 255 );
// Create and place an axis model
axis = newAxisModel( 3.0, 3.0, 3.0 );
mountModel( axis, rendererID );
// Create and place a box-shaped model
box = newBoxModel( 2.0, 2.0, 2.0 );
mountModel( box, rendererID );
}
// Function called several dozen times per second during each frame update
void onUpdate (int renderer) {
// Gradually rotate the box around the Z-axis
rotModelZ( box, 0.02 );
}
RotAnimation.vcssl
When you run this program, the white box will slowly and continuously rotate like an animation.