SGL User's ManualPROGRAMMER'S STRUCT
BackForward
PROGRAMMER'S STRUCT

Demo program B

Matrix Animation


Here, I will introduce a slightly complicated program that uses a hierarchical structure.

Hierarchical structure is a concept peculiar to 3D graphics. This is due to the fact that in the case of 3D graphics, since a vast space of 3D is handled, a fairly large number of parameters are handled at once. In particular, it is necessary to handle a group of parameters that are simple but quite complicated at once, such as when multiple objects are related and moved at once in 3D space. That is where the idea of a hierarchical structure emerged.
Hierarchical structure is also called parent-child structure. Hierarchical structures are mainly used to represent joints (human body models, etc.) and object groups.

Figure B-1 Joint representation using a 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 a joint structure) are easily realized by giving relationships (parent-child structure) to the three objects. This can be a fairly complex and cumbersome program without the idea of hierarchy.

Figure B-2 Image model of demo B


List 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, we will explain the background scrolling, drawing of letters and numbers, input / output control, etc., which are indispensable for game development, focusing on the surface attributes of polygons (priority, color, texture, etc.).


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