![]() |
The Three Dimensions by Gaurav Hello and welcome to the first tutorial I have ever written in my life. This tutorial will teach you how to create 3-D objects through C++ although you can use the 3-D functions in other languages too but I am taking up C++ here. Before I start I assume that you have some little knowledge of the basic graphics functions in Turbo C++ v3.0 i.e. drawing a line, circle etc . . . . Please note that I am using the TC++ v3.0 compiler here and have not tested it on any other C++ compiler. Now let’s start with a program. It will be better if I take up a program and explain it in detail instead of giving all the boring theoretical information. The program which we will develop is ROTATING 3-D PYRAMID. Don’t be nervous by the name it’s a very simple program with only a 50 line code! I have even attached the code along with this tutorial OK Let’s Start! /* This is just including the header files */ #include<graphics.h> /* End of Header Files */ # define pi
3.141592736; //Declaring a
constant named pi /* Till now it was simple you do that in any C++ program. From now onwards starts the CORE */ /* We use the function below for rotation in both horizontal and vertical direction All we have to do is pass the angle in degrees to this function and we will get the corresponding values */ void ini(float
cos1,float cos2) /* The function below takes 3 values i.e the values for the 3 Co-ordinates (x,y,z) and returns us the values which we actually plot on the screen i.e it takes the 3-D values and converts them in to a 2-D value which when plotted on the screen seems 3-D */ void plot(int x,int
y,int z,int &x1,int &y1) /* Above we add 320 and 240 respectively to x1 and y1 to shift the co-ordinate system to the centre of the screen */ /* The above 2 functions are what you will only need to create any 3-D object */ void main() /* Here we are initializing the graphics */
int gd=DETECT,gm;
while(!kbhit()) ini(p,0) – Horizontal Rotation ini(0,p) – Vertical Rotation ini(p,p) – Both Veretical and Horizontal Rotation */ int x[5],y[5]; /*Declaration of array used for storing the converted x,y,z values by the plot function */ /* Below 4 lines are responsible for plotting the co-ordinates of the base of the Pyramid */
plot(5,5,1,x[0],y[0]); setcolor(RED); // Sets the color of the lines as RED /* Below 4 lines are responsible for actually drawing the base of the pyramid from the values x & y returned by the plot function. Different co-ordinates of the base(4 pts.) are stored in the array declared above*/
line(x[3],y[3],x[4],y[4]); /* Below we plot the top of the pyramid and from there using the for loop join the peak to the 4 co-ordinates of the base hence completing the pyramid*/
plot(20,20,140,x[2],y[2]); cleardevice(); /* If we don’t use cleardevice here then the pyramid will leave marks all around it’s rotation path. */ } This is it you have made your first 3-D program in just 50 lines! Easy isn’t it. You are free to ask my anything about the above tutorial! Download The Above Code Click Here HAPPY CODING!!! Add your tutorials to this site Mail Me
|
GauravCreations's Store / Home / Register / Awards / Opinions / Downloads / Query |
Copyright © 2001-04 [Gaurav Creations]. All rights reserved |
|