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

6. Camera


This chapter describes the definition of a camera and how to operate it.
A camera defines where and how to look around a space in 3D space, and is also called a viewing transformation. The camera consists of three elements: viewpoint, line of sight, and angle angle. By changing these three elements, you can freely draw in 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 to the origin and the line of sight to the Z axis, and move the object to get the image you want to see.
  2. Get the image you want to see by fixing the object side and moving the viewpoint and line of sight

To realize 1), it is necessary to apply modeling transformation to all the objects existing in the space.
To realize 2), use the library function "slLookAt".

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

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

6-2. Camera settings using “slLookAt”

SGL supports the library function "slLookAt" for easy camera settings.

[Void slLookAt (FIXED * camera, FIXED * target, ANGLE angz);]
This function controls the camera (which direction to put in the field of view) in 3D graphics. It holds three parameters: the XYZ coordinate value of the camera (three-dimensional array), the XYZ coordinate value of the target that determines the line-of-sight direction (three-dimensional array), and the rotation angle of the camera with respect to the line-of-sight direction.
When used with the functions "slPerspective" and "slWindow", the drawing area of 3D graphics can be completely determined.

Figure 6-1 Image model of camera

Here, I will explain the details about angles. To put it simply, the angle is the angle at which the camera is held with respect to the line of sight. By changing the angle value, you can draw different images for the same line of sight.

<Fig. 6-2 Differences in images depending on the angle>
a) When the upper part of the camera is aligned with the Y axis b) When the upper part of the camera is tilted by -45 degrees with respect to the Y axis (-45 degrees by rotating the Z axis)

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

6-3. Actual camera operation

The following program list shows the changes in the image when the camera parameters are changed and moved in 3D space. In the space, cubes with different polygon colors for each surface are placed offset from the target coordinates of the camera.

<List 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

Appendix SGL library functions that appeared in this chapter

In this chapter, the functions in the following table are explained.

Table 6-1 SGL library functions appearing in this chapter
 Functional type
 Function name
 Parameters
      function
void slLookAt FIXED * camera, * target, ANGLE angz Multiply the line-of-sight matrix by the current matrix


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