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

2. Drawing


In this chapter, we will explain the concept of polygons, which is the basic concept of 3D graphics, and how to set polygons in SGL.

2-1. Polygon

A polygon consists of three or more vertices "v1, v2, v3, v4 ... vn" on the same plane. The closed area obtained by combining each of these vertices in the order of v1 to v2, v2 to v3, v3 to v4, and so on, and finally vn and v1 is called a "polygon". Generally, the straight line connecting each vertex is called an "edge".

Figure 2-1 Example of general polygon

If the number of vertices is 3 or more, a polygon is established, but it is generally said that a polygon with 3 or 4 vertices is optimal (SGL supports only polygons with 4 vertices).

2-2. Polygons in SGL

SGL only supports polygons with 4 vertices. Therefore, polygons with 3 or 5 or more vertices cannot be expressed. However, by setting 2 of the 4 vertices to the same coordinates, it is possible to draw a polygon with 3 vertices in appearance.
Edges are always joined clockwise, regardless of the number of vertices.

Figure 2-2 Example of polygons at Sega Saturn

In Sega Saturn, polygons are drawn as filled areas on the screen. The procedure for drawing polygons is as follows.

  1. Creating a list of vertex coordinates
  2. Creating a polygon face list
  3. Surface attributes are determined for each polygon surface
  4. Draw on screen

Next, we will explain the related functions and function parameters with reference to the actual drawing routine.

Polygon drawing routine

To actually draw a polygon with SGL, use the library function "slPutPolygon". This function draws the polygon specified as a parameter, but the display position and various conversion operations (rotation, translation, enlargement / reduction) follow the current matrix.

[Void slPutPolygon (PDATA * pat);]

Draws data-defined polygons.
Substitute the start address of the area where the polygon data table containing the data of the vertex data list, the number of vertices, the face list, the number of faces, and the face attributes of the polygon is stored in the parameter. For details on the parameters, refer to "Parameters required for polygon drawing" in the next section.

Below, I will explain with reference to the sample program.

In the sample program, after making the necessary preparations for drawing polygons (initialization, setting of coordinates and display angle, etc.), polygons are displayed using the concept of hierarchical matrix (see "Chapter 5: Matrix" for details). The position, display angle, enlargement / reduction ratio (not used this time), etc. are determined, and the actual polygon drawing is performed based on those data.
The library function " slPutPolygon (& PD_DATA): " is used to draw polygons.
“& PD_DATA” in parentheses is a variable that collects the parameters required for polygon display, and is defined in the data file “polygon.c”.
(See “List 2-2: polygon.c: Polygon Parameters”).

Listing 2-1 sample_2_2: Polygon drawing routine
/ * ------------------------------------------------ ---------------------- * /
/ * Draw 1 Polygon * /
/ * ------------------------------------------------ ---------------------- * /
#include "sgl.h"

extern PDATA PD_PLANE1;

void ss_main (void)
{
	static ANGLE ang [XYZ];
	static FIXED pos [XYZ];

	slInitSystem (TV_320x224, NULL, 1);

	ang [X] = ang [Y] = ang [Z] = DEGtoANG (0.0);
	pos [X] = toFIXED (0.0);
	pos [Y] = toFIXED (0.0);
	pos [Z] = toFIXED (220.0);

	slPrint ("Sample program 2.2", slLocate (9,2));

	while (-1) {
		slPushMatrix ();
		{
			slTranslate (pos [X], pos [Y], pos [Z]);
			slRotX (ang [X]);
			slRotY (ang [Y]);
			slRotZ (ang [Z]);
			slPutPolygon (& PD_PLANE1);
		}
		slPopMatrix ();

		slSynch ();
	}
}

Flow 2-1 sample_2_2: Polygon drawing flowchart

Parameters required for polygon drawing

The following list is a set of parameters for drawing polygons that are loaded as “polygon.c” in the program. By rewriting this part, you can change the shape, size, number of faces, surface attributes, etc. of the polygon.

Listing 2-2 polygon.c: polygon parameters>
#include "sgl.h"

POINT point_PLANE1 [] = {
        POStoFIXED (-10.0, -10.0, 0.0),
        POStoFIXED (10.0, -10.0, 0.0),
        POStoFIXED (10.0, 10.0, 0.0),
        POStoFIXED (-10.0, 10.0, 0.0),
};

