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

6. Camera


This chapter explains the definition of a camera and how to operate it.
A camera defines where and how to view a 3D space, and is also called a viewing transformation. A camera consists of three elements: viewpoint, line of sight, and angle. By changing these three elements, you can freely draw the inside of space on the projection surface.

6-1. Camera definition and settings

There are two types of camera definitions and settings in SGL:

  1. Fix the viewpoint at the origin, the line of sight at the Z axis, and move the object to get the image you want to see.
  2. Fix the object side and move the viewpoint and line of sight to get the image you want to see.

To achieve 1), it is necessary to apply modeling transformation to all objects existing in the space.
To achieve 2), please use the library function “slLookAt”.

However, even in case 2), method 1) is used internally, and it only appears that the camera is moving freely in space.

If you want to make more detailed camera settings (I will not explain them here), please use method 1). From the viewpoint of processing speed, 1) allows you to change the viewpoint and line of sight more efficiently.

6-2. Camera settings using “slLookAt”

SGL supports the library function "slLookAt" to easily configure camera settings.

[void slLookAt ( FIXED *camera , FIXED *target , ANGLE angz ) ;]
This function controls the camera (which direction is in view) in 3D graphics. It holds three parameters: the camera's XYZ coordinates (3-dimensional array), the target's XYZ coordinates (3-dimensional array) that determine the line-of-sight direction, and the rotation angle of the camera relative to the line-of-sight direction.
By using it with the functions "slPerspective" and "slWindow", the drawing area of 3D graphics can be completely determined.

Figure 6-1 Camera image model

Here we will explain the details about angles. Simply put, angle refers to the angle at which you hold the camera relative to your line of sight. By changing the angle value, you can draw different images for the same line of sight.

Figure 6-2 Differences in images depending on angle
a) When the top of the camera is aligned with the Y axis b) When the top of the camera is tilted -45 degrees to the Y axis
(-45 degrees in Z-axis rotation)

note)
The camera position, target coordinates, and object status are exactly the same for both a) and b).

6-3. Actual camera operation

The following program list shows how the image changes when the camera parameters are changed and the camera is moved in 3D space. In the space, a cube with a polygon color that changes for each side is placed offset from the camera's target coordinates.

Listing 6-1 sample_6_3: Moving the camera
/*------------------------------------------------ ----------------------*/
/* Camera Action */
/*------------------------------------------------ ----------------------*/
#include "sgl.h"

#define POS_Z 100.0

typedef struct cam{
	FIXED pos[XYZ];
	FIXED target[XYZ];
	ANGLE ang[XYZ];
} CAMERA;

extern PDATA PD_CUBE;

static FIXED cubepos[][3] = {
	POStoFIXED( 20 , 0 , 270) ,
	POStoFIXED(-70 , 0 , 270) ,
	POStoFIXED( 40 , 0 , 350) ,
	POStoFIXED(-60 , 0 , 370)
};

void dispCube(FIXED pos[XYZ])
{
	slPushMatrix();
	{
		slTranslate(pos[X] , pos[Y] , pos[Z]);
		slPutPolygon(&PD_CUBE);
	}
	slPopMatrix();
}

void ss_main(void)
{
	static ANGLE ang[XYZ];
	static CAMERA cmbuf;
	static ANGLE tmp = DEGtoANG(0.0);

	slInitSystem(TV_320x224,NULL,1);
	slPrint("Sample program 6.3" , slLocate(9,2));

	cmbuf.ang[X] = cmbuf.ang[Y] = cmbuf.ang[Z] = DEGtoANG(0.0);
	cmbuf.target[X] = cmbuf.target[Y] = toFIXED(0.0);
	cmbuf.target[Z] = toFIXED(320.0);
	cmbuf.pos[X] = toFIXED( 0.0);
	cmbuf.pos[Y] = toFIXED(-20.0);
	cmbuf.pos[Z] = toFIXED( 0.0);

	while(-1){
		slUnitMatrix(CURRENT);
		slLookAt(cmbuf.pos , cmbuf.target , cmbuf.ang[Z]);
		tmp += DEGtoANG(2.0);
		cmbuf.pos[X] = POS_Z * slCos(tmp);
		cmbuf.pos[Z] = POS_Z * slSin(tmp);

		dispCube(cubepos[0]);
		dispCube(cubepos[1]);
		dispCube(cubepos[2]);
		dispCube(cubepos[3]);

		slSynch();
	}
}

Flow 6-1 sample_6_3: Camera movement flowchart

Additional notes: SGL library functions that appeared in this chapter

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

Table 6-1 SGL library functions that appeared in this chapter
 functional type
 function name
 parameters
      function
void slLookAt FIXED *camera,*target,ANGLE angz Multiply the gaze matrix by the current matrix


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