C++ Source Code

#include <graphics.h>

#include <conio.h>

#include <iostream.h>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

 

#define VGA_INPUT_STATUS_1 0x3DA

#define VGA_SYNC_MASK 0x08

 

 

void vwait(void)

{

while(inp(VGA_INPUT_STATUS_1) & VGA_SYNC_MASK)

{

}

while(!(inp(VGA_INPUT_STATUS_1) & VGA_SYNC_MASK))

{

}

 

}

 

 

class TGraph {

 

private:

int forecolour;

int backcolour;

int MaxX;

int MaxY;

int xo;

int yo;

int page;

int CurrentX;

int CurrentY;

public:

 

TGraph(int mode,char path[]);

 

int GetMaxX() { return MaxX; }

int GetMaxY() { return MaxY; }

int GetScrCentreX() { return xo; }

int GetScrCentreY() { return yo; }

 

void SetForeColour(int colour) { setcolor(colour);forecolour = colour; }

void SetBackColour(int colour) { setbkcolor(colour);backcolour = colour; }

 

void LineStyle(unsigned upattern) { setlinestyle(4,upattern,1);}

void DrawLine(int x1,int y1 ,int x2, int y2) { line(x1,y1,x2,y2); }

 

void DrawLine(int x1,int y1) { line(CurrentX,CurrentY,x1,y1); CurrentX = x1; CurrentY = y1;}

void MoveTo(int x1,int y1) { CurrentX= x1; CurrentY = y1;}

 

void PutPixel(int x1,int y1) { putpixel(x1,y1,forecolour); }

void ClearScreen() {cleardevice();}

 

};

 

class TMaths {

 

private:

float sin_vals[255];

float cos_vals[255];

 

public:

TMaths();

float Sine(unsigned char s);

float CoSine(unsigned char c);

};

 

class TCoords {

private:

int x;

int y;

public:

TCoords() { x = 0; y = 0; }

TCoords(int xx, int yy) { x = xx; y = yy; }

void DefineCoords(int xx, int yy) { x = xx; y = yy; }

 

int GetX() { return x; }

int GetY() { return y; }

};

 

class T3dCoords {

private:

int x,y,z;

public:

T3dCoords() { x = 0; y = 0; z = 0; }

T3dCoords(int xx, int yy, int zz) { x = xx; y = yy; z = zz; }

void DefineCoords(int xx , int yy , int zz) { x = xx; y = yy; z = zz; }

 

int GetX() { return x; }

int GetY() { return y; }

int GetZ() { return z; }

 

TCoords Convertto2d(float xang, float yang, float zang, int dist, int prp);

 

};

class TFace {

private:

int Vertex1;

int Vertex2;

int Vertex3;

int BorderColour,FaceColour;

public:

TFace() { Vertex1 = 0; Vertex2 = 0; Vertex3 = 0; BorderColour = 0; FaceColour = 0;}

TFace(int vertex1, int vertex2, int vertex3, int BordCol, int FaceCol)

{ Vertex1 = vertex1; Vertex2 = vertex2;

Vertex3 = vertex3; BorderColour = BordCol;

FaceColour = FaceCol;}

void DefineFace(int vertex1, int vertex2, int vertex3, int BordCol, int FaceCol)

{ Vertex1 = vertex1; Vertex2 = vertex2;

Vertex3 = vertex3; BorderColour = BordCol;

FaceColour = FaceCol;}

int GetVertex(int c) { if (c == 0) return Vertex1;

if (c == 1) return Vertex2;

if (c == 2) return Vertex3; }

bool Visible(TCoords *Vertices);

 

};

/*******MAIN******************************/

void main()