POLYGON polygon_PLANE1 [] = {
        NORMAL (0.0, 1.0, 0.0), VERTICES (0, 1, 2, 3),
};

ATTR attribute_PLANE1 [] = {
        ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (31, 31, 31), No_Gouraud, MESHoff, sprPolygon, No_Option),
};

PDATA PD_PLANE1 = {
        point_PLANE1, sizeof (point_PLANE1) / sizeof (POINT),
        polygon_PLANE1, sizeof (polygon_PLANE1) / sizeof (POLYGON),
        attribute_PLANE1
};

Figure 2-3 Drawing model by “polygon.c”

Figure 2-4 Procedure for creating parameter data string
● Numbers for vertex identification are automatically assigned in the order of "0,1,2,3,4, ~ n" from the top according to the order of the vertex list.
● For each polygon surface, select any 4 points from the vertex list by vertex number to create a list of polygon surfaces. When using a light source, etc., also set a normal vector for each surface.
● Determine the attributes of polygon faces in order from the top according to the order of the face list. In addition to polygon color, attributes include surface treatment methods.
● Calculate the number of vertices from the vertex list and the number of faces from the face list, and add them to the list. Pass this as a polygon parameter to the drawing function.

Next, I will explain the procedure for creating polygon data based on the data string to be created.

[Creation of vertex list: POINT point_ <label name> []]

A list of initial coordinates of polygon vertices. The vertices are automatically assigned identification numbers such as "0,1,2,3,4 to n" from the top according to the order of the list.

Variable name: POS2FIXED (vertical_x, vertex_y, vertex_z),
Represents the coordinates of each vertex. Floating-point numerical values can be assigned to coordinate values by using the macro "POStoFIXED" (POS2FIXED is a macro supported by SGL).

[Creating a surface list: POLYGON polygon_ <label name> []]

Create a list of polygon faces and normal vectors based on the vertex list.

Variable name: NORMAL (vector_x, vector_y, vector_z),
Define a normal vector for each face. A normal vector is used to represent the orientation of a polygonal surface, and is a unit vector that extends perpendicular to the polygonal surface.
Each parameter is an XYZ value that represents the direction of the radiation vector, and the normal vector must always be specified as a unit vector.

Variable name: VERTICES (v1, v2, v3, v4),
Create a list of which vertices in the vertex list are used to form the polygon face. The number of vertices selected is always four. However, only when the 3rd and 4th vertex numbers are the same, a polygon with a triangular appearance can be expressed.
Also, there is no relationship between the vertex numbers and the order in which the edges are joined. The edges are always joined clockwise from the first vertex selected by the list.

[Creation of surface attribute list: ATTR attribute_ <label name> []]

Set the surface attributes of the polygon for each face according to the order of the face list.
For details on the settings, refer to the chapter "Chapter 7: Polygon Face Attributes".

Variable name: ATTRIBUTE (plane, sort, texture, color, gouraud, mode, dir, option),

Sets the surface attributes of polygons. For each parameter, set a total of 8 items such as front / back judgment, Z sort, texture, color, gourd processing, drawing mode, sprite inversion processing, and other functions.
(For details, refer to the chapter "Chapter 7: Polygon surface attributes")

[Creation of data string for drawing function: PDATA PD_ <label name>]

Create a data structure to pass to the library function “slPutPolygon” with each setting of polygon data as a parameter.

Variable name: Vertex list, number of vertices, face list, face number, face attribute list

Create a data string to actually pass to “slPutPolygon”.
Here, the number of vertices and the number of polygon faces are newly calculated from the information such as the vertex list and added to the parameter data string.

Figure 2-5 Parameters of “PDATA PD_ <label name>”

● Polygon data structure ●

PDATA PD_PLANE1 = {
	point_PLANE1, / * Vertex list * /
	sizeof (point_PLANE1) / sizeof (POINT), / * Number of vertices * /
	polygon_PLANE1, / * Face list * /
	sizeof (polygon_PLANE1) / sizeof (POLYGON), / * number of faces * /
	attribute_PLANE1 / * Attribute list * /
};

