Category : Files from Magazines
Archive   : DDJ9407C.ZIP
Filename : RAYOBJ.H
/*** (C) Copyright A.MANGEN 1994 ***/
/***********************************************************************/
/*** PROJECT : RAY TRACING PROGRAM ***/
/*** PROGRAM NAME: RAYOBJ.H ***/
/*** VERSION : 1.1 ***/
/***-----------------------------------------------------------------***/
/*** DESCRIPTION : Ray Tracing Objects - Include File ***/
/***********************************************************************/
#include
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
typedef float Coord; // Point Coordinate
/***********************************************************************/
/*** Color : Base class for color ***/
/*** The overloaded operators are declared inline in order ***/
/*** to optimize speed ***/
/***********************************************************************/
typedef float Col; // Color Component
class Color
{
public:
Col R,G,B; // Red,Green,Blue Colors
Color(Col r=0,Col g=0,Col b=0) // Inline Constructor
{ R=r; G=g; B=b; }
Color operator +(Color & C) // Overloaded + operator
{ return Color(R+C.R,G+C.G,B+C.B); }
Color operator *(Col f) // Overloaded * operator
{ return Color(f*R,f*G,f*B); }
Col GrayScale() // GrayScale function
{ Col W=0.39*R+0.50*G+0.11*B;
return min(255,W);
}
Col Associate(int Spread,int Search); // Associate function
};
/***********************************************************************/
/*** Vector : Base class for vector ***/
/*** The overloaded operators are declared inline in order ***/
/*** to optimize speed ***/
/***********************************************************************/
class Vector
{
public:
Coord X,Y,Z,dX,dY,dZ; // Vector Direction
Vector(Coord x=0,Coord y=0,Coord z=0, // Inline Constructor
Coord dx=0,Coord dy=0,Coord dz=0)
{ X=x; Y=y; Z=z; dX=dx; dY=dy; dZ=dz; }
Vector operator *(Coord f) // Overloaded * : fV
{ return Vector(X,Y,Z,f*dX,f*dY,f*dZ); }
Vector operator ^(Coord f) // Overloaded ^ : V(t)
{ Coord N=sqrt(dX*dX+dY*dY+dZ*dZ);
return Vector(X+f*dX/N,Y+f*dY/N,Z+f*dZ/N,dX,dY,dZ); }
Coord operator *(Vector V) // Overloaded * : Scalar product
{ return(dX*V.dX+dY*V.dY+dZ*V.dZ); }
Vector operator +(Vector V) // Overloaded + : Vector addition
{ return Vector(X,Y,Z,dX+V.dX,dY+V.dY,dZ+V.dZ); }
Vector operator -(Vector V) // Overloaded - : Vector substraction
{ return Vector(X,Y,Z,dX-V.dX,dY-V.dY,dZ-V.dZ); }
Vector operator !() // Overloaded ! : Norm
{ Coord N=sqrt(dX*dX+dY*dY+dZ*dZ);
return Vector(X,Y,Z,dX/N,dY/N,dZ/N);
}
Vector operator -() // Overloaded - : Unary -
{ return Vector(X,Y,Z,-dX,-dY,-dZ); }
Coord Length() // Length of vector
{ return sqrt(dX*dX+dY*dY+dZ*dZ); }
};
/***********************************************************************/
/*** Point : Generic base class with linked list ***/
/***********************************************************************/
#define NO_INTERSECTION 1e99
#define DEFAULT_RD 0.9 // Diffuse reflection factor
#define DEFAULT_RS 0.0 // Specular reflection factor
#define DEFAULT_RR 0.0 // Reflection factor
#define DEFAULT_MAXREF 0 // Maximum number of reflections
class Point
{
protected:
Point *Prev,*Next; // Integrated linked list
Coord X,Y,Z; // Point Coordinates
public:
Color C; // Point Color
Col Rd; // Diffuse reflection factor
Col Rs; // Specular reflection factor
Col Rr; // Reflection factor
int MaxRef; // Maximum number of reflections
int MakeShadow; // Gives shadow or not
Point(Coord x,Coord y,Coord z, // Constructor
Color c,
Col rd=DEFAULT_RD,
Col rs=DEFAULT_RS,
Col rr=DEFAULT_RR,
int maxref=DEFAULT_MAXREF,
int makeshadow=TRUE);
~Point(); // Destructor
Point *GetNext() // Get Next Point in linked list
{ return Next; };
virtual Coord Intersect(Vector V) = 0; // Intersection method - designed
// to be overridden by descendants
virtual Vector GetNormal(Vector P) = 0; // Get normal
};
/***********************************************************************/
/*** Sphere : Inherited Point for spheres ***/
/***********************************************************************/
class Sphere:public Point
{
Coord R; // Radius of the sphere
public:
Sphere(Coord x,Coord y,Coord z, // Inline Constructor
Coord r,Color c,
Col rd=DEFAULT_RD,
Col rs=DEFAULT_RS,
Col rr=DEFAULT_RR,
int maxref=DEFAULT_MAXREF,
int makeshadow=TRUE)
:Point(x,y,z,c,rd,rs,rr,maxref,makeshadow) { R=r; };
virtual Coord Intersect(Vector V); // Intersection method
virtual Vector GetNormal(Vector P) // Get normal
{ return Vector(P.X,P.Y,P.Z,P.X-X,P.Y-Y,P.Z-Z); }
};
/***********************************************************************/
/*** Plane : Inherited Point for planes ***/
/***********************************************************************/
class Plane:public Point
{
Coord D; // Position of the plane
public:
Plane(Coord x,Coord y,Coord z, // Inline Constructor
Coord d,Color c,
Col rd=DEFAULT_RD,
Col rs=DEFAULT_RS,
Col rr=DEFAULT_RR,
int maxref=DEFAULT_MAXREF,
int makeshadow=TRUE)
:Point(x,y,z,c,rd,rs,rr,maxref,makeshadow) { D=d; };
virtual Coord Intersect(Vector V); // Intersection method
virtual Vector GetNormal(Vector P) // Get normal
{ return Vector(P.X,P.Y,P.Z,X,Y,Z); }
};
/***********************************************************************/
/*** Scene : Scene object ***/
/***********************************************************************/
const MAX_LIGHT=10;
class Scene
{ Coord CameraX,CameraY,CameraZ; // Position of the camera
Color Ambient; // Ambient luminosity
int NLight; // Number of lights
Vector LightV[MAX_LIGHT]; // Lights orientation
Color LightColor[MAX_LIGHT]; // Lights color
char ScreenLine[VESA_Max_Width]; // Screen buffer
char TGALine[3*VESA_Max_Width]; // TARGA File buffer
public:
Scene(Coord camerax,Coord cameray,Coord cameraz,Color ambient);
int AddLight(Vector V,Color I);
Coord ClosestIntersect(Vector V,
Point **ObjectMin,int ShouldMakeShadow=FALSE);
void Ray(int N,Vector V,Coord f,Color & I);
void RayTrace(int Mode,int GetMaxX,int GetMaxY,int BW,
char *FileName=NULL,int Spread=0,int Search=1);
};
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/