Mouse Control and Animation

Since 3D graphics involve working with solid objects, the image you see changes depending on the viewing angle (camera angle).

It's very useful to be able to adjust the viewpoint using mouse controls. This section explains how to implement such functionality.

- Table of Contents -

Event Handlers

We won't go into full detail here, but when a user performs actions -- like mouse operations -- on a GUI component, a special function called an event handler is automatically called by the system. For more details, please refer to the GUI Library Guide.

Event Handlers
Event Handlers
When a user performs an action (an event), the corresponding event handler is automatically triggered.

For example, if you want to change the camera angle based on mouse input, you'll need to set up a mouse event handler.

There are two approaches:

In this guide, we'll use the first approach -- the built-in handler.

- Function Format -

void setGraphics3DDefaultEventHandler (
  int rendererID, int componentID
)

Arguments:

Animation

Infinite Loop

To perform animation, your program must keep redrawing the screen continuously from the moment it starts until it ends. In other words, you need a control flow that resembles an infinite loop.

Note: Of course, the loop eventually ends -- when the animation or program finishes -- so it's not a truly infinite loop. However, while running, it behaves just like one.
Infinite Loop for Animation
Infinite Loop for Animation
The program must keep rendering the screen continuously from start to finish, like an infinite loop.

You can implement this loop using the while control structure with a boolean variable that starts as true. The loop keeps running as long as the condition is true. To end the animation, simply set the variable to false.


bool mainLoopState = true; // Set to false when you want to exit the animation
while( mainLoopState ){

    /* This is the main animation loop: write rendering logic here */

}
Loop.vcssl

Exiting the Loop When the Window Is Closed

If you use an infinite loop like the one above, you need to make sure the program can automatically exit when the window is closed. Otherwise, especially in cases where the VCSSL console is hidden or not available, there would be no way to stop the program -- it would keep animating forever.

To handle this, define an event handler named "onWindowClose", which will be called automatically when the window is closed:

- Function Format -

void onWindowClose( int componentID )

The "componentID" parameter receives the ID of the GUI component (the window) that was closed.

When you define this function, the system will call it automatically when the window is closed.

Example Program

Now let's write an actual animation program.

To make camera movement easier to understand, we'll add an axis model to the scene. We'll cover model generation and placement in detail in the next section, but for now, just copy and run the following:


import Graphics;
import Graphics3D;
import GUI;


// Create graphics resource and 3D renderer
int graphicsID = newGraphics( );
int rendererID = newGraphics3DRenderer( 800, 600, graphicsID );

// Create display window
int windowID = newWindow( 0, 0, 800, 600, " Hello 3DCG ! " );
int labelID = newImageLabel( 0, 0, 800, 600, graphicsID );
mountComponent( labelID, windowID );

// Enable mouse-based camera control
setGraphics3DDefaultEventHandler( rendererID, labelID );

// Set background color to black
setGraphics3DColor( rendererID, 0, 0, 0, 255 );

// Generate and place the axis model
int axis = newAxisModel( 3.0, 3.0, 3.0 );
mountModel( axis, rendererID );


// Animation loop
bool mainLoopState = true;
while( mainLoopState ) {
	sleep( 30 );                    // Wait for 30 milliseconds
	paintGraphics3D( rendererID );  // Render 3DCG
	paintComponent( labelID );      // Refresh GUI
	paintComponent( windowID );     // Refresh GUI
}
exit(); // Exit program after breaking the loop


// Called when the window is closed
void onWindowClose( int id ) {
    mainLoopState = false;   // Exit the loop
}
Sample.vcssl

When you run this program, a window will appear showing a coordinate axis model against a black background.

実行結果、黒い画面に座標軸モデルの図。
Execution Result
The axis model appears on a black screen. You can interact with it using the mouse.