Выбрать главу

Lesson 10

This tutorial was created by Lionel Brits (ßetelgeuse). This lesson only explains the sections of code that have been added. By adding just the lines below, the program will not run. If you're interested to know where each of the lines of code below go, download the source code, and follow through it, as you read the tutorial.

Welcome to the infamous Tutorial 10. By now you have a spinning cube or a couple of stars, and you have the basic feel for 3D programming. But wait! Don't run off and start to code Quake IV just yet. Spinning cubes just aren't going to make cool deathmatch opponents :-) These days you need a large, complicated and dynamic 3D world with 6 degrees of freedom and fancy effects like mirrors, portals, warping and of course, high framerates. This tutorial explains the basic "structure" of a 3D world, and also how to move around in it.

Data structure

While it is perfectly alright to code a 3D environment as a long series of numbers, it becomes increasingly hard as the complexity of the environment goes up. For this reason, we must catagorize our data into a more workable fashion. At the top of our list is the sector. Each 3D world is basically a collection of sectors. A sector can be a room, a cube, or any enclosed volume.

typedef struct tagSECTOR // Build Our Sector Structure

{

 int numtriangles; // Number Of Triangles In Sector

 TRIANGLE* triangle; // Pointer To Array Of Triangles

} SECTOR; // Call It SECTOR

A sector holds a series of polygons, so the next catagory will be the triangle (we will stick to triangles for now, as they are alot easier to code.)

typedef struct tagTRIANGLE // Build Our Triangle Structure

{

 VERTEX vertex[3]; // Array Of Three Vertices

} TRIANGLE; // Call It TRIANGLE

The triangle is basically a polygon made up of vertices (plural of vertex), which brings us to our last catagory. The vertex holds the real data that OpenGL is interested in. We define each point on the triangle with it's position in 3D space (x, y, z) as well as it's texture coordinates (u, v).

typedef struct tagVERTEX // Build Our Vertex Structure

{

 float x, y, z; // 3D Coordinates

 float u, v; // Texture Coordinates

} VERTEX; // Call It VERTEX

Loading files

Storing our world data inside our program makes our program quite static and boring. Loading worlds from disk, however, gives us much more flexibility as we can test different worlds without having to recompile our program. Another advantage is that the user can interchange worlds and modify them without having to know the in's and out's of our program. The type of data file we are going to be using will be text. This makes for easy editing, and less code. We will leave binary files for a later date.

The question is, how do we get our data from our file. First, we create a new function called SetupWorld(). We define our file as filein, and we open it for read-only access. We must also close our file when we are done. Let us take a look at the code so far:

// Previous Declaration: char* worldfile = "data\\world.txt";

void SetupWorld() // Setup Our World

{

 FILE *filein; // File To Work With

 filein = fopen(worldfile, "rt"); // Open Our File

 …

 (read our data)

 …

 fclose(filein); // Close Our File

 return; // Jump Back

}

Our next challenge is to read each individual line of text into a variable. This can be done in a number of ways. One problem is that not all lines in the file will contain meaningful information. Blank lines and comments shouldn't be read. Let us create a function called readstr(). This function will read one meaningful line of text into an initialised string. Here's the code:

void readstr(FILE *f, char *string) // Read In A String

{

 do // Start A Loop

 {

  fgets(string, 255, f); // Read One Line

 } while ((string[0] == '/') || (string[0] == '\n')); // See If It Is Worthy Of Processing

 return; // Jump Back

}

Next, we must read in the sector data. This lesson will deal with one sector only, but it is easy to implement a multi-sector engine. Let us turn back to SetupWorld().Our program must know how many triangles are in our sector. In our data file, we will define the number of triangles as follows:

NUMPOLLIES n

Here's the code to read the number of triangles:

int numtriangles; // Number Of Triangles In Sector

char oneline[255]; // String To Store Data In

readstr(filein,oneline); // Get Single Line Of Data

sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles); // Read In Number Of Triangles

The rest of our world-loading process will use the same process. Next, we initialize our sector and read some data into it:

// Previous Declaration: SECTOR sector1;

char oneline[255]; // String To Store Data In

int numtriangles; // Number Of Triangles In Sector

float x, y, z, u, v; // 3D And Texture Coordinates

sector1.triangle = new TRIANGLE[numtriangles]; // Allocate Memory For numtriangles And Set Pointer

sector1.numtriangles = numtriangles; // Define The Number Of Triangles In Sector 1

// Step Through Each Triangle In Sector

for (int triloop = 0; triloop < numtriangles; triloop++) // Loop Through All The Triangles

{

 // Step Through Each Vertex In Triangle

 for (int vertloop = 0; vertloop < 3; vertloop++) // Loop Through All The Vertices

 {

  readstr(filein,oneline); // Read String To Work With

  // Read Data Into Respective Vertex Values

  sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);

  // Store Values Into Respective Vertices

  sector1.triangle[triloop].vertex[vertloop].x = x; // Sector 1, Triangle triloop, Vertice vertloop, x Value=x

  sector1.triangle[triloop].vertex[vertloop].y = y; // Sector 1, Triangle triloop, Vertice vertloop, y Value=y

  sector1.triangle[triloop].vertex[vertloop].z = z; // Sector 1, Triangle triloop, Vertice vertloop, z Value=z

  sector1.triangle[triloop].vertex[vertloop].u = u; // Sector 1, Triangle triloop, Vertice vertloop, u Value=u

  sector1.triangle[triloop].vertex[vertloop].v = v; // Sector 1, Triangle triloop, Vertice vertloop, v Value=v

 }

}

Each triangle in our data file is declared as follows:

X1 Y1 Z1 U1 V1

X2 Y2 Z2 U2 V2

X3 Y3 Z3 U3 V3

Displaying Worlds

Now that we can load our sector into memory, we need to display it on screen. So far we have done some minor rotations and translations, but our camera was always centered at the origin (0,0,0). Any good 3D engine would have the user be able to walk around and explore the world, and so will ours. One way of doing this is to move the camera around and draw the 3D environment relative to the camera position. This is slow and hard to code. What we will do is this: