Fix up all whitespace across project.

This commit is contained in:
Christopher Wellons 2017-02-06 20:13:41 -05:00
parent 0d780bfec3
commit 8212787b96
40 changed files with 8265 additions and 8274 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,53 +1,53 @@
#ifndef ENTITY
#include "entity.h"
#endif
enum
{
BUILDING_SIMPLE,
BUILDING_BLOCKY,
BUILDING_MODERN,
BUILDING_TOWER
};
class CBuilding : public CEntity
{
private:
int _x;
int _y;
int _width;
int _depth;
int _height;
int _texture_type;
int _seed;
int _roof_tiers;
GLrgba _color;
GLrgba _trim_color;
class CMesh* _mesh;
class CMesh* _mesh_flat;
bool _have_lights;
bool _have_trim;
bool _have_logo;
void CreateSimple ();
void CreateBlocky ();
void CreateModern ();
void CreateTower ();
float ConstructWall (int start_x, int start_y, int start_z, int direction, int length, int height, int window_groups, float uv_start, bool blank_corners);
void ConstructSpike (int left, int right, int front, int back, int bottom, int top);
void ConstructCube (int left, int right, int front, int back, int bottom, int top);
void ConstructCube (float left, float right, float front, float back, float bottom, float top);
void ConstructRoof (float left, float right, float front, float back, float bottom);
public:
CBuilding (int type, int x, int y, int height, int width, int depth, int seed, GLrgba color);
~CBuilding ();
void Render (void);
int PolyCount ();
void RenderFlat (bool colored);
unsigned Texture ();
};
#ifndef ENTITY
#include "entity.h"
#endif
enum
{
BUILDING_SIMPLE,
BUILDING_BLOCKY,
BUILDING_MODERN,
BUILDING_TOWER
};
class CBuilding : public CEntity
{
private:
int _x;
int _y;
int _width;
int _depth;
int _height;
int _texture_type;
int _seed;
int _roof_tiers;
GLrgba _color;
GLrgba _trim_color;
class CMesh* _mesh;
class CMesh* _mesh_flat;
bool _have_lights;
bool _have_trim;
bool _have_logo;
void CreateSimple ();
void CreateBlocky ();
void CreateModern ();
void CreateTower ();
float ConstructWall (int start_x, int start_y, int start_z, int direction, int length, int height, int window_groups, float uv_start, bool blank_corners);
void ConstructSpike (int left, int right, int front, int back, int bottom, int top);
void ConstructCube (int left, int right, int front, int back, int bottom, int top);
void ConstructCube (float left, float right, float front, float back, float bottom, float top);
void ConstructRoof (float left, float right, float front, float back, float bottom);
public:
CBuilding (int type, int x, int y, int height, int width, int depth, int seed, GLrgba color);
~CBuilding ();
void Render (void);
int PolyCount ();
void RenderFlat (bool colored);
unsigned Texture ();
};

View File