{

TGraph tg(VGAMED,"c:\bc5\bgi");

T3dCoords co[8];

co[0].DefineCoords(100,100,100);

co[1].DefineCoords(-100,100,100);

co[2].DefineCoords(-100,-100,100);

co[3].DefineCoords(100,-100,100);

 

co[4].DefineCoords(100,100,-100);

co[5].DefineCoords(-100,100,-100);

co[6].DefineCoords(-100,-100,-100);

co[7].DefineCoords(100,-100,-100);

 

TFace vertices[12];

vertices[0].DefineFace(0,1,2,15,0);

vertices[1].DefineFace(2,3,0,15,0);

 

vertices[2].DefineFace(4,0,3,15,0);

vertices[3].DefineFace(3,7,4,15,0);

 

vertices[4].DefineFace(5,4,7,15,0);

vertices[5].DefineFace(7,6,5,15,0);

 

vertices[6].DefineFace(1,5,6,15,0);

vertices[7].DefineFace(6,2,1,15,0);

 

vertices[8].DefineFace(3,2,6,15,0);

vertices[9].DefineFace(6,7,3,15,0);

 

vertices[10].DefineFace(4,5,1,15,0);

vertices[11].DefineFace(1,0,4,15,0);

 

int numverts = 8;

int numfaces = 12;

int s;

unsigned char xang = 0;

do {

{

int loop = 0 ;

xang +=1;

vwait();

tg.ClearScreen();

char ch;

TCoords *tc;

tc = new TCoords[numverts];

do {

tc[loop] = co[loop].Convertto2d(xang,xang,xang,1000,500);

loop++;

} while (loop < numverts);

loop = 0;

 

do {

{

int loop2 =0;

 

tg.MoveTo(tg.GetScrCentreX() + tc[vertices[loop].GetVertex(loop2)].GetX(),tg.GetScrCentreY() + tc[vertices[loop].GetVertex(loop2)].GetY());

 

if (vertices[loop].Visible(tc) == true) tg.LineStyle(0xffff);

else tg.LineStyle(0x0000);

 

do {

tg.DrawLine(tg.GetScrCentreX() + tc[vertices[loop].GetVertex(loop2)].GetX(),tg.GetScrCentreY() + tc[vertices[loop].GetVertex(loop2)].GetY());

loop2++;

} while (loop2< 3);

loop2 = 0;

tg.DrawLine(tg.GetScrCentreX() + tc[vertices[loop].GetVertex(loop2)].GetX(),tg.GetScrCentreY() + tc[vertices[loop].GetVertex(loop2)].GetY());

 

loop++;

 

 

 

}

} while (loop < numfaces);

delete[] tc;

}

} while (!kbhit());

 

}

/*******End of MAIN***********************/

 

/*******TGraph****************************/

TGraph::TGraph(int mode, char path[])

{

int gd=VGA;

initgraph(&gd,&mode,path);

forecolour = 15;

backcolour = 0;

setcolor(forecolour);

setbkcolor(backcolour);

MaxX = getmaxx();

MaxY = getmaxy();

xo = MaxX / 2;

yo = MaxY / 2;

page = 0;

CurrentX = 0;

CurrentY = 0;

}

/*******End of TGraph*********************/

 

/*******TMaths****************************/

TMaths::TMaths()

{

for (int i=0;i<255;i++)

{

sin_vals[i] = sin(i*(M_PI / 128));

cos_vals[i] = cos(i*(M_PI / 128));

}

}

 

float TMaths::Sine(unsigned char s)

{

if ((s >= 0 ) && (s <= 255)) return sin_vals[s];

return 0;

}

 

float TMaths::CoSine(unsigned char c)

{

if ((c >= 0 ) && (c <= 255)) return cos_vals[c];

return 0;

}

 

/*******End of TMaths*********************/

 

/*******T3dCoords*************************/

 

TCoords T3dCoords::Convertto2d(float xang, float yang, float zang, int dist, int prp)

{

TMaths *tm;

tm = new TMaths;

 

float sxa = tm->Sine(xang);

float sya = tm->Sine(yang);

float sza = tm->Sine(zang);

 

float cxa = tm->CoSine(xang);

float cya = tm->CoSine(yang);

float cza = tm->CoSine(zang);

 

float x1 = (x * cza) - (y * sza);

float y1 = (x * sza) + (y * cza);

float z1 = z;

 

float x2 = x1;

float y2 = (y1 * cxa) - (z1 * sxa);

float z2 = (y1 * sxa) + (z1 * cxa);

 

x1 = (z2 * sya) + (x2 * cya);

y1 = y2;

z1 = (z2 * cya) - (x2 * sya);

 

z1 = z1 - dist;

 

if (z1 == 0) z1 = -1;

 

TCoords tc((prp * (x1 / z1)), (prp * (y1 / z1)));

 

delete tm;

 

return tc;

}

 

/*******End of T3dCoords******************/

 

/*******TFace*****************************/

 

bool TFace::Visible(TCoords *Vertices)

{

int ax = Vertices[Vertex1].GetX() - Vertices[Vertex2].GetX();

int ay = Vertices[Vertex1].GetY() - Vertices[Vertex2].GetY();

int bx = Vertices[Vertex3].GetX() - Vertices[Vertex2].GetX();

int by = Vertices[Vertex3].GetY() - Vertices[Vertex2].GetY();

 

int resvec = ax * by - ay * bx;

 

if (resvec <= 0) return true;

else return false;

 

}

 

 

/*******End of TFace**********************/