[ Prev | Index | Next ]
Japanese English

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 -

Sponsored Link


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:

  • Use a built-in default event handler (simpler, one line of code)
  • Write a custom event handler (more flexible and fully customizable)

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

- Function Format -

void setGraphics3DDefaultEventHandler (
  int rendererID, int componentID
)

Arguments:

  • rendererID: The ID of the 3D renderer.
  • componentID: The ID of the GUI component that displays the graphics (e.g., a graphics label). When mouse actions are performed on this component, the camera angle will automatically update in response.

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.

  • Left mouse drag rotates the viewpoint.
  • Right mouse drag pans the view.
  • Mouse wheel zooms in and out.

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



Sponsored Link



Japanese English
Index
[ Prev | Index | Next ]
News From RINEARN
* VCSSL is developed by RINEARN.

English Documentation for Our Software and VCSSL Is Now Nearly Complete
2025-06-30 - We're happy to announce that the large-scale expansion of our English documentation with the support of AI — a project that began two years ago — has now reached its initial target milestone.

VCSSL 3.4.52 Released: Enhanced Integration with External Programs and More
2025-05-25 - This update introduces enhancements to the external program integration features (e.g., for running C-language executables). Several other improvements and fixes are also included. Details inside.

Released: Latest Version of VCSSL with Fixes for Behavioral Changes on Java 24
2025-04-22 - VCSSL 3.4.50 released with a fix for a subtle behavioral change in absolute path resolution on network drives, introduced in Java 24. Details inside.

Released the Latest Versions of RINEARN Graph and VCSSL - Now Supporting Customizable Tick Positions and Labels!
2024-11-24 - Starting with this update, a new "MANUAL" tick mode is now supported, allowing users to freely specify the positions and labels of ticks on the graph. We'll explain the details and how to use it.

Released Exevalator 2.2: Now Compatible with TypeScript and Usable in Web Browsers
2024-10-22 - The open-source expression evaluation library, Exevalator, has been updated to version 2.2. It now supports TypeScript and can be used for evaluating expressions directly in web browsers. Explains the details.

Behind the Scenes of Creating an Assistant AI (Part 1: Fundamental Knowledge)
2024-10-07 - The first part of a series on how to create an Assistant AI. In this article, we introduce the essential knowledge you need to grasp before building an Assistant AI. What exactly is an LLM-based AI? What is RAG? And more.

Launching an Assistant AI to Support Software Usage!
2024-09-20 - We've launched an Assistant AI that answers questions about how to use RINEARN software and helps with certain tasks. Anyone with a ChatGPT account can use it for free. We'll explain how to use it.

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024-02-05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Dives into each of them!