Japanese | English | Korean

Samples

Coordinate systems and the camera

Let's take a look at MascotCapsule coordinate systems, coordinate transformation, and how to specify the camera setting.

Table of contents

1. Coordinate system

MascotCapsule uses four coordinate systems: model coordinate system, world coordinate system, camera coordinate system, and screen coordinate system. All these coordinate systems are treated as orthogonal coordinate systems.

Model coordinate system

It is a coordinate system specific for each 3D model.

World coordinate system

It is a coordinate system that serves as the standard for the world. Each 3D model is placed on the world coordinate system.

Camera coordinate system

It is a coordinate system as seen from a camera. The area to be rendered in the camera coordinate system fits within a view volume specified by the near clipping plane, far clipping plane, angle of view, etc.

Screen coordinate system

It is a coordinate system when the model is actually rendered on the display screen. The view volume in the camera coordinate system will be enlarged or reduced to a cube whose length of each side is 1, and projected onto the display screen.

In order to display 3D graphics on the display screen, MascotCapsule executes coordinate transformation three times as follows:
Model coordinate system -> World coordinate system -> Camera coordinate system -> Screen coordinate system



2. AffineTrans class

The AffineTrans class retains the 4x4 matrix for the three-dimensional affine transformation. This class defines the setup and movement of the camera and 3D objects. Let's take a look at how to define the camera setup and operate 3D models using the AffineTrans class.

2.1. Camera setup

Rendered 3D models will be placed on the world coordinate system.
To display these models on the display screen, the world coordinate system must be transformed to the camera coordinate system, and further transformed to the screen coordinate system. To do so, place the camera on the world coordinate system, in order to transform the world coordinate system to the camera coordinate system.
To define the camera setup, provide three-dimensional vectors for the camera position (position), the line of sight (lookAt), and the up-direction vector of the camera body (up), respectively.

MascotCapsule handles these three-dimensional vectors as instances of a three-dimensional vector class called Vector3D. The Vector3D class contains three elements: x-element, y-element, and z-element in the orthogonal coordinate system.
In order to specify the camera setup, define the position, lookAt, and up vectors as Vector3D class objects, respectively. Then pass these objects as parameters to the setViewTrans( Vector3D position, Vector3D lookAt, Vector3D up ) method in the AffineTrans class.

The lookAt method receives Vector3D objects as parameters. Details of these three Vector3D objects are as follows:

position

This object specifies where to place the camera.

lookAt

The camera is placed at the spot defined by the position. And the camera faces the direction towards the point defined by lookAt. In other words, the direction vector of the camera equals the lookAt vector minus the position vector.

up

This object specifies the up-direction vector of the camera body. This vector's direction is the up direction of the display screen. However, it is not possible to define the lookAt and position vectors so that the camera's line of sight vector and up vector are facing the opposite direction. Also, up vector is a direction vector; therefore, specify it as a normalized vector (whose length is 1) in ordinary cases.

Pass these three objects as parameters of the lookAt method, in order of position, lookAt, and up. Camera setup is completed.


Let's take a look at the sample code that specifies the camera setting.

/**
  * CanvasEx
  * Constructor (initialization of data loading, etc.)
  */
public CanvasEx(){

. . .

/* Creating the view transformation matrix to the camera coordinates */
vtrans.setViewTrans( new Vector3D( 0, 512, 2048 ),
new Vector3D( 0, -512, -4096 ),
new Vector3D( 0, 4096, 0 ));

2.2. Operating the 3D model

Perform matrix calculation to the 3D model using the AffineTrans class, in order to execute model operations such as rotation, translation, scaling, etc. Every time rendering is executed in the sample program, the view transformation matrix is multiplied with the matrix that executes a single rotation around the y-axis. The result looks as if the camera is rotating.

/* Camera rotation */
AffineTrans trans = new AffineTrans();
trans.setRotationY( 4096 / 360 );
vtrans.multiply( trans );

/* Coordinate transformation-related setting */
layout.setAffineTrans( vtrans );


SampleCamera screenshot