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

3.Light source


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.

3-1. Light source

A light source typically contains the following information:

・Light source position
・Light intensity
・Color of light

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

  • Polygon surfaces change polygon color depending on the angle of incidence of light.
  • The closer the angle of incidence is vertical, the brighter it is, and the closer the angle of incidence is horizontal, the darker it is.
  • It is darkest when the angle of incidence is negative and vertical.

    3-2. Setting the light source

    Please use the function “slLight” to set the light source in SGL. The light source function only has a direction vector (light source vector) that represents the direction of the light ray, and does not include light intensity or color information.

    [void slLight (VECTOR light);]

    Configure the light source in SGL.
    Assign a VECTOR type variable (light source vector) that indicates the direction of the light ray to the parameter.
    Information such as light source intensity and color is not included in the parameters.
    In SGL, a ray is defined as always being emitted from a specified direction vector.
    Also, unless light source calculation is turned on in the object's surface attributes, shadows caused by the light source will not be reflected even if a light source is set (for details, see “ Chapter 7 Polygon Surface Attributes ”).

    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.

    Precautions when setting the light source

    If various conversion operations such as scaling are applied to the current matrix, light source calculation may not be performed correctly.
    This occurs because the user uses matrix operations to calculate the light source vector specified by the function “ slLight ”. Therefore, when setting the light source, it is necessary to initialize the current matrix.
    Use the function “ slPushUnitMatrix ” or “ slUnitMatrix ” to initialize the current matrix.

    [Bool slPushUnitMatrix ( void )]

    Allocate the identity matrix on the stack and make it the current matrix.
    The previous current matrix is temporarily saved at a higher level in the stack.

    [void slUnitMatrix ( MATRIX mtptr )]

    Makes the matrix specified by the parameter an identity matrix.
    Assign the specified MATRIX type variable to the parameter.
    Also, if "CURRENT" is specified for the parameter, the conversion target matrix becomes the current matrix, and the current matrix can be initialized.

    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.

    List 3-1 sample_3_2: Change in object surface due to light source movement
    /*------------------------------------------------ ----------------------*/
    /* 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();
    	}
    }
    

    Flow 3-1 sample_3_2: Light source movement flowchart

    Appendix: SGL library functions that appeared in this chapter

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

    Table 3-1 SGL library functions that appeared in this chapter
    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


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