Copyright Tristan Aubrey-Jones January 2008.
This is an example OpenGL SDL application which creates an animated 3D underwater world (screenshot), with "Thunderbird 4" with headlights moving on a spline path, a randomly generated sand terrain, sunken ship/submarine/treasure chest models, and swaying fish and seaweed. It is designed to demonstrate basic OpenGL features, and the structure is as follows:
#include "global.h"
#include "vectors.h"
#include "textures.h"
#include "clipping.h"
/*
* RegularGridVertex
* Entry in the regular grid
*/
typedef struct {
// y coordinate
GLfloat y;
// normal value for lighting
Vec3 normal;
// colour and material information about the vertex
}
RegularGridVertex;
/*
* RegularGrid class
* ----------------------
* Used to hold regular polygon meshes for holding
* landscapes etc...
* -------------------------
* Tristan Aubrey-Jones 18/12/2007
*/
class RegularGrid
{
private:
// the grid of element that holds the
// mesh data
RegularGridVertex* grid;
// the width and height in elements of the grid.
int w, d;
Vec3 sz;
// unsafe (no checking) accessors
// gets the value of a given vertex
RegularGridVertex getV(int x, int z) {
return grid[((x*d)+z)];
}
// sets the value of a given vertex
void setV(int x, int z, RegularGridVertex v) {
grid[((x*d)+z)] = v;
}
public:
// texture if any
Texture* tex;
// creates a regular grid with
// xsz x zsz entries
// v diamensions - to draw in real world
// tex - the texture to map onto the mesh
RegularGrid(int xsz, int zsz, Vec3 sz, Texture* tex);
// destructor
~RegularGrid();
// draws the grid as x,z plane in
// the unit cubed.
int draw(CameraView* view);
//void draw() { draw(NULL); }
// get dimensions
int width() { return w; }
int depth() { return d; }
// gets the value of a given vertex,
// and if exceeds size wraps roung
RegularGridVertex get(int x, int z) {
if (x < 0) x = w-x;
if (z < 0) z = d-z;
return getV(x%w, z%d);
}
// sets the value of a given vertex,
// and if exceeds size wraps round
void set(int x, int z, RegularGridVertex v) {
if (x < 0) x = w-x;
if (z < 0) z = d-z;
setV(x%w, z%d, v);
}
// sets all entries to the given value
void setAll(RegularGridVertex v);
// clear, sets all vertices to default values
void clear() {
RegularGridVertex v;
v.y = 0.0;
v.normal.set(0.0, 1.0, 0.0);
setAll(v);
}
// computes the normal value for each vertex
// by averaging the normal vector to each adjacent face
void computeNormals();
};