@ -1,403 +1,403 @@
/*-----------------------------------------------------------------------------
Camera.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This tracks the position and oritentation of the camera. In screensaver
mode, it moves the camera around the world in order to create dramatic
views of the hot zone.
-----------------------------------------------------------------------------*/
#define EYE_HEIGHT 2.0f
#define MAX_PITCH 85
#define FLYCAM_CIRCUT 60000
#define FLYCAM_CIRCUT_HALF (FLYCAM_CIRCUT / 2)
#define FLYCAM_LEG (FLYCAM_CIRCUT / 4)
#define ONE_SECOND 1000
#define CAMERA_CHANGE_INTERVAL 15
#define CAMERA_CYCLE_LENGTH (CAMERA_MODES*CAMERA_CHANGE_INTERVAL)
#include <windows.h>
#include <math.h>
#include <time.h>
#include "glTypes.h"
#include "ini.h"
#include "macro.h"
#include "math.h"
#include "world.h"
#include "win.h"
enum
{
CAMERA_FLYCAM1,
CAMERA_ORBIT_INWARD,
CAMERA_ORBIT_OUTWARD,
CAMERA_ORBIT_ELLIPTICAL,
CAMERA_FLYCAM2,
CAMERA_SPEED,
CAMERA_SPIN,
CAMERA_FLYCAM3,
CAMERA_MODES
};
static GLvector angle;
static GLvector position;
static GLvector auto_angle;
static GLvector auto_position;
static float distance;
static GLvector movement;
static bool cam_auto;
static float tracker;
static unsigned last_update;
static int camera_behavior;
static unsigned last_move;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static GLvector flycam_position (unsigned t)
{
unsigned leg;
float delta;
GLvector start, end;
GLbbox hot_zone;
hot_zone = WorldHotZone ();
t %= FLYCAM_CIRCUT;
leg = t / FLYCAM_LEG;
delta = (float)(t % FLYCAM_LEG) / FLYCAM_LEG;
switch (leg) {
case 0:
start = glVector (hot_zone.min.x, 25.0f, hot_zone.min.z);
end = glVector (hot_zone.min.x, 60.0f, hot_zone.max.z);
break;
case 1:
start = glVector (hot_zone.min.x, 60.0f, hot_zone.max.z);
end = glVector (hot_zone.max.x, 25.0f, hot_zone.max.z);
break;
case 2:
start = glVector (hot_zone.max.x, 25.0f, hot_zone.max.z);
end = glVector (hot_zone.max.x, 60.0f, hot_zone.min.z);
break;
case 3:
start = glVector (hot_zone.max.x, 60.0f, hot_zone.min.z);
end = glVector (hot_zone.min.x, 25.0f, hot_zone.min.z);
break;
}
delta = MathScalarCurve (delta);
return glVectorInterpolate (start, end, delta);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_auto_cam ()
{
float dist;
unsigned t;
unsigned elapsed;
unsigned now;
int behavior;
GLvector target;
now = GetTickCount ();
elapsed = now - last_update;
elapsed = MIN (elapsed, 50); //limit to 1/20th second worth of time
if (elapsed == 0)
return;
last_update = now;
t = time (NULL) % CAMERA_CYCLE_LENGTH;
#if SCREENSAVER
behavior = t / CAMERA_CHANGE_INTERVAL;
#else
behavior = camera_behavior;
#endif
tracker += (float)elapsed / 300.0f;
//behavior = CAMERA_FLYCAM1;
switch (behavior) {
case CAMERA_ORBIT_INWARD:
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 150.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 150.0f;
target = glVector (WORLD_HALF, 40.0f, WORLD_HALF);
break;
case CAMERA_ORBIT_OUTWARD:
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 250.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 250.0f;
target = glVector (WORLD_HALF, 30.0f, WORLD_HALF);
break;
case CAMERA_ORBIT_ELLIPTICAL:
dist = 150.0f + sinf (tracker * DEGREES_TO_RADIANS / 1.1f) * 50;
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * dist;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * dist;
target = glVector (WORLD_HALF, 50.0f, WORLD_HALF);
break;
case CAMERA_FLYCAM1:
case CAMERA_FLYCAM2:
case CAMERA_FLYCAM3:
auto_position = (flycam_position (now) + flycam_position (now + 4000)) / 2.0f;
target = flycam_position (now + FLYCAM_CIRCUT_HALF - ONE_SECOND * 3);
break;
case CAMERA_SPEED:
auto_position = (flycam_position (now) + flycam_position (now + 500)) / 2.0f;
target = flycam_position (now + ONE_SECOND * 5);
auto_position.y /= 2;
target.y /= 2;
break;
case CAMERA_SPIN:
default:
target.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 300.0f;
target.y = 30.0f;
target.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 300.0f;
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 50.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 50.0f;
}
dist = MathDistance (auto_position.x, auto_position.z, target.x, target.z);
auto_angle.y = MathAngle (-MathAngle (auto_position.x, auto_position.z, target.x, target.z));
auto_angle.x = 90.0f + MathAngle (0, auto_position.y, dist, target.y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraAutoToggle ()
{
cam_auto = !cam_auto;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraNextBehavior ()
{
camera_behavior++;
camera_behavior %= CAMERA_MODES;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraYaw (float delta)
{
angle.y -= delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPitch (float delta)
{
angle.x -= delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPan (float delta)
{
float move_x, move_y;
move_x = (float)sin (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
move_y = (float)cos (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
position.x -= move_y * delta;
position.z -= -move_x * delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraForward (float delta)
{
float move_x, move_y;
move_y = (float)sin (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
move_x = (float)cos (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
position.x -= move_y * delta;
position.z -= move_x * delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraVertical (float val)
{
movement.y += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraLateral (float val)
{
movement.x += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraMedial (float val)
{
movement.z += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector CameraPosition (void)
{
if (cam_auto)
return auto_position;
return position;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraReset ()
{
position.y = 50.0f;
position.x = WORLD_HALF;
position.z = WORLD_HALF;
angle.x = 0.0f;
angle.y = 0.0f;
angle.z = 0.0f;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPositionSet (GLvector new_pos)
{
position = new_pos;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector CameraAngle (void)
{
if (cam_auto)
return auto_angle;
return angle;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraAngleSet (GLvector new_angle)
{
angle = new_angle;
angle.x = CLAMP (angle.x, -80.0f, 80.0f);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraInit (void)
{
angle = IniVector ("CameraAngle");
position = IniVector ("CameraPosition");
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraUpdate (void)
{
CameraPan (movement.x);
CameraForward (movement.z);
position.y += movement.y / 10.0f;
if (GetTickCount () - last_move > 1000)
movement *= 0.9f;
else
movement *= 0.99f;
if (SCREENSAVER)
cam_auto = true;
if (cam_auto)
do_auto_cam ();
if (angle.y < 0.0f)
angle.y = 360.0f - (float)fmod (fabs (angle.y), 360.0f);
angle.y = (float)fmod (angle.y, 360.0f);
angle.x = CLAMP (angle.x, -MAX_PITCH, MAX_PITCH);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraTerm (void)
{
//just store our most recent position in the ini
IniVectorSet ("CameraAngle", angle);
IniVectorSet ("CameraPosition", position);
}
/*-----------------------------------------------------------------------------
Camera.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This tracks the position and oritentation of the camera. In screensaver
mode, it moves the camera around the world in order to create dramatic
views of the hot zone.
-----------------------------------------------------------------------------*/
#define EYE_HEIGHT 2.0f
#define MAX_PITCH 85
#define FLYCAM_CIRCUT 60000
#define FLYCAM_CIRCUT_HALF (FLYCAM_CIRCUT / 2)
#define FLYCAM_LEG (FLYCAM_CIRCUT / 4)
#define ONE_SECOND 1000
#define CAMERA_CHANGE_INTERVAL 15
#define CAMERA_CYCLE_LENGTH (CAMERA_MODES*CAMERA_CHANGE_INTERVAL)
#include <windows.h>
#include <math.h>
#include <time.h>
#include "glTypes.h"
#include "ini.h"
#include "macro.h"
#include "math.h"
#include "world.h"
#include "win.h"
enum
{
CAMERA_FLYCAM1,
CAMERA_ORBIT_INWARD,
CAMERA_ORBIT_OUTWARD,
CAMERA_ORBIT_ELLIPTICAL,
CAMERA_FLYCAM2,
CAMERA_SPEED,
CAMERA_SPIN,
CAMERA_FLYCAM3,
CAMERA_MODES
};
static GLvector angle;
static GLvector position;
static GLvector auto_angle;
static GLvector auto_position;
static float distance;
static GLvector movement;
static bool cam_auto;
static float tracker;
static unsigned last_update;
static int camera_behavior;
static unsigned last_move;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static GLvector flycam_position (unsigned t)
{
unsigned leg;
float delta;
GLvector start, end;
GLbbox hot_zone;
hot_zone = WorldHotZone ();
t %= FLYCAM_CIRCUT;
leg = t / FLYCAM_LEG;
delta = (float)(t % FLYCAM_LEG) / FLYCAM_LEG;
switch (leg) {
case 0:
start = glVector (hot_zone.min.x, 25.0f, hot_zone.min.z);
end = glVector (hot_zone.min.x, 60.0f, hot_zone.max.z);
break;
case 1:
start = glVector (hot_zone.min.x, 60.0f, hot_zone.max.z);
end = glVector (hot_zone.max.x, 25.0f, hot_zone.max.z);
break;
case 2:
start = glVector (hot_zone.max.x, 25.0f, hot_zone.max.z);
end = glVector (hot_zone.max.x, 60.0f, hot_zone.min.z);
break;
case 3:
start = glVector (hot_zone.max.x, 60.0f, hot_zone.min.z);
end = glVector (hot_zone.min.x, 25.0f, hot_zone.min.z);
break;
}
delta = MathScalarCurve (delta);
return glVectorInterpolate (start, end, delta);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_auto_cam ()
{
float dist;
unsigned t;
unsigned elapsed;
unsigned now;
int behavior;
GLvector target;
now = GetTickCount ();
elapsed = now - last_update;
elapsed = MIN (elapsed, 50); //limit to 1/20th second worth of time
if (elapsed == 0)
return;
last_update = now;
t = time (NULL) % CAMERA_CYCLE_LENGTH;
#if SCREENSAVER
behavior = t / CAMERA_CHANGE_INTERVAL;
#else
behavior = camera_behavior;
#endif
tracker += (float)elapsed / 300.0f;
//behavior = CAMERA_FLYCAM1;
switch (behavior) {
case CAMERA_ORBIT_INWARD:
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 150.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 150.0f;
target = glVector (WORLD_HALF, 40.0f, WORLD_HALF);
break;
case CAMERA_ORBIT_OUTWARD:
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 250.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 250.0f;
target = glVector (WORLD_HALF, 30.0f, WORLD_HALF);
break;
case CAMERA_ORBIT_ELLIPTICAL:
dist = 150.0f + sinf (tracker * DEGREES_TO_RADIANS / 1.1f) * 50;
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * dist;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * dist;
target = glVector (WORLD_HALF, 50.0f, WORLD_HALF);
break;
case CAMERA_FLYCAM1:
case CAMERA_FLYCAM2:
case CAMERA_FLYCAM3:
auto_position = (flycam_position (now) + flycam_position (now + 4000)) / 2.0f;
target = flycam_position (now + FLYCAM_CIRCUT_HALF - ONE_SECOND * 3);
break;
case CAMERA_SPEED:
auto_position = (flycam_position (now) + flycam_position (now + 500)) / 2.0f;
target = flycam_position (now + ONE_SECOND * 5);
auto_position.y /= 2;
target.y /= 2;
break;
case CAMERA_SPIN:
default:
target.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 300.0f;
target.y = 30.0f;
target.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 300.0f;
auto_position.x = WORLD_HALF + sinf (tracker * DEGREES_TO_RADIANS) * 50.0f;
auto_position.y = 60.0f;
auto_position.z = WORLD_HALF + cosf (tracker * DEGREES_TO_RADIANS) * 50.0f;
}
dist = MathDistance (auto_position.x, auto_position.z, target.x, target.z);
auto_angle.y = MathAngle (-MathAngle (auto_position.x, auto_position.z, target.x, target.z));
auto_angle.x = 90.0f + MathAngle (0, auto_position.y, dist, target.y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraAutoToggle ()
{
cam_auto = !cam_auto;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraNextBehavior ()
{
camera_behavior++;
camera_behavior %= CAMERA_MODES;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraYaw (float delta)
{
angle.y -= delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPitch (float delta)
{
angle.x -= delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPan (float delta)
{
float move_x, move_y;
move_x = (float)sin (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
move_y = (float)cos (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
position.x -= move_y * delta;
position.z -= -move_x * delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraForward (float delta)
{
float move_x, move_y;
move_y = (float)sin (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
move_x = (float)cos (-angle.y * DEGREES_TO_RADIANS) / 10.0f;
position.x -= move_y * delta;
position.z -= move_x * delta;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraVertical (float val)
{
movement.y += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraLateral (float val)
{
movement.x += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraMedial (float val)
{
movement.z += val;
last_move = GetTickCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector CameraPosition (void)
{
if (cam_auto)
return auto_position;
return position;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraReset ()
{
position.y = 50.0f;
position.x = WORLD_HALF;
position.z = WORLD_HALF;
angle.x = 0.0f;
angle.y = 0.0f;
angle.z = 0.0f;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraPositionSet (GLvector new_pos)
{
position = new_pos;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector CameraAngle (void)
{
if (cam_auto)
return auto_angle;
return angle;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraAngleSet (GLvector new_angle)
{
angle = new_angle;
angle.x = CLAMP (angle.x, -80.0f, 80.0f);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraInit (void)
{
angle = IniVector ("CameraAngle");
position = IniVector ("CameraPosition");
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraUpdate (void)
{
CameraPan (movement.x);
CameraForward (movement.z);
position.y += movement.y / 10.0f;
if (GetTickCount () - last_move > 1000)
movement *= 0.9f;
else
movement *= 0.99f;
if (SCREENSAVER)
cam_auto = true;
if (cam_auto)
do_auto_cam ();
if (angle.y < 0.0f)
angle.y = 360.0f - (float)fmod (fabs (angle.y), 360.0f);
angle.y = (float)fmod (angle.y, 360.0f);
angle.x = CLAMP (angle.x, -MAX_PITCH, MAX_PITCH);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CameraTerm (void)
{
//just store our most recent position in the ini
IniVectorSet ("CameraAngle", angle);
IniVectorSet ("CameraPosition", position);
}

View File

@ -1,24 +1,24 @@
#ifndef TYPES
#include "glTypes.h"
#endif
GLvector CameraAngle (void);
void CameraAngleSet (GLvector new_angle);
void CameraAutoToggle ();
float CameraDistance (void);
void CameraDistanceSet (float new_distance);
void CameraInit (void);
void CameraNextBehavior (void);
GLvector CameraPosition (void);
void CameraPositionSet (GLvector new_pos);
void CameraReset ();
void CameraUpdate (void);
void CameraTerm (void);
void CameraForward (float delta);
void CameraPan (float delta_x);
void CameraPitch (float delta_y);
void CameraYaw (float delta_x);
void CameraVertical (float val);
void CameraLateral (float val);
void CameraMedial (float val);
#ifndef TYPES
#include "glTypes.h"
#endif
GLvector CameraAngle (void);
void CameraAngleSet (GLvector new_angle);
void CameraAutoToggle ();
float CameraDistance (void);
void CameraDistanceSet (float new_distance);
void CameraInit (void);
void CameraNextBehavior (void);
GLvector CameraPosition (void);
void CameraPositionSet (GLvector new_pos);
void CameraReset ();
void CameraUpdate (void);
void CameraTerm (void);
void CameraForward (float delta);
void CameraPan (float delta_x);
void CameraPitch (float delta_y);
void CameraYaw (float delta_x);
void CameraVertical (float val);
void CameraLateral (float val);
void CameraMedial (float val);

614
car.cpp
View File

@ -1,307 +1,307 @@
/*-----------------------------------------------------------------------------
Car.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This creates the little two-triangle cars and moves them around the map.
-----------------------------------------------------------------------------*/
#define DEAD_ZONE 200
#define STUCK_TIME 230
#define UPDATE_INTERVAL 50 //milliseconds
#define MOVEMENT_SPEED 0.61f
#define CAR_SIZE 3.0f
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "building.h"
#include "car.h"
#include "camera.h"
#include "mesh.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
#include "win.h"
static GLvector direction[] =
{
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
};
static int dangles[] = { 0, 90, 180, 270};
static GLvector2 angles[360];
static bool angles_done;
static unsigned char carmap[WORLD_SIZE][WORLD_SIZE];
static CCar* head;
static unsigned next_update;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int CarCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarClear ()
{
CCar* c;
for (c = head; c; c = c->m_next)
c->Park ();
ZeroMemory (carmap, sizeof (carmap));
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarRender ()
{
CCar* c;
if (!angles_done) {
for (int i = 0 ;i < 360; i++) {
angles[i].x = cosf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
angles[i].y = sinf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture (GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_HEADLIGHT));
for (c = head; c; c = c->m_next)
c->Render ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarUpdate ()
{
CCar* c;
unsigned now;
if (!TextureReady () || !EntityReady ())
return;
now = GetTickCount ();
if (next_update > now)
return;
next_update = now + UPDATE_INTERVAL;
for (c = head; c; c = c->m_next)
c->Update ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CCar::CCar ()
{
m_ready = false;
m_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool CCar::TestPosition (int row, int col)
{
//test the given position and see if it's already occupied
if (carmap[row][col])
return false;
//now make sure that the lane is going the right direction
if (WorldCell (row, col) != WorldCell (m_row, m_col))
return false;
return true;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Update (void)
{
int new_row, new_col;
GLvector old_pos;
GLvector camera;
//If the car isn't ready, place it on the map and get it moving
camera = CameraPosition ();
if (!m_ready) {
//if the car isn't ready, we need to place it somewhere on the map
m_row = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
m_col = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
//if there is already a car here, forget it.
if (carmap[m_row][m_col] > 0)
return;
//if this spot is not a road, forget it
if (!(WorldCell (m_row, m_col) & CLAIM_ROAD))
return;
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
return;
//good spot. place the car
m_position = glVector ((float)m_row, 0.1f, (float)m_col);
m_drive_position = m_position;
m_ready = true;
if (WorldCell (m_row, m_col) & MAP_ROAD_NORTH)
m_direction = NORTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_EAST)
m_direction = EAST;
if (WorldCell (m_row, m_col) & MAP_ROAD_SOUTH)
m_direction = SOUTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_WEST)
m_direction = WEST;
m_drive_angle = dangles[m_direction];
m_max_speed = (float)(4 + RandomVal (6)) / 10.0f;
m_speed = 0.0f;
m_change = 3;
m_stuck = 0;
carmap[m_row][m_col]++;
}
//take the car off the map and move it
carmap[m_row][m_col]--;
old_pos = m_position;
m_speed += m_max_speed * 0.05f;
m_speed = MIN (m_speed, m_max_speed);
m_position += direction[m_direction] * MOVEMENT_SPEED * m_speed;
//If the car has moved out of view, there's no need to keep simulating it.
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
m_ready = false;
//if the car is far away, remove it. We use manhattan units because buildings almost always
//block views of cars on the diagonal.
if (fabs (camera.x - m_position.x) + fabs (camera.z - m_position.z) > RenderFogDistance ())
m_ready = false;
//if the car gets too close to the edge of the map, take it out of play
if (m_position.x < DEAD_ZONE || m_position.x > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_position.z < DEAD_ZONE || m_position.z > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_stuck >= STUCK_TIME)
m_ready = false;
if (!m_ready)
return;
//Check the new position and make sure its not in another car
new_row = (int)m_position.x;
new_col = (int)m_position.z;
if (new_row != m_row || new_col != m_col) {
//see if the new position places us on top of another car
if (carmap[new_row][new_col]) {
m_position = old_pos;
m_speed = 0.0f;
m_stuck++;
} else {
//look at the new position and decide if we're heading towards or away from the camera
m_row = new_row;
m_col = new_col;
m_change--;
m_stuck = 0;
if (m_direction == NORTH)
m_front = camera.z < m_position.z;
else if (m_direction == SOUTH)
m_front = camera.z > m_position.z;
else if (m_direction == EAST)
m_front = camera.x > m_position.x;
else
m_front = camera.x < m_position.x;
}
}
m_drive_position = (m_drive_position + m_position) / 2.0f;
//place the car back on the map
carmap[m_row][m_col]++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Render ()
{
GLvector pos;
int angle;
int turn;
float top;
if (!m_ready)
return;
if (!Visible (m_drive_position))
return;
if (m_front) {
glColor3f (1, 1, 0.8f);
top = CAR_SIZE;
} else {
glColor3f (0.5, 0.2f, 0);
top = 0.0f;
}
glBegin (GL_QUADS);
angle = dangles[m_direction];
pos = m_drive_position;//
angle = 360 - (int)MathAngle (m_position.x, m_position.z, pos.x, pos.z);
angle %= 360;
turn = (int)MathAngleDifference ((float)m_drive_angle, (float)angle);
m_drive_angle += SIGN (turn);
pos += glVector (0.5f, 0.0f, 0.5f);
glTexCoord2f (0, 0);
glVertex3f (pos.x + angles[angle].x, -CAR_SIZE, pos.z + angles[angle].y);
glTexCoord2f (1, 0);
glVertex3f (pos.x - angles[angle].x, -CAR_SIZE, pos.z - angles[angle].y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - angles[angle].x, top, pos.z - angles[angle].y);
glTexCoord2f (0, 1);
glVertex3f (pos.x + angles[angle].x, top, pos.z + angles[angle].y);
glEnd ();
}
/*-----------------------------------------------------------------------------
Car.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This creates the little two-triangle cars and moves them around the map.
-----------------------------------------------------------------------------*/
#define DEAD_ZONE 200
#define STUCK_TIME 230
#define UPDATE_INTERVAL 50 //milliseconds
#define MOVEMENT_SPEED 0.61f
#define CAR_SIZE 3.0f
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "building.h"
#include "car.h"
#include "camera.h"
#include "mesh.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
#include "win.h"
static GLvector direction[] =
{
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
};
static int dangles[] = { 0, 90, 180, 270};
static GLvector2 angles[360];
static bool angles_done;
static unsigned char carmap[WORLD_SIZE][WORLD_SIZE];
static CCar* head;
static unsigned next_update;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int CarCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarClear ()
{
CCar* c;
for (c = head; c; c = c->m_next)
c->Park ();
ZeroMemory (carmap, sizeof (carmap));
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarRender ()
{
CCar* c;
if (!angles_done) {
for (int i = 0 ;i < 360; i++) {
angles[i].x = cosf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
angles[i].y = sinf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture (GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_HEADLIGHT));
for (c = head; c; c = c->m_next)
c->Render ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarUpdate ()
{
CCar* c;
unsigned now;
if (!TextureReady () || !EntityReady ())
return;
now = GetTickCount ();
if (next_update > now)
return;
next_update = now + UPDATE_INTERVAL;
for (c = head; c; c = c->m_next)
c->Update ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CCar::CCar ()
{
m_ready = false;
m_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool CCar::TestPosition (int row, int col)
{
//test the given position and see if it's already occupied
if (carmap[row][col])
return false;
//now make sure that the lane is going the right direction
if (WorldCell (row, col) != WorldCell (m_row, m_col))
return false;
return true;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Update (void)
{
int new_row, new_col;
GLvector old_pos;
GLvector camera;
//If the car isn't ready, place it on the map and get it moving
camera = CameraPosition ();
if (!m_ready) {
//if the car isn't ready, we need to place it somewhere on the map
m_row = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
m_col = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
//if there is already a car here, forget it.
if (carmap[m_row][m_col] > 0)
return;
//if this spot is not a road, forget it
if (!(WorldCell (m_row, m_col) & CLAIM_ROAD))
return;
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
return;
//good spot. place the car
m_position = glVector ((float)m_row, 0.1f, (float)m_col);
m_drive_position = m_position;
m_ready = true;
if (WorldCell (m_row, m_col) & MAP_ROAD_NORTH)
m_direction = NORTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_EAST)
m_direction = EAST;
if (WorldCell (m_row, m_col) & MAP_ROAD_SOUTH)
m_direction = SOUTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_WEST)
m_direction = WEST;
m_drive_angle = dangles[m_direction];
m_max_speed = (float)(4 + RandomVal (6)) / 10.0f;
m_speed = 0.0f;
m_change = 3;
m_stuck = 0;
carmap[m_row][m_col]++;
}
//take the car off the map and move it
carmap[m_row][m_col]--;
old_pos = m_position;
m_speed += m_max_speed * 0.05f;
m_speed = MIN (m_speed, m_max_speed);
m_position += direction[m_direction] * MOVEMENT_SPEED * m_speed;
//If the car has moved out of view, there's no need to keep simulating it.
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
m_ready = false;
//if the car is far away, remove it. We use manhattan units because buildings almost always
//block views of cars on the diagonal.
if (fabs (camera.x - m_position.x) + fabs (camera.z - m_position.z) > RenderFogDistance ())
m_ready = false;
//if the car gets too close to the edge of the map, take it out of play
if (m_position.x < DEAD_ZONE || m_position.x > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_position.z < DEAD_ZONE || m_position.z > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_stuck >= STUCK_TIME)
m_ready = false;
if (!m_ready)
return;
//Check the new position and make sure its not in another car
new_row = (int)m_position.x;
new_col = (int)m_position.z;
if (new_row != m_row || new_col != m_col) {
//see if the new position places us on top of another car
if (carmap[new_row][new_col]) {
m_position = old_pos;
m_speed = 0.0f;
m_stuck++;
} else {
//look at the new position and decide if we're heading towards or away from the camera
m_row = new_row;
m_col = new_col;
m_change--;
m_stuck = 0;
if (m_direction == NORTH)
m_front = camera.z < m_position.z;
else if (m_direction == SOUTH)
m_front = camera.z > m_position.z;
else if (m_direction == EAST)
m_front = camera.x > m_position.x;
else
m_front = camera.x < m_position.x;
}
}
m_drive_position = (m_drive_position + m_position) / 2.0f;
//place the car back on the map
carmap[m_row][m_col]++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Render ()
{
GLvector pos;
int angle;
int turn;
float top;
if (!m_ready)
return;
if (!Visible (m_drive_position))
return;
if (m_front) {
glColor3f (1, 1, 0.8f);
top = CAR_SIZE;
} else {
glColor3f (0.5, 0.2f, 0);
top = 0.0f;
}
glBegin (GL_QUADS);
angle = dangles[m_direction];
pos = m_drive_position;//
angle = 360 - (int)MathAngle (m_position.x, m_position.z, pos.x, pos.z);
angle %= 360;
turn = (int)MathAngleDifference ((float)m_drive_angle, (float)angle);
m_drive_angle += SIGN (turn);
pos += glVector (0.5f, 0.0f, 0.5f);
glTexCoord2f (0, 0);
glVertex3f (pos.x + angles[angle].x, -CAR_SIZE, pos.z + angles[angle].y);
glTexCoord2f (1, 0);
glVertex3f (pos.x - angles[angle].x, -CAR_SIZE, pos.z - angles[angle].y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - angles[angle].x, top, pos.z - angles[angle].y);
glTexCoord2f (0, 1);
glVertex3f (pos.x + angles[angle].x, top, pos.z + angles[angle].y);
glEnd ();
}

60
car.h
View File

@ -1,30 +1,30 @@
class CCar
{
GLvector m_position;
GLvector m_drive_position;
bool m_ready;
bool m_front;
int m_drive_angle;
int m_row;
int m_col;
int m_direction;
int m_change;
int m_stuck;
float m_speed;
float m_max_speed;
public:
CCar ();
bool TestPosition (int row, int col);
void Render ();
void Update ();
void Park () { m_ready = false;}
class CCar* m_next;
};
void CarClear ();
int CarCount ();
void CarRender ();
void CarUpdate ();
class CCar
{
GLvector m_position;
GLvector m_drive_position;
bool m_ready;
bool m_front;
int m_drive_angle;
int m_row;
int m_col;
int m_direction;
int m_change;
int m_stuck;
float m_speed;
float m_max_speed;
public:
CCar ();
bool TestPosition (int row, int col);
void Render ();
void Update ();
void Park () { m_ready = false;}
class CCar* m_next;
};
void CarClear ();
int CarCount ();
void CarRender ();
void CarUpdate ();

582
deco.cpp
View File

@ -1,291 +1,291 @@
/*-----------------------------------------------------------------------------
Deco.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This handles building and rendering decoration objects - infrastructure &
such around the city.
-----------------------------------------------------------------------------*/
#define LOGO_OFFSET 0.2f //How far a logo sticks out from the given surface
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "deco.h"
#include "light.h"
#include "mesh.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CDeco::~CDeco ()
{
delete _mesh;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CDeco::CDeco ()
{
_mesh = new CMesh ();
_use_alpha = false;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::Render ()
{
glColor3fv (&_color.red);
_mesh->Render ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::RenderFlat (bool colored)
{
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool CDeco::Alpha ()
{
return _use_alpha;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int CDeco::PolyCount ()
{
return _mesh->PolyCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned CDeco::Texture ()
{
return _texture;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateRadioTower (GLvector pos, float height)
{
CLight* l;
float offset;
GLvertex v;
fan f;
for(int i=0; i<6; i++)
f.index_list.push_back(i);
offset = height / 15.0f;
_center = pos;
_use_alpha = true;
//Radio tower
v.position = glVector (_center.x, _center.y + height, _center.z); v.uv = glVector (0,1);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z - offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x + offset, _center.y, _center.z - offset); v.uv = glVector (0,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x + offset, _center.y, _center.z + offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z + offset); v.uv = glVector (0,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z - offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
_mesh->FanAdd (f);
l = new CLight (glVector (_center.x, _center.y + height + 1.0f, _center.z), glRgba (255,192,160), 1);
l->Blink ();
_texture = TextureId (TEXTURE_LATTICE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLogo (GLvector2 start, GLvector2 end, float bottom, int seed, GLrgba color)
{
GLvertex p;
quad_strip qs;
float u1, u2, v1, v2;
float top;
float height, length;
GLvector2 center2d;
GLvector to;
GLvector out;
int logo_index;
qs.index_list.push_back(0);
qs.index_list.push_back(1);
qs.index_list.push_back(3);
qs.index_list.push_back(2);
_use_alpha = true;
_color = color;
logo_index = seed % LOGO_ROWS;
to = glVector (start.x, 0.0f, start.y) - glVector (end.x, 0.0f, end.y);
to = glVectorNormalize (to);
out = glVectorCrossProduct (glVector (0.0f, 1.0f, 0.0f), to) * LOGO_OFFSET;
center2d = (start + end) / 2;
_center = glVector (center2d.x, bottom, center2d.y);
length = glVectorLength (start - end);
height = (length / 8.0f) * 1.5f;
top = bottom + height;
u1 = 0.0f;
u2 = 0.5f;//We actually only use the left half of the texture
v1 = (float)logo_index / LOGO_ROWS;
v2 = v1 + (1.0f / LOGO_ROWS);
p.position = glVector (start.x, bottom, start.y) + out; p.uv = glVector (u1,v1);
_mesh->VertexAdd (p);
p.position = glVector (end.x, bottom, end.y) + out; p.uv = glVector (u2, v1);
_mesh->VertexAdd (p);
p.position = glVector (end.x, top, end.y) + out; p.uv = glVector (u2, v2);
_mesh->VertexAdd (p);
p.position = glVector (start.x, top, start.y) + out; p.uv = glVector (u1, v2);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_LOGOS);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLightStrip (float x, float z, float width, float depth, float height, GLrgba color)
{
GLvertex p;
quad_strip qs1;
float u, v;
qs1.index_list.push_back(0);
qs1.index_list.push_back(1);
qs1.index_list.push_back(3);
qs1.index_list.push_back(2);
_color = color;
_use_alpha = true;
_center = glVector (x + width / 2, height, z + depth / 2);
if (width < depth) {
u = 1.0f;
v = (float)((int)(depth / width));
} else {
v = 1.0f;
u = (float)((int)(width / depth));
}
_texture = TextureId (TEXTURE_LIGHT);
p.position = glVector (x, height, z); p.uv = glVector (0.0f, 0.0f);
_mesh->VertexAdd (p);
p.position = glVector (x, height, z + depth); p.uv = glVector (0.0f, v);
_mesh->VertexAdd (p);
p.position = glVector (x + width, height, z + depth); p.uv = glVector (u, v);
_mesh->VertexAdd (p);
p.position = glVector (x + width, height, z); p.uv = glVector (u, 0.0f);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (qs1);
_mesh->Compile ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLightTrim (GLvector* chain, int count, float height, int seed, GLrgba color)
{
GLvertex p;
GLvector to;
GLvector out;
int i;
int index;
int prev, next;
float u, v1, v2;
float row;
quad_strip qs;
_color = color;
_center = glVector (0.0f, 0.0f, 0.0f);
qs.index_list.reserve(count * 2 + 2);
for (i = 0; i < count; i++)
_center += chain[i];
_center /= (float)count;
row = (float)(seed % TRIM_ROWS);
v1 = row * TRIM_SIZE;
v2 = (row + 1.0f) * TRIM_SIZE;
index = 0;
u = 0.0f;
for (i = 0; i < count + 1; i++) {
if (i)
u += glVectorLength (chain[i % count] - p.position) * 0.1f;
//Add the bottom point
prev = i - 1;
if (prev < 0)
prev = count + prev;
next = (i + 1) % count;
to = glVectorNormalize (chain[next] - chain[prev]);
out = glVectorCrossProduct (glVector (0.0f, 1.0f, 0.0f), to) * LOGO_OFFSET;
p.position = chain[i % count] + out; p.uv = glVector (u, v2);
_mesh->VertexAdd (p);
qs.index_list.push_back(index++);
//Top point
p.position.y += height;p.uv = glVector (u, v1);
_mesh->VertexAdd (p);
qs.index_list.push_back(index++);
}
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_TRIM);
_mesh->Compile ();
}
/*-----------------------------------------------------------------------------
Deco.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This handles building and rendering decoration objects - infrastructure &
such around the city.
-----------------------------------------------------------------------------*/
#define LOGO_OFFSET 0.2f //How far a logo sticks out from the given surface
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "deco.h"
#include "light.h"
#include "mesh.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CDeco::~CDeco ()
{
delete _mesh;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CDeco::CDeco ()
{
_mesh = new CMesh ();
_use_alpha = false;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::Render ()
{
glColor3fv (&_color.red);
_mesh->Render ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::RenderFlat (bool colored)
{
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool CDeco::Alpha ()
{
return _use_alpha;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int CDeco::PolyCount ()
{
return _mesh->PolyCount ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned CDeco::Texture ()
{
return _texture;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateRadioTower (GLvector pos, float height)
{
CLight* l;
float offset;
GLvertex v;
fan f;
for(int i=0; i<6; i++)
f.index_list.push_back(i);
offset = height / 15.0f;
_center = pos;
_use_alpha = true;
//Radio tower
v.position = glVector (_center.x, _center.y + height, _center.z); v.uv = glVector (0,1);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z - offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x + offset, _center.y, _center.z - offset); v.uv = glVector (0,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x + offset, _center.y, _center.z + offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z + offset); v.uv = glVector (0,0);
_mesh->VertexAdd (v);
v.position = glVector (_center.x - offset, _center.y, _center.z - offset); v.uv = glVector (1,0);
_mesh->VertexAdd (v);
_mesh->FanAdd (f);
l = new CLight (glVector (_center.x, _center.y + height + 1.0f, _center.z), glRgba (255,192,160), 1);
l->Blink ();
_texture = TextureId (TEXTURE_LATTICE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLogo (GLvector2 start, GLvector2 end, float bottom, int seed, GLrgba color)
{
GLvertex p;
quad_strip qs;
float u1, u2, v1, v2;
float top;
float height, length;
GLvector2 center2d;
GLvector to;
GLvector out;
int logo_index;
qs.index_list.push_back(0);
qs.index_list.push_back(1);
qs.index_list.push_back(3);
qs.index_list.push_back(2);
_use_alpha = true;
_color = color;
logo_index = seed % LOGO_ROWS;
to = glVector (start.x, 0.0f, start.y) - glVector (end.x, 0.0f, end.y);
to = glVectorNormalize (to);
out = glVectorCrossProduct (glVector (0.0f, 1.0f, 0.0f), to) * LOGO_OFFSET;
center2d = (start + end) / 2;
_center = glVector (center2d.x, bottom, center2d.y);
length = glVectorLength (start - end);
height = (length / 8.0f) * 1.5f;
top = bottom + height;
u1 = 0.0f;
u2 = 0.5f;//We actually only use the left half of the texture
v1 = (float)logo_index / LOGO_ROWS;
v2 = v1 + (1.0f / LOGO_ROWS);
p.position = glVector (start.x, bottom, start.y) + out; p.uv = glVector (u1,v1);
_mesh->VertexAdd (p);
p.position = glVector (end.x, bottom, end.y) + out; p.uv = glVector (u2, v1);
_mesh->VertexAdd (p);
p.position = glVector (end.x, top, end.y) + out; p.uv = glVector (u2, v2);
_mesh->VertexAdd (p);
p.position = glVector (start.x, top, start.y) + out; p.uv = glVector (u1, v2);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_LOGOS);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLightStrip (float x, float z, float width, float depth, float height, GLrgba color)
{
GLvertex p;
quad_strip qs1;
float u, v;
qs1.index_list.push_back(0);
qs1.index_list.push_back(1);
qs1.index_list.push_back(3);
qs1.index_list.push_back(2);
_color = color;
_use_alpha = true;
_center = glVector (x + width / 2, height, z + depth / 2);
if (width < depth) {
u = 1.0f;
v = (float)((int)(depth / width));
} else {
v = 1.0f;
u = (float)((int)(width / depth));
}
_texture = TextureId (TEXTURE_LIGHT);
p.position = glVector (x, height, z); p.uv = glVector (0.0f, 0.0f);
_mesh->VertexAdd (p);
p.position = glVector (x, height, z + depth); p.uv = glVector (0.0f, v);
_mesh->VertexAdd (p);
p.position = glVector (x + width, height, z + depth); p.uv = glVector (u, v);
_mesh->VertexAdd (p);
p.position = glVector (x + width, height, z); p.uv = glVector (u, 0.0f);
_mesh->VertexAdd (p);
_mesh->QuadStripAdd (qs1);
_mesh->Compile ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CDeco::CreateLightTrim (GLvector* chain, int count, float height, int seed, GLrgba color)
{
GLvertex p;
GLvector to;
GLvector out;
int i;
int index;
int prev, next;
float u, v1, v2;
float row;
quad_strip qs;
_color = color;
_center = glVector (0.0f, 0.0f, 0.0f);
qs.index_list.reserve(count * 2 + 2);
for (i = 0; i < count; i++)
_center += chain[i];
_center /= (float)count;
row = (float)(seed % TRIM_ROWS);
v1 = row * TRIM_SIZE;
v2 = (row + 1.0f) * TRIM_SIZE;
index = 0;
u = 0.0f;
for (i = 0; i < count + 1; i++) {
if (i)
u += glVectorLength (chain[i % count] - p.position) * 0.1f;
//Add the bottom point
prev = i - 1;
if (prev < 0)
prev = count + prev;
next = (i + 1) % count;
to = glVectorNormalize (chain[next] - chain[prev]);
out = glVectorCrossProduct (glVector (0.0f, 1.0f, 0.0f), to) * LOGO_OFFSET;
p.position = chain[i % count] + out; p.uv = glVector (u, v2);
_mesh->VertexAdd (p);
qs.index_list.push_back(index++);
//Top point
p.position.y += height;p.uv = glVector (u, v1);
_mesh->VertexAdd (p);
qs.index_list.push_back(index++);
}
_mesh->QuadStripAdd (qs);
_texture = TextureId (TEXTURE_TRIM);
_mesh->Compile ();
}

56
deco.h
View File

@ -1,28 +1,28 @@
#ifndef ENTITY
#include "entity.h"
#endif
class CDeco : CEntity
{
GLrgba _color;
class CMesh* _mesh;
int _type;
unsigned _texture;
bool _use_alpha;
public:
CDeco ();
~CDeco ();
void CreateLogo (GLvector2 start, GLvector2 end, float base, int seed, GLrgba color);
void CreateLightStrip (float x, float z, float width, float depth, float height, GLrgba color);
void CreateLightTrim (GLvector* chain, int count, float height, int seed, GLrgba color);
void CreateRadioTower (GLvector pos, float height);
void Render (void);
void RenderFlat (bool colored);
bool Alpha ();
int PolyCount ();
unsigned Texture ();
};
#ifndef ENTITY
#include "entity.h"
#endif
class CDeco : CEntity
{
GLrgba _color;
class CMesh* _mesh;
int _type;
unsigned _texture;
bool _use_alpha;
public:
CDeco ();
~CDeco ();
void CreateLogo (GLvector2 start, GLvector2 end, float base, int seed, GLrgba color);
void CreateLightStrip (float x, float z, float width, float depth, float height, GLrgba color);
void CreateLightTrim (GLvector* chain, int count, float height, int seed, GLrgba color);
void CreateRadioTower (GLvector pos, float height);
void Render (void);
void RenderFlat (bool colored);
bool Alpha ();
int PolyCount ();
unsigned Texture ();
};

View File

@ -1,399 +1,397 @@
/*-----------------------------------------------------------------------------
Entity.cpp
Copyright (c) 2005 Shamus Young
All Rights Reserved
-------------------------------------------------------------------------------
An entity is any renderable stationary object in the world. This is an
abstract class. This module gathers up the Entities, sorts them by
texture use and location, and then stores them in OpenGL render lists
for faster rendering.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include "camera.h"
#include "entity.h"
#include "macro.h"
#include "math.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
#include "win.h"
struct entity
{
CEntity* object;
};
struct cell
{
unsigned list_textured;
unsigned list_flat;
unsigned list_flat_wireframe;
unsigned list_alpha;
GLvector pos;
};
static cell cell_list[GRID_SIZE][GRID_SIZE];
static int entity_count;
static entity* entity_list;
static bool sorted;
static bool compiled;
static int polycount;
static int compile_x;
static int compile_y;
static int compile_count;
static int compile_end;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static int do_compare (const void *arg1, const void *arg2 )
{
struct entity* e1 = (struct entity*)arg1;
struct entity* e2 = (struct entity*)arg2;
if (e1->object->Alpha () && !e2->object->Alpha ())
return 1;
if (!e1->object->Alpha () && e2->object->Alpha ())
return -1;
if (e1->object->Texture () > e2->object->Texture ())
return 1;
else if (e1->object->Texture () < e2->object->Texture ())
return -1;
return 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void add (CEntity* b)
{
entity_list = (entity*)realloc (entity_list, sizeof (entity) * (entity_count + 1));
entity_list[entity_count].object = b;
entity_count++;
polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_compile ()
{
int i;
int x, y;
if (compiled)
return;
x = compile_x;
y = compile_y;
//Changing textures is pretty expensive, and thus sorting the entites so that
//they are grouped by texture used can really improve framerate.
//qsort (entity_list, entity_count, sizeof (struct entity), do_compare);
//sorted = true;
//Now group entites on the grid
//make a list for the textured objects in this region
if (!cell_list[x][y].list_textured)
cell_list[x][y].list_textured = glGenLists(1);
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, entity_list[i].object->Texture ());
entity_list[i].object->Render ();
}
}
glEndList();
//Make a list of flat-color stuff (A/C units, ledges, roofs, etc.)
if (!cell_list[x][y].list_flat)
cell_list[x][y].list_flat = glGenLists(1);
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
entity_list[i].object->RenderFlat (false);
}
}
glEndList();
//Now a list of flat-colored stuff that will be wireframe friendly
if (!cell_list[x][y].list_flat_wireframe)
cell_list[x][y].list_flat_wireframe = glGenLists(1);
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
entity_list[i].object->RenderFlat (true);
}
}
glEndList();
//Now a list of stuff to be alpha-blended, and thus rendered last
if (!cell_list[x][y].list_alpha)
cell_list[x][y].list_alpha = glGenLists(1);
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && entity_list[i].object->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, entity_list[i].object->Texture ());
entity_list[i].object->Render ();
}
}
glDepthMask (true);
glEndList();
//now walk the grid
compile_x++;
if (compile_x == GRID_SIZE) {
compile_x = 0;
compile_y++;
if (compile_y == GRID_SIZE)
compiled = true;
compile_end = GetTickCount ();
}
compile_count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool EntityReady ()
{
return compiled;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float EntityProgress ()
{
return (float)compile_count / (GRID_SIZE * GRID_SIZE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityUpdate ()
{
unsigned stop_time;
if (!TextureReady ()) {
sorted = false;
return;
}
if (!sorted) {
qsort (entity_list, entity_count, sizeof (struct entity), do_compare);
sorted = true;
}
//We want to do several cells at once. Enough to get things done, but
//not so many that the program is unresponsive.
if (LOADING_SCREEN) { //If we're using a loading screen, we want to build as fast as possible
stop_time = GetTickCount () + 100;
while (!compiled && GetTickCount () < stop_time)
do_compile ();
} else //Take it slow
do_compile ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityRender ()
{
int polymode[2];
bool wireframe;
int x, y;
int elapsed;
//Draw all textured objects
glGetIntegerv (GL_POLYGON_MODE, &polymode[0]);
wireframe = polymode[0] != GL_FILL;
if (RenderFlat ())
glDisable (GL_TEXTURE_2D);
//If we're not using a loading screen, make the wireframe fade out via fog
if (!LOADING_SCREEN && wireframe) {
elapsed = 6000 - WorldSceneElapsed ();
if (elapsed >= 0 && elapsed <= 6000)
RenderFogFX ((float)elapsed / 6000.0f);
else
return;
}
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y))
glCallList (cell_list[x][y].list_textured);
}
}
//draw all flat colored objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x, y)) {
if (wireframe)
glCallList (cell_list[x][y].list_flat_wireframe);
else
glCallList (cell_list[x][y].list_flat);
}
}
}
//draw all alpha-blended objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
glEnable (GL_BLEND);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y)) {
glCallList (cell_list[x][y].list_alpha);
}
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityClear ()
{
for (int i = 0; i < entity_count; i++) {
delete entity_list[i].object;
}
if (entity_list)
free (entity_list);
entity_list = NULL;
entity_count = 0;
compile_x = 0;
compile_y = 0;
compile_count = 0;
compiled = false;
sorted = false;
int x, y;
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEndList();
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityCount ()
{
return entity_count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityInit (void)
{
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityPolyCount (void)
{
if (!sorted)
return 0;
if (polycount)
return polycount;
for (int i = 0; i < entity_count; i++)
polycount += entity_list[i].object->PolyCount ();
return polycount;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CEntity::CEntity (void)
{
add (this);
}
void CEntity::Render (void)
{
}
void CEntity::RenderFlat (bool wireframe)
{
}
void CEntity::Update (void)
{
}
/*-----------------------------------------------------------------------------
Entity.cpp
Copyright (c) 2005 Shamus Young
All Rights Reserved
-------------------------------------------------------------------------------
An entity is any renderable stationary object in the world. This is an
abstract class. This module gathers up the Entities, sorts them by
texture use and location, and then stores them in OpenGL render lists
for faster rendering.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include "camera.h"
#include "entity.h"
#include "macro.h"
#include "math.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
#include "win.h"
struct entity
{
CEntity* object;
};
struct cell
{
unsigned list_textured;
unsigned list_flat;
unsigned list_flat_wireframe;
unsigned list_alpha;
GLvector pos;
};
static cell cell_list[GRID_SIZE][GRID_SIZE];
static int entity_count;
static entity* entity_list;
static bool sorted;
static bool compiled;
static int polycount;
static int compile_x;
static int compile_y;
static int compile_count;
static int compile_end;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static int do_compare (const void *arg1, const void *arg2 )
{
struct entity* e1 = (struct entity*)arg1;
struct entity* e2 = (struct entity*)arg2;
if (e1->object->Alpha () && !e2->object->Alpha ())
return 1;
if (!e1->object->Alpha () && e2->object->Alpha ())
return -1;
if (e1->object->Texture () > e2->object->Texture ())
return 1;
else if (e1->object->Texture () < e2->object->Texture ())
return -1;
return 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void add (CEntity* b)
{
entity_list = (entity*)realloc (entity_list, sizeof (entity) * (entity_count + 1));
entity_list[entity_count].object = b;
entity_count++;
polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void do_compile ()
{
int i;
int x, y;
if (compiled)
return;
x = compile_x;
y = compile_y;
//Changing textures is pretty expensive, and thus sorting the entites so that
//they are grouped by texture used can really improve framerate.
//qsort (entity_list, entity_count, sizeof (struct entity), do_compare);
//sorted = true;
//Now group entites on the grid
//make a list for the textured objects in this region
if (!cell_list[x][y].list_textured)
cell_list[x][y].list_textured = glGenLists(1);
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, entity_list[i].object->Texture ());
entity_list[i].object->Render ();
}
}
glEndList();
//Make a list of flat-color stuff (A/C units, ledges, roofs, etc.)
if (!cell_list[x][y].list_flat)
cell_list[x][y].list_flat = glGenLists(1);
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
entity_list[i].object->RenderFlat (false);
}
}
glEndList();
//Now a list of flat-colored stuff that will be wireframe friendly
if (!cell_list[x][y].list_flat_wireframe)
cell_list[x][y].list_flat_wireframe = glGenLists(1);
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEnable (GL_CULL_FACE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && !entity_list[i].object->Alpha ()) {
entity_list[i].object->RenderFlat (true);
}
}
glEndList();
//Now a list of stuff to be alpha-blended, and thus rendered last
if (!cell_list[x][y].list_alpha)
cell_list[x][y].list_alpha = glGenLists(1);
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
cell_list[x][y].pos = glVector (GRID_TO_WORLD(x), 0.0f, (float)y * GRID_RESOLUTION);
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
for (i = 0; i < entity_count; i++) {
GLvector pos = entity_list[i].object->Center ();
if (WORLD_TO_GRID(pos.x) == x && WORLD_TO_GRID(pos.z) == y && entity_list[i].object->Alpha ()) {
glBindTexture(GL_TEXTURE_2D, entity_list[i].object->Texture ());
entity_list[i].object->Render ();
}
}
glDepthMask (true);
glEndList();
//now walk the grid
compile_x++;
if (compile_x == GRID_SIZE) {
compile_x = 0;
compile_y++;
if (compile_y == GRID_SIZE)
compiled = true;
compile_end = GetTickCount ();
}
compile_count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool EntityReady ()
{
return compiled;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float EntityProgress ()
{
return (float)compile_count / (GRID_SIZE * GRID_SIZE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityUpdate ()
{
unsigned stop_time;
if (!TextureReady ()) {
sorted = false;
return;
}
if (!sorted) {
qsort (entity_list, entity_count, sizeof (struct entity), do_compare);
sorted = true;
}
//We want to do several cells at once. Enough to get things done, but
//not so many that the program is unresponsive.
if (LOADING_SCREEN) { //If we're using a loading screen, we want to build as fast as possible
stop_time = GetTickCount () + 100;
while (!compiled && GetTickCount () < stop_time)
do_compile ();
} else //Take it slow
do_compile ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityRender ()
{
int polymode[2];
bool wireframe;
int x, y;
int elapsed;
//Draw all textured objects
glGetIntegerv (GL_POLYGON_MODE, &polymode[0]);
wireframe = polymode[0] != GL_FILL;
if (RenderFlat ())
glDisable (GL_TEXTURE_2D);
//If we're not using a loading screen, make the wireframe fade out via fog
if (!LOADING_SCREEN && wireframe) {
elapsed = 6000 - WorldSceneElapsed ();
if (elapsed >= 0 && elapsed <= 6000)
RenderFogFX ((float)elapsed / 6000.0f);
else
return;
}
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y))
glCallList (cell_list[x][y].list_textured);
}
}
//draw all flat colored objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x, y)) {
if (wireframe)
glCallList (cell_list[x][y].list_flat_wireframe);
else
glCallList (cell_list[x][y].list_flat);
}
}
}
//draw all alpha-blended objects
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f (0, 0, 0);
glEnable (GL_BLEND);
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
if (Visible (x,y)) {
glCallList (cell_list[x][y].list_alpha);
}
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityClear ()
{
for (int i = 0; i < entity_count; i++) {
delete entity_list[i].object;
}
if (entity_list)
free (entity_list);
entity_list = NULL;
entity_count = 0;
compile_x = 0;
compile_y = 0;
compile_count = 0;
compiled = false;
sorted = false;
int x, y;
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
glNewList (cell_list[x][y].list_textured, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_alpha, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat_wireframe, GL_COMPILE);
glEndList();
glNewList (cell_list[x][y].list_flat, GL_COMPILE);
glEndList();
}
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityCount ()
{
return entity_count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void EntityInit (void)
{
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int EntityPolyCount (void)
{
if (!sorted)
return 0;
if (polycount)
return polycount;
for (int i = 0; i < entity_count; i++)
polycount += entity_list[i].object->PolyCount ();
return polycount;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CEntity::CEntity (void)
{
add (this);
}
void CEntity::Render (void)
{
}
void CEntity::RenderFlat (bool wireframe)
{
}
void CEntity::Update (void)
{
}

View File

@ -1,37 +1,37 @@
#ifndef TYPES
#include "glTypes.h"
#endif
#ifndef ENTITY
#define ENTITY
class CEntity
{
private:
protected:
GLvector _center;
public:
CEntity (void);
virtual ~CEntity () {};
virtual void Render (void);
virtual void RenderFlat (bool wirefame);
virtual unsigned Texture () { return 0; }
virtual void Update (void);
virtual bool Alpha () { return false; }
virtual int PolyCount () { return 0; }
GLvector Center () { return _center; }
};
void EntityClear ();
int EntityCount (void);
float EntityProgress ();
bool EntityReady ();
void EntityRender (void);
void EntityUpdate (void);
int EntityPolyCount (void);
#endif
#ifndef TYPES
#include "glTypes.h"
#endif
#ifndef ENTITY
#define ENTITY
class CEntity
{
private:
protected:
GLvector _center;
public:
CEntity (void);
virtual ~CEntity () {};
virtual void Render (void);
virtual void RenderFlat (bool wirefame);
virtual unsigned Texture () { return 0; }
virtual void Update (void);
virtual bool Alpha () { return false; }
virtual int PolyCount () { return 0; }
GLvector Center () { return _center; }
};
void EntityClear ();
int EntityCount (void);
float EntityProgress ();
bool EntityReady ();
void EntityRender (void);
void EntityUpdate (void);
int EntityPolyCount (void);
#endif

417
glTypes.h
View File

@ -1,209 +1,208 @@
#ifndef glTYPES
#define glTYPES
#define GL_CLAMP_TO_EDGE 0x812F
#define OPERATORS(type) \
type operator+ (const type& c); \
type operator+ (const float& c);\
void operator+= (const type& c);\
void operator+= (const float& c);\
type operator- (const type& c);\
type operator- (const float& c);\
void operator-= (const type& c);\
void operator-= (const float& c);\
type operator* (const type& c);\
type operator* (const float& c);\
void operator*= (const type& c);\
void operator*= (const float& c);\
type operator/ (const type& c);\
type operator/ (const float& c);\
void operator/= (const type& c);\
void operator/= (const float& c);\
bool operator== (const type& c);
#define JOINT_MAX_CHILDREN 8
struct GLquat
{
float x;
float y;
float z;
float w;
};
struct GLvector
{
float x;
float y;
float z;
OPERATORS(GLvector);
};
typedef GLvector GLvector3;
struct GLvector2
{
float x;
float y;
OPERATORS(GLvector2);
};
struct GLrgba
{
float red;
float green;
float blue;
float alpha;
OPERATORS(GLrgba);
};
struct GLmatrix
{
float elements[4][4];
};
struct GLbbox
{
GLvector3 min;
GLvector3 max;
};
struct GLvertex
{
GLvector3 position;
GLvector2 uv;
GLrgba color;
int bone;
};
struct GLrect
{
float left;
float top;
float right;
float bottom;
};
struct GLtriangle
{
int v1;
int v2;
int v3;
int normal1;
int normal2;
int normal3;
};
/*
class GLmodel
{
public:
unsigned vertex_count;
unsigned triangle_count;
unsigned normal_count;
GLvertex* vertex;
GLvector* normal;
GLtriangle* triangle;
void TriangleRender (unsigned n);
GLtriangle* TriangleAdd (unsigned v1, int unsigned, int unsigned);
GLtriangle* TriangleAdd (GLtriangle c);
void NormalAdd (GLvector n);
void VertexAdd (GLvertex v);
GLmodel ();
~GLmodel ();
void Render ();
GLbbox BBox ();
private:
GLbbox m_bbox;
};
struct GLkeyframe
{
float time;
GLvector offset;
GLvector rotation;
};
struct GLsegment
{
int index;
GLvector rotation;
GLvector offset;
GLkeyframe keyframes[255];
int frame_count;
};
class GLanimate
{
public:
GLanimate ();
void KeyframeAdd (int joint, float time, GLquat q);
void TimeSet (float time);
void PositionSet (float pos);
GLvector Rotation (int);
GLvector Offset (int);
private:
GLsegment* m_segments;
int m_segment_count;
float m_length;
};
*/
GLbbox glBboxClear (void);
GLbbox glBboxContainPoint (GLbbox box, GLvector point);
bool glBboxTestPoint (GLbbox box, GLvector point);
GLrgba glRgba (char* string);
GLrgba glRgba (float red, float green, float blue);
GLrgba glRgba (float luminance);
GLrgba glRgba (float red, float green, float blue, float alpha);
GLrgba glRgba (long c);
GLrgba glRgba (int red, int green, int blue);
GLrgba glRgbaAdd (GLrgba c1, GLrgba c2);
GLrgba glRgbaSubtract (GLrgba c1, GLrgba c2);
GLrgba glRgbaInterpolate (GLrgba c1, GLrgba c2, float delta);
GLrgba glRgbaScale (GLrgba c, float scale);
GLrgba glRgbaMultiply (GLrgba c1, GLrgba c2);
GLrgba glRgbaUnique (int i);
GLrgba glRgbaFromHsl (float h, float s, float l);
GLmatrix glMatrixIdentity (void);
void glMatrixElementsSet (GLmatrix* m, float* in);
GLmatrix glMatrixMultiply (GLmatrix a, GLmatrix b);
GLvector glMatrixTransformPoint (GLmatrix m, GLvector in);
GLmatrix glMatrixTranslate (GLmatrix m, GLvector in);
GLmatrix glMatrixRotate (GLmatrix m, float theta, float x, float y, float z);
GLvector glMatrixToEuler (GLmatrix mat, int order);
GLquat glQuat (float x, float y, float z, float w);
GLvector glQuatToEuler (GLquat q, int order);
GLvector glVector (float x, float y, float z);
GLvector glVectorCrossProduct (GLvector v1, GLvector v2);
float glVectorDotProduct (GLvector v1, GLvector v2);
void glVectorGl (GLvector v);
GLvector glVectorInterpolate (GLvector v1, GLvector v2, float scalar);
float glVectorLength (GLvector v);
GLvector glVectorNormalize (GLvector v);
GLvector glVectorReflect (GLvector3 ray, GLvector3 normal);
GLvector2 glVector (float x, float y);
GLvector2 glVectorAdd (GLvector2 val1, GLvector2 val2);
GLvector2 glVectorSubtract (GLvector2 val1, GLvector2 val2);
GLvector2 glVectorNormalize (GLvector2 v);
GLvector2 glVectorInterpolate (GLvector2 v1, GLvector2 v2, float scalar);
GLvector2 glVectorSinCos (float angle);
float glVectorLength (GLvector2 v);
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef glTYPES
#define glTYPES
#define GL_CLAMP_TO_EDGE 0x812F
#define OPERATORS(type) \
type operator+ (const type& c); \
type operator+ (const float& c);\
void operator+= (const type& c);\
void operator+= (const float& c);\
type operator- (const type& c);\
type operator- (const float& c);\
void operator-= (const type& c);\
void operator-= (const float& c);\
type operator* (const type& c);\
type operator* (const float& c);\
void operator*= (const type& c);\
void operator*= (const float& c);\
type operator/ (const type& c);\
type operator/ (const float& c);\
void operator/= (const type& c);\
void operator/= (const float& c);\
bool operator== (const type& c);
#define JOINT_MAX_CHILDREN 8
struct GLquat
{
float x;
float y;
float z;
float w;
};
struct GLvector
{
float x;
float y;
float z;
OPERATORS(GLvector);
};
typedef GLvector GLvector3;
struct GLvector2
{
float x;
float y;
OPERATORS(GLvector2);
};
struct GLrgba
{
float red;
float green;
float blue;
float alpha;
OPERATORS(GLrgba);
};
struct GLmatrix
{
float elements[4][4];
};
struct GLbbox
{
GLvector3 min;
GLvector3 max;
};
struct GLvertex
{
GLvector3 position;
GLvector2 uv;
GLrgba color;
int bone;
};
struct GLrect
{
float left;
float top;
float right;
float bottom;
};
struct GLtriangle
{
int v1;
int v2;
int v3;
int normal1;
int normal2;
int normal3;
};
/*
class GLmodel
{
public:
unsigned vertex_count;
unsigned triangle_count;
unsigned normal_count;
GLvertex* vertex;
GLvector* normal;
GLtriangle* triangle;
void TriangleRender (unsigned n);
GLtriangle* TriangleAdd (unsigned v1, int unsigned, int unsigned);
GLtriangle* TriangleAdd (GLtriangle c);
void NormalAdd (GLvector n);
void VertexAdd (GLvertex v);
GLmodel ();
~GLmodel ();
void Render ();
GLbbox BBox ();
private:
GLbbox m_bbox;
};
struct GLkeyframe
{
float time;
GLvector offset;
GLvector rotation;
};
struct GLsegment
{
int index;
GLvector rotation;
GLvector offset;
GLkeyframe keyframes[255];
int frame_count;
};
class GLanimate
{
public:
GLanimate ();
void KeyframeAdd (int joint, float time, GLquat q);
void TimeSet (float time);
void PositionSet (float pos);
GLvector Rotation (int);
GLvector Offset (int);
private:
GLsegment* m_segments;
int m_segment_count;
float m_length;
};
*/
GLbbox glBboxClear (void);
GLbbox glBboxContainPoint (GLbbox box, GLvector point);
bool glBboxTestPoint (GLbbox box, GLvector point);
GLrgba glRgba (char* string);
GLrgba glRgba (float red, float green, float blue);
GLrgba glRgba (float luminance);
GLrgba glRgba (float red, float green, float blue, float alpha);
GLrgba glRgba (long c);
GLrgba glRgba (int red, int green, int blue);
GLrgba glRgbaAdd (GLrgba c1, GLrgba c2);
GLrgba glRgbaSubtract (GLrgba c1, GLrgba c2);
GLrgba glRgbaInterpolate (GLrgba c1, GLrgba c2, float delta);
GLrgba glRgbaScale (GLrgba c, float scale);
GLrgba glRgbaMultiply (GLrgba c1, GLrgba c2);
GLrgba glRgbaUnique (int i);
GLrgba glRgbaFromHsl (float h, float s, float l);
GLmatrix glMatrixIdentity (void);
void glMatrixElementsSet (GLmatrix* m, float* in);
GLmatrix glMatrixMultiply (GLmatrix a, GLmatrix b);
GLvector glMatrixTransformPoint (GLmatrix m, GLvector in);
GLmatrix glMatrixTranslate (GLmatrix m, GLvector in);
GLmatrix glMatrixRotate (GLmatrix m, float theta, float x, float y, float z);
GLvector glMatrixToEuler (GLmatrix mat, int order);
GLquat glQuat (float x, float y, float z, float w);
GLvector glQuatToEuler (GLquat q, int order);
GLvector glVector (float x, float y, float z);
GLvector glVectorCrossProduct (GLvector v1, GLvector v2);
float glVectorDotProduct (GLvector v1, GLvector v2);
void glVectorGl (GLvector v);
GLvector glVectorInterpolate (GLvector v1, GLvector v2, float scalar);
float glVectorLength (GLvector v);
GLvector glVectorNormalize (GLvector v);
GLvector glVectorReflect (GLvector3 ray, GLvector3 normal);
GLvector2 glVector (float x, float y);
GLvector2 glVectorAdd (GLvector2 val1, GLvector2 val2);
GLvector2 glVectorSubtract (GLvector2 val1, GLvector2 val2);
GLvector2 glVectorNormalize (GLvector2 v);
GLvector2 glVectorInterpolate (GLvector2 v1, GLvector2 v2, float scalar);
GLvector2 glVectorSinCos (float angle);
float glVectorLength (GLvector2 v);
#endif
#ifndef NULL
#define NULL 0
#endif

View File

@ -1,70 +1,68 @@
/*-----------------------------------------------------------------------------
glBbox.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This module has a few functions useful for manipulating the bounding-box
structs.
-----------------------------------------------------------------------------*/
#define MAX_VALUE 999999999999999.9f
#include <math.h>
#include "macro.h"
#include "glTypes.h"
/*-----------------------------------------------------------------------------
Does the given point fall within the given Bbox?
-----------------------------------------------------------------------------*/
bool glBboxTestPoint (GLbbox box, GLvector point)
{
if (point.x > box.max.x || point.x < box.min.x)
return false;
if (point.y > box.max.y || point.y < box.min.y)
return false;
if (point.z > box.max.z || point.z < box.min.z)
return false;
return true;
}
/*-----------------------------------------------------------------------------
Expand Bbox (if needed) to contain given point
-----------------------------------------------------------------------------*/
GLbbox glBboxContainPoint (GLbbox box, GLvector point)
{
box.min.x = MIN (box.min.x, point.x);
box.min.y = MIN (box.min.y, point.y);
box.min.z = MIN (box.min.z, point.z);
box.max.x = MAX (box.max.x, point.x);
box.max.y = MAX (box.max.y, point.y);
box.max.z = MAX (box.max.z, point.z);
return box;
}
/*-----------------------------------------------------------------------------
This will invalidate the bbox.
-----------------------------------------------------------------------------*/
GLbbox glBboxClear (void)
{
GLbbox result;
result.max = glVector (-MAX_VALUE, -MAX_VALUE, -MAX_VALUE);
result.min = glVector ( MAX_VALUE, MAX_VALUE, MAX_VALUE);
return result;
}
/*-----------------------------------------------------------------------------
glBbox.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This module has a few functions useful for manipulating the bounding-box
structs.
-----------------------------------------------------------------------------*/
#define MAX_VALUE 999999999999999.9f
#include <math.h>
#include "macro.h"
#include "glTypes.h"
/*-----------------------------------------------------------------------------
Does the given point fall within the given Bbox?
-----------------------------------------------------------------------------*/
bool glBboxTestPoint (GLbbox box, GLvector point)
{
if (point.x > box.max.x || point.x < box.min.x)
return false;
if (point.y > box.max.y || point.y < box.min.y)
return false;
if (point.z > box.max.z || point.z < box.min.z)
return false;
return true;
}
/*-----------------------------------------------------------------------------
Expand Bbox (if needed) to contain given point
-----------------------------------------------------------------------------*/
GLbbox glBboxContainPoint (GLbbox box, GLvector point)
{
box.min.x = MIN (box.min.x, point.x);
box.min.y = MIN (box.min.y, point.y);
box.min.z = MIN (box.min.z, point.z);
box.max.x = MAX (box.max.x, point.x);
box.max.y = MAX (box.max.y, point.y);
box.max.z = MAX (box.max.z, point.z);
return box;
}
/*-----------------------------------------------------------------------------
This will invalidate the bbox.
-----------------------------------------------------------------------------*/
GLbbox glBboxClear (void)
{
GLbbox result;
result.max = glVector (-MAX_VALUE, -MAX_VALUE, -MAX_VALUE);
result.min = glVector ( MAX_VALUE, MAX_VALUE, MAX_VALUE);
return result;
}

View File

@ -1,318 +1,318 @@
/*-----------------------------------------------------------------------------
glMatrix.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions useful for manipulating the Matrix struct
-----------------------------------------------------------------------------*/
#define M(e,x,y) (e.elements[x][y])
/*** Order type constants, constructors, extractors ***/
/* There are 24 possible conventions, designated by: */
/* o EulAxI = axis used initially */
/* o EulPar = parity of axis permutation */
/* o EulRep = repetition of initial axis as last */
/* o EulFrm = frame from which axes are taken */
/* Axes I,J,K will be a permutation of X,Y,Z. */
/* Axis H will be either I or K, depending on EulRep. */
/* Frame S takes axes from initial static frame. */
/* If ord = (AxI=X, Par=Even, Rep=No, Frm=S), then */
/* {a,b,c,ord} means Rz(c)Ry(b)Rx(a), where Rz(c)v */
/* rotates v around Z by c radians. */
#define EulFrmS 0
#define EulFrmR 1
#define EulFrm(ord) ((unsigned)(ord)&1)
#define EulRepNo 0
#define EulRepYes 1
#define EulRep(ord) (((unsigned)(ord)>>1)&1)
#define EulParEven 0
#define EulParOdd 1
#define EulPar(ord) (((unsigned)(ord)>>2)&1)
#define EulSafe "\000\001\002\000"
#define EulNext "\001\002\000\001"
#define EulAxI(ord) ((int)(EulSafe[(((unsigned)(ord)>>3)&3)]))
#define EulAxJ(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)==EulParOdd)]))
#define EulAxK(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)!=EulParOdd)]))
#define EulAxH(ord) ((EulRep(ord)==EulRepNo)?EulAxK(ord):EulAxI(ord))
/* EulGetOrd unpacks all useful information about order simultaneously. */
#define EulGetOrd(ord,i,j,k,h,n,s,f) {unsigned o=ord;f=o&1;o>>=1;s=o&1;o>>=1;\
n=o&1;o>>=1;i=EulSafe[o&3];j=EulNext[i+n];k=EulNext[i+1-n];h=s?k:i;}
/* EulOrd creates an order value between 0 and 23 from 4-tuple choices. */
#define EulOrd(i,p,r,f) (((((((i)<<1)+(p))<<1)+(r))<<1)+(f))
/* Static axes */
#define EulOrdXYZs EulOrd(X,EulParEven,EulRepNo,EulFrmS)
#define EulOrdXYXs EulOrd(X,EulParEven,EulRepYes,EulFrmS)
#define EulOrdXZYs EulOrd(X,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdXZXs EulOrd(X,EulParOdd,EulRepYes,EulFrmS)
#define EulOrdYZXs EulOrd(Y,EulParEven,EulRepNo,EulFrmS)
#define EulOrdYZYs EulOrd(Y,EulParEven,EulRepYes,EulFrmS)
#define EulOrdYXZs EulOrd(Y,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdYXYs EulOrd(Y,EulParOdd,EulRepYes,EulFrmS)
#define EulOrdZXYs EulOrd(Z,EulParEven,EulRepNo,EulFrmS)
#define EulOrdZXZs EulOrd(Z,EulParEven,EulRepYes,EulFrmS)
#define EulOrdZYXs EulOrd(Z,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdZYZs EulOrd(Z,EulParOdd,EulRepYes,EulFrmS)
/* Rotating axes */
#define EulOrdZYXr EulOrd(X,EulParEven,EulRepNo,EulFrmR)
#define EulOrdXYXr EulOrd(X,EulParEven,EulRepYes,EulFrmR)
#define EulOrdYZXr EulOrd(X,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdXZXr EulOrd(X,EulParOdd,EulRepYes,EulFrmR)
#define EulOrdXZYr EulOrd(Y,EulParEven,EulRepNo,EulFrmR)
#define EulOrdYZYr EulOrd(Y,EulParEven,EulRepYes,EulFrmR)
#define EulOrdZXYr EulOrd(Y,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdYXYr EulOrd(Y,EulParOdd,EulRepYes,EulFrmR)
#define EulOrdYXZr EulOrd(Z,EulParEven,EulRepNo,EulFrmR)
#define EulOrdZXZr EulOrd(Z,EulParEven,EulRepYes,EulFrmR)
#define EulOrdXYZr EulOrd(Z,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdZYZr EulOrd(Z,EulParOdd,EulRepYes,EulFrmR)
#include <math.h>
#include <float.h>
#include "macro.h"
#include "glTypes.h"
static float identity[4][4] =
{
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f},
};
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void* glMatrixCreate (void)
{
GLmatrix* m;
int x;
int y;
m = new GLmatrix;
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
m -> elements[x][y] = identity[x][y];
}
}
return (void*)m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixIdentity (void)
{
GLmatrix m;
int x;
int y;
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
M(m, x, y) = identity[x][y];
}
}
return m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void glMatrixElementsSet (GLmatrix* m, float* in)
{
m -> elements[0][0] = in[0];
m -> elements[0][1] = in[1];
m -> elements[0][2] = in[2];
m -> elements[0][3] = in[3];
m -> elements[1][0] = in[4];
m -> elements[1][1] = in[5];
m -> elements[1][2] = in[6];
m -> elements[1][3] = in[7];
m -> elements[2][0] = in[8];
m -> elements[2][1] = in[9];
m -> elements[2][2] = in[10];
m -> elements[2][3] = in[11];
m -> elements[3][0] = in[12];
m -> elements[3][1] = in[13];
m -> elements[3][2] = in[14];
m -> elements[3][3] = in[15];
}
/*---------------------------------------------------------------------------
A matrix multiplication (dot product) of two 4x4 matrices.
---------------------------------------------------------------------------*/
GLmatrix glMatrixMultiply (GLmatrix a, GLmatrix b)
{
GLmatrix result;
M(result, 0,0) = M(a, 0,0) * M(b, 0, 0) + M(a, 1,0) * M(b, 0, 1) + M(a, 2,0) * M(b, 0, 2);
M(result, 1,0) = M(a, 0,0) * M(b, 1, 0) + M(a, 1,0) * M(b, 1, 1) + M(a, 2,0) * M(b, 1, 2);
M(result, 2,0) = M(a, 0,0) * M(b, 2, 0) + M(a, 1,0) * M(b, 2, 1) + M(a, 2,0) * M(b, 2, 2);
M(result, 3,0) = M(a, 0,0) * M(b, 3, 0) + M(a, 1,0) * M(b, 3, 1) + M(a, 2,0) * M(b, 3, 2) + M(a, 3,0);
M(result, 0,1) = M(a, 0,1) * M(b, 0, 0) + M(a, 1,1) * M(b, 0, 1) + M(a, 2,1) * M(b, 0, 2);
M(result, 1,1) = M(a, 0,1) * M(b, 1, 0) + M(a, 1,1) * M(b, 1, 1) + M(a, 2,1) * M(b, 1, 2);
M(result, 2,1) = M(a, 0,1) * M(b, 2, 0) + M(a, 1,1) * M(b, 2, 1) + M(a, 2,1) * M(b, 2, 2);
M(result, 3,1) = M(a, 0,1) * M(b, 3, 0) + M(a, 1,1) * M(b, 3, 1) + M(a, 2,1) * M(b, 3, 2) + M(a, 3,1);
M(result, 0,2) = M(a, 0,2) * M(b, 0, 0) + M(a, 1,2) * M(b, 0, 1) + M(a, 2,2) * M(b, 0, 2);
M(result, 1,2) = M(a, 0,2) * M(b, 1, 0) + M(a, 1,2) * M(b, 1, 1) + M(a, 2,2) * M(b, 1, 2);
M(result, 2,2) = M(a, 0,2) * M(b, 2, 0) + M(a, 1,2) * M(b, 2, 1) + M(a, 2,2) * M(b, 2, 2);
M(result, 3,2) = M(a, 0,2) * M(b, 3, 0) + M(a, 1,2) * M(b, 3, 1) + M(a, 2,2) * M(b, 3, 2) + M(a, 3,2);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector glMatrixTransformPoint (GLmatrix m, GLvector in)
{
GLvector out;
out.x = M(m,0,0) * in.x + M(m,1,0) * in.y + M(m,2,0) * in.z + M(m,3,0);
out.y = M(m,0,1) * in.x + M(m,1,1) * in.y + M(m,2,1) * in.z + M(m,3,1);
out.z = M(m,0,2) * in.x + M(m,1,2) * in.y + M(m,2,2) * in.z + M(m,3,2);
return out;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixTranslate (GLmatrix m, GLvector in)
{
GLvector old;
old.x = M(m,3,0);
old.y = M(m,3,1);
old.z = M(m,3,2);
M(m, 3, 0) = 0.0f;
M(m, 3, 1) = 0.0f;
M(m, 3, 2) = 0.0f;
in = glMatrixTransformPoint (m, in);
M(m, 3, 0) = old.x;
M(m, 3, 1) = old.y;
M(m, 3, 2) = old.z;
M(m,3,0) += in.x;
M(m,3,1) += in.y;
M(m,3,2) += in.z;
return m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixRotate (GLmatrix m, float theta, float x, float y, float z)
{
GLmatrix r;
float length;
float s, c, t;
GLvector in;
theta *= DEGREES_TO_RADIANS;
r = glMatrixIdentity ();
length = (float)sqrt (x * x + y * y + z * z);
if (length < 0.00001f)
return m;
x /= length;
y /= length;
z /= length;
s = (float)sin (theta);
c = (float)cos (theta);
t = 1.0f - c;
in.x = in.y = in.z = 1.0f;
M(r, 0,0) = t*x*x + c;
M(r, 1,0) = t*x*y - s*z;
M(r, 2,0) = t*x*z + s*y;
M(r, 3,0) = 0;
M(r, 0,1) = t*x*y + s*z;
M(r, 1,1) = t*y*y + c;
M(r, 2,1) = t*y*z - s*x;
M(r, 3,1) = 0;
M(r, 0,2) = t*x*z - s*y;
M(r, 1,2) = t*y*z + s*x;
M(r, 2,2) = t*z*z + c;
M(r, 3,2) = 0;
m = glMatrixMultiply (m, r);
return m;
}
/* Convert matrix to Euler angles (in radians). */
GLvector glMatrixToEuler (GLmatrix mat, int order)
{
GLvector ea;
int i,j,k,h,n,s,f;
EulGetOrd (order,i,j,k,h,n,s,f);
if (s==EulRepYes) {
float sy = (float)sqrt(mat.elements[i][j]*mat.elements[i][j] + mat.elements[i][k]*mat.elements[i][k]);
if (sy > 16 * FLT_EPSILON) {
ea.x = (float)atan2(mat.elements[i][j], mat.elements[i][k]);
ea.y = (float)atan2(sy, mat.elements[i][i]);
ea.z = (float)atan2(mat.elements[j][i], -mat.elements[k][i]);
} else {
ea.x = (float)atan2(-mat.elements[j][k], mat.elements[j][j]);
ea.y = (float)atan2(sy, mat.elements[i][i]);
ea.z = 0;
}
} else {
float cy = (float)sqrt(mat.elements[i][i]*mat.elements[i][i] + mat.elements[j][i]*mat.elements[j][i]);
if (cy > 16*FLT_EPSILON) {
ea.x = (float)atan2(mat.elements[k][j], mat.elements[k][k]);
ea.y = (float)atan2(-mat.elements[k][i], cy);
ea.z = (float)atan2(mat.elements[j][i], mat.elements[i][i]);
} else {
ea.x = (float)atan2(-mat.elements[j][k], mat.elements[j][j]);
ea.y = (float)atan2(-mat.elements[k][i], cy);
ea.z = 0;
}
}
if (n==EulParOdd) {
ea.x = -ea.x;
ea.y = - ea.y;
ea.z = -ea.z;
}
if (f==EulFrmR) {
float t = ea.x;
ea.x = ea.z;
ea.z = t;
}
//ea.w = order;
return (ea);
}
/*-----------------------------------------------------------------------------
glMatrix.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions useful for manipulating the Matrix struct
-----------------------------------------------------------------------------*/
#define M(e,x,y) (e.elements[x][y])
/*** Order type constants, constructors, extractors ***/
/* There are 24 possible conventions, designated by: */
/* o EulAxI = axis used initially */
/* o EulPar = parity of axis permutation */
/* o EulRep = repetition of initial axis as last */
/* o EulFrm = frame from which axes are taken */
/* Axes I,J,K will be a permutation of X,Y,Z. */
/* Axis H will be either I or K, depending on EulRep. */
/* Frame S takes axes from initial static frame. */
/* If ord = (AxI=X, Par=Even, Rep=No, Frm=S), then */
/* {a,b,c,ord} means Rz(c)Ry(b)Rx(a), where Rz(c)v */
/* rotates v around Z by c radians. */
#define EulFrmS 0
#define EulFrmR 1
#define EulFrm(ord) ((unsigned)(ord)&1)
#define EulRepNo 0
#define EulRepYes 1
#define EulRep(ord) (((unsigned)(ord)>>1)&1)
#define EulParEven 0
#define EulParOdd 1
#define EulPar(ord) (((unsigned)(ord)>>2)&1)
#define EulSafe "\000\001\002\000"
#define EulNext "\001\002\000\001"
#define EulAxI(ord) ((int)(EulSafe[(((unsigned)(ord)>>3)&3)]))
#define EulAxJ(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)==EulParOdd)]))
#define EulAxK(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)!=EulParOdd)]))
#define EulAxH(ord) ((EulRep(ord)==EulRepNo)?EulAxK(ord):EulAxI(ord))
/* EulGetOrd unpacks all useful information about order simultaneously. */
#define EulGetOrd(ord,i,j,k,h,n,s,f) {unsigned o=ord;f=o&1;o>>=1;s=o&1;o>>=1;\
n=o&1;o>>=1;i=EulSafe[o&3];j=EulNext[i+n];k=EulNext[i+1-n];h=s?k:i;}
/* EulOrd creates an order value between 0 and 23 from 4-tuple choices. */
#define EulOrd(i,p,r,f) (((((((i)<<1)+(p))<<1)+(r))<<1)+(f))
/* Static axes */
#define EulOrdXYZs EulOrd(X,EulParEven,EulRepNo,EulFrmS)
#define EulOrdXYXs EulOrd(X,EulParEven,EulRepYes,EulFrmS)
#define EulOrdXZYs EulOrd(X,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdXZXs EulOrd(X,EulParOdd,EulRepYes,EulFrmS)
#define EulOrdYZXs EulOrd(Y,EulParEven,EulRepNo,EulFrmS)
#define EulOrdYZYs EulOrd(Y,EulParEven,EulRepYes,EulFrmS)
#define EulOrdYXZs EulOrd(Y,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdYXYs EulOrd(Y,EulParOdd,EulRepYes,EulFrmS)
#define EulOrdZXYs EulOrd(Z,EulParEven,EulRepNo,EulFrmS)
#define EulOrdZXZs EulOrd(Z,EulParEven,EulRepYes,EulFrmS)
#define EulOrdZYXs EulOrd(Z,EulParOdd,EulRepNo,EulFrmS)
#define EulOrdZYZs EulOrd(Z,EulParOdd,EulRepYes,EulFrmS)
/* Rotating axes */
#define EulOrdZYXr EulOrd(X,EulParEven,EulRepNo,EulFrmR)
#define EulOrdXYXr EulOrd(X,EulParEven,EulRepYes,EulFrmR)
#define EulOrdYZXr EulOrd(X,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdXZXr EulOrd(X,EulParOdd,EulRepYes,EulFrmR)
#define EulOrdXZYr EulOrd(Y,EulParEven,EulRepNo,EulFrmR)
#define EulOrdYZYr EulOrd(Y,EulParEven,EulRepYes,EulFrmR)
#define EulOrdZXYr EulOrd(Y,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdYXYr EulOrd(Y,EulParOdd,EulRepYes,EulFrmR)
#define EulOrdYXZr EulOrd(Z,EulParEven,EulRepNo,EulFrmR)
#define EulOrdZXZr EulOrd(Z,EulParEven,EulRepYes,EulFrmR)
#define EulOrdXYZr EulOrd(Z,EulParOdd,EulRepNo,EulFrmR)
#define EulOrdZYZr EulOrd(Z,EulParOdd,EulRepYes,EulFrmR)
#include <math.h>
#include <float.h>
#include "macro.h"
#include "glTypes.h"
static float identity[4][4] =
{
{1.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 1.0f},
};
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void* glMatrixCreate (void)
{
GLmatrix* m;
int x;
int y;
m = new GLmatrix;
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
m -> elements[x][y] = identity[x][y];
}
}
return (void*)m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixIdentity (void)
{
GLmatrix m;
int x;
int y;
for (x = 0; x < 4; x++) {
for (y = 0; y < 4; y++) {
M(m, x, y) = identity[x][y];
}
}
return m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void glMatrixElementsSet (GLmatrix* m, float* in)
{
m -> elements[0][0] = in[0];
m -> elements[0][1] = in[1];
m -> elements[0][2] = in[2];
m -> elements[0][3] = in[3];
m -> elements[1][0] = in[4];
m -> elements[1][1] = in[5];
m -> elements[1][2] = in[6];
m -> elements[1][3] = in[7];
m -> elements[2][0] = in[8];
m -> elements[2][1] = in[9];
m -> elements[2][2] = in[10];
m -> elements[2][3] = in[11];
m -> elements[3][0] = in[12];
m -> elements[3][1] = in[13];
m -> elements[3][2] = in[14];
m -> elements[3][3] = in[15];
}
/*---------------------------------------------------------------------------
A matrix multiplication (dot product) of two 4x4 matrices.
---------------------------------------------------------------------------*/
GLmatrix glMatrixMultiply (GLmatrix a, GLmatrix b)
{
GLmatrix result;
M(result, 0,0) = M(a, 0,0) * M(b, 0, 0) + M(a, 1,0) * M(b, 0, 1) + M(a, 2,0) * M(b, 0, 2);
M(result, 1,0) = M(a, 0,0) * M(b, 1, 0) + M(a, 1,0) * M(b, 1, 1) + M(a, 2,0) * M(b, 1, 2);
M(result, 2,0) = M(a, 0,0) * M(b, 2, 0) + M(a, 1,0) * M(b, 2, 1) + M(a, 2,0) * M(b, 2, 2);
M(result, 3,0) = M(a, 0,0) * M(b, 3, 0) + M(a, 1,0) * M(b, 3, 1) + M(a, 2,0) * M(b, 3, 2) + M(a, 3,0);
M(result, 0,1) = M(a, 0,1) * M(b, 0, 0) + M(a, 1,1) * M(b, 0, 1) + M(a, 2,1) * M(b, 0, 2);
M(result, 1,1) = M(a, 0,1) * M(b, 1, 0) + M(a, 1,1) * M(b, 1, 1) + M(a, 2,1) * M(b, 1, 2);
M(result, 2,1) = M(a, 0,1) * M(b, 2, 0) + M(a, 1,1) * M(b, 2, 1) + M(a, 2,1) * M(b, 2, 2);
M(result, 3,1) = M(a, 0,1) * M(b, 3, 0) + M(a, 1,1) * M(b, 3, 1) + M(a, 2,1) * M(b, 3, 2) + M(a, 3,1);
M(result, 0,2) = M(a, 0,2) * M(b, 0, 0) + M(a, 1,2) * M(b, 0, 1) + M(a, 2,2) * M(b, 0, 2);
M(result, 1,2) = M(a, 0,2) * M(b, 1, 0) + M(a, 1,2) * M(b, 1, 1) + M(a, 2,2) * M(b, 1, 2);
M(result, 2,2) = M(a, 0,2) * M(b, 2, 0) + M(a, 1,2) * M(b, 2, 1) + M(a, 2,2) * M(b, 2, 2);
M(result, 3,2) = M(a, 0,2) * M(b, 3, 0) + M(a, 1,2) * M(b, 3, 1) + M(a, 2,2) * M(b, 3, 2) + M(a, 3,2);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector glMatrixTransformPoint (GLmatrix m, GLvector in)
{
GLvector out;
out.x = M(m,0,0) * in.x + M(m,1,0) * in.y + M(m,2,0) * in.z + M(m,3,0);
out.y = M(m,0,1) * in.x + M(m,1,1) * in.y + M(m,2,1) * in.z + M(m,3,1);
out.z = M(m,0,2) * in.x + M(m,1,2) * in.y + M(m,2,2) * in.z + M(m,3,2);
return out;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixTranslate (GLmatrix m, GLvector in)
{
GLvector old;
old.x = M(m,3,0);
old.y = M(m,3,1);
old.z = M(m,3,2);
M(m, 3, 0) = 0.0f;
M(m, 3, 1) = 0.0f;
M(m, 3, 2) = 0.0f;
in = glMatrixTransformPoint (m, in);
M(m, 3, 0) = old.x;
M(m, 3, 1) = old.y;
M(m, 3, 2) = old.z;
M(m,3,0) += in.x;
M(m,3,1) += in.y;
M(m,3,2) += in.z;
return m;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLmatrix glMatrixRotate (GLmatrix m, float theta, float x, float y, float z)
{
GLmatrix r;
float length;
float s, c, t;
GLvector in;
theta *= DEGREES_TO_RADIANS;
r = glMatrixIdentity ();
length = (float)sqrt (x * x + y * y + z * z);
if (length < 0.00001f)
return m;
x /= length;
y /= length;
z /= length;
s = (float)sin (theta);
c = (float)cos (theta);
t = 1.0f - c;
in.x = in.y = in.z = 1.0f;
M(r, 0,0) = t*x*x + c;
M(r, 1,0) = t*x*y - s*z;
M(r, 2,0) = t*x*z + s*y;
M(r, 3,0) = 0;
M(r, 0,1) = t*x*y + s*z;
M(r, 1,1) = t*y*y + c;
M(r, 2,1) = t*y*z - s*x;
M(r, 3,1) = 0;
M(r, 0,2) = t*x*z - s*y;
M(r, 1,2) = t*y*z + s*x;
M(r, 2,2) = t*z*z + c;
M(r, 3,2) = 0;
m = glMatrixMultiply (m, r);
return m;
}
/* Convert matrix to Euler angles (in radians). */
GLvector glMatrixToEuler (GLmatrix mat, int order)
{
GLvector ea;
int i,j,k,h,n,s,f;
EulGetOrd (order,i,j,k,h,n,s,f);
if (s==EulRepYes) {
float sy = (float)sqrt(mat.elements[i][j]*mat.elements[i][j] + mat.elements[i][k]*mat.elements[i][k]);
if (sy > 16 * FLT_EPSILON) {
ea.x = (float)atan2(mat.elements[i][j], mat.elements[i][k]);
ea.y = (float)atan2(sy, mat.elements[i][i]);
ea.z = (float)atan2(mat.elements[j][i], -mat.elements[k][i]);
} else {
ea.x = (float)atan2(-mat.elements[j][k], mat.elements[j][j]);
ea.y = (float)atan2(sy, mat.elements[i][i]);
ea.z = 0;
}
} else {
float cy = (float)sqrt(mat.elements[i][i]*mat.elements[i][i] + mat.elements[j][i]*mat.elements[j][i]);
if (cy > 16*FLT_EPSILON) {
ea.x = (float)atan2(mat.elements[k][j], mat.elements[k][k]);
ea.y = (float)atan2(-mat.elements[k][i], cy);
ea.z = (float)atan2(mat.elements[j][i], mat.elements[i][i]);
} else {
ea.x = (float)atan2(-mat.elements[j][k], mat.elements[j][j]);
ea.y = (float)atan2(-mat.elements[k][i], cy);
ea.z = 0;
}
}
if (n==EulParOdd) {
ea.x = -ea.x;
ea.y = - ea.y;
ea.z = -ea.z;
}
if (f==EulFrmR) {
float t = ea.x;
ea.x = ea.z;
ea.z = t;
}
//ea.w = order;
return (ea);
}

View File

@ -1,78 +1,78 @@
/*-----------------------------------------------------------------------------
glQuat.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with Quaternions
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "math.h"
#include "glTypes.h"
enum QuatPart {X, Y, Z, W};
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLquat glQuat (float x, float y, float z, float w)
{
GLquat result;
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
}
/* Convert quaternion to Euler angles (in radians). */
/*
EulerAngles Eul_FromQuat(Quat q, int order)
{
HMatrix M;
double Nq = q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w;
double s = (Nq > 0.0) ? (2.0 / Nq) : 0.0;
double xs = q.x*s, ys = q.y*s, zs = q.z*s;
double wx = q.w*xs, wy = q.w*ys, wz = q.w*zs;
double xx = q.x*xs, xy = q.x*ys, xz = q.x*zs;
double yy = q.y*ys, yz = q.y*zs, zz = q.z*zs;
M[X][X] = 1.0 - (yy + zz); M[X][Y] = xy - wz; M[X][Z] = xz + wy;
M[Y][X] = xy + wz; M[Y][Y] = 1.0 - (xx + zz); M[Y][Z] = yz - wx;
M[Z][X] = xz - wy; M[Z][Y] = yz + wx; M[Z][Z] = 1.0 - (xx + yy);
M[W][X]=M[W][Y]=M[W][Z]=M[X][W]=M[Y][W]=M[Z][W]=0.0; M[W][W]=1.0;
return (Eul_FromHMatrix(M, order));
}
*/
GLvector glQuatToEuler (GLquat q, int order)
{
GLmatrix M;
float Nq = q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w;
float s = (Nq > 0.0f) ? (2.0f / Nq) : 0.0f;
float xs = q.x*s, ys = q.y*s, zs = q.z*s;
float wx = q.w*xs, wy = q.w*ys, wz = q.w*zs;
float xx = q.x*xs, xy = q.x*ys, xz = q.x*zs;
float yy = q.y*ys, yz = q.y*zs, zz = q.z*zs;
M.elements[X][X] = 1.0f - (yy + zz); M.elements[X][Y] = xy - wz; M.elements[X][Z] = xz + wy;
M.elements[Y][X] = xy + wz; M.elements[Y][Y] = 1.0f - (xx + zz); M.elements[Y][Z] = yz - wx;
M.elements[Z][X] = xz - wy; M.elements[Z][Y] = yz + wx; M.elements[Z][Z] = 1.0f - (xx + yy);
M.elements[W][X] = M.elements[W][Y] =
M.elements[W][Z] = M.elements[X][W] =
M.elements[Y][W] = M.elements[Z][W] = 0.0f;
M.elements[W][W] = 1.0f;
return (glMatrixToEuler(M, order));
}
/*-----------------------------------------------------------------------------
glQuat.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with Quaternions
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "math.h"
#include "glTypes.h"
enum QuatPart {X, Y, Z, W};
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLquat glQuat (float x, float y, float z, float w)
{
GLquat result;
result.x = x;
result.y = y;
result.z = z;
result.w = w;
return result;
}
/* Convert quaternion to Euler angles (in radians). */
/*
EulerAngles Eul_FromQuat(Quat q, int order)
{
HMatrix M;
double Nq = q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w;
double s = (Nq > 0.0) ? (2.0 / Nq) : 0.0;
double xs = q.x*s, ys = q.y*s, zs = q.z*s;
double wx = q.w*xs, wy = q.w*ys, wz = q.w*zs;
double xx = q.x*xs, xy = q.x*ys, xz = q.x*zs;
double yy = q.y*ys, yz = q.y*zs, zz = q.z*zs;
M[X][X] = 1.0 - (yy + zz); M[X][Y] = xy - wz; M[X][Z] = xz + wy;
M[Y][X] = xy + wz; M[Y][Y] = 1.0 - (xx + zz); M[Y][Z] = yz - wx;
M[Z][X] = xz - wy; M[Z][Y] = yz + wx; M[Z][Z] = 1.0 - (xx + yy);
M[W][X]=M[W][Y]=M[W][Z]=M[X][W]=M[Y][W]=M[Z][W]=0.0; M[W][W]=1.0;
return (Eul_FromHMatrix(M, order));
}
*/
GLvector glQuatToEuler (GLquat q, int order)
{
GLmatrix M;
float Nq = q.x*q.x+q.y*q.y+q.z*q.z+q.w*q.w;
float s = (Nq > 0.0f) ? (2.0f / Nq) : 0.0f;
float xs = q.x*s, ys = q.y*s, zs = q.z*s;
float wx = q.w*xs, wy = q.w*ys, wz = q.w*zs;
float xx = q.x*xs, xy = q.x*ys, xz = q.x*zs;
float yy = q.y*ys, yz = q.y*zs, zz = q.z*zs;
M.elements[X][X] = 1.0f - (yy + zz); M.elements[X][Y] = xy - wz; M.elements[X][Z] = xz + wy;
M.elements[Y][X] = xy + wz; M.elements[Y][Y] = 1.0f - (xx + zz); M.elements[Y][Z] = yz - wx;
M.elements[Z][X] = xz - wy; M.elements[Z][Y] = yz + wx; M.elements[Z][Z] = 1.0f - (xx + yy);
M.elements[W][X] = M.elements[W][Y] =
M.elements[W][Z] = M.elements[X][W] =
M.elements[Y][W] = M.elements[Z][W] = 0.0f;
M.elements[W][W] = 1.0f;
return (glMatrixToEuler(M, order));
}

View File

@ -1,401 +1,401 @@
/*-----------------------------------------------------------------------------
glRgba.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with RGBA color values.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <gl/gl.h>
#include <math.h>
#include "math.h"
#include "glTypes.h"
#include "macro.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaFromHsl (float h, float sl, float l)
{
float v;
float r,g,b;
r = l; // default to gray
g = l;
b = l;
v = (l <= 0.5f) ? (l * (1.0f + sl)) : (l + sl - l * sl);
if (v > 0) {
float m;
float sv;
int sextant;
float fract, vsf, mid1, mid2;
m = l + l - v;
sv = (v - m ) / v;
h *= 6.0f;
sextant = (int)h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant) {
case 0:
r = v; g = mid1; b = m;
break;
case 1:
r = mid2; g = v; b = m;
break;
case 2:
r = m; g = v; b = mid1;
break;
case 3:
r = m; g = mid2; b = v;
break;
case 4:
r = mid1; g = m; b = v;
break;
case 5:
r = v; g = m; b = mid2;
break;
}
}
return glRgba (r, g, b);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaInterpolate (GLrgba c1, GLrgba c2, float delta)
{
GLrgba result;
result.red = MathInterpolate (c1.red, c2.red, delta);
result.green = MathInterpolate (c1.green, c2.green, delta);
result.blue = MathInterpolate (c1.blue, c2.blue, delta);
result.alpha = MathInterpolate (c1.alpha, c2.alpha, delta);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaAdd (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red + c2.red;
result.green = c1.green + c2.green;
result.blue = c1.blue + c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaSubtract (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red - c2.red;
result.green = c1.green - c2.green;
result.blue = c1.blue - c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaMultiply (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red * c2.red;
result.green = c1.green * c2.green;
result.blue = c1.blue * c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaScale (GLrgba c, float scale)
{
c.red *= scale;
c.green *= scale;
c.blue *= scale;
return c;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (char* string)
{
long color;
char buffer[10];
char* pound;
GLrgba result;
strncmp (buffer, string, 10);
if (pound = strchr (buffer, '#'))
pound[0] = ' ';
if (sscanf (string, "%x", &color) != 1)
return glRgba (0.0f);
result.red = (float)GetBValue (color) / 255.0f;
result.green = (float)GetGValue (color) / 255.0f;
result.blue = (float)GetRValue (color) / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (int red, int green, int blue)
{
GLrgba result;
result.red = (float)red / 255.0f;
result.green = (float)green / 255.0f;
result.blue = (float)blue / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float red, float green, float blue)
{
GLrgba result;
result.red = red;
result.green = green;
result.blue = blue;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float red, float green, float blue, float alpha)
{
GLrgba result;
result.red = red;
result.green = green;
result.blue = blue;
result.alpha = alpha;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (long c)
{
GLrgba result;
result.red = (float)GetRValue (c) / 255.0f;
result.green = (float)GetGValue (c) / 255.0f;
result.blue = (float)GetBValue (c) / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float luminance)
{
GLrgba result;
result.red = luminance;
result.green = luminance;
result.blue = luminance;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
Takes the given index and returns a "random" color unique for that index.
512 Unique values: #0 and #512 will be the same, as will #1 and #513, etc
Useful for visual debugging in some situations.
-----------------------------------------------------------------------------*/
GLrgba glRgbaUnique (int i)
{
GLrgba c;
c.alpha = 1.0f;
c.red = 0.4f + ((i & 1) ? 0.2f : 0.0f) + ((i & 8) ? 0.3f : 0.0f) - ((i & 64) ? 0.3f : 0.0f);
c.green = 0.4f + ((i & 2) ? 0.2f : 0.0f) + ((i & 32) ? 0.3f : 0.0f) - ((i & 128) ? 0.3f : 0.0f);
c.blue = 0.4f + ((i & 4) ? 0.2f : 0.0f) + ((i & 16) ? 0.3f : 0.0f) - ((i & 256) ? 0.3f : 0.0f);
return c;
}
/*-----------------------------------------------------------------------------
+ operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator+ (const GLrgba& c)
{
return glRgba (red + c.red, green + c.green, blue + c.blue, alpha);
}
GLrgba GLrgba::operator+ (const float& c)
{
return glRgba (red + c, green + c, blue + c, alpha);
}
void GLrgba::operator+= (const GLrgba& c)
{
red += c.red;
green += c.green;
blue += c.blue;
}
void GLrgba::operator+= (const float& c)
{
red += c;
green += c;
blue += c;
}
/*-----------------------------------------------------------------------------
- operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator- (const GLrgba& c)
{
return glRgba (red - c.red, green - c.green, blue - c.blue);
}
GLrgba GLrgba::operator- (const float& c)
{
return glRgba (red - c, green - c, blue - c, alpha);
}
void GLrgba::operator-= (const GLrgba& c)
{
red -= c.red;
green -= c.green;
blue -= c.blue;
}
void GLrgba::operator-= (const float& c)
{
red -= c;
green -= c;
blue -= c;
}
/*-----------------------------------------------------------------------------
* operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator* (const GLrgba& c)
{
return glRgba (red * c.red, green * c.green, blue * c.blue);
}
GLrgba GLrgba::operator* (const float& c)
{
return glRgba (red * c, green * c, blue * c, alpha);
}
void GLrgba::operator*= (const GLrgba& c)
{
red *= c.red;
green *= c.green;
blue *= c.blue;
}
void GLrgba::operator*= (const float& c)
{
red *= c;
green *= c;
blue *= c;
}
/*-----------------------------------------------------------------------------
/ operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator/ (const GLrgba& c)
{
return glRgba (red / c.red, green / c.green, blue / c.blue);
}
GLrgba GLrgba::operator/ (const float& c)
{
return glRgba (red / c, green / c, blue / c, alpha);
}
void GLrgba::operator/= (const GLrgba& c)
{
red /= c.red;
green /= c.green;
blue /= c.blue;
}
void GLrgba::operator/= (const float& c)
{
red /= c;
green /= c;
blue /= c;
}
bool GLrgba::operator== (const GLrgba& c)
{
return (red == c.red && green == c.green && blue == c.blue);
}
/*-----------------------------------------------------------------------------
glRgba.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with RGBA color values.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <gl/gl.h>
#include <math.h>
#include "math.h"
#include "glTypes.h"
#include "macro.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaFromHsl (float h, float sl, float l)
{
float v;
float r,g,b;
r = l; // default to gray
g = l;
b = l;
v = (l <= 0.5f) ? (l * (1.0f + sl)) : (l + sl - l * sl);
if (v > 0) {
float m;
float sv;
int sextant;
float fract, vsf, mid1, mid2;
m = l + l - v;
sv = (v - m ) / v;
h *= 6.0f;
sextant = (int)h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant) {
case 0:
r = v; g = mid1; b = m;
break;
case 1:
r = mid2; g = v; b = m;
break;
case 2:
r = m; g = v; b = mid1;
break;
case 3:
r = m; g = mid2; b = v;
break;
case 4:
r = mid1; g = m; b = v;
break;
case 5:
r = v; g = m; b = mid2;
break;
}
}
return glRgba (r, g, b);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaInterpolate (GLrgba c1, GLrgba c2, float delta)
{
GLrgba result;
result.red = MathInterpolate (c1.red, c2.red, delta);
result.green = MathInterpolate (c1.green, c2.green, delta);
result.blue = MathInterpolate (c1.blue, c2.blue, delta);
result.alpha = MathInterpolate (c1.alpha, c2.alpha, delta);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaAdd (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red + c2.red;
result.green = c1.green + c2.green;
result.blue = c1.blue + c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaSubtract (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red - c2.red;
result.green = c1.green - c2.green;
result.blue = c1.blue - c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaMultiply (GLrgba c1, GLrgba c2)
{
GLrgba result;
result.red = c1.red * c2.red;
result.green = c1.green * c2.green;
result.blue = c1.blue * c2.blue;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgbaScale (GLrgba c, float scale)
{
c.red *= scale;
c.green *= scale;
c.blue *= scale;
return c;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (char* string)
{
long color;
char buffer[10];
char* pound;
GLrgba result;
strncmp (buffer, string, 10);
if (pound = strchr (buffer, '#'))
pound[0] = ' ';
if (sscanf (string, "%x", &color) != 1)
return glRgba (0.0f);
result.red = (float)GetBValue (color) / 255.0f;
result.green = (float)GetGValue (color) / 255.0f;
result.blue = (float)GetRValue (color) / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (int red, int green, int blue)
{
GLrgba result;
result.red = (float)red / 255.0f;
result.green = (float)green / 255.0f;
result.blue = (float)blue / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float red, float green, float blue)
{
GLrgba result;
result.red = red;
result.green = green;
result.blue = blue;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float red, float green, float blue, float alpha)
{
GLrgba result;
result.red = red;
result.green = green;
result.blue = blue;
result.alpha = alpha;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (long c)
{
GLrgba result;
result.red = (float)GetRValue (c) / 255.0f;
result.green = (float)GetGValue (c) / 255.0f;
result.blue = (float)GetBValue (c) / 255.0f;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLrgba glRgba (float luminance)
{
GLrgba result;
result.red = luminance;
result.green = luminance;
result.blue = luminance;
result.alpha = 1.0f;
return result;
}
/*-----------------------------------------------------------------------------
Takes the given index and returns a "random" color unique for that index.
512 Unique values: #0 and #512 will be the same, as will #1 and #513, etc
Useful for visual debugging in some situations.
-----------------------------------------------------------------------------*/
GLrgba glRgbaUnique (int i)
{
GLrgba c;
c.alpha = 1.0f;
c.red = 0.4f + ((i & 1) ? 0.2f : 0.0f) + ((i & 8) ? 0.3f : 0.0f) - ((i & 64) ? 0.3f : 0.0f);
c.green = 0.4f + ((i & 2) ? 0.2f : 0.0f) + ((i & 32) ? 0.3f : 0.0f) - ((i & 128) ? 0.3f : 0.0f);
c.blue = 0.4f + ((i & 4) ? 0.2f : 0.0f) + ((i & 16) ? 0.3f : 0.0f) - ((i & 256) ? 0.3f : 0.0f);
return c;
}
/*-----------------------------------------------------------------------------
+ operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator+ (const GLrgba& c)
{
return glRgba (red + c.red, green + c.green, blue + c.blue, alpha);
}
GLrgba GLrgba::operator+ (const float& c)
{
return glRgba (red + c, green + c, blue + c, alpha);
}
void GLrgba::operator+= (const GLrgba& c)
{
red += c.red;
green += c.green;
blue += c.blue;
}
void GLrgba::operator+= (const float& c)
{
red += c;
green += c;
blue += c;
}
/*-----------------------------------------------------------------------------
- operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator- (const GLrgba& c)
{
return glRgba (red - c.red, green - c.green, blue - c.blue);
}
GLrgba GLrgba::operator- (const float& c)
{
return glRgba (red - c, green - c, blue - c, alpha);
}
void GLrgba::operator-= (const GLrgba& c)
{
red -= c.red;
green -= c.green;
blue -= c.blue;
}
void GLrgba::operator-= (const float& c)
{
red -= c;
green -= c;
blue -= c;
}
/*-----------------------------------------------------------------------------
* operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator* (const GLrgba& c)
{
return glRgba (red * c.red, green * c.green, blue * c.blue);
}
GLrgba GLrgba::operator* (const float& c)
{
return glRgba (red * c, green * c, blue * c, alpha);
}
void GLrgba::operator*= (const GLrgba& c)
{
red *= c.red;
green *= c.green;
blue *= c.blue;
}
void GLrgba::operator*= (const float& c)
{
red *= c;
green *= c;
blue *= c;
}
/*-----------------------------------------------------------------------------
/ operator
-----------------------------------------------------------------------------*/
GLrgba GLrgba::operator/ (const GLrgba& c)
{
return glRgba (red / c.red, green / c.green, blue / c.blue);
}
GLrgba GLrgba::operator/ (const float& c)
{
return glRgba (red / c, green / c, blue / c, alpha);
}
void GLrgba::operator/= (const GLrgba& c)
{
red /= c.red;
green /= c.green;
blue /= c.blue;
}
void GLrgba::operator/= (const float& c)
{
red /= c;
green /= c;
blue /= c;
}
bool GLrgba::operator== (const GLrgba& c)
{
return (red == c.red && green == c.green && blue == c.blue);
}

View File

@ -1,223 +1,223 @@
/*-----------------------------------------------------------------------------
Vector2.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with 2d (usually texture mapping) values.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "glTypes.h"
#include "math.h"
#include "macro.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorNormalize (GLvector2 v)
{
float length;
length = glVectorLength (v);
if (length < 0.000001f)
return v;
return v * (1.0f / length);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorLength (GLvector2 v)
{
return (float)sqrt (v.x * v.x + v.y * v.y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorSinCos (float a)
{
GLvector2 val;
a *= DEGREES_TO_RADIANS;
val.x = sinf (a);
val.y = cosf (a);
return val;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVector (float x, float y)
{
GLvector2 val;
val.x = x;
val.y = y;
return val;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorAdd (GLvector2 val1, GLvector2 val2)
{
GLvector2 result;
result.x = val1.x + val2.x;
result.y = val1.y + val2.y;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorInterpolate (GLvector2 v1, GLvector2 v2, float scalar)
{
GLvector2 result;
result.x = MathInterpolate (v1.x, v2.x, scalar);
result.y = MathInterpolate (v1.y, v2.y, scalar);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorSubtract (GLvector2 val1, GLvector2 val2)
{
GLvector2 result;
result.x = val1.x - val2.x;
result.y = val1.y - val2.y;
return result;
}
/*-----------------------------------------------------------------------------
+
-----------------------------------------------------------------------------*/
GLvector2 GLvector2::operator+ (const GLvector2& c)
{
return glVector (x + c.x, y + c.y);
}
GLvector2 GLvector2::operator+ (const float& c)
{
return glVector (x + c, y + c);
}
void GLvector2::operator+= (const GLvector2& c)
{
x += c.x;
y += c.y;
}
void GLvector2::operator+= (const float& c)
{
x += c;
y += c;
}
GLvector2 GLvector2::operator- (const GLvector2& c)
{
return glVector (x - c.x, y - c.y);
}
GLvector2 GLvector2::operator- (const float& c)
{
return glVector (x - c, y - c);
}
void GLvector2::operator-= (const GLvector2& c)
{
x -= c.x;
y -= c.y;
}
void GLvector2::operator-= (const float& c)
{
x -= c;
y -= c;
}
GLvector2 GLvector2::operator* (const GLvector2& c)
{
return glVector (x * c.x, y * c.y);
}
GLvector2 GLvector2::operator* (const float& c)
{
return glVector (x * c, y * c);
}
void GLvector2::operator*= (const GLvector2& c)
{
x *= c.x;
y *= c.y;
}
void GLvector2::operator*= (const float& c)
{
x *= c;
y *= c;
}
GLvector2 GLvector2::operator/ (const GLvector2& c)
{
return glVector (x / c.x, y / c.y);
}
GLvector2 GLvector2::operator/ (const float& c)
{
return glVector (x / c, y / c);
}
void GLvector2::operator/= (const GLvector2& c)
{
x /= c.x;
y /= c.y;
}
void GLvector2::operator/= (const float& c)
{
x /= c;
y /= c;
}
bool GLvector2::operator== (const GLvector2& c)
{
if (x == c.x && y == c.y)
return true;
return false;
}
/*-----------------------------------------------------------------------------
Vector2.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with 2d (usually texture mapping) values.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "glTypes.h"
#include "math.h"
#include "macro.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorNormalize (GLvector2 v)
{
float length;
length = glVectorLength (v);
if (length < 0.000001f)
return v;
return v * (1.0f / length);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorLength (GLvector2 v)
{
return (float)sqrt (v.x * v.x + v.y * v.y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorSinCos (float a)
{
GLvector2 val;
a *= DEGREES_TO_RADIANS;
val.x = sinf (a);
val.y = cosf (a);
return val;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVector (float x, float y)
{
GLvector2 val;
val.x = x;
val.y = y;
return val;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorAdd (GLvector2 val1, GLvector2 val2)
{
GLvector2 result;
result.x = val1.x + val2.x;
result.y = val1.y + val2.y;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorInterpolate (GLvector2 v1, GLvector2 v2, float scalar)
{
GLvector2 result;
result.x = MathInterpolate (v1.x, v2.x, scalar);
result.y = MathInterpolate (v1.y, v2.y, scalar);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector2 glVectorSubtract (GLvector2 val1, GLvector2 val2)
{
GLvector2 result;
result.x = val1.x - val2.x;
result.y = val1.y - val2.y;
return result;
}
/*-----------------------------------------------------------------------------
+
-----------------------------------------------------------------------------*/
GLvector2 GLvector2::operator+ (const GLvector2& c)
{
return glVector (x + c.x, y + c.y);
}
GLvector2 GLvector2::operator+ (const float& c)
{
return glVector (x + c, y + c);
}
void GLvector2::operator+= (const GLvector2& c)
{
x += c.x;
y += c.y;
}
void GLvector2::operator+= (const float& c)
{
x += c;
y += c;
}
GLvector2 GLvector2::operator- (const GLvector2& c)
{
return glVector (x - c.x, y - c.y);
}
GLvector2 GLvector2::operator- (const float& c)
{
return glVector (x - c, y - c);
}
void GLvector2::operator-= (const GLvector2& c)
{
x -= c.x;
y -= c.y;
}
void GLvector2::operator-= (const float& c)
{
x -= c;
y -= c;
}
GLvector2 GLvector2::operator* (const GLvector2& c)
{
return glVector (x * c.x, y * c.y);
}
GLvector2 GLvector2::operator* (const float& c)
{
return glVector (x * c, y * c);
}
void GLvector2::operator*= (const GLvector2& c)
{
x *= c.x;
y *= c.y;
}
void GLvector2::operator*= (const float& c)
{
x *= c;
y *= c;
}
GLvector2 GLvector2::operator/ (const GLvector2& c)
{
return glVector (x / c.x, y / c.y);
}
GLvector2 GLvector2::operator/ (const float& c)
{
return glVector (x / c, y / c);
}
void GLvector2::operator/= (const GLvector2& c)
{
x /= c.x;
y /= c.y;
}
void GLvector2::operator/= (const float& c)
{
x /= c;
y /= c;
}
bool GLvector2::operator== (const GLvector2& c)
{
if (x == c.x && y == c.y)
return true;
return false;
}

View File

@ -1,256 +1,256 @@
/*-----------------------------------------------------------------------------
glVector3.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with 3d vectors.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "macro.h"
#include "math.h"
#include "glTypes.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector glVectorReflect (GLvector3 ray, GLvector3 normal)
{
float dot;
dot = glVectorDotProduct (ray, normal);
return ray - (normal * (2.0f * dot));
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVector (float x, float y, float z)
{
GLvector3 result;
result.x = x;
result.y = y;
result.z = z;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorInterpolate (GLvector3 v1, GLvector3 v2, float scalar)
{
GLvector3 result;
result.x = MathInterpolate (v1.x, v2.x, scalar);
result.y = MathInterpolate (v1.y, v2.y, scalar);
result.z = MathInterpolate (v1.z, v2.z, scalar);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorLength (GLvector3 v)
{
return (float)sqrt (v.x * v.x + v.y * v.y + v.z * v.z);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorDotProduct (GLvector3 v1, GLvector3 v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorCrossProduct (GLvector3 v1, GLvector3 v2)
{
GLvector3 result;
result.x = v1.y * v2.z - v2.y * v1.z;
result.y = v1.z * v2.x - v2.z * v1.x;
result.z = v1.x * v2.y - v2.x * v1.y;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorInvert (GLvector3 v)
{
v.x *= -v.x;
v.y *= -v.y;
v.z *= -v.z;
return v;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorScale (GLvector3 v, float scale)
{
v.x *= scale;
v.y *= scale;
v.z *= scale;
return v;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorNormalize (GLvector3 v)
{
float length;
length = glVectorLength (v);
if (length < 0.000001f)
return v;
return glVectorScale (v, 1.0f / length);
}
/*-----------------------------------------------------------------------------
+
-----------------------------------------------------------------------------*/
GLvector GLvector::operator+ (const GLvector& c)
{
return glVector (x + c.x, y + c.y, z + c.z);
}
GLvector GLvector::operator+ (const float& c)
{
return glVector (x + c, y + c, z + c);
}
void GLvector::operator+= (const GLvector& c)
{
x += c.x;
y += c.y;
z += c.z;
}
void GLvector::operator+= (const float& c)
{
x += c;
y += c;
z += c;
}
GLvector GLvector::operator- (const GLvector& c)
{
return glVector (x - c.x, y - c.y, z - c.z);
}
GLvector GLvector::operator- (const float& c)
{
return glVector (x - c, y - c, z - c);
}
void GLvector::operator-= (const GLvector& c)
{
x -= c.x;
y -= c.y;
z -= c.z;
}
void GLvector::operator-= (const float& c)
{
x -= c;
y -= c;
z -= c;
}
GLvector GLvector::operator* (const GLvector& c)
{
return glVector (x * c.x, y * c.y, z * c.z);
}
GLvector GLvector::operator* (const float& c)
{
return glVector (x * c, y * c, z * c);
}
void GLvector::operator*= (const GLvector& c)
{
x *= c.x;
y *= c.y;
z *= c.z;
}
void GLvector::operator*= (const float& c)
{
x *= c;
y *= c;
z *= c;
}
GLvector GLvector::operator/ (const GLvector& c)
{
return glVector (x / c.x, y / c.y, z / c.z);
}
GLvector GLvector::operator/ (const float& c)
{
return glVector (x / c, y / c, z / c);
}
void GLvector::operator/= (const GLvector& c)
{
x /= c.x;
y /= c.y;
z /= c.z;
}
void GLvector::operator/= (const float& c)
{
x /= c;
y /= c;
z /= c;
}
bool GLvector::operator== (const GLvector& c)
{
if (x == c.x && y == c.y && z == c.z)
return true;
return false;
}
/*-----------------------------------------------------------------------------
glVector3.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Functions for dealing with 3d vectors.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <float.h>
#include <math.h>
#include <gl/gl.h>
#include "macro.h"
#include "math.h"
#include "glTypes.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector glVectorReflect (GLvector3 ray, GLvector3 normal)
{
float dot;
dot = glVectorDotProduct (ray, normal);
return ray - (normal * (2.0f * dot));
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVector (float x, float y, float z)
{
GLvector3 result;
result.x = x;
result.y = y;
result.z = z;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorInterpolate (GLvector3 v1, GLvector3 v2, float scalar)
{
GLvector3 result;
result.x = MathInterpolate (v1.x, v2.x, scalar);
result.y = MathInterpolate (v1.y, v2.y, scalar);
result.z = MathInterpolate (v1.z, v2.z, scalar);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorLength (GLvector3 v)
{
return (float)sqrt (v.x * v.x + v.y * v.y + v.z * v.z);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float glVectorDotProduct (GLvector3 v1, GLvector3 v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorCrossProduct (GLvector3 v1, GLvector3 v2)
{
GLvector3 result;
result.x = v1.y * v2.z - v2.y * v1.z;
result.y = v1.z * v2.x - v2.z * v1.x;
result.z = v1.x * v2.y - v2.x * v1.y;
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorInvert (GLvector3 v)
{
v.x *= -v.x;
v.y *= -v.y;
v.z *= -v.z;
return v;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorScale (GLvector3 v, float scale)
{
v.x *= scale;
v.y *= scale;
v.z *= scale;
return v;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector3 glVectorNormalize (GLvector3 v)
{
float length;
length = glVectorLength (v);
if (length < 0.000001f)
return v;
return glVectorScale (v, 1.0f / length);
}
/*-----------------------------------------------------------------------------
+
-----------------------------------------------------------------------------*/
GLvector GLvector::operator+ (const GLvector& c)
{
return glVector (x + c.x, y + c.y, z + c.z);
}
GLvector GLvector::operator+ (const float& c)
{
return glVector (x + c, y + c, z + c);
}
void GLvector::operator+= (const GLvector& c)
{
x += c.x;
y += c.y;
z += c.z;
}
void GLvector::operator+= (const float& c)
{
x += c;
y += c;
z += c;
}
GLvector GLvector::operator- (const GLvector& c)
{
return glVector (x - c.x, y - c.y, z - c.z);
}
GLvector GLvector::operator- (const float& c)
{
return glVector (x - c, y - c, z - c);
}
void GLvector::operator-= (const GLvector& c)
{
x -= c.x;
y -= c.y;
z -= c.z;
}
void GLvector::operator-= (const float& c)
{
x -= c;
y -= c;
z -= c;
}
GLvector GLvector::operator* (const GLvector& c)
{
return glVector (x * c.x, y * c.y, z * c.z);
}
GLvector GLvector::operator* (const float& c)
{
return glVector (x * c, y * c, z * c);
}
void GLvector::operator*= (const GLvector& c)
{
x *= c.x;
y *= c.y;
z *= c.z;
}
void GLvector::operator*= (const float& c)
{
x *= c;
y *= c;
z *= c;
}
GLvector GLvector::operator/ (const GLvector& c)
{
return glVector (x / c.x, y / c.y, z / c.z);
}
GLvector GLvector::operator/ (const float& c)
{
return glVector (x / c, y / c, z / c);
}
void GLvector::operator/= (const GLvector& c)
{
x /= c.x;
y /= c.y;
z /= c.z;
}
void GLvector::operator/= (const float& c)
{
x /= c;
y /= c;
z /= c;
}
bool GLvector::operator== (const GLvector& c)
{
if (x == c.x && y == c.y && z == c.z)
return true;
return false;
}

274
ini.cpp
View File

@ -1,137 +1,137 @@
/*-----------------------------------------------------------------------------
Ini.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This takes various types of data and dumps them into a predefined ini file.
-----------------------------------------------------------------------------*/
#define FORMAT_VECTOR "%f %f %f"
#define MAX_RESULT 256
#define FORMAT_FLOAT "%1.2f"
#define INI_FILE ".\\" APP ".ini"
#define SECTION "Settings"
#include <windows.h>
#include <stdio.h>
#include "glTypes.h"
#include "ini.h"
#include "win.h"
static char result[MAX_RESULT];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int IniInt (char* entry)
{
int result;
result = GetPrivateProfileInt (SECTION, entry, 0, INI_FILE);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniIntSet (char* entry, int val)
{
char buf[20];
sprintf (buf, "%d", val);
WritePrivateProfileString (SECTION, entry, buf, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float IniFloat (char* entry)
{
float f;
GetPrivateProfileString (SECTION, entry, "", result, MAX_RESULT, INI_FILE);
f = (float)atof (result);
return f;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniFloatSet (char* entry, float val)
{
char buf[20];
sprintf (buf, FORMAT_FLOAT, val);
WritePrivateProfileString (SECTION, entry, buf, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
char* IniString (char* entry)
{
GetPrivateProfileString (SECTION, entry, "", result, MAX_RESULT, INI_FILE);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniStringSet (char* entry, char* val)
{
WritePrivateProfileString (SECTION, entry, val, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniVectorSet (char* entry, GLvector v)
{
sprintf (result, FORMAT_VECTOR, v.x, v.y, v.z);
WritePrivateProfileString (SECTION, entry, result, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector IniVector (char* entry)
{
GLvector v;
v.x = v.y = v.z = 0.0f;
GetPrivateProfileString (SECTION, entry, "0 0 0", result, MAX_RESULT, INI_FILE);
sscanf (result, FORMAT_VECTOR, &v.x, &v.y, &v.z);
return v;
}
/*-----------------------------------------------------------------------------
Ini.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This takes various types of data and dumps them into a predefined ini file.
-----------------------------------------------------------------------------*/
#define FORMAT_VECTOR "%f %f %f"
#define MAX_RESULT 256
#define FORMAT_FLOAT "%1.2f"
#define INI_FILE ".\\" APP ".ini"
#define SECTION "Settings"
#include <windows.h>
#include <stdio.h>
#include "glTypes.h"
#include "ini.h"
#include "win.h"
static char result[MAX_RESULT];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int IniInt (char* entry)
{
int result;
result = GetPrivateProfileInt (SECTION, entry, 0, INI_FILE);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniIntSet (char* entry, int val)
{
char buf[20];
sprintf (buf, "%d", val);
WritePrivateProfileString (SECTION, entry, buf, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
float IniFloat (char* entry)
{
float f;
GetPrivateProfileString (SECTION, entry, "", result, MAX_RESULT, INI_FILE);
f = (float)atof (result);
return f;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniFloatSet (char* entry, float val)
{
char buf[20];
sprintf (buf, FORMAT_FLOAT, val);
WritePrivateProfileString (SECTION, entry, buf, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
char* IniString (char* entry)
{
GetPrivateProfileString (SECTION, entry, "", result, MAX_RESULT, INI_FILE);
return result;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniStringSet (char* entry, char* val)
{
WritePrivateProfileString (SECTION, entry, val, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void IniVectorSet (char* entry, GLvector v)
{
sprintf (result, FORMAT_VECTOR, v.x, v.y, v.z);
WritePrivateProfileString (SECTION, entry, result, INI_FILE);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
GLvector IniVector (char* entry)
{
GLvector v;
v.x = v.y = v.z = 0.0f;
GetPrivateProfileString (SECTION, entry, "0 0 0", result, MAX_RESULT, INI_FILE);
sscanf (result, FORMAT_VECTOR, &v.x, &v.y, &v.z);
return v;
}

16
ini.h
View File

@ -1,8 +1,8 @@
int IniInt (char* entry);
void IniIntSet (char* entry, int val);
float IniFloat (char* entry);
void IniFloatSet (char* entry, float val);
char* IniString (char* entry);
void IniStringSet (char* entry, char* val);
void IniVectorSet (char* entry, GLvector v);
GLvector IniVector (char* entry);
int IniInt (char* entry);
void IniIntSet (char* entry, int val);
float IniFloat (char* entry);
void IniFloatSet (char* entry, float val);
char* IniString (char* entry);
void IniStringSet (char* entry, char* val);
void IniVectorSet (char* entry, GLvector v);
GLvector IniVector (char* entry);

352
light.cpp
View File

@ -1,176 +1,176 @@
/*-----------------------------------------------------------------------------
Light.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This tracks and renders the light sources. (Note that they do not really
CAST light in the OpenGL sense of the world, these are just simple panels.)
These are NOT subclassed to entities because these are dynamic. Some lights
blink, and thus they can't go into the fixed render lists managed by
Entity.cpp.
-----------------------------------------------------------------------------*/
#define MAX_SIZE 5
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "camera.h"
#include "entity.h"
#include "light.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "visible.h"
static GLvector2 angles[5][360];
static CLight* head;
static bool angles_done;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightClear ()
{
CLight* l;
while (head) {
l = head;
head = l->_next;
delete l;
}
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int LightCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightRender ()
{
CLight* l;
if (!EntityReady ())
return;
if (!angles_done) {
for (int size = 0; size < MAX_SIZE; size++) {
for (int i = 0 ;i < 360; i++) {
angles[size][i].x = cosf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
angles[size][i].y = sinf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
}
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_LIGHT));
glDisable (GL_CULL_FACE);
glBegin (GL_QUADS);
for (l = head; l; l = l->_next)
l->Render ();
glEnd ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CLight::CLight (GLvector pos, GLrgba color, int size)
{
_position = pos;
_color = color;
_size = CLAMP (size, 0, (MAX_SIZE - 1));
_vert_size = (float)_size + 0.5f;
_flat_size = _vert_size + 0.5f;
_blink = false;
_cell_x = WORLD_TO_GRID(pos.x);
_cell_z = WORLD_TO_GRID(pos.z);
_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Blink ()
{
_blink = true;
//we don't want blinkers to be in sync, so have them blink at
//slightly different rates. (Milliseconds)
_blink_interval = 1500 + RandomVal (500);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Render ()
{
int angle;
GLvector pos;
GLvector camera;
GLvector camera_position;
GLvector2 offset;
if (!Visible (_cell_x, _cell_z))
return;
camera = CameraAngle ();
camera_position = CameraPosition ();
if (fabs (camera_position.x - _position.x) > RenderFogDistance ())
return;
if (fabs (camera_position.z - _position.z) > RenderFogDistance ())
return;
if (_blink && (GetTickCount () % _blink_interval) > 200)
return;
angle = (int)MathAngle (camera.y);
offset = angles[_size][angle];
pos = _position;
glColor4fv (&_color.red);
glTexCoord2f (0, 0);
glVertex3f (pos.x + offset.x, pos.y - _vert_size, pos.z + offset.y);
glTexCoord2f (0, 1);
glVertex3f (pos.x - offset.x, pos.y - _vert_size, pos.z - offset.y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - offset.x, pos.y + _vert_size, pos.z - offset.y);
glTexCoord2f (1, 0);
glVertex3f (pos.x + offset.x, pos.y + _vert_size, pos.z + offset.y);
}
/*-----------------------------------------------------------------------------
Light.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This tracks and renders the light sources. (Note that they do not really
CAST light in the OpenGL sense of the world, these are just simple panels.)
These are NOT subclassed to entities because these are dynamic. Some lights
blink, and thus they can't go into the fixed render lists managed by
Entity.cpp.
-----------------------------------------------------------------------------*/
#define MAX_SIZE 5
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include "glTypes.h"
#include "camera.h"
#include "entity.h"
#include "light.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "visible.h"
static GLvector2 angles[5][360];
static CLight* head;
static bool angles_done;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightClear ()
{
CLight* l;
while (head) {
l = head;
head = l->_next;
delete l;
}
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int LightCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightRender ()
{
CLight* l;
if (!EntityReady ())
return;
if (!angles_done) {
for (int size = 0; size < MAX_SIZE; size++) {
for (int i = 0 ;i < 360; i++) {
angles[size][i].x = cosf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
angles[size][i].y = sinf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
}
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_LIGHT));
glDisable (GL_CULL_FACE);
glBegin (GL_QUADS);
for (l = head; l; l = l->_next)
l->Render ();
glEnd ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CLight::CLight (GLvector pos, GLrgba color, int size)
{
_position = pos;
_color = color;
_size = CLAMP (size, 0, (MAX_SIZE - 1));
_vert_size = (float)_size + 0.5f;
_flat_size = _vert_size + 0.5f;
_blink = false;
_cell_x = WORLD_TO_GRID(pos.x);
_cell_z = WORLD_TO_GRID(pos.z);
_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Blink ()
{
_blink = true;
//we don't want blinkers to be in sync, so have them blink at
//slightly different rates. (Milliseconds)
_blink_interval = 1500 + RandomVal (500);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Render ()
{
int angle;
GLvector pos;
GLvector camera;
GLvector camera_position;
GLvector2 offset;
if (!Visible (_cell_x, _cell_z))
return;
camera = CameraAngle ();
camera_position = CameraPosition ();
if (fabs (camera_position.x - _position.x) > RenderFogDistance ())
return;
if (fabs (camera_position.z - _position.z) > RenderFogDistance ())
return;
if (_blink && (GetTickCount () % _blink_interval) > 200)
return;
angle = (int)MathAngle (camera.y);
offset = angles[_size][angle];
pos = _position;
glColor4fv (&_color.red);
glTexCoord2f (0, 0);
glVertex3f (pos.x + offset.x, pos.y - _vert_size, pos.z + offset.y);
glTexCoord2f (0, 1);
glVertex3f (pos.x - offset.x, pos.y - _vert_size, pos.z - offset.y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - offset.x, pos.y + _vert_size, pos.z - offset.y);
glTexCoord2f (1, 0);
glVertex3f (pos.x + offset.x, pos.y + _vert_size, pos.z + offset.y);
}

46
light.h
View File

@ -1,23 +1,23 @@
class CLight
{
GLvector _position;
GLrgba _color;
int _size;
float _vert_size;
float _flat_size;
bool _blink;
unsigned _blink_interval;
int _cell_x;
int _cell_z;
public:
CLight(GLvector pos, GLrgba color, int size);
class CLight* _next;
void Render ();
void Blink ();
};
void LightRender ();
void LightClear ();
int LightCount ();
class CLight
{
GLvector _position;
GLrgba _color;
int _size;
float _vert_size;
float _flat_size;
bool _blink;
unsigned _blink_interval;
int _cell_x;
int _cell_z;
public:
CLight(GLvector pos, GLrgba color, int size);
class CLight* _next;
void Render ();
void Blink ();
};
void LightRender ();
void LightClear ();
int LightCount ();

30
macro.h
View File

@ -1,15 +1,15 @@
#define LIMIT_INTERVAL(interval) { static unsigned next_update; if (next_update > GetTickCount ()) return; next_update = GetTickCount () + interval;}
#define DEGREES_TO_RADIANS .017453292F
#define RADIANS_TO_DEGREES 57.29577951F
#define PI ((double)3.1415926535F)
#define PI2 PI*PI
#define GRAVITY 9.5f
#define CLAMP(a,b,c) (a < b ? b : (a > c ? c : a))
#define WRAP(x,y) ((unsigned)x % y)
#define SIGN(x) (((x) > 0) ? 1 : ((x) < 0) ? -1 : 0)
#define ABS(x) (((x) < 0 ? (-x) : (x)))
#define SMALLEST(x,y) (ABS(x) < ABS(y) ? 0 : x)
#define MIN(x,y) ((x) < (y) ? x : y)
#define MAX(x,y) ((x) > (y) ? x : y)
#define POW(x,y) (float)pow(x,y)
#define SWAP(a,b) {int temp = a;a = b; b = temp;}
#define LIMIT_INTERVAL(interval) { static unsigned next_update; if (next_update > GetTickCount ()) return; next_update = GetTickCount () + interval;}
#define DEGREES_TO_RADIANS .017453292F
#define RADIANS_TO_DEGREES 57.29577951F
#define PI ((double)3.1415926535F)
#define PI2 PI*PI
#define GRAVITY 9.5f
#define CLAMP(a,b,c) (a < b ? b : (a > c ? c : a))
#define WRAP(x,y) ((unsigned)x % y)
#define SIGN(x) (((x) > 0) ? 1 : ((x) < 0) ? -1 : 0)
#define ABS(x) (((x) < 0 ? (-x) : (x)))
#define SMALLEST(x,y) (ABS(x) < ABS(y) ? 0 : x)
#define MIN(x,y) ((x) < (y) ? x : y)
#define MAX(x,y) ((x) > (y) ? x : y)
#define POW(x,y) (float)pow(x,y)
#define SWAP(a,b) {int temp = a;a = b; b = temp;}

362
math.cpp
View File

@ -1,181 +1,181 @@
/*-----------------------------------------------------------------------------
Math.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Various useful math functions.
-----------------------------------------------------------------------------*/
#include <math.h>
#include "macro.h"
#include "math.h"
/*-----------------------------------------------------------------------------
Keep an angle between 0 and 360
-----------------------------------------------------------------------------*/
float MathAngle (float angle)
{
if (angle < 0.0f)
angle = 360.0f - (float)fmod (fabs (angle), 360.0f);
else
angle = (float)fmod (angle, 360.0f);
return angle;
}
/*-----------------------------------------------------------------------------
Get an angle between two given points on a grid
-----------------------------------------------------------------------------*/
float MathAngle (float x1, float y1, float x2, float y2)
{
float x_delta;
float z_delta;
float angle;
z_delta = (y1 - y2);
x_delta = (x1 - x2);
if (x_delta == 0) {
if (z_delta > 0)
return 0.0f;
else
return 180.0f;
}
if (fabs (x_delta) < fabs (z_delta)) {
angle = 90 - (float)atan (z_delta / x_delta) * RADIANS_TO_DEGREES;
if (x_delta < 0)
angle -= 180.0f;
} else {
angle = (float)atan (x_delta / z_delta) * RADIANS_TO_DEGREES;
if (z_delta < 0.0f)
angle += 180.0f;
}
if (angle< 0.0f)
angle += 360.0f;
return angle;
}
/*-----------------------------------------------------------------------------
Get distance (squared) between 2 points on a plane
-----------------------------------------------------------------------------*/
float MathDistance2 (float x1, float y1, float x2, float y2)
{
float dx;
float dy;
dx = x1 - x2;
dy = y1 - y2;
return dx * dx + dy * dy;
}
/*-----------------------------------------------------------------------------
Get distance between 2 points on a plane. This is slightly slower than
MathDistance2 ()
-----------------------------------------------------------------------------*/
float MathDistance (float x1, float y1, float x2, float y2)
{
float dx;
float dy;
dx = x1 - x2;
dy = y1 - y2;
return (float)sqrt (dx * dx + dy * dy);
}
/*-----------------------------------------------------------------------------
difference between two angles
-----------------------------------------------------------------------------*/
float MathAngleDifference (float a1, float a2)
{
float result;
result = (float)fmod (a1 - a2, 360.0f);
if (result > 180.0)
return result - 360.0F;
if (result < -180.0)
return result + 360.0F;
return result;
}
/*-----------------------------------------------------------------------------
interpolate between two values
-----------------------------------------------------------------------------*/
float MathInterpolate (float n1, float n2, float delta)
{
return n1 * (1.0f - delta) + n2 * delta;
}
/*-----------------------------------------------------------------------------
return a scalar of 0.0 to 1.0, based an the given values position within a range
-----------------------------------------------------------------------------*/
float MathSmoothStep (float val, float a, float b)
{
if (b == a)
return 0.0f;
val -= a;
val /= (b - a);
return CLAMP (val, 0.0f, 1.0f);
}
/*-----------------------------------------------------------------------------
Average two values
-----------------------------------------------------------------------------*/
float MathAverage (float n1, float n2)
{
return (n1 + n2) / 2.0f;
}
/*-----------------------------------------------------------------------------
This will take linear input values from 0.0 to 1.0 and convert them to
values along a curve. This could also be acomplished with sin (), but this
way avoids converting to radians and back.
-----------------------------------------------------------------------------*/
float MathScalarCurve (float val)
{
float sign;
val = (val - 0.5f) * 2.0f;
if (val < 0.0f)
sign = -1.0f;
else
sign = 1.0f;
if (val < 0.0f)
val = -val;
val = 1.0f - val;
val *= val;
val = 1.0f - val;
val *= sign;
val = (val + 1.0f) / 2.0f;
return val;
}
/*-----------------------------------------------------------------------------
Math.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Various useful math functions.
-----------------------------------------------------------------------------*/
#include <math.h>
#include "macro.h"
#include "math.h"
/*-----------------------------------------------------------------------------
Keep an angle between 0 and 360
-----------------------------------------------------------------------------*/
float MathAngle (float angle)
{
if (angle < 0.0f)
angle = 360.0f - (float)fmod (fabs (angle), 360.0f);
else
angle = (float)fmod (angle, 360.0f);
return angle;
}
/*-----------------------------------------------------------------------------
Get an angle between two given points on a grid
-----------------------------------------------------------------------------*/
float MathAngle (float x1, float y1, float x2, float y2)
{
float x_delta;
float z_delta;
float angle;
z_delta = (y1 - y2);
x_delta = (x1 - x2);
if (x_delta == 0) {
if (z_delta > 0)
return 0.0f;
else
return 180.0f;
}
if (fabs (x_delta) < fabs (z_delta)) {
angle = 90 - (float)atan (z_delta / x_delta) * RADIANS_TO_DEGREES;
if (x_delta < 0)
angle -= 180.0f;
} else {
angle = (float)atan (x_delta / z_delta) * RADIANS_TO_DEGREES;
if (z_delta < 0.0f)
angle += 180.0f;
}
if (angle< 0.0f)
angle += 360.0f;
return angle;
}
/*-----------------------------------------------------------------------------
Get distance (squared) between 2 points on a plane
-----------------------------------------------------------------------------*/
float MathDistance2 (float x1, float y1, float x2, float y2)
{
float dx;
float dy;
dx = x1 - x2;
dy = y1 - y2;
return dx * dx + dy * dy;
}
/*-----------------------------------------------------------------------------
Get distance between 2 points on a plane. This is slightly slower than
MathDistance2 ()
-----------------------------------------------------------------------------*/
float MathDistance (float x1, float y1, float x2, float y2)
{
float dx;
float dy;
dx = x1 - x2;
dy = y1 - y2;
return (float)sqrt (dx * dx + dy * dy);
}
/*-----------------------------------------------------------------------------
difference between two angles
-----------------------------------------------------------------------------*/
float MathAngleDifference (float a1, float a2)
{
float result;
result = (float)fmod (a1 - a2, 360.0f);
if (result > 180.0)
return result - 360.0F;
if (result < -180.0)
return result + 360.0F;
return result;
}
/*-----------------------------------------------------------------------------
interpolate between two values
-----------------------------------------------------------------------------*/
float MathInterpolate (float n1, float n2, float delta)
{
return n1 * (1.0f - delta) + n2 * delta;
}
/*-----------------------------------------------------------------------------
return a scalar of 0.0 to 1.0, based an the given values position within a range
-----------------------------------------------------------------------------*/
float MathSmoothStep (float val, float a, float b)
{
if (b == a)
return 0.0f;
val -= a;
val /= (b - a);
return CLAMP (val, 0.0f, 1.0f);
}
/*-----------------------------------------------------------------------------
Average two values
-----------------------------------------------------------------------------*/
float MathAverage (float n1, float n2)
{
return (n1 + n2) / 2.0f;
}
/*-----------------------------------------------------------------------------
This will take linear input values from 0.0 to 1.0 and convert them to
values along a curve. This could also be acomplished with sin (), but this
way avoids converting to radians and back.
-----------------------------------------------------------------------------*/
float MathScalarCurve (float val)
{
float sign;
val = (val - 0.5f) * 2.0f;
if (val < 0.0f)
sign = -1.0f;
else
sign = 1.0f;
if (val < 0.0f)
val = -val;
val = 1.0f - val;
val *= val;
val = 1.0f - val;
val *= sign;
val = (val + 1.0f) / 2.0f;
return val;
}

20
math.h
View File

@ -1,10 +1,10 @@
float MathAngle (float angle);
float MathAngle (float x1, float y1, float x2, float y2);
float MathAngleDifference (float a1, float a2);
float MathAverage (float n1, float n2);
float MathInterpolate (float n1, float n2, float delta);
float MathLine_distance (float x1, float y1, float x2, float y2, float px, float py);
float MathDistance (float x1, float y1, float x2, float y2);
float MathDistance2 (float x1, float y1, float x2, float y2);
float MathSmoothStep (float val, float a, float b);
float MathScalarCurve (float val);
float MathAngle (float angle);
float MathAngle (float x1, float y1, float x2, float y2);
float MathAngleDifference (float a1, float a2);
float MathAverage (float n1, float n2);
float MathInterpolate (float n1, float n2, float delta);
float MathLine_distance (float x1, float y1, float x2, float y2, float px, float py);
float MathDistance (float x1, float y1, float x2, float y2);
float MathDistance2 (float x1, float y1, float x2, float y2);
float MathSmoothStep (float val, float a, float b);
float MathScalarCurve (float val);

356
mesh.cpp
View File

@ -1,178 +1,178 @@
/*-----------------------------------------------------------------------------
Mesh.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This class is used to make constructing objects easier. It handles
allocating vertex lists, polygon lists, and suchlike.
If you were going to implement vertex buffers, this would be the place to
do it. Take away the _vertex member variable and store verts for ALL meshes
in a common list, which could then be unloaded onto the good 'ol GPU.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <vector>
#include "glTypes.h"
#include "mesh.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::CMesh ()
{
_list = glGenLists(1);
_compiled = false;
_polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::~CMesh ()
{
glDeleteLists (_list, 1);
_vertex.clear ();
_fan.clear ();
_quad_strip.clear ();
_cube.clear ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::VertexAdd (const GLvertex& v)
{
_vertex.push_back(v);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::CubeAdd (const cube& c)
{
_cube.push_back(c);
_polycount += 5;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::QuadStripAdd (const quad_strip& qs)
{
_quad_strip.push_back(qs);
_polycount += (qs.index_list.size() - 2) / 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::FanAdd (const fan& f)
{
_fan.push_back(f);
_polycount += f.index_list.size() - 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Render ()
{
std::vector<quad_strip>::iterator qsi;
std::vector<cube>::iterator ci;
std::vector<fan>::iterator fi;
std::vector<int>::iterator n;
if (_compiled) {
glCallList (_list);
return;
}
for (qsi = _quad_strip.begin(); qsi < _quad_strip.end(); ++qsi) {
glBegin (GL_QUAD_STRIP);
for (n = qsi->index_list.begin(); n < qsi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
for (ci = _cube.begin(); ci < _cube.end(); ++ci) {
glBegin (GL_QUAD_STRIP);
for (n = ci->index_list.begin(); n < ci->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[7]].uv.x);
glVertex3fv (&_vertex[ci->index_list[7]].position.x);
glVertex3fv (&_vertex[ci->index_list[5]].position.x);
glVertex3fv (&_vertex[ci->index_list[3]].position.x);
glVertex3fv (&_vertex[ci->index_list[1]].position.x);
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[6]].uv.x);
glVertex3fv (&_vertex[ci->index_list[0]].position.x);
glVertex3fv (&_vertex[ci->index_list[2]].position.x);
glVertex3fv (&_vertex[ci->index_list[4]].position.x);
glVertex3fv (&_vertex[ci->index_list[6]].position.x);
glEnd ();
}
for (fi = _fan.begin(); fi < _fan.end(); ++fi) {
glBegin (GL_TRIANGLE_FAN);
for (n = fi->index_list.begin(); n < fi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Compile ()
{
glNewList (_list, GL_COMPILE);
Render ();
glEndList();
_compiled = true;
}
/*-----------------------------------------------------------------------------
Mesh.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This class is used to make constructing objects easier. It handles
allocating vertex lists, polygon lists, and suchlike.
If you were going to implement vertex buffers, this would be the place to
do it. Take away the _vertex member variable and store verts for ALL meshes
in a common list, which could then be unloaded onto the good 'ol GPU.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <vector>
#include "glTypes.h"
#include "mesh.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::CMesh ()
{
_list = glGenLists(1);
_compiled = false;
_polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::~CMesh ()
{
glDeleteLists (_list, 1);
_vertex.clear ();
_fan.clear ();
_quad_strip.clear ();
_cube.clear ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::VertexAdd (const GLvertex& v)
{
_vertex.push_back(v);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::CubeAdd (const cube& c)
{
_cube.push_back(c);
_polycount += 5;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::QuadStripAdd (const quad_strip& qs)
{
_quad_strip.push_back(qs);
_polycount += (qs.index_list.size() - 2) / 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::FanAdd (const fan& f)
{
_fan.push_back(f);
_polycount += f.index_list.size() - 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Render ()
{
std::vector<quad_strip>::iterator qsi;
std::vector<cube>::iterator ci;
std::vector<fan>::iterator fi;
std::vector<int>::iterator n;
if (_compiled) {
glCallList (_list);
return;
}
for (qsi = _quad_strip.begin(); qsi < _quad_strip.end(); ++qsi) {
glBegin (GL_QUAD_STRIP);
for (n = qsi->index_list.begin(); n < qsi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
for (ci = _cube.begin(); ci < _cube.end(); ++ci) {
glBegin (GL_QUAD_STRIP);
for (n = ci->index_list.begin(); n < ci->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[7]].uv.x);
glVertex3fv (&_vertex[ci->index_list[7]].position.x);
glVertex3fv (&_vertex[ci->index_list[5]].position.x);
glVertex3fv (&_vertex[ci->index_list[3]].position.x);
glVertex3fv (&_vertex[ci->index_list[1]].position.x);
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[6]].uv.x);
glVertex3fv (&_vertex[ci->index_list[0]].position.x);
glVertex3fv (&_vertex[ci->index_list[2]].position.x);
glVertex3fv (&_vertex[ci->index_list[4]].position.x);
glVertex3fv (&_vertex[ci->index_list[6]].position.x);
glEnd ();
}
for (fi = _fan.begin(); fi < _fan.end(); ++fi) {
glBegin (GL_TRIANGLE_FAN);
for (n = fi->index_list.begin(); n < fi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Compile ()
{
glNewList (_list, GL_COMPILE);
Render ();
glEndList();
_compiled = true;
}

81
mesh.h
View File

@ -1,41 +1,40 @@
#include <vector>
struct cube
{
std::vector<int> index_list; // probably always .size() == 10...
};
struct quad_strip
{
std::vector<int> index_list;
};
struct fan
{
std::vector<int> index_list;
};
class CMesh
{
public:
CMesh ();
~CMesh ();
unsigned _list;
int _polycount;
std::vector<GLvertex> _vertex;
std::vector<cube> _cube;
std::vector<quad_strip> _quad_strip;
std::vector<fan> _fan;
bool _compiled;
void VertexAdd (const GLvertex& v);
int VertexCount () { return _vertex.size(); }
int PolyCount () { return _polycount; }
void CubeAdd (const cube& c);
void QuadStripAdd (const quad_strip& qs);
void FanAdd (const fan& f);
void Render ();
void Compile ();
};
#include <vector>
struct cube
{
std::vector<int> index_list; // probably always .size() == 10...
};
struct quad_strip
{
std::vector<int> index_list;
};
struct fan
{
std::vector<int> index_list;
};
class CMesh
{
public:
CMesh ();
~CMesh ();
unsigned _list;
int _polycount;
std::vector<GLvertex> _vertex;
std::vector<cube> _cube;
std::vector<quad_strip> _quad_strip;
std::vector<fan> _fan;
bool _compiled;
void VertexAdd (const GLvertex& v);
int VertexCount () { return _vertex.size(); }
int PolyCount () { return _polycount; }
void CubeAdd (const cube& c);
void QuadStripAdd (const quad_strip& qs);
void FanAdd (const fan& f);
void Render ();
void Compile ();
};

View File

@ -1,93 +1,92 @@
/*-----------------------------------------------------------------------------
r a n d o m
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
The Mersenne Twister by Matsumoto and Nishimura <matumoto@math.keio.ac.jp>.
It sets new standards for the period, quality and speed of random number
generators. The incredible period is 2^19937 - 1, a number with about 6000
digits; the 32-bit random numbers exhibit best possible equidistribution
properties in dimensions up to 623; and it's fast, very fast.
-----------------------------------------------------------------------------*/
#define LOWER_MASK 0x7fffffff
#define M 397
#define MATRIX_A 0x9908b0df
#define N 624
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_L(y) (y >> 18)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define UPPER_MASK 0x80000000
#include <memory.h>
#include "random.h"
static int k = 1;
static unsigned long mag01[2] = {0x0, MATRIX_A};
static unsigned long ptgfsr[N];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned long RandomVal (void)
{
int kk;
unsigned long y;
if (k == N) {
for (kk = 0; kk < N - M; kk++) {
y = (ptgfsr[kk] & UPPER_MASK) | (ptgfsr[kk + 1] & LOWER_MASK);
ptgfsr[kk] = ptgfsr[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (; kk < N - 1; kk++) {
y = (ptgfsr[kk] & UPPER_MASK) | (ptgfsr[kk + 1] & LOWER_MASK);
ptgfsr[kk] = ptgfsr[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (ptgfsr[N - 1] & UPPER_MASK) | (ptgfsr[0] & LOWER_MASK);
ptgfsr[N - 1] = ptgfsr[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];
k = 0;
}
y = ptgfsr[k++];
y ^= TEMPERING_SHIFT_U (y);
y ^= TEMPERING_SHIFT_S (y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T (y) & TEMPERING_MASK_C;
return y ^= TEMPERING_SHIFT_L (y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned long RandomVal (int range)
{
return range ? (RandomVal () % range) : 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RandomInit (unsigned long seed)
{
//int k;
//memset (ptgfsr, 0, sizeof (ptgfsr));
//mag01[0] = 0x0;
//mag01[1] = MATRIX_A;
ptgfsr[0] = seed;
for (k = 1; k < N; k++)
ptgfsr[k] = 69069 * ptgfsr[k - 1];
k = 1;
}
/*-----------------------------------------------------------------------------
r a n d o m
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
The Mersenne Twister by Matsumoto and Nishimura <matumoto@math.keio.ac.jp>.
It sets new standards for the period, quality and speed of random number
generators. The incredible period is 2^19937 - 1, a number with about 6000
digits; the 32-bit random numbers exhibit best possible equidistribution
properties in dimensions up to 623; and it's fast, very fast.
-----------------------------------------------------------------------------*/
#define LOWER_MASK 0x7fffffff
#define M 397
#define MATRIX_A 0x9908b0df
#define N 624
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_L(y) (y >> 18)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define UPPER_MASK 0x80000000
#include <memory.h>
#include "random.h"
static int k = 1;
static unsigned long mag01[2] = {0x0, MATRIX_A};
static unsigned long ptgfsr[N];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned long RandomVal (void)
{
int kk;
unsigned long y;
if (k == N) {
for (kk = 0; kk < N - M; kk++) {
y = (ptgfsr[kk] & UPPER_MASK) | (ptgfsr[kk + 1] & LOWER_MASK);
ptgfsr[kk] = ptgfsr[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (; kk < N - 1; kk++) {
y = (ptgfsr[kk] & UPPER_MASK) | (ptgfsr[kk + 1] & LOWER_MASK);
ptgfsr[kk] = ptgfsr[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (ptgfsr[N - 1] & UPPER_MASK) | (ptgfsr[0] & LOWER_MASK);
ptgfsr[N - 1] = ptgfsr[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];
k = 0;
}
y = ptgfsr[k++];
y ^= TEMPERING_SHIFT_U (y);
y ^= TEMPERING_SHIFT_S (y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T (y) & TEMPERING_MASK_C;
return y ^= TEMPERING_SHIFT_L (y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
unsigned long RandomVal (int range)
{
return range ? (RandomVal () % range) : 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void RandomInit (unsigned long seed)
{
//int k;
//memset (ptgfsr, 0, sizeof (ptgfsr));
//mag01[0] = 0x0;
//mag01[1] = MATRIX_A;
ptgfsr[0] = seed;
for (k = 1; k < N; k++)
ptgfsr[k] = 69069 * ptgfsr[k - 1];
k = 1;
}

View File

@ -1,5 +1,5 @@
#define COIN_FLIP (RandomVal (2) == 0)
unsigned long RandomVal (int range);
unsigned long RandomVal (void);
void RandomInit (unsigned long seed);
#define COIN_FLIP (RandomVal (2) == 0)
unsigned long RandomVal (int range);
unsigned long RandomVal (void);
void RandomInit (unsigned long seed);

1684
render.cpp

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +1,27 @@
bool RenderBloom ();
void RenderEffectCycle ();
bool RenderFlat ();
void RenderFlatToggle ();
float RenderFogDistance ();
bool RenderFog ();
void RenderFogToggle ();
void RenderFogFX (float scalar);
void RenderFPSToggle ();
void RenderInit ();
void RenderLetterboxToggle ();
int RenderMaxTextureSize ();
void RenderResize ();
void RenderTerm ();
void RenderUpdate ();
bool RenderWireframe ();
void RenderWireframeToggle ();
void RenderHelpToggle ();
void RenderPrint (int x, int y, int font, GLrgba color, const char *fmt, ...);
void RenderPrint (int line, const char *fmt, ...);
bool RenderBloom ();
void RenderEffectCycle ();
bool RenderFlat ();
void RenderFlatToggle ();
float RenderFogDistance ();
bool RenderFog ();
void RenderFogToggle ();
void RenderFogFX (float scalar);
void RenderFPSToggle ();
void RenderInit ();
void RenderLetterboxToggle ();
int RenderMaxTextureSize ();
void RenderResize ();
void RenderTerm ();
void RenderUpdate ();
bool RenderWireframe ();
void RenderWireframeToggle ();
void RenderHelpToggle ();
void RenderPrint (int x, int y, int font, GLrgba color, const char *fmt, ...);
void RenderPrint (int line, const char *fmt, ...);

282
sky.cpp
View File

@ -1,141 +1,141 @@
/*-----------------------------------------------------------------------------
Sky.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Did this need to be written as a class? It did not. There will never be
more than one sky in play, so the whole class structure here is superflous,
but harmless.
-----------------------------------------------------------------------------*/
#define SKYPOINTS 24
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include "camera.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "sky.h"
#include "texture.h"
#include "glTypes.h"
#include "world.h"
static CSky* sky;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyRender ()
{
if (sky && !RenderFlat ())
sky->Render ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyClear ()
{
if(sky)
delete sky;
sky = NULL;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CSky::Render ()
{
GLvector angle, position;
if (!TextureReady ())
return;
glDepthMask (false);
glPushAttrib (GL_POLYGON_BIT | GL_FOG_BIT);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glDisable (GL_CULL_FACE);
glDisable (GL_FOG);
glPushMatrix ();
glLoadIdentity();
angle = CameraAngle ();
position = CameraPosition ();
glRotatef (angle.x, 1.0f, 0.0f, 0.0f);
glRotatef (angle.y, 0.0f, 1.0f, 0.0f);
glRotatef (angle.z, 0.0f, 0.0f, 1.0f);
glTranslatef (0.0f, -position.y / 100.0f, 0.0f);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_SKY));
glCallList (m_list);
glPopMatrix ();
glPopAttrib ();
glDepthMask (true);
glEnable (GL_COLOR_MATERIAL);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CSky::CSky ()
{
GLvertex circle[SKYPOINTS];
GLvector pos;
float angle;
int i;
float size;
float rad;
float lum;
size = 10.0f;
for (i = 0; i < SKYPOINTS; i++) {
angle = (float)i / (float)(SKYPOINTS - 1);
angle *= 360;
angle *= DEGREES_TO_RADIANS;
circle[i].position.x = sinf (angle) * size;
circle[i].position.y = 0.1f;
circle[i].position.z = cosf (angle) * size;
circle[i].uv.x = ((float)i / (float)(SKYPOINTS - 1)) * 5.0f;
circle[i].uv.y = 0.5f;
rad = ((float)i / (SKYPOINTS - 1)) * 180.0f * DEGREES_TO_RADIANS;
lum = sinf (rad);
lum = (float)pow (lum, 5);
circle[i].color = glRgba (lum);
}
m_list = glGenLists(1);
glNewList (m_list, GL_COMPILE);
glColor3f (1, 1, 1);
glBegin (GL_QUAD_STRIP);
for (i = 0; i < SKYPOINTS; i++) {
glTexCoord2f (circle[i].uv.x, 0.0f);
glVertex3fv (&circle[i].position.x);
pos = circle[i].position;
pos.y = size / 3.5f;
glTexCoord2f (circle[i].uv.x, 1.0f);
glVertex3fv (&pos.x);
}
glEnd ();
glEndList();
sky = this;
}
/*-----------------------------------------------------------------------------
Sky.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
Did this need to be written as a class? It did not. There will never be
more than one sky in play, so the whole class structure here is superflous,
but harmless.
-----------------------------------------------------------------------------*/
#define SKYPOINTS 24
#include <windows.h>
#include <math.h>
#include <gl/gl.h>
#include "camera.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "sky.h"
#include "texture.h"
#include "glTypes.h"
#include "world.h"
static CSky* sky;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyRender ()
{
if (sky && !RenderFlat ())
sky->Render ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void SkyClear ()
{
if(sky)
delete sky;
sky = NULL;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CSky::Render ()
{
GLvector angle, position;
if (!TextureReady ())
return;
glDepthMask (false);
glPushAttrib (GL_POLYGON_BIT | GL_FOG_BIT);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glDisable (GL_CULL_FACE);
glDisable (GL_FOG);
glPushMatrix ();
glLoadIdentity();
angle = CameraAngle ();
position = CameraPosition ();
glRotatef (angle.x, 1.0f, 0.0f, 0.0f);
glRotatef (angle.y, 0.0f, 1.0f, 0.0f);
glRotatef (angle.z, 0.0f, 0.0f, 1.0f);
glTranslatef (0.0f, -position.y / 100.0f, 0.0f);
glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_SKY));
glCallList (m_list);
glPopMatrix ();
glPopAttrib ();
glDepthMask (true);
glEnable (GL_COLOR_MATERIAL);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CSky::CSky ()
{
GLvertex circle[SKYPOINTS];
GLvector pos;
float angle;
int i;
float size;
float rad;
float lum;
size = 10.0f;
for (i = 0; i < SKYPOINTS; i++) {
angle = (float)i / (float)(SKYPOINTS - 1);
angle *= 360;
angle *= DEGREES_TO_RADIANS;
circle[i].position.x = sinf (angle) * size;
circle[i].position.y = 0.1f;
circle[i].position.z = cosf (angle) * size;
circle[i].uv.x = ((float)i / (float)(SKYPOINTS - 1)) * 5.0f;
circle[i].uv.y = 0.5f;
rad = ((float)i / (SKYPOINTS - 1)) * 180.0f * DEGREES_TO_RADIANS;
lum = sinf (rad);
lum = (float)pow (lum, 5);
circle[i].color = glRgba (lum);
}
m_list = glGenLists(1);
glNewList (m_list, GL_COMPILE);
glColor3f (1, 1, 1);
glBegin (GL_QUAD_STRIP);
for (i = 0; i < SKYPOINTS; i++) {
glTexCoord2f (circle[i].uv.x, 0.0f);
glVertex3fv (&circle[i].position.x);
pos = circle[i].position;
pos.y = size / 3.5f;
glTexCoord2f (circle[i].uv.x, 1.0f);
glVertex3fv (&pos.x);
}
glEnd ();
glEndList();
sky = this;
}

48
sky.h
View File

@ -1,24 +1,24 @@
#define SKY_GRID 21
#define SKY_HALF (SKY_GRID / 2)
struct sky_point
{
GLrgba color;
GLvector position;
};
class CSky
{
private:
int m_list;
int m_stars_list;
sky_point m_grid[SKY_GRID][SKY_GRID];
public:
CSky ();
void Render (void);
};
void SkyRender ();
void SkyClear ();
#define SKY_GRID 21
#define SKY_HALF (SKY_GRID / 2)
struct sky_point
{
GLrgba color;
GLvector position;
};
class CSky
{
private:
int m_list;
int m_stars_list;
sky_point m_grid[SKY_GRID][SKY_GRID];
public:
CSky ();
void Render (void);
};
void SkyRender ();
void SkyClear ();

File diff suppressed because it is too large Load Diff

View File

@ -1,47 +1,47 @@
#define SEGMENTS_PER_TEXTURE 64
#define ONE_SEGMENT (1.0f / SEGMENTS_PER_TEXTURE)
#define LANES_PER_TEXTURE 8
#define LANE_SIZE (1.0f / LANES_PER_TEXTURE)
#define LANE_PIXELS (_size / LANES_PER_TEXTURE)
#define TRIM_RESOLUTION 256
#define TRIM_ROWS 4
#define TRIM_SIZE (1.0f / TRIM_ROWS)
#define TRIM_PIXELS (TRIM_RESOLUTION / TRIM_ROWS)
#define LOGO_RESOLUTION 512
#define LOGO_ROWS 16
#define LOGO_SIZE (1.0f / LOGO_ROWS)
#define LOGO_PIXELS (LOGO_RESOLUTION / LOGO_ROWS)
enum
{
TEXTURE_LIGHT,
TEXTURE_SOFT_CIRCLE,
TEXTURE_SKY,
TEXTURE_LOGOS,
TEXTURE_TRIM,
TEXTURE_BLOOM,
TEXTURE_HEADLIGHT,
TEXTURE_LATTICE,
TEXTURE_BUILDING1,
TEXTURE_BUILDING2,
TEXTURE_BUILDING3,
TEXTURE_BUILDING4,
TEXTURE_BUILDING5,
TEXTURE_BUILDING6,
TEXTURE_BUILDING7,
TEXTURE_BUILDING8,
TEXTURE_BUILDING9,
TEXTURE_COUNT,
};
#define BUILDING_COUNT ((TEXTURE_BUILDING9 - TEXTURE_BUILDING1) + 1)
unsigned TextureFromName (char* name);
unsigned TextureId (int id);
void TextureInit (void);
void TextureTerm (void);
unsigned TextureRandomBuilding (int index);
bool TextureReady ();
void TextureReset (void);
void TextureUpdate (void);
#define SEGMENTS_PER_TEXTURE 64
#define ONE_SEGMENT (1.0f / SEGMENTS_PER_TEXTURE)
#define LANES_PER_TEXTURE 8
#define LANE_SIZE (1.0f / LANES_PER_TEXTURE)
#define LANE_PIXELS (_size / LANES_PER_TEXTURE)
#define TRIM_RESOLUTION 256
#define TRIM_ROWS 4
#define TRIM_SIZE (1.0f / TRIM_ROWS)
#define TRIM_PIXELS (TRIM_RESOLUTION / TRIM_ROWS)
#define LOGO_RESOLUTION 512
#define LOGO_ROWS 16
#define LOGO_SIZE (1.0f / LOGO_ROWS)
#define LOGO_PIXELS (LOGO_RESOLUTION / LOGO_ROWS)
enum
{
TEXTURE_LIGHT,
TEXTURE_SOFT_CIRCLE,
TEXTURE_SKY,
TEXTURE_LOGOS,
TEXTURE_TRIM,
TEXTURE_BLOOM,
TEXTURE_HEADLIGHT,
TEXTURE_LATTICE,
TEXTURE_BUILDING1,
TEXTURE_BUILDING2,
TEXTURE_BUILDING3,
TEXTURE_BUILDING4,
TEXTURE_BUILDING5,
TEXTURE_BUILDING6,
TEXTURE_BUILDING7,
TEXTURE_BUILDING8,
TEXTURE_BUILDING9,
TEXTURE_COUNT,
};
#define BUILDING_COUNT ((TEXTURE_BUILDING9 - TEXTURE_BUILDING1) + 1)
unsigned TextureFromName (char* name);
unsigned TextureId (int id);
void TextureInit (void);
void TextureTerm (void);
unsigned TextureRandomBuilding (int index);
bool TextureReady ();
void TextureReset (void);
void TextureUpdate (void);

View File

@ -1,132 +1,131 @@
/*-----------------------------------------------------------------------------
Visible.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This module runs the visibility grid, a 2-dimensional array that aids in
culling objects during rendering.
There are many ways this could be refined or sped up, although tests indicate
it's not a huge drain on performance.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <math.h>
#include "glTypes.h"
#include "camera.h"
#include "macro.h"
#include "math.h"
#include "visible.h"
#include "world.h"
#include "win.h"
static bool vis_grid[GRID_SIZE][GRID_SIZE];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool Visible (GLvector pos)
{
return vis_grid[WORLD_TO_GRID(pos.x)][WORLD_TO_GRID(pos.z)];
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool Visible (int x, int z)
{
return vis_grid[x][z];
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void VisibleUpdate (void)
{
GLvector angle;
GLvector position;
int x, y, grid_x, grid_z;
int left, right, front, back;
float angle_to;
float angle_diff;
float target_x, target_z;
//Clear the visibility table
ZeroMemory (vis_grid, sizeof (vis_grid));
//Calculate which cell the camera is in
angle = CameraAngle ();
position = CameraPosition ();
grid_x = WORLD_TO_GRID(position.x);
grid_z = WORLD_TO_GRID(position.z);
//Cells directly adjactent to the camera might technically fall out of the fov,
//but still have a few objects poking into screenspace when looking up or down.
//Rather than obsess over sorting those objects properly, it's more efficient to
//just mark them visible.
left = right = front = back = 3;
//Looking north, can't see south.
if (angle.y < 45.0f || angle.y > 315.0f)
front = 0;
//Looking south, can't see north
if (angle.y > 135.0f && angle.y < 225.0f)
back = 0;
//Looking east, can't see west
if (angle.y > 45.0f && angle.y < 135.0f)
left = 0;
//Looking west, can't see east
if (angle.y > 225.0f && angle.y < 315.0f)
right = 0;
//Now mark the block around us the might be visible
for (x = grid_x - left; x <= grid_x + right; x++) {
if (x < 0 || x >= GRID_SIZE) //just in case the camera leaves the world map
continue;
for (y = grid_z - back; y <= grid_z + front; y++) {
if (y < 0 || y >= GRID_SIZE) //just in case the camera leaves the world map
continue;
vis_grid[x][y] = true;
}
}
//Doesn't matter where we are facing, objects in current cell are always visible
vis_grid[grid_x][grid_z] = true;
//Here, we look at the angle from the current camera position to the cell
//on the grid, and how much that angle deviates from the current view angle.
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
//if we marked it visible earlier, skip all this math
if (vis_grid[x][y])
continue;
//if the camera is to the left of this cell, use the left edge
if (grid_x < x)
target_x = (float)x * GRID_RESOLUTION;
else
target_x = (float)(x + 1) * GRID_RESOLUTION;
if (grid_z < y)
target_z = (float)y * GRID_RESOLUTION;
else
target_z = (float)(y + 1) * GRID_RESOLUTION;
angle_to = 180 - MathAngle (target_x, target_z, position.x, position.z);
//Store how many degrees the cell is to the
angle_diff = (float)fabs (MathAngleDifference (angle.y, angle_to));
vis_grid[x][y] = angle_diff < 45;
}
}
}
/*-----------------------------------------------------------------------------
Visible.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This module runs the visibility grid, a 2-dimensional array that aids in
culling objects during rendering.
There are many ways this could be refined or sped up, although tests indicate
it's not a huge drain on performance.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <math.h>
#include "glTypes.h"
#include "camera.h"
#include "macro.h"
#include "math.h"
#include "visible.h"
#include "world.h"
#include "win.h"
static bool vis_grid[GRID_SIZE][GRID_SIZE];
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool Visible (GLvector pos)
{
return vis_grid[WORLD_TO_GRID(pos.x)][WORLD_TO_GRID(pos.z)];
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool Visible (int x, int z)
{
return vis_grid[x][z];
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void VisibleUpdate (void)
{
GLvector angle;
GLvector position;
int x, y, grid_x, grid_z;
int left, right, front, back;
float angle_to;
float angle_diff;
float target_x, target_z;
//Clear the visibility table
ZeroMemory (vis_grid, sizeof (vis_grid));
//Calculate which cell the camera is in
angle = CameraAngle ();
position = CameraPosition ();
grid_x = WORLD_TO_GRID(position.x);
grid_z = WORLD_TO_GRID(position.z);
//Cells directly adjactent to the camera might technically fall out of the fov,
//but still have a few objects poking into screenspace when looking up or down.
//Rather than obsess over sorting those objects properly, it's more efficient to
//just mark them visible.
left = right = front = back = 3;
//Looking north, can't see south.
if (angle.y < 45.0f || angle.y > 315.0f)
front = 0;
//Looking south, can't see north
if (angle.y > 135.0f && angle.y < 225.0f)
back = 0;
//Looking east, can't see west
if (angle.y > 45.0f && angle.y < 135.0f)
left = 0;
//Looking west, can't see east
if (angle.y > 225.0f && angle.y < 315.0f)
right = 0;
//Now mark the block around us the might be visible
for (x = grid_x - left; x <= grid_x + right; x++) {
if (x < 0 || x >= GRID_SIZE) //just in case the camera leaves the world map
continue;
for (y = grid_z - back; y <= grid_z + front; y++) {
if (y < 0 || y >= GRID_SIZE) //just in case the camera leaves the world map
continue;
vis_grid[x][y] = true;
}
}
//Doesn't matter where we are facing, objects in current cell are always visible
vis_grid[grid_x][grid_z] = true;
//Here, we look at the angle from the current camera position to the cell
//on the grid, and how much that angle deviates from the current view angle.
for (x = 0; x < GRID_SIZE; x++) {
for (y = 0; y < GRID_SIZE; y++) {
//if we marked it visible earlier, skip all this math
if (vis_grid[x][y])
continue;
//if the camera is to the left of this cell, use the left edge
if (grid_x < x)
target_x = (float)x * GRID_RESOLUTION;
else
target_x = (float)(x + 1) * GRID_RESOLUTION;
if (grid_z < y)
target_z = (float)y * GRID_RESOLUTION;
else
target_z = (float)(y + 1) * GRID_RESOLUTION;
angle_to = 180 - MathAngle (target_x, target_z, position.x, position.z);
//Store how many degrees the cell is to the
angle_diff = (float)fabs (MathAngleDifference (angle.y, angle_to));
vis_grid[x][y] = angle_diff < 45;
}
}
}

View File

@ -1,11 +1,11 @@
#define GRID_RESOLUTION 32
#define GRID_CELL (GRID_RESOLUTION / 2)
#define GRID_SIZE (WORLD_SIZE / GRID_RESOLUTION)
#define WORLD_TO_GRID(x) (int)(x / GRID_RESOLUTION)
#define GRID_TO_WORLD(x) ((float)x * GRID_RESOLUTION)
void VisibleUpdate (void);
bool Visible (GLvector pos);
bool Visible (int x, int z);
#define GRID_RESOLUTION 32
#define GRID_CELL (GRID_RESOLUTION / 2)
#define GRID_SIZE (WORLD_SIZE / GRID_RESOLUTION)
#define WORLD_TO_GRID(x) (int)(x / GRID_RESOLUTION)
#define GRID_TO_WORLD(x) ((float)x * GRID_RESOLUTION)
void VisibleUpdate (void);
bool Visible (GLvector pos);
bool Visible (int x, int z);

966
win.cpp
View File

@ -1,483 +1,483 @@
/*-----------------------------------------------------------------------------
Win.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Create the main window and make it go.
-----------------------------------------------------------------------------*/
#define MOUSE_MOVEMENT 0.5f
#include <windows.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <scrnsave.h>
#include "camera.h"
#include "car.h"
#include "entity.h"
#include "glTypes.h"
#include "ini.h"
#include "macro.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "win.h"
#include "world.h"
#include "visible.h"
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "winmm.lib")
#pragma comment (lib, "glu32.lib")
#if SCREENSAVER
#pragma comment (lib, "scrnsave.lib")
#endif
static HWND hwnd;
static HINSTANCE module;
static int width;
static int height;
static int half_width;
static int half_height;
static bool lmb;
static bool rmb;
static bool mouse_forced;
static POINT select_pos;
static POINT mouse_pos;
static bool quit;
static HINSTANCE instance;
LONG WINAPI ScreenSaverProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void CenterCursor ()
{
int center_x;
int center_y;
RECT rect;
SetCursor (NULL);
mouse_forced = true;
GetWindowRect (hwnd, &rect);
center_x = rect.left + (rect.right - rect.left) / 2;
center_y = rect.top + (rect.bottom - rect.top) / 2;
SetCursorPos (center_x, center_y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void MoveCursor (int x, int y)
{
int center_x;
int center_y;
RECT rect;
SetCursor (NULL);
mouse_forced = true;
GetWindowRect (hwnd, &rect);
center_x = rect.left + x;
center_y = rect.top + y;
SetCursorPos (center_x, center_y);
}
/*-----------------------------------------------------------------------------
n o t e
-----------------------------------------------------------------------------*/
void WinPopup (char* message, ...)
{
va_list marker;
char buf[1024];
va_start (marker, message);
vsprintf (buf, message, marker);
va_end (marker);
MessageBox (NULL, buf, APP_TITLE, MB_ICONSTOP | MB_OK |
MB_TASKMODAL);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int WinWidth (void)
{
return width;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void WinMousePosition (int* x, int* y)
{
*x = select_pos.x;
*y = select_pos.y;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int WinHeight (void)
{
return height;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void WinTerm (void)
{
#if !SCREENAVER
DestroyWindow (hwnd);
#endif
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
HWND WinHwnd (void)
{
return hwnd;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppQuit ()
{
quit = true;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppUpdate ()
{
CameraUpdate ();
EntityUpdate ();
WorldUpdate ();
TextureUpdate ();
VisibleUpdate ();
CarUpdate ();
RenderUpdate ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppInit (void)
{
RandomInit (time (NULL));
CameraInit ();
RenderInit ();
TextureInit ();
WorldInit ();
}
/*-----------------------------------------------------------------------------
W i n M a i n
-----------------------------------------------------------------------------*/
void AppTerm (void)
{
TextureTerm ();
WorldTerm ();
RenderTerm ();
CameraTerm ();
WinTerm ();
}
/*-----------------------------------------------------------------------------
W i n M a i n
-----------------------------------------------------------------------------*/
#if !SCREENSAVER
int PASCAL WinMain (HINSTANCE instance_in, HINSTANCE previous_instance,
LPSTR command_line, int show_style)
{
MSG msg;
instance = instance_in;
WinInit ();
AppInit ();
while (!quit) {
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
quit = true;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} else
AppUpdate ();
}
AppTerm ();
return 0;
}
#else
BOOL WINAPI ScreenSaverConfigureDialog (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { return FALSE; }
BOOL WINAPI RegisterDialogClasses(HANDLE hInst) { return TRUE; }
#endif
LONG WINAPI ScreenSaverProc(HWND hwnd_in,UINT message,WPARAM wparam,LPARAM lparam)
{
RECT r;
int key;
float delta_x, delta_y;
POINT p;
// Handles screen saver messages
switch(message)
{
case WM_SIZE:
width = LOWORD(lparam); // width of client area
height = HIWORD(lparam); // height of client area
if (wparam == SIZE_MAXIMIZED) {
IniIntSet ("WindowMaximized", 1);
} else {
IniIntSet ("WindowWidth", width);
IniIntSet ("WindowHeight", height);
IniIntSet ("WindowMaximized", 0);
}
RenderResize ();
break;
case WM_KEYDOWN:
key = (int) wparam;
if (key == 'R')
WorldReset ();
else if (key == 'W')
RenderWireframeToggle ();
else if (key == 'E')
RenderEffectCycle ();
else if (key == 'L')
RenderLetterboxToggle ();
else if (key == 'F')
RenderFPSToggle ();
else if (key == 'G')
RenderFogToggle ();
else if (key == 'T')
RenderFlatToggle ();
else if (key == VK_F1)
RenderHelpToggle ();
else if (key == VK_ESCAPE)
break;
else if (!SCREENSAVER) {
//Dev mode keys
if (key == 'C')
CameraAutoToggle ();
if (key == 'B')
CameraNextBehavior ();
if (key == VK_F5)
CameraReset ();
if (key == VK_UP)
CameraMedial (1.0f);
if (key == VK_DOWN)
CameraMedial (-1.0f);
if (key == VK_LEFT)
CameraLateral (1.0f);
if (key == VK_RIGHT)
CameraLateral (-1.0f);
if (key == VK_PRIOR)
CameraVertical (1.0f);
if (key == VK_NEXT)
CameraVertical (-1.0f);
if (key == VK_F5)
CameraReset ();
return 0;
} else
break;
return 0;
case WM_MOVE:
GetClientRect (hwnd, &r);
height = r.bottom - r.top;
width = r.right - r.left;
IniIntSet ("WindowX", r.left);
IniIntSet ("WindowY", r.top);
IniIntSet ("WindowWidth", width);
IniIntSet ("WindowHeight", height);
half_width = width / 2;
half_height = height / 2;
return 0;
case WM_LBUTTONDOWN:
lmb = true;
SetCapture (hwnd);
break;
case WM_RBUTTONDOWN:
rmb = true;
SetCapture (hwnd);
break;
case WM_LBUTTONUP:
lmb = false;
if (!rmb) {
ReleaseCapture ();
MoveCursor (select_pos.x, select_pos.y);
}
break;
case WM_RBUTTONUP:
rmb = false;
if (!lmb) {
ReleaseCapture ();
MoveCursor (select_pos.x, select_pos.y);
}
break;
case WM_MOUSEMOVE:
p.x = LOWORD(lparam); // horizontal position of cursor
p.y = HIWORD(lparam); // vertical position of cursor
if (p.x < 0 || p.x > width)
break;
if (p.y < 0 || p.y > height)
break;
if (!mouse_forced && !lmb && !rmb) {
select_pos = p;
}
if (mouse_forced) {
mouse_forced = false;
} else if (rmb || lmb) {
CenterCursor ();
delta_x = (float)(mouse_pos.x - p.x) * MOUSE_MOVEMENT;
delta_y = (float)(mouse_pos.y - p.y) * MOUSE_MOVEMENT;
if (rmb && lmb) {
GLvector pos;
CameraPan (delta_x);
pos = CameraPosition ();
pos.y += delta_y;
CameraPositionSet (pos);
} else if (rmb) {
CameraPan (delta_x);
CameraForward (delta_y);
} else if (lmb) {
GLvector angle;
angle = CameraAngle ();
angle.y -= delta_x;
angle.x += delta_y;
CameraAngleSet (angle);
}
}
mouse_pos = p;
break;
case WM_CREATE:
hwnd = hwnd_in;
if (SCREENSAVER)
AppInit ();
SetTimer (hwnd, 1, 7, NULL);
return 0;
case WM_TIMER:
AppUpdate ();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
#if SCREENSAVER
return DefScreenSaverProc(hwnd_in,message,wparam,lparam);
#else
return DefWindowProc (hwnd_in,message,wparam,lparam);
#endif
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool WinInit (void)
{
WNDCLASSEX wcex;
int x, y;
int style;
bool max;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)ScreenSaverProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = instance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = APP_TITLE;
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex)) {
WinPopup ("Cannot create window class");
return false;
}
x = IniInt ("WindowX");
y = IniInt ("WindowY");
style = WS_TILEDWINDOW;
style |= WS_MAXIMIZE;
width = IniInt ("WindowWidth");
height = IniInt ("WindowHeight");
width = CLAMP (width, 800, 2048);
height = CLAMP (height, 600, 2048);
half_width = width / 2;
half_height = height / 2;
max = IniInt ("WindowMaximized") == 1;
if (!(hwnd = CreateWindowEx (0, APP_TITLE, APP_TITLE, style,
CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, instance, NULL))) {
WinPopup ("Cannot create window");
return false;
}
if (max)
ShowWindow (hwnd, SW_MAXIMIZE);
else
ShowWindow (hwnd, SW_SHOW);
UpdateWindow (hwnd);
return true;
}
/*-----------------------------------------------------------------------------
Win.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
Create the main window and make it go.
-----------------------------------------------------------------------------*/
#define MOUSE_MOVEMENT 0.5f
#include <windows.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <scrnsave.h>
#include "camera.h"
#include "car.h"
#include "entity.h"
#include "glTypes.h"
#include "ini.h"
#include "macro.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "win.h"
#include "world.h"
#include "visible.h"
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "winmm.lib")
#pragma comment (lib, "glu32.lib")
#if SCREENSAVER
#pragma comment (lib, "scrnsave.lib")
#endif
static HWND hwnd;
static HINSTANCE module;
static int width;
static int height;
static int half_width;
static int half_height;
static bool lmb;
static bool rmb;
static bool mouse_forced;
static POINT select_pos;
static POINT mouse_pos;
static bool quit;
static HINSTANCE instance;
LONG WINAPI ScreenSaverProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void CenterCursor ()
{
int center_x;
int center_y;
RECT rect;
SetCursor (NULL);
mouse_forced = true;
GetWindowRect (hwnd, &rect);
center_x = rect.left + (rect.right - rect.left) / 2;
center_y = rect.top + (rect.bottom - rect.top) / 2;
SetCursorPos (center_x, center_y);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
static void MoveCursor (int x, int y)
{
int center_x;
int center_y;
RECT rect;
SetCursor (NULL);
mouse_forced = true;
GetWindowRect (hwnd, &rect);
center_x = rect.left + x;
center_y = rect.top + y;
SetCursorPos (center_x, center_y);
}
/*-----------------------------------------------------------------------------
n o t e
-----------------------------------------------------------------------------*/
void WinPopup (char* message, ...)
{
va_list marker;
char buf[1024];
va_start (marker, message);
vsprintf (buf, message, marker);
va_end (marker);
MessageBox (NULL, buf, APP_TITLE, MB_ICONSTOP | MB_OK |
MB_TASKMODAL);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int WinWidth (void)
{
return width;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void WinMousePosition (int* x, int* y)
{
*x = select_pos.x;
*y = select_pos.y;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int WinHeight (void)
{
return height;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void WinTerm (void)
{
#if !SCREENAVER
DestroyWindow (hwnd);
#endif
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
HWND WinHwnd (void)
{
return hwnd;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppQuit ()
{
quit = true;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppUpdate ()
{
CameraUpdate ();
EntityUpdate ();
WorldUpdate ();
TextureUpdate ();
VisibleUpdate ();
CarUpdate ();
RenderUpdate ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void AppInit (void)
{
RandomInit (time (NULL));
CameraInit ();
RenderInit ();
TextureInit ();
WorldInit ();
}
/*-----------------------------------------------------------------------------
W i n M a i n
-----------------------------------------------------------------------------*/
void AppTerm (void)
{
TextureTerm ();
WorldTerm ();
RenderTerm ();
CameraTerm ();
WinTerm ();
}
/*-----------------------------------------------------------------------------
W i n M a i n
-----------------------------------------------------------------------------*/
#if !SCREENSAVER
int PASCAL WinMain (HINSTANCE instance_in, HINSTANCE previous_instance,
LPSTR command_line, int show_style)
{
MSG msg;
instance = instance_in;
WinInit ();
AppInit ();
while (!quit) {
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
quit = true;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} else
AppUpdate ();
}
AppTerm ();
return 0;
}
#else
BOOL WINAPI ScreenSaverConfigureDialog (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) { return FALSE; }
BOOL WINAPI RegisterDialogClasses(HANDLE hInst) { return TRUE; }
#endif
LONG WINAPI ScreenSaverProc(HWND hwnd_in,UINT message,WPARAM wparam,LPARAM lparam)
{
RECT r;
int key;
float delta_x, delta_y;
POINT p;
// Handles screen saver messages
switch(message)
{
case WM_SIZE:
width = LOWORD(lparam); // width of client area
height = HIWORD(lparam); // height of client area
if (wparam == SIZE_MAXIMIZED) {
IniIntSet ("WindowMaximized", 1);
} else {
IniIntSet ("WindowWidth", width);
IniIntSet ("WindowHeight", height);
IniIntSet ("WindowMaximized", 0);
}
RenderResize ();
break;
case WM_KEYDOWN:
key = (int) wparam;
if (key == 'R')
WorldReset ();
else if (key == 'W')
RenderWireframeToggle ();
else if (key == 'E')
RenderEffectCycle ();
else if (key == 'L')
RenderLetterboxToggle ();
else if (key == 'F')
RenderFPSToggle ();
else if (key == 'G')
RenderFogToggle ();
else if (key == 'T')
RenderFlatToggle ();
else if (key == VK_F1)
RenderHelpToggle ();
else if (key == VK_ESCAPE)
break;
else if (!SCREENSAVER) {
//Dev mode keys
if (key == 'C')
CameraAutoToggle ();
if (key == 'B')
CameraNextBehavior ();
if (key == VK_F5)
CameraReset ();
if (key == VK_UP)
CameraMedial (1.0f);
if (key == VK_DOWN)
CameraMedial (-1.0f);
if (key == VK_LEFT)
CameraLateral (1.0f);
if (key == VK_RIGHT)
CameraLateral (-1.0f);
if (key == VK_PRIOR)
CameraVertical (1.0f);
if (key == VK_NEXT)
CameraVertical (-1.0f);
if (key == VK_F5)
CameraReset ();
return 0;
} else
break;
return 0;
case WM_MOVE:
GetClientRect (hwnd, &r);
height = r.bottom - r.top;
width = r.right - r.left;
IniIntSet ("WindowX", r.left);
IniIntSet ("WindowY", r.top);
IniIntSet ("WindowWidth", width);
IniIntSet ("WindowHeight", height);
half_width = width / 2;
half_height = height / 2;
return 0;
case WM_LBUTTONDOWN:
lmb = true;
SetCapture (hwnd);
break;
case WM_RBUTTONDOWN:
rmb = true;
SetCapture (hwnd);
break;
case WM_LBUTTONUP:
lmb = false;
if (!rmb) {
ReleaseCapture ();
MoveCursor (select_pos.x, select_pos.y);
}
break;
case WM_RBUTTONUP:
rmb = false;
if (!lmb) {
ReleaseCapture ();
MoveCursor (select_pos.x, select_pos.y);
}
break;
case WM_MOUSEMOVE:
p.x = LOWORD(lparam); // horizontal position of cursor
p.y = HIWORD(lparam); // vertical position of cursor
if (p.x < 0 || p.x > width)
break;
if (p.y < 0 || p.y > height)
break;
if (!mouse_forced && !lmb && !rmb) {
select_pos = p;
}
if (mouse_forced) {
mouse_forced = false;
} else if (rmb || lmb) {
CenterCursor ();
delta_x = (float)(mouse_pos.x - p.x) * MOUSE_MOVEMENT;
delta_y = (float)(mouse_pos.y - p.y) * MOUSE_MOVEMENT;
if (rmb && lmb) {
GLvector pos;
CameraPan (delta_x);
pos = CameraPosition ();
pos.y += delta_y;
CameraPositionSet (pos);
} else if (rmb) {
CameraPan (delta_x);
CameraForward (delta_y);
} else if (lmb) {
GLvector angle;
angle = CameraAngle ();
angle.y -= delta_x;
angle.x += delta_y;
CameraAngleSet (angle);
}
}
mouse_pos = p;
break;
case WM_CREATE:
hwnd = hwnd_in;
if (SCREENSAVER)
AppInit ();
SetTimer (hwnd, 1, 7, NULL);
return 0;
case WM_TIMER:
AppUpdate ();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
#if SCREENSAVER
return DefScreenSaverProc(hwnd_in,message,wparam,lparam);
#else
return DefWindowProc (hwnd_in,message,wparam,lparam);
#endif
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool WinInit (void)
{
WNDCLASSEX wcex;
int x, y;
int style;
bool max;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)ScreenSaverProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = instance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = APP_TITLE;
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex)) {
WinPopup ("Cannot create window class");
return false;
}
x = IniInt ("WindowX");
y = IniInt ("WindowY");
style = WS_TILEDWINDOW;
style |= WS_MAXIMIZE;
width = IniInt ("WindowWidth");
height = IniInt ("WindowHeight");
width = CLAMP (width, 800, 2048);
height = CLAMP (height, 600, 2048);
half_width = width / 2;
half_height = height / 2;
max = IniInt ("WindowMaximized") == 1;
if (!(hwnd = CreateWindowEx (0, APP_TITLE, APP_TITLE, style,
CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, instance, NULL))) {
WinPopup ("Cannot create window");
return false;
}
if (max)
ShowWindow (hwnd, SW_MAXIMIZE);
else
ShowWindow (hwnd, SW_SHOW);
UpdateWindow (hwnd);
return true;
}

100
win.h
View File

@ -1,50 +1,50 @@
//Versioning info
#define APP_TITLE "PixelCity"
#define APP "pixelcity"
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_REVISION 10
//Best to disable screensaver mode when working on the program.
#define SCREENSAVER 0
//Do we hide scene building behing a loading screen or show it?
#define LOADING_SCREEN 1
//Controls the density of cars.
#define CARS 500
//The "dead zone" along the edge of the world, with super-low detail.
#define WORLD_EDGE 200
//How often to rebuild the city
#define RESET_INTERVAL (SCREENSAVER ? 120000 : 999999)//milliseconds
//How long the screen fade takes when transitioning to a new city
#define FADE_TIME (SCREENSAVER ? 1500 : 1) //milliseconds
//Debug ground texture that shows traffic lanes
#define SHOW_DEBUG_GROUND 0
//Controls the ammount of space available for buildings.
//Other code is wrtten assuming this will be a power of two.
#define WORLD_SIZE 1024
#define WORLD_HALF (WORLD_SIZE / 2)
//Bitflags used to track how world space is being used.
#define CLAIM_ROAD 1
#define CLAIM_WALK 2
#define CLAIM_BUILDING 4
#define MAP_ROAD_NORTH 8
#define MAP_ROAD_SOUTH 16
#define MAP_ROAD_EAST 32
#define MAP_ROAD_WEST 64
//Random SATURATED color
#define RANDOM_COLOR (glRgbaFromHsl ((float)RandomVal (255)/255,1.0f, 0.75f))
//Used in orienting roads and cars
enum
{
NORTH,
EAST,
SOUTH,
WEST
};
HWND WinHwnd (void);
void WinPopup (char* message, ...);
void WinTerm (void);
bool WinInit (void);
int WinWidth (void);
int WinHeight (void);
void WinMousePosition (int* x, int* y);
//Versioning info
#define APP_TITLE "PixelCity"
#define APP "pixelcity"
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_REVISION 10
//Best to disable screensaver mode when working on the program.
#define SCREENSAVER 0
//Do we hide scene building behing a loading screen or show it?
#define LOADING_SCREEN 1
//Controls the density of cars.
#define CARS 500
//The "dead zone" along the edge of the world, with super-low detail.
#define WORLD_EDGE 200
//How often to rebuild the city
#define RESET_INTERVAL (SCREENSAVER ? 120000 : 999999)//milliseconds
//How long the screen fade takes when transitioning to a new city
#define FADE_TIME (SCREENSAVER ? 1500 : 1) //milliseconds
//Debug ground texture that shows traffic lanes
#define SHOW_DEBUG_GROUND 0
//Controls the ammount of space available for buildings.
//Other code is wrtten assuming this will be a power of two.
#define WORLD_SIZE 1024
#define WORLD_HALF (WORLD_SIZE / 2)
//Bitflags used to track how world space is being used.
#define CLAIM_ROAD 1
#define CLAIM_WALK 2
#define CLAIM_BUILDING 4
#define MAP_ROAD_NORTH 8
#define MAP_ROAD_SOUTH 16
#define MAP_ROAD_EAST 32
#define MAP_ROAD_WEST 64
//Random SATURATED color
#define RANDOM_COLOR (glRgbaFromHsl ((float)RandomVal (255)/255,1.0f, 0.75f))
//Used in orienting roads and cars
enum
{
NORTH,
EAST,
SOUTH,
WEST
};
HWND WinHwnd (void);
void WinPopup (char* message, ...);
void WinTerm (void);
bool WinInit (void);
int WinWidth (void);
int WinHeight (void);
void WinMousePosition (int* x, int* y);

1578
world.cpp

File diff suppressed because it is too large Load Diff

32
world.h
View File

@ -1,16 +1,16 @@
GLrgba WorldBloomColor ();
char WorldCell (int x, int y);
GLrgba WorldLightColor (unsigned index);
int WorldLogoIndex ();
GLbbox WorldHotZone ();
void WorldInit (void);
float WorldFade (void);
void WorldRender ();
void WorldReset (void);
int WorldSceneBegin ();
int WorldSceneElapsed ();
void WorldTerm (void);
void WorldUpdate (void);
GLrgba WorldBloomColor ();
char WorldCell (int x, int y);
GLrgba WorldLightColor (unsigned index);
int WorldLogoIndex ();
GLbbox WorldHotZone ();
void WorldInit (void);
float WorldFade (void);
void WorldRender ();
void WorldReset (void);
int WorldSceneBegin ();
int WorldSceneElapsed ();
void WorldTerm (void);
void WorldUpdate (void);