This chapter explains the concept of polygons, which is the basic concept of 3D graphics, and how to set polygons in SGL.
Figure 2-1 General polygon example
Figure 2-2 Polygon example on Sega Saturn
/*------------------------------------------------ ----------------------*/ /* 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(); } }
#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 using “polygon.c”
●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 four points from the vertex list by vertex number to create a list of polygon surfaces. Also, if you use a light source, etc., also set the normal vector for each surface. | ●According to the order of the surface list, determine the attributes of the polygon surfaces starting from the top. Attributes include not only polygon color but also surface treatment method. | ●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. |
[Create vertex list: POINT point_< label name> [ ]]
Variable name: POS2FIXED ( vertex_x , vertex_y , vertex_z ),
Represents each vertex coordinate. By using the macro "POStoFIXED", floating point values can be substituted for coordinate values (POS2FIXED is a macro supported by SGL).
Create a list of polygon faces and normal vectors based on the vertex list.
Variable name: NORMAL (vector_x, vector_y, vector_z),
Define the normal vector for each face. A normal vector is used to express the direction of a polygon surface, and is a unit vector that extends perpendicular to the polygon surface.
Each parameter is an XYZ value that represents the direction of the radial 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 will be used to form a polygon surface. The number of vertices selected is always 4. However, only if the third and fourth vertex numbers are the same, a polygon that looks triangular can be expressed.
Also, there is no relationship between the vertex number and the order in which edges are joined. Edges are always connected clockwise starting from the first vertex selected by the list.
Set the polygon surface attributes for each face in the order of the face list.
For details on the settings, please refer to “Chapter 7: Polygon Surface Attributes”.
Sets surface attributes of polygons. Each parameter sets a total of 8 items: front/back detection, Z sort, texture, color, Gouraud processing, drawing mode, sprite reversal processing, and other functions.
(For details, refer to “Chapter 7: Polygon Surface Attributes”)
Create a data structure to pass each polygon data setting as a parameter to the library function “slPutPolygon”.
Create the data column that will actually be passed to “slPutPolygon”.
Here, the number of vertices and the number of polygon faces are newly calculated from information such as the vertex list and added to the parameter data string.
●Polygon data structure● PDATA PD_PLANE1={ point_PLANE1, /* Vertex list */ sizeof(point_PLANE1)/sizeof(POINT), /* Number of vertices */ polygon_PLANE1, /* Plane list */ sizeof(polygon_PLANE1)/sizeof(POLYGON), /* Number of planes */ attribute_PLANE1 /* attribute list */ };
Listing 2-3 below is an example of creating cubic polygon data.
#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 plane list */ NORMAL(-1.0, 0.0, 0.0), /* Normal vector settings */ VERTICES(0, 1, 3, 2), /* Vertex number to be selected in one plane */ 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 plane 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 column for drawing function */ point_PLANE1, sizeof(point_PLANE1)/sizeof(POINT), /* Calculate the number of vertices */ polygon_PLANE1, sizeof(polygon_PLANE1)/sizeof(POLYGON), /* Calculate the number of polygon faces */ attribute_PLANE1 };
Figure 2-6 Drawing model with parameters in List 2-3
Note) Since it is a left-handed coordinate system, the positive Z-axis direction is at the back of the screen.
functional type | function name | parameters | function |
---|---|---|---|
void | slPutPolygon | PDATA*pat | Drawing polygons (setting parameters) |