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

Demo program B

Matrix Animation


Here I will introduce a slightly more complex program that uses a hierarchical structure.

Hierarchical structure is a concept unique to 3D graphics. This is because 3D graphics deals with a vast three-dimensional space, so a large number of parameters are handled at once. In particular, when multiple objects are related in 3D space and moved at the same time, it is necessary to handle a simple but quite complex set of parameters at the same time. This is where the concept of hierarchical structure emerged.
A hierarchical structure is also called a parent-child structure. Hierarchical structures are mainly used to represent joints (such as human body models) and groups of objects.

Figure B-1 Joint representation using hierarchical structure


The demo program “demo_B” is a sample program for realizing animation using a hierarchical structure. In the demo, complex movements (animation of a group of objects with joint structures) are easily realized by creating a relationship (parent-child structure) between three objects. This would result in a fairly complex and cumbersome program if the idea of hierarchy was not used.

Figure B-2 Image model of Demo B


Listing B-1 demo_B: Animation using hierarchical structure

/*------------------------------------------------ ----------------------*/
/* Matrix Animation */
/*------------------------------------------------ ----------------------*/
#include "sgl.h"

extern PDATA PD_PLANE1, PD_PLANE2, PD_PLANE3;

static void set_poly(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], ang3[XYZ];
	static FIXED pos1[XYZ], pos2[XYZ], pos3[XYZ];
	static ANGLE tang, aang;

	slInitSystem(TV_320x224,NULL,1);
	slPrint("demo B", slLocate(6,2));

	ang1[X] = ang1[Y] = ang1[Z] = DEGtoANG(0.0);
	ang2[X] = ang2[Y] = ang2[Z] = DEGtoANG(0.0);
	ang3[X] = ang3[Y] = ang3[Z] = DEGtoANG(0.0);
	pos1[X] = toFIXED( 0.0);
	pos1[Y] = toFIXED( 40.0);
	pos1[Z] = toFIXED(170.0);
	pos2[X] = toFIXED( 0.0);
	pos2[Y] = toFIXED(-40.0);
	pos2[Z] = toFIXED( 0.0);
	pos3[X] = toFIXED( 0.0);
	pos3[Y] = toFIXED(-40.0);
	pos3[Z] = toFIXED( 0.0);
	tang = DEGtoANG(0.0);
	aang = DEGtoANG(2.0);

	while(-1) {
		slUnitMatrix(CURRENT);
		ang1[Z] = ang2[Z] = tang;
		tang += aang;
		if(tang< DEGtoANG(-90.0)) {
			aang = DEGtoANG(2.0);
		} else if(tang> DEGtoANG(90.0)) {
			aang = -DEGtoANG(2.0);
		}

		slPushMatrix();
		{
			set_poly(ang1, pos1);
			slPutPolygon(&PD_PLANE1);

			slPushMatrix();
			{
				set_poly(ang2, pos2);
				slPutPolygon(&PD_PLANE2);

				slPushMatrix();
				{
					set_poly(ang3, pos3);
					ang3[Y] += DEGtoANG(5.0);
					slPutPolygon(&PD_PLANE3);
				}
				slPopMatrix();
			}	
			slPopMatrix();
		}
		slPopMatrix();

		slSynch();
	}
}

Flow B-1 demo_B: Hierarchical flowchart

From the next chapter onwards, we will focus on the surface attributes of polygons (priority, color, texture, etc.), and will explain background scrolling, drawing alphanumeric characters, input/output control, etc., which are essential for game development.


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