Japanese
SGL User's ManualPROGRAMMER'S TUTORIAL
BackForward
PROGRAMMER'S TUTORIAL

5. Matrix


This chapter explains the matrix that is the basis for constructing 3D graphics.
The first half explains matrix operations and their concepts, and the second half explains how to construct objects with a hierarchical structure using stack matrices.
In particular, the hierarchical structure of the matrix is an important element in 3D graphics expression on the Sega Saturn, so please read and understand it carefully.

5-1. Matrix

A matrix is also called a matrix. This is a concept for treating groups of numbers as a unit, unlike operations on single numbers.

A matrix is a group of numbers with n rows and m columns, and although it is different from normal numerical operations, it is also possible to perform four arithmetic operations between matrices (for details, please refer to specialized books).
The figure below is an example of multiplication between 2-row x 2-column matrices.
In the case of SGL, in order to accurately represent 3D space, a 4 row x 3 column matrix is used as a matrix variable (to realize XYZ coordinate values and various transformation operations).

Figure 5-1 General concepts and calculation examples of matrices

note)
M11~M22, T11~T22 are normal variables

The modeling transformation explained in “ Chapter 4 Coordinate Transformation ” actually creates a new polygon vertex data sequence by multiplying the polygon vertex data sequence represented as a matrix by various transformation matrices (rotation, translation, scale, etc.) is.

5-2. Object representation using hierarchical structure

This article explains the hierarchical structure representation of objects using a matrix using a stack. Simply put, this means creating relationships between multiple objects. This relationship is generally called a parent-child structure .

stack

A stack is a structural model for efficiently handling matrices. This is represented as shown in Figure 5-2 , and the functions to perform this operation are “slPushMatrix” and “slPopMatrix”.
Also, the matrix at the lowest level in the stack is called the current matrix, and various matrix conversion operations are performed on the current matrix.

[Bool slPushMatrix ( void );]
Copies the current current matrix to the lower stack, moving the current matrix down the stack at the same time. Used to temporarily save the matrix.

[Bool slPopMatrix ( void );]
Restores the temporarily saved upper matrix and moves the current matrix to the upper level.

Matrices in lower stacks are discarded.

Figure 5-2 Stack image model

note)
The stack consists of 20 stacks, and the matrices are nested in order from the upper stack to the lower stack.

SGL maintains a matrix called the environment matrix as the current matrix when the matrix environment is initialized.
This defines the basis of SGL's 3D coordinate environment, and if this is rewritten, various conversion operations may not be performed correctly.
Therefore, SGL supports “slInitMatrix” as a library function that initializes the stack and various matrix variables.

[void slInitMatrix (void)]
Initializes the stack and matrix variables. At this time, the environment matrix is allocated on the stack as the current matrix.

Hierarchy concept

Figure 5-3 is an image model of a group of objects given a hierarchical structure. object1 is set at the shallowest level of the hierarchy, and object2 and object3 are defined at the following levels.

Figure 5-3 Image model of hierarchical structure

When using a hierarchical structure, objects behave as shown in Figure 5-3 . Figures 5-4 and 5-5 show the results when the same transformation is applied to a group of objects that do not use a hierarchical structure and a group of objects that use a hierarchical structure. (multiply by movement).

Figure 5-4 Example of moving objects without using a hierarchical structure

Figure 5-5 Example of object conversion using hierarchical structure

Definition of hierarchical structure by Sega Saturn

Next, using a sample program as a reference, we will explain how to implement a hierarchical structure on the Sega Saturn.

Listing 5-1 sample_5_2: Hierarchical matrix definition
/*------------------------------------------------ ----------------------*/
/* Double Cube Circle Action */
/*------------------------------------------------ ----------------------*/
#include "sgl.h"

#define DISTANCE_R1 40
#define DISTANCE_R2 40

extern PDATA PD_CUBE;

static void set_star(ANGLE ang[XYZ] , FIXED pos[XYZ])
{
	slTranslate(pos[X] , pos[Y] , pos[Z]);
	slRotX(ang[X]);
	slRotY(ang[Y]);
	slRotZ(ang[Z]);
}

void ss_main(void)
{
	static ANGLE ang1[XYZ] , ang2[XYZ];
	static FIXED pos1[XYZ] , pos2[XYZ];
	static ANGLE tmp = DEGtoANG(0.0);

	slInitSystem(TV_320x224,NULL,1);
	slPrint("Sample program 5.2" , slLocate(6,2));

	ang1[X] = ang2[X] = DEGtoANG(30.0);
	ang1[Y] = ang2[Y] = DEGtoANG(45.0);
	ang1[Z] = ang2[Z] = DEGtoANG( 0.0);

	pos2[X] = toFIXED(DISTANCE_R2);
	pos2[Y] = toFIXED(0.0);
	pos2[Z] = toFIXED(0.0);
	while(-1){
		slUnitMatrix(CURRENT);

		slPushMatrix();
		{
			pos1[X] = DISTANCE_R1 * slSin(tmp);
			pos1[Y] = toFIXED(30.0);
			pos1[Z] = toFIXED(220.0) + DISTANCE_R1 * slCos(tmp);
			set_star(ang1 , pos1);
			slPutPolygon(&PD_CUBE);

			slPushMatrix();
			{
				set_star(ang2, pos2);
				slPutPolygon(&PD_CUBE);
			}
			slPopMatrix();
		}
		slPopMatrix();

		ang1[Y] += DEGtoANG(1.0);
		ang2[Y] -= DEGtoANG(1.0);
		tmp += DEGtoANG(1.0);

		slSynch();
	}
}

Flow 5-1 sample_5_2: Hierarchical matrix definition flowchart


5-3. Matrix function

SGL supports the following functions in addition to those mentioned above as library functions related to matrix operations.

[void slLoadMatrix ( MATRIX mtptr )]
Copies the matrix specified as a parameter to the current matrix
. Assign the MATRIX type variable of the specified matrix to the parameter.

[void slGetMatrix ( MATRIX mtptr )]
Copies the current matrix to the matrix specified as a parameter. Assign the MATRIX type variable of the specified matrix to the parameter.

[void slMultiMatrix ( MATRIX mtrx )]
Multiplies the current matrix by the matrix specified as a parameter. Assign the MATRIX type variable of the specified matrix to the parameter.

[Bool slPushUnitMatrix ( void )]
Allocate the identity matrix on the stack and make it the current matrix.
The previous current matrix is temporarily saved at a higher level in the stack.

[void slUnitMatrix ( MATRIX mtptr )]
Makes the matrix specified as a parameter an identity matrix. Assign the MATRIX type variable of the specified matrix to the parameter.
Also, only when “CURRENT” is assigned to the parameter, the target matrix becomes the current matrix, and the current matrix can be initialized.

Additional notes: SGL library functions that appeared in this chapter

In this chapter, we explained the functions in the table below.

Table 5-1 SGL library functions that appeared in this chapter
 functional type
 function name
 parameters
      function
void slLoadMatrix MATRIX mtpr Copy specified matrix to current matrix
Bool slPushMatrix void Temporary reservation of matrix
Bool slPushUintMatrix void Temporarily allocate the identity matrix on the stack
void slGetMatrix MATRIX mtpr Copy current matrix to specified matrix
void slInitMatrix void Initialization of matrix variables and buffers
void slMultiMatrix MATRIX mptr Multiply the current matrix by the specified matrix
Bool slPopMatrix void Restoring a temporarily saved matrix
void slUintMatrix MATRIX mptr Make the specified matrix an identity matrix


BackForward
SGL User's ManualPROGRAMMER'S TUTORIAL
Copyright SEGA ENTERPRISES, LTD., 1997