This chapter explains how to set up the light source in SGL. By using a light source, objects displayed in 3D graphics are given shadows, making it possible to draw more realistic images.
However, it takes a huge amount of time to calculate all of this information as light source information and draw it accurately. Therefore, in SGL, only the light source direction is selected from among these light source information and reflected in polygon drawing.
Regarding the direction of light, light is usually defined as radiation from a single point, but this also takes too much time to calculate. Therefore, the light source position is assumed to be infinitely far away, and the direction of the light is treated as always coming from the same direction, regardless of the coordinate position of the object.
< Figure 3-1 Light source image model>
The variation of an object's surface due to a light source is determined by the direction of the rays and the orientation of the normal vector of each polygonal face. An image of this is shown below.
Figure 3-2 Shade image model
Shading of polygon surface by light source
[void slLight (VECTOR light);]
For polygon surfaces, the polygon color is determined from the angle of incidence of light on the surface.
The closer the angle of incidence is to vertical, the brighter it appears, and the closer it is to horizontal, the darker it appears. Also, the negative vertical direction is expressed darkest.
[Bool slPushUnitMatrix ( void )]
[void slUnitMatrix ( MATRIX mtptr )]
For details on the current matrix and matrix functions, please refer to “ Chapter 5 Matrix ”.
List 3-1 below shows how the shading of a cube polygon surface changes when the direction vector of the light source is successively changed for a cube placed in space.
/*------------------------------------------------ ----------------------*/ /* Cube & A Light Source */ /*------------------------------------------------ ----------------------*/ #include "sgl.h" extern PDATA PD_PLANE1; void ss_main(void) { static ANGLE ang[XYZ]; static FIXED pos[XYZ]; static FIXED light[XYZ]; static ANGLE tmp = DEGtoANG(0.0); slInitSystem(TV_320x224,NULL,1); ang[X] = DEGtoANG(30.0); ang[Y] = DEGtoANG(45.0); ang[Z] = DEGtoANG( 0.0); pos[X] = toFIXED( 0.0); pos[Y] = toFIXED( 0.0); pos[Z] = toFIXED(190.0); light[X] = toFIXED(-1.0); light[Y] = toFIXED( 0.0); light[Z] = toFIXED( 0.0); slPrint("Sample program 3.2" , slLocate(9,2)); while(-1){ slPushMatrix(); { slRotY(tmp); slRotX(DEGtoANG(15.0)); slRotZ(DEGtoANG(15.0)); slCalcPoint(toFIXED(0.0),toFIXED(0.0),toFIXED(1.0),light); } slPopMatrix(); slLight(light); tmp += DEGtoANG(1.0); slPushMatrix(); { slTranslate(pos[X] , pos[Y] , pos[Z]); slRotX(ang[X]); slRotY(ang[Y]); slRotZ(ang[Z]); slPutPolygon(&PD_PLANE1); } slPopMatrix(); slSynch(); } }
functional type | function name | parameters | function |
---|---|---|---|
void | slLight | VECTOR light | Setting the light source vector |
Bool | slPushUintMatrix | void | Allocate identity matrix on stack |
void | slUintMatrix | MATRIX mptrt | Make the specified matrix an identity matrix |