2-3. Combination of multiple polygons

When actually expressing some shape using polygons, it is impossible to realize it with only one polygon. Therefore, here we will explain how to create object data using multiple polygon faces.

Creating a cube

To represent a cube, it is necessary to set an object with 8 vertices and 6 polygon faces as parameter values.
In SGL, this can be achieved by rewriting the contents of the above-mentioned parameter setting data file "polygon.c" as follows.
In the list, first create a list of 8 vertices that are the basis of the cube, then create a list of 6 faces from this vertex, determine the surface attributes for each face, and then set each data as the parameter data string "PD_PLANE". Store in. By passing this as a polygon parameter to the drawing function " slPutPolygon ", the cube is drawn on the screen.

Listing 2-3 below is an example of creating cubic polygon data.

Listing 2-3 polygon.c: Cube polygon parameters
#include "sgl.h"

POINT point_PLANE1 [] = {/ * Create vertex list * /
	POStoFIXED (-15.0, -15.0, -15.0), / * Vertex coordinates (XYZ array) * /
	POStoFIXED (-15.0, -15.0, 15.0),
	POStoFIXED (-15.0, 15.0, -15.0),
	POStoFIXED (-15.0, 15.0, 15.0),
	POStoFIXED (15.0, -15.0, -15.0),
	POStoFIXED (15.0, -15.0, 15.0),
	POStoFIXED (15.0, 15.0, -15.0),
	POStoFIXED (15.0, 15.0, 15.0),
};

POLYGON polygon_PLANE1 [] = {/ * Create surface list * /
	NORMAL (-1.0, 0.0, 0.0), / * Normal vector setting * /
	VERTICES (0, 1, 3, 2), / * Vertex number selected on one side * /
	NORMAL (0.0, 0.0, 1.0), 
	VERTICES (1, 5, 7, 3),
	NORMAL (1.0, 0.0, 0.0), 
	VERTICES (5, 4, 6, 7),
	NORMAL (0.0, 0.0, -1.0), 
	VERTICES (4, 0, 2, 6),
	NORMAL (0.0, -1.0, 0.0),
	VERTICES (4, 5, 1, 0),
	NORMAL (0.0, 1.0, 0.0),
	VERTICES (2, 3, 7, 6),
};

ATTR attribute_PLANE1 [] = {/ * Create surface attribute list * /
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (31,31,00), No_Gouraud, MESHoff, sprPolygon, UseLight),
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (31,00,00), No_Gouraud, MESHoff, sprPolygon, UseLight),
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (00,31,00), No_Gouraud, MESHoff, sprPolygon, UseLight),
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (00,00,31), No_Gouraud, MESHoff, sprPolygon, UseLight),
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (31,00,31), No_Gouraud, MESHoff, sprPolygon, UseLight),
	ATTRIBUTE (Dual_Plane, SORT_CEN, No_Texture, C_RGB (00,31,31), No_Gouraud, MESHoff, sprPolygon, UseLight),
};

PDATA PD_PLANE1 = {/ * Create data string for drawing function * /
	point_PLANE1,
	sizeof (point_PLANE1) / sizeof (POINT), / * Calculation of the number of vertices * /
	polygon_PLANE1,
	sizeof (polygon_PLANE1) / sizeof (POLYGON), / * Calculation of the number of polygon faces * /
	attribute_PLANE1
};

Figure 2-6 Drawing model with the parameters in Listing 2-3
Note) Since it is a left-handed coordinate system, the Z-axis positive direction is at the back of the screen.

2-4. Polygon distortion problem

Distortion issues can occur when looking at polygons.
Distortion is often caused by computational error.
It may also be due to the fact that the vertices of the polygon are initially specified in 3D and then projected onto the 2D screen after the conversion operation.
The only distortion that makes it impossible to identify a true polygon (a polygon whose vertices are all on the same plane) is that the polygon becomes a single line when viewed from the edge direction (horizontal to the polygon surface). It can be put away.

Appendix SGL library functions that appeared in this chapter

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

Table 2-1 SGL library functions appearing in this chapter
Functional type Function name Parameters function
void slPutPolygon PDATA * pat Drawing polygons (setting parameters)


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