// ROT6.CPP
//
// Author : Saket Soni
// Requirements : DOS/Windows, Turbo C++ 3.0
//
// Description : Program displays a 3D cube which can be rotated
// along x, y or z axis. To rotate along :
// *) x-axis - press 'd'
// *) y-axis - press 'a'
// *) z-axis - press 'w'
// To change the direction of rotation along any axis press 's'
//
//
////////////////////////////////////////////////////////////////////////////
// Header files ...
////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include"rotlib.h"
////////////////////////////////////////////////////////////////////////////
// Global constant declarations ...
////////////////////////////////////////////////////////////////////////////
const long double PI = 3.1415926535897932384626433832795L;
const long double to_degree = 180.0L/PI; // used for conversion
const long double to_radian = PI/180.0L; // between degree and radians
const char * const DRIVER_PATH = "\\tc\\bgi"; // Path for EGAVGA.BGI
// (graphics driver) file
const float ANGLE = 2; // rotation angle in degrees
extern float MaxX;
extern float MaxY;
extern float MidX;
extern float MidY;
////////////////////////////////////////////////////////////////////////////
// Startup code to initialize the graphics system ...
////////////////////////////////////////////////////////////////////////////
void startup_func() {
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, DRIVER_PATH);
errorcode = graphresult();
if (errorcode != grOk)
{
printf("\nGraphics error: %s\n", grapherrormsg(errorcode));
printf("Is the path set in \"DRIVER_PATH\" macro for EGAVGA.BGI correct ?\n");
printf("Press any key to halt ... ");
getch();
exit(1);
}
MaxX = getmaxx();
MaxY = getmaxy();
MidX = MaxX/2;
MidY = MaxY/2;
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
}
#pragma startup startup_func 64
////////////////////////////////////////////////////////////////////////////
// Shutdown code to close the graphics system ...
////////////////////////////////////////////////////////////////////////////
void exit_func() {
closegraph();
}
#pragma exit exit_func 64
////////////////////////////////////////////////////////////////////////////
// Main function ...
////////////////////////////////////////////////////////////////////////////
int main()
{
// required variables ...
// CBosS b1();
// CBoxS b1(50,50,50,150,150,150);
CBoxS b1(-120,-120,-120,240,240,240);
// CBoxS b1(-20,-20,-20,40,40,40);
// b1 is a 3D cube
float angle = ANGLE*to_radian; // to rotate by ANGLE degrees
int dir = 1; // to control direction of rotation
char ch; // to rotate along what (x,y or z)?
// draw the reference lines ...
/* setcolor(LIGHTRED);
line(0,MidY,MaxX,MidY);
line(MidX,0,MidX,MaxY);
*/
// draw the cube ...
setcolor(YELLOW);
b1.draw();
// set the rotation angle ...
CVertex::set_theta(angle);
// repeat while user presses ESC ...
while ( (ch=getch()) != 27 )
{
// erase the previous cube from the screen ...
// clearviewport();
// setcolor(BLACK);
// b1.show();
b1.hide();
/* setcolor(LIGHTRED);
setlinestyle(SOLID_LINE,SOLID_FILL,THICK_WIDTH);
line(0,MidY,MaxX,MidY);
line(MidX,0,MidX,MaxY);
*/
// rotate the cube ...
switch (ch)
{
case 'A':
case 'a': // rotate along y if 'a' is pressed
b1.rotate_y();
break;
case 'D':
case 'd': // rotate along x if 'd' is pressed
b1.rotate_x();
break;
case 'W':
case 'w': // rotate along z if 'w' is pressed
b1.rotate_z();
break;
case 'S':
case 's':// change the direction if 's' is pressed
dir = dir * (-1);
CVertex::set_theta(angle*dir);
break;
default:
printf("\a");
}
// redraw the cube on the screen
setcolor(YELLOW);
b1.draw();
// delay(20);
}
return 0; // return successfully
}