Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<windows.h> /** Only for windows */
- #include<GL/glut.h>
- /** Define for animation and some translation of clouds only */
- static GLfloat cl1=560,cl2=300, clo1=560,clxp=0,clxm=200;
- static GLfloat c4x=600,c4y=560;
- static GLfloat c3y=560;
- static GLfloat c6x=1100,c6y=480;
- static GLfloat c7x=-1000,c7y=560;
- static GLfloat c8x=820,c9x=-2800;
- /** Define END */
- /** Global Variable defination for trees, clouds, robot and car */
- float yll=36,ylu=130,yln=80,yrh=320,ye=328,yre=325,yrf=348,yrnc=295,rlh=492,rrh=708,rla=495,rra=705,wty=45;
- float sx=1,sy=1,sz=1,fsx=1,fsy=1,fsz=1,tx=0,ty=0,tz=0;
- int tpos=640,ftx=0,fty=0,ftz=0,rxa=0,rya=0,flag=0,vflag=0,fflag=0,vtx=520,allover=0,i=0;
- /** Define END */
- void spinDisplay(void) /** This is a function for use only animation purpose of clouds */
- {
- cl1 +=0.2;
- if (cl1 > 800) /** If cloud 1 is out of window(800) then reverse back to previous size */
- {
- cl1=560;
- clxm=200;
- }
- clo1+=0.2; /** This is for cloud 2 */
- if (clo1 > 650)
- clo1=560;
- clxp+=.005;
- clxm-=.5;
- c4x+=.2; /** This is for cloud 4 and it's moving x & y axis*/
- c4y+=.2;
- if (c4y > 750)
- {
- c4x=600;
- c4y=560;
- }
- c3y+=.02;
- if (c3y > 650)
- c3y=560;
- c6y +=0.1; /** This is for cloud 6 and it's moving x & y axis*/
- c6x +=0.6;
- if (c6y > 700)
- {
- c6y=480;
- c6x=1100;
- }
- c7x-=.3; /** This is for cloud 7 and it's moving x & y axis*/
- c7y+=.1;
- if (c7y > 650)
- {
- c7y=560;
- c7x=-1000;
- }
- c8x-=.5;
- if(c8x<600)
- {
- c8x=820;
- }
- c9x-=.3;
- if(c9x<-2900)
- c9x=-2800;
- glutPostRedisplay(); /** glutPostRedisplay() marks the current window as needing to be redisplayed */
- }
- void tree() /** Function for creating trees */
- {
- //Tree's Base
- glColor3f(0.325,0.208,0.039);
- glScalef(1.5,0.3,0.3);
- glTranslatef(0,-10,0);
- glutSolidSphere(10,20,20); /// (radius, slices(num of subdivision z axis), stacks(num of subdivision))
- glTranslatef(0,10,0);
- glScalef(1/1.5,1/0.3,1/0.3);
- glutSolidCube(12);
- //Tree
- glColor3f(0.196, 0.804, 0.196);
- for(int c=0;c<=16;c+=5)
- {
- glTranslatef(c,25,0); /** (x,y,z) */
- glScalef(1,0.8,1);
- glutSolidSphere(20,20,20);
- glScalef(1,1/0.8,1);
- glTranslatef(-c,-25,0);
- }
- }
- void trans() /** This anouther transformation function (Only for robot and the car) **/
- {
- if (yll<130)
- {
- yll+=1;
- ylu+=1;
- yln+=1;
- }
- else if(yrh>240)
- {
- yrh-=1;
- yre-=1;
- yrf-=1;
- ye-=1;
- yrnc-=1;
- }
- else if(yll<220)
- {
- yll+=1;
- }
- else if(rlh<=560)
- {
- rlh+=1;
- rrh-=1;
- rla+=0.9;
- rra-=0.9;
- }
- else if(rxa<=90)
- {
- rxa+=1;
- tz-=2;
- ty+=1.2;
- }
- else
- {
- flag=1;
- rxa=0;
- sx=1.3;
- sy=0.5;
- sz=1;
- }
- }
- void clouds() /** function for clouds */
- {
- //Sky Clouds
- glColor3ub(196,217,245);
- glTranslatef(0,0,0);
- glScalef(1.6,1,1);
- glutSolidSphere(30,25,25);
- glScalef(1/1.6,1,1);
- glTranslatef(0,0,0);
- glTranslatef(0,0,30);
- glScalef(1.6,1,1);
- glutSolidSphere(30,25,25);
- glScalef(1/1.6,1,1);
- glTranslatef(0,0,-30);
- glTranslatef(0,0,-40);
- glScalef(1.6,1,1);
- glutSolidSphere(30,25,25);
- glScalef(1/1.6,1,1);
- glTranslatef(0,0,40);
- glTranslatef(40,0,0);
- glScalef(1.6,1,1);
- glutSolidSphere(30,25,25);
- glScalef(1/1.6,1,1);
- glTranslatef(-40,0,0);
- glTranslatef(-40,0,0);
- glScalef(1.6,1,1);
- glutSolidSphere(30,25,25);
- glScalef(1/1.6,1,1);
- glTranslatef(40,0,0);
- }
- void sky() /** A function for clouds */
- {
- glPushMatrix();
- glColor3f(0.275, 0.510, 0.706);
- glBegin(GL_QUADS);
- glColor3ub(30, 144, 255);
- glVertex3i(-2400,0,500);
- glColor3ub(26,59,110);
- glVertex3i(3500,0,500);
- glColor3ub(31,72,134);
- glVertex3i(3600,1200,500);
- glColor3ub(255,255,204);
- glVertex3i(-2400,1200,500);
- glEnd();
- glPopMatrix();
- }
- void road() /** A function for road between trees */
- {
- glPushMatrix();
- glColor3f(0.4,0.4,0.4);
- glTranslatef(0,-120,0);
- glBegin(GL_QUADS);
- glVertex3i(-2400,0,300);
- glVertex3i(3500,0,300);
- glVertex3i(3600,100,300);
- glVertex3i(-2400,100,300);
- glEnd();
- glPopMatrix();
- }
- void background()
- {
- //Floor
- glPushMatrix();
- glBegin(GL_QUADS);
- glColor3f(0.6,0.4,0.2);
- glVertex3i(-2400,0,0);
- glVertex3i(3600,0,0);
- glVertex3i(3600,0,500);
- glVertex3i(-2400,0,500);
- glEnd();
- glPopMatrix();
- for(int c=-400;c<=1500;c+=300)
- {
- //Trees on Right Side
- glTranslatef(c,170,0);
- tree();
- glTranslatef(-c,-170,0);
- }
- glPushMatrix();
- glTranslatef(clxm,cl1,300);
- clouds(); /** clouds define **/
- glTranslatef(-200,-560,-300);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(0,clo1,10);
- clouds(); /** clouds define **/
- glTranslatef(0,-560,-10);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(1200,560,50);
- clouds(); /** clouds define **/
- glTranslatef(-1200,-560,-50);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(c4x,c4y,400);
- clouds(); /** clouds define **/
- glTranslatef(-600,-560,-400);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(550,c3y,50);
- clouds(); /** clouds define **/
- glTranslatef(-550,-560,-50);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(c7x,c7y,500);
- clouds(); /** clouds define **/
- glTranslatef(1000,-560,-500);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(4000,560,1200);
- clouds(); /** clouds define **/
- glTranslatef(-4000,-560,-1200);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(3750,560,1200);
- clouds(); /** clouds define **/
- glTranslatef(-3750,-560,-1200);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(c6x,c6y,300);
- clouds(); /** clouds define **/
- glTranslatef(-1100,-480,-300);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(c8x,460,250);
- clouds(); /** clouds define **/
- glTranslatef(-820,-420,-250);
- glEnd();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(c9x,520,800);
- clouds(); /** clouds define **/
- glTranslatef(2800,-560,-800);
- glEnd();
- glPopMatrix();
- }
- void robot() /** Robot part **/
- {
- /** These three lines for rotating after animation **/
- glRotatef(rxa,1,0,0);
- glTranslatef(tx,ty,tz);
- glScalef(sx,sy,sz);
- //Robot Head
- glColor3f(0.824, 0.412, 0.118);
- glTranslatef(600,yrh,200);
- glScalef(1.8,1,0.2);
- glutSolidSphere(25,50,50);
- glScalef(1/1.8,1,1/0.2);
- glTranslatef(-600,-yrh,-200);
- //Left Ear
- glColor3f(0.2,0.2,0.7);
- glTranslatef(564,ye,200);
- glScalef(0.15,1,0.2);
- glutSolidCube(50);
- glScalef(1/0.15,1,1/0.2);
- glTranslatef(-564,-ye,-200);
- //Right Ear
- //glColor3f(0,0,1);
- glTranslatef(636,ye,200);
- glScalef(0.15,1,0.2);
- glutSolidCube(50);
- glScalef(1/0.15,1,1/0.2);
- glTranslatef(-636,-ye,-200);
- //Robot Eye
- glColor3f(0.502, 0.000, 0.000);
- glTranslatef(600,yre,200);
- glScalef(1,0.10,0.15);
- glutSolidCube(72);
- glScalef(1,1/0.10,1/0.15);
- glTranslatef(-600,-yre,-200);
- //Robot Neck
- glColor3f(0.7,0.7,0.7);
- glTranslatef(600,yrnc,200);
- glScalef(3.5,1,0.1);
- glutSolidCube(15);
- glScalef(1/3.5,1,1/0.1);
- glTranslatef(-600,-yrnc,-200);
- //Robot Left Arm
- glColor3f(1,0,0);
- glTranslatef(rla,280,200);
- glScalef(1.2,0.4,0.2);
- glutSolidSphere(25,50,50);
- glScalef(1/1.2,1/0.4,1/0.2);
- glTranslatef(-rla,-280,-200);
- //Robot Right Arm
- glColor3f(1,0,0);
- glTranslatef(rra,280,200);
- glScalef(1.2,0.4,0.2);
- glutSolidSphere(25,50,50);
- glScalef(1/1.2,1/0.4,1/0.2);
- glTranslatef(-rra,-280,-200);
- //Robot Left Knee
- glColor3f(1.0, 0.0, 1.0);
- glTranslatef(545,yln,200);
- glScalef(1.3,0.5,0.2);
- glutSolidSphere(25,50,50);
- glScalef(1/1.3,1/0.5,1/0.2);
- glTranslatef(-545,-yln,-200);
- //Robot Right Knee
- glColor3f(1.0, 0.0, 1.0);
- glTranslatef(650,yln,200);
- glScalef(1.3,0.5,0.2);
- glutSolidSphere(25,50,50);
- glScalef(1/1.3,1/0.5,1/0.2);
- glTranslatef(-650,-yln,-200);
- glColor3f(1,0.639,0.102);
- glTranslatef(600,155,200);
- glScalef(1,0.45,0.2);
- glutSolidSphere(60,5,5);
- glScalef(1,1/0.45,1/0.2);
- glTranslatef(-600,-155,-200);
- //Robot Body
- glColor3f(0.2,0.2,0.7);
- glTranslatef(600,228,200);
- glScalef(1.3,1,0.6);
- glutSolidCube(120);
- glScalef(1/1.3,1,1/0.6);
- glTranslatef(-600,-228,-200);
- //Robot Left Leg Part 1
- //glColor3f(0,0,0.9);
- glTranslatef(550,ylu,200);
- glScalef(0.8,1.2,0.2);
- glutSolidCube(60);
- glScalef(1/0.8,1/1.2,1/0.2);
- glTranslatef(-550,-ylu,-200);
- //Robot Right Leg Part 1
- //glColor3f(0,0,0.9);
- glTranslatef(650,ylu,200);
- glScalef(0.8,1.2,0.2);
- glutSolidCube(60);
- glScalef(1/0.8,1/1.2,1/0.2);
- glTranslatef(-650,-ylu,-200);
- //Robot Left Leg Part 2
- //glColor3f(0,0,0.9);
- //glColor3f(0.2,0.2,0.7);
- glTranslatef(550,yll,200);
- glScalef(1,1.2,0.2);
- glutSolidCube(60);
- glScalef(1,1/1.2,1/0.2);
- glTranslatef(-550,-yll,-200);
- //Robot Right Leg Part 2
- //glColor3f(0,0,0.9);
- glTranslatef(650,yll,200);
- glScalef(1,1.2,0.2);
- glutSolidCube(60);
- glScalef(1,1/1.2,1/0.2);
- glTranslatef(-650,-yll,-200);
- //Robot Left Hand
- //glColor3f(0,0,0.9);
- glTranslatef(rlh,220,200);
- glScalef(0.8,1.8,0.2);
- glutSolidCube(60);
- glScalef(1/0.8,1/1.8,1/0.2);
- glTranslatef(-rlh,-220,-200);
- //Robot Right Hand
- //glColor3f(0,0,0.9);
- glTranslatef(rrh,220,200);
- glScalef(0.8,1.8,0.2);
- glutSolidCube(60);
- glScalef(1/0.8,1/1.8,1/0.2);
- glTranslatef(-rrh,-220,-200);
- glScalef(1/sx,1/sy,1/sz);
- glTranslatef(-tx,-ty,-tz);
- glRotatef(-rxa,1,0,0);
- }
- void vehicle() /** Car Part **/
- {
- glTranslatef(ftx,fty,ftz);
- glScalef(fsx,fsy,fsz);
- if(vflag==1)
- {
- //Vehicle Hood
- glColor3f(1,0.639,0.102);
- glBegin(GL_QUADS);
- glVertex3i(vtx,35,200);
- glVertex3i(vtx+60,35,200);
- glVertex3i(vtx+60,90,200);
- glVertex3i(vtx,58,200);
- glEnd();
- //Front Wheel
- glColor3f(0.2,0.2,0.2);
- glTranslatef(540,wty,200);
- glScalef(1.7,1,0.2);
- glutSolidSphere(12.5,50,50);
- glScalef(1/1.7,1,1/0.2);
- glTranslatef(-540,-wty,-200);
- //Rear Wheel
- glColor3f(0.2,0.2,0.2);
- glTranslatef(655,wty,200);
- glScalef(1.7,1,0.2);
- glutSolidSphere(12.5,50,50);
- glScalef(1/1.7,1,1/0.2);
- glTranslatef(-655,-wty,-200);
- }
- glColor3f(0.2,0.2,0.7);
- glTranslatef(600,60,200);
- glScalef(sx,sy,sz);
- glRotatef(rya,0,1,0);
- glutSolidCube(120);
- glRotatef(-rya,0,1,0);
- glScalef(1/sx,1/sy,1/sz);
- glTranslatef(-600,-60,-200);
- glScalef(1/fsx,1/fsy,1/fsz);
- glTranslatef(-ftx,-fty,-ftz);
- }
- void calc() /** A calculation function for calculating the car animation **/
- {
- if(rya<=90)
- {
- rya+=1;
- sx+=0.002;
- sz-=0.01;
- }
- else if(fsx<=2.5)
- {
- fsx+=0.05;
- fsy+=0.05;
- fsz+=0.05;
- ftx-=20;
- fty-=10;
- }
- else
- {
- vflag=1;
- }
- if(vflag==1&&vtx>=465)
- {
- vtx-=1;
- }
- else if(vtx<465&&wty>=30)
- {
- wty-=0.5;
- }
- else if(wty<30)
- {
- allover=1;
- }
- if(allover==1)
- {
- if(i<=540)
- {
- ftx-=5;
- i++;
- }
- else if(i>540)
- fflag=1;
- }
- }
- void display() /** A display function for displaying every objects **/
- {
- sky();
- background();
- road();
- if(flag==0) /** flag = 0 for robot with animation **/
- {
- trans();
- robot();
- }
- else if(flag==1) /** flag = 1 for car with animation **/
- {
- calc();
- vehicle();
- }
- //Draw Trees alongside the road
- for(int c=-400;c<=1500;c+=300)
- {
- //Trees on Left Side
- glTranslatef(c,110,0);
- tree();
- glTranslatef(-c,-110,0);
- }
- glFlush();
- glutSwapBuffers();
- }
- void changeSize(int w, int h)
- {
- if(h == 0)
- h = 1;
- float ratio = 1.0* w / h;
- glMatrixMode(GL_PROJECTION); /** This is used for projectiong when it's scaling and translating **/
- glLoadIdentity(); /** glLoadIdentity replaces the current matrix with the identity matrix **/
- glViewport(0, 0, w, h); /** (x, y, width, height) **/
- gluPerspective(78,ratio,1,500); /** (fovy, aspect, znear, zfar) **/
- glMatrixMode(GL_MODELVIEW);
- }
- //void Timer(int value) /** Declaration of timer function **/
- /*{
- if(fflag==0)
- {
- glutTimerFunc(10,Timer,0);
- glutPostRedisplay();
- }
- }*/
- void myinit() /** I have initialized background color and window size**/
- {
- glClearColor(1.0,1.0,1.0,1.0);
- glOrtho(0,1200,0,640,0,500);
- //glutTimerFunc(5000,Timer,0); /** glutTimerFunc registers a timer callback to be triggered in a specified number of milliseconds. **/
- }
- int main(int argc,char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); /** This double buffered window. This overrides GLUT_SINGLE if it is also specified. **/
- glutInitWindowPosition(0,0);
- glutInitWindowSize(1200,640);
- glutCreateWindow("A Robotic Car by OpenGL");
- glutReshapeFunc(changeSize);
- glutDisplayFunc(display);
- glutIdleFunc(spinDisplay);
- myinit();
- trans();
- glutMainLoop();
- }
- /********************************/
- /******** Documentation *********/
- /********************************/
- /**
- --> glutSolidSphere()
- radius
- The radius of the sphere.
- slices
- The number of subdivisions around the Z axis (similar to lines of longitude).
- stacks
- The number of subdivisions along the Z axis (similar to lines of latitude).
- --> glViewPort(x, y, width, height)
- glViewPort -- is in screen pixel units: that's it, it has nothing to do with the 3D world
- "inside" your graphics card. It just tells wich part of the window will be used for
- rendering (or just will be visible).
- glOrtho -- instead changes the "inner" world and is OpenGL units: More OpenGL units will fit
- into the visible part of the screen, so "bigger" objects will fit easily into viewable area
- if you increase the ortho size.
- --> gluPerspective(fovy, aspect, zNear, zFar)
- fovy -- Specifies the field of view angle, in degrees, in the y direction.
- aspect -- Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
- zNear -- Specifies the distance from the viewer to the near clipping plane (always positive).
- zFar -- Specifies the distance from the viewer to the far clipping plane (always positive).
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement