How to make meshes for 3D rendering engines.

Intro: once you get the hang of making arrays of vertices and indices making custom meshes is a breeze! This is what this post will cover.

What you’ll learn after reading this:
—Everything is made of triangles
—To make one triangle, you make an array of three vertices (to make more than one, you’d still use one array but you’d put more vertices into it)
—In addition to the array of vertices, you need an array of triangle indices.  This array represents the order in which the graphics card should draw the vertices.

Triangles are the what everything is made out of in OpenGL-based 3D rendering.
For example, a rectangle would be made with 2 triangles.

Here’s a picture of a triangle with its three vertices color-coded to help explain them.

vertices-color-coded

Let’s call the three vertices blueV, purpleV and yellowV.
Made up code to create these three vectors could look like this:

Vector3f blueV = new Vector3f(2, 1, 4); //(x,y,z)
Vector3f purpleV = new Vector3f(5, 1, 4);
Vector3f yellowV = new Vector3f(4, 7, 3);

Then, somewhere else in code, we would put the three vectors into an array.

Vector3f[] myTrianglePoints = new Vector3f[] { blueV,  yellowV, purpleV };

And then feed that array to a mesh (psuedo-code):

mesh.giveMeMyVerticesPlease( myTrianglePoints ); // fake method

In the picture, we’re looking at the triangle in the z-negative direction (i.e. we’re further out along the z axis than it is and we’re looking back at it). And we’ve artificially decided that we want to be able to see it from this side for the purposes of this talk.
Look at the array declaration again:

Vector3f[] myTrianglePoints = new Vector3f[] { blueV,  yellowV, purpleV };

Notice that blueV to yellowV to purpleV takes us around the triangle clock-wise. (Look at the picture and trace a line from blue to yellow to purple to prove this to yourself.)

Yet, we want the verts to be drawn in CCW order, to be able to see them from the side that we’re looking at in the picture.

We might have other reasons to care about the order of the verts, but the point here is that for the purposes of getting the graphics card to draw the verts in some CCW order, we can put the verts in any arbitrary order because we are going to make another array—an index array—that tells the graphics card the order.

Meaning…

in myTrianglePoints (new Vector3f[] { blueV,  yellowV, purpleV }) …if we start counting from zero…

blueV is at index 0
yellowV is at index 1
purpleV is at index 2

we make an index array with those numbers but in any order we need:

int[] indices = new int[] {0,2,1}; // tells the graphics card to first draw blueV then purpleV then yellowV

this is also fed to the mesh (psuedo-code):

mesh.giveMeMyIndicesPlease( indices ); // fake method…

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation