3D Underwater World (using OpenGL and SDL)

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:

worldobjects.h

home Home   up Up   ( Download )


#ifndef WORLDOBJECT_H_INCLUDED #define WORLDOBJECT_H_INCLUDED #include "global.h" #include "clipping.h" #include "primitives.h" #include "vectors.h" #include "splines.h" /* WorldObject class Base class for all objects in the 3D enviroment. */ class WorldObject { protected: // world position of object Vec3 pos; float bearing; // abstract draw virtual void drawInternal() = 0; // checks if a point in this world object's // space is inside the collision box. bool isInside(Vec3* loc); public: // size for collision detection float width, height, depth; Vec3 org; // constructor WorldObject(); // draw void draw(CameraView* view); // check for collision, and if // one occurs provides the point of // collision // position - vertex to check // direction - direction of vertex as it approached // result - when returns true contains the point of collision bool collisionDetect(Vec3* position, Vec3* direction, Vec3* result); }; /* ActorObject class Base class for all objects that have direction as well as location. */ class ActorObject: public WorldObject { }; /* Vehicle class Thunderbird 4 model */ class VehicleObject: public ActorObject { private: // position and orientation Vec3 obj; Vec3 above; // draw the vehicle at the origin static void drawMesh(); static void drawLeftSide(); static void drawRightSide(); static void drawLightBar(); static void drawPipe(); protected: void drawInternal(); public: // constructor VehicleObject(); // the path MotionPath path; // simulate (animate) void simulate(GLfloat dT); // reset void reset(); }; /* A fish */ class FishObject: public ActorObject { private: float time; float bearing; float v; bool moving; float w, h, s; Vec3 loc; Vec3 obj; int tex; void drawMesh(); public: FishObject(); // draw internal void drawInternal() { drawMesh(); } // simulate void simulate(GLfloat dT); // init textures static Texture** textures; static void initTextures(); }; /** * Floating Sea Weed */ class GrassObject: public ActorObject { private: float time; float h; int count; Vec3* loc; Vec3* now; float* shades; public: // constructor GrassObject(); ~GrassObject(); // draw void drawInternal(); // simulate void simulate(GLfloat dT); }; /* StaticObject class Base class for all static objects. */ class StaticObject: public WorldObject { public: // single function call to position and setup // the static object in the scene void init(float x, float y, float z, float bearing); }; /* UBoatObject class Submarine model */ class UBoatObject: public StaticObject { protected: // draws mesh static void drawMesh(); // draw internal void drawInternal() { drawMesh(); } public: UBoatObject(); }; /* Ship class A sunken ship */ class ShipObject: public StaticObject { protected: // draws mesh static void drawMesh(); // draw internal void drawInternal() { drawMesh(); } public: ShipObject(); }; /* TreasureChest class A treasure chest */ class TreasureChestObject: public StaticObject { protected: Texture* goldT; // draws mesh void drawMesh(); // draw internal void drawInternal() { drawMesh(); } public: TreasureChestObject(); ~TreasureChestObject() { delete goldT; } }; #endif