home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cutting-Edge 3D Game Programming with C++
/
CE3DC++.ISO
/
BOOK
/
CHAP03
/
EXTRA
/
3DCLASS.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-10-24
|
2KB
|
99 lines
//
//
//
#define PI 3.141592654
#include <Stdio.h>
#define COS(a) ( CosTable [a & (DEGREECOUNT - 1)] )
#define SIN(a) ( SinTable [a & (DEGREECOUNT - 1)] )
const unsigned int DEGREECOUNT = 1024;
extern double CosTable [ DEGREECOUNT ];
extern double SinTable [ DEGREECOUNT ];
void InitMath ();
struct Vertex {
double Wx, Wy, Wz; // The world X, Y and Z.
};
struct Vector {
double X, Y, Z, Distance; // X, Y and Z direction.
};
struct ScreenVertex {
long X, Y; //The screen x and y of a point.
double Zp; //The proportional (1/z) z at (x,y) point.
};
// A view matrix class
class Matrix3D {
protected:
void MergeMatrix ( double NewMatrix[4][4] );
double Matrix [ 4 ] [ 4 ];
int Xr, Zr, Yr;
public:
Matrix3D () { InitMath (); Zr = Yr = Zr = 0; }
void Rotate ( int Xa, int Ya, int Za );
void Translate ( double Xt, double Yt, double Zt );
void Scale ( double Xs, double Ys, double Zs );
void Shear ( double Xs, double Ys );
void Initialize ();
Vertex inline &Transform ( Vertex &V );
Vector inline &Transform ( Vector &V );
};
class Triangle {
protected:
void Project ();
void Rasterize ( unsigned char *Buffer );
void CalculateNormal ();
void Backface ();
public:
Vector Normal; // The surface normal
Vertex VPoint [ 3 ]; // A pointer to the triangle's vertices
ScreenVertex SPoint [ 3 ];
unsigned char Visible, Color; // Visible flag and color info
void Transform ( Matrix3D &Matrix ); // The transform function
int Load ( FILE *File ); // A load function for
// ASCII RAW files
void inline Display ( unsigned char *Buffer );
};
class TriangleWorld {
public:
Triangle *World;
unsigned int TriCount;
int Load ( char *FileName );
void Transform ( Matrix3D &Matrix );
void Display ( unsigned char *Buffer );
~TriangleWorld ()
{
if ( World )
delete [] World;
}
};
class PolyEdge {
protected:
long X, Y, StepX, StepY, Numerator, Denominator, ErrorTerm;
long EdgeHeight, Zr;
public:
void inline Step ();
PolyEdge () { }
PolyEdge ( ScreenVertex &P1, ScreenVertex &P2 )
{
Initialize ( P1, P2 );
}
void Initialize ( ScreenVertex &P1, ScreenVertex &P2 );
long Height () { return EdgeHeight; }
long GetX () { return X; }
long GetY () { return Y; }
long GetZr () { return Zr; }
long GetStepY() { return StepY; }
void operator ++ () { Step (); }
void operator ++ (int) { Step (); }
};