3D for GML: Polygons

From ENIGMA
Jump to navigation Jump to search

17.1 Understanding polygons

Polygon literally means a shape that has ‘many angles’. In GM6 you can draw 3D polygons, starting with this code:

//start primitive
d3d_primitive_begin(kind);

The kind of primitive you want drawn needs to be set (to learn more about kinds of primitives, see 17.2). You end drawing any kind of polygonal shape (or primitive) with this code:

//end primitive
d3d_primitive_end();

The shape is defined by points called vertices. Adding points to the primitive is done by using this code:

//add vertex
d3d_vertex(x, y, z);

By defining many points, you can create complicated shapes. Really, it’s like drawing a 3D shape by connecting the dots.

17.2 Defining different kinds of primitives

When you create a 3D shape made up out of points, you don’t want all the defined points to be connected to all other points by countless lines. So, you will have to set how you want the points to be connected. In other words, you will have to set how the defined vertices are related to each other. Primitives can be defined and drawn in several ways:

  • point list: separate points are drawn without connecting lines,
  • line list: connecting lines are drawn between two separate points,
  • line strip: connecting lines are drawn between one point and the next,
  • triangle list: triangles are drawn defined by three separate points,
  • triangle strip: same principle as ‘line strip’ but now for triangles,
  • triangle fan: all triangles are drawn connected to the starting point.

You have to define the kind of primitive drawing you want by using the following values: pr_pointlist, pr_linelist, pr_linestrip, pr_trianglelist, pr_trianglestrip and pr_trianglefan. At the start of the primitive, you simply fill in the kind you want, for example:

//start primitive
d3d_primitive_begin(pr_trianglefan);

17.3 Adding vertices

Now that we’ve told GM what kind of primitive we want, it’s time to actually add some points. This is done using the following code:

//add simple vertex
d3d_vertex(x, y, z);

Each point in 3D space is called a vertex. It is defined by x, y and z. Depending on the kind of primitive you have defined at the start, the points will be drawn and/or connected. If you would like to draw colored or transparent vertices, you can use the following code (in stead of the previous):

//add coloured / transparent vertex
d3d_vertex_color(x, y, z, col, alpha);

When creating points (vertices) with different colors, there will be smooth transitions between the colors. Same is true for the alpha values. Using this code means you can do some pretty advanced drawing. One important note: you will need to switch shading on at the start of your program using the following code:

//switch shading on
d3d_set_shading(true);

Otherwise, your polygons will not be shaded smoothly. Be sure to put this in your 3D initialization script somewhere.

17.4 Drawing textured primitives

Not only can you draw using smooth transitions of color and transparency, you can also use textures on primitives, using this code at the start of a primitive:

//start textured primitive
d3d_primitive_begin_texture(kind, texture);

Again, you need to define the kind of primitive you want drawn. But this time you can add a texture. In fact, you’ll need to define a texture, for example you could write:

//start textured primitive
var tex;
tex=background_get_texture(bk_bricks);
d3d_primitive_begin_texture(pr_trianglefan, tex);

Adding vertices when drawing a textured primitive means you can’t add pointsthe usual way. This is what the code for adding points in a textured primitive looks like:

//add textured vertex
d3d_vertex_texture(x, y, z, xtex, ytex);

As you would expect, the first three values indicate the location of the point in 3D space. The last two values allow for texture mapping. The texture values ‘xtex’ and ‘ytex’ define the placement of the texture image. (Note that sometimes you will have to flip the source texture background or sprite image vertically). Not only can you add texture, you can also add color and transparency to the texture:

//add coloured / transparent textured vertex
d3d_vertex_texture_color(x, y, z, xtex, ytex, col, alpha);

Again, shading needs to be switched on (for example, in your 3D initialization script).

17.5 Polygons and culling

Maybe you remember the tutorial on ‘Back face culling’ in which you could use this code to switch back face culling on:

//use backface culling
d3d_set_culling(true);

Polygons have back faces like any other 3D object. If you’re going to use culling,suggest you take a look at the tutorial on ‘back face culling’. Anyway, if you’re going to use culling with 3D polygonal models, you will need to make sure the back faces are placed at the right position (side).

17.6 Models

In the gm6 file that comes with this text, you will see a demonstration of some of the things discussed in this tutorial. You can look around in the 3D room by using the mouse and move using the WASD keys. By pressing the Space key you will cycle through the following shapes:

  • an ellipsoid, not a polygon but a basic shape (just for reference),
  • a solid pyramid, smoothly colored pyramid with white top,
  • a transparent pyramid, smoothly colored around the corners,
  • a textured wall, made with vertices,
  • a textured wall, with colors and transparency.

Complicated shapes can be created using polygons, as you can understand but designing them manually, can become a daunting or even impossible task. An easier way to create 3D polygonal models is creating a 3D modeling program or using an existing one. In a 3D modeling program or tool what you see is what you get. This means you can create models more easily and them import them into GM.


All rights reserved. Copyright © 2004 by John J.A.H. Weeren. Unauthorized use or reproduction, whole or in part, without written permission from the author is strictly prohibited. This page is part of the tutorial 3D For GML. Reproduced with permission. For more information, please see the talk page.