0% found this document useful (0 votes)
87 views45 pages

Programas para Generar Carros

This document contains code for animating a 3D car model in OpenGL. It defines variables and functions for initializing the OpenGL scene, drawing the car geometry, and handling user input and animation. The car model consists of quadrilaterals defined with GL_QUADS for the various body panels. Functions are provided for initializing lighting, transforming the viewport, and clearing and redrawing the scene in response to input.

Uploaded by

claudia serrano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views45 pages

Programas para Generar Carros

This document contains code for animating a 3D car model in OpenGL. It defines variables and functions for initializing the OpenGL scene, drawing the car geometry, and handling user input and animation. The car model consists of quadrilaterals defined with GL_QUADS for the various body panels. Functions are provided for initializing lighting, transforming the viewport, and clearing and redrawing the scene in response to input.

Uploaded by

claudia serrano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Carro

#include <stdio.h>
#include <stdlib.h>
#include <glut.h>
#include <math.h>
#include <string.h>

/* ASCII code for the escape key. */


#define ESCAPE 27

GLint window;
GLint window2;
GLint Xsize=1000;
GLint Ysize=800;
float i,theta;
GLint nml=0,day=1;

char name3[]="PROJECT: 3D CAR ANIMATION";

GLfloat xt=0.0,yt=0.0,zt=0.0,xw=0.0; /* x,y,z translation */


GLfloat tx=295,ty=62;
GLfloat xs=1.0,ys=1.0,zs=1.0;

GLfloat xangle=0.0,yangle=0.0,zangle=0.0,angle=0.0; /* axis angles


*/

GLfloat r=0,g=0,b=1;
GLint light=1;
int count=1,flg=1;
int view=0;
int flag1=0,aflag=1; //to switch car driving mode
int flag2=0,wheelflag=0; //to switch fog effect
GLUquadricObj *t;

static void SpecialKeyFunc( int Key, int x, int y );

/* Simple transformation routine */


GLvoid Transform(GLfloat Width, GLfloat Height)
{
glViewport(0, 0, Width, Height); /* Set the viewport */
glMatrixMode(GL_PROJECTION); /* Select the
projection matrix */
glLoadIdentity(); /* Reset The Projection
Matrix */
gluPerspective(45.0,Width/Height,0.1,100.0); /* Calculate The
Aspect Ratio Of The Window */
glMatrixMode(GL_MODELVIEW); /* Switch back to the
modelview matrix */
}

/* A general OpenGL initialization function. Sets all of the initial


parameters. */
GLvoid InitGL(GLfloat Width, GLfloat Height)
{

glClearColor(1.0, 1.0, 1.0, 1.0);


glLineWidth(2.0); /* Add line width, ditto */
Transform( Width, Height ); /* Perform the transformation */
//newly added
t=gluNewQuadric();
gluQuadricDrawStyle(t, GLU_FILL);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

// Create light components


GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { 1.5f, 1.0f, 4.0f, 1.0f };

// Assign created components to GL_LIGHT0


glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

/* The function called when our window is resized */


GLvoid ReSizeGLScene(GLint Width, GLint Height)
{
if (Height==0) Height=1; /* Sanity checks */
if (Width==0) Width=1;
Transform( Width, Height ); /* Perform the
transformation */
}

void init()
{
glClearColor(0,0,0,0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,900.0,0.0,600.0,50.0,-50.0);
glutPostRedisplay(); // request redisplay
}

/* The main drawing function

In here we put all the OpenGL and calls to routines which


manipulate
the OpenGL state and environment.
This is the function which will be called when a "redisplay" is
requested.
*/

void display_string(int x, int y, char *string, int font)


{
int len,i;
glColor3f(0.8,0.52,1.0);
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
if(font==1)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]);
if(font==2)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,string[i]);
if(font==3)

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,string[i]);
if(font==4)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,string[i]);
}

void display1(void)
{

glClearColor(1.0,1.0,0.1,1.0);
display_string(180,540,"NAME OF THE ENGINEERING COLLEGE",1);
//correct cordinate according to name
display_string(215,500,name3,1);
display_string(390,470,"HELP",2);
display_string(10,450,"MOUSE",2);
display_string(10,410,"PRESS RIGHT BUTTON FOR MENU",3);
display_string(10,370,"KEYBOARD",2);
display_string(10,340,"X-Y-Z KEYS FOR CORRESPONDING
ROTATION",3);
display_string(10,310,"A-S-Q CAR CUSTOM SIZE SELECTION",3);
display_string(10,280,"U-F FOR CAMERA VIEW SETTINGS",3);
display_string(10,250,"USE LEFT ARROW(<-) AND RIGHT ARROW(->)
TO MOVE CAR",3);
display_string(10,220,"ESCAPE TO EXIT",3);
display_string(250,150,"PRESS SPACE BAR TO ENTER",2);
glutPostRedisplay();
glutSwapBuffers();

GLvoid DrawGLScene()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Clear


The Screen And The Depth Buffer */
if(view==0)
{
init();
display1();
}
else
{
if(count==1)
InitGL(Xsize,Ysize);
if(aflag==1)/* Initialize our window. */
glClearColor(1,1,1,1);
else
glClearColor(0.1,0.1,0.1,0);
glPushMatrix();
glLoadIdentity();
glTranslatef(-1.0,0.0,-3.5);
glRotatef(xangle,1.0,0.0,0.0);
glRotatef(yangle,0.0,1.0,0.0);
glRotatef(zangle,0.0,0.0,1.0);
glTranslatef(xt,yt,zt);
glScalef(xs,ys,zs);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

if(flag2==1)
{
GLfloat fogcolour[4]={1.0,1.0,1.0,1.0};

glFogfv(GL_FOG_COLOR,fogcolour); /* Define the fog


colour */
glFogf(GL_FOG_DENSITY,0.1); /* How dense */
glFogi(GL_FOG_MODE,GL_EXP); /* exponential decay
*/
glFogf(GL_FOG_START,3.0); /* Where wwe start
fogging */
glFogf(GL_FOG_END,100.0); /* end */
glHint(GL_FOG_HINT, GL_FASTEST); /* compute per vertex
*/
glEnable(GL_FOG);/* ENABLE */
}
if(flag2==0)
{
glDisable(GL_FOG);
}

if(!aflag){
glBegin(GL_POINTS);
glColor3f(1,1,1);
glPointSize(200.0);
int ccount=0;
float x=10,y=10;
while(ccount<20)
{
glVertex2f(x,y);
x+=10;
y+=10;
if(y>Ysize) y-=10;
if(x>Xsize) x-=10;
ccount++;
}
glEnd();}

glColor3f(1.0,.75,0.0);
glPointSize(30.0);
glBegin(GL_POINTS);
glVertex3f(0.2,0.3,0.3);
glVertex3f(0.2,0.3,0.5);
glEnd();
glPointSize(200.0);

glBegin(GL_QUADS); /* OBJECT MODULE*/

/* top of cube*/
//************************FRONT
BODY****************************************
glColor3f(r,g,b);
glVertex3f( 0.2, 0.4,0.6);
glVertex3f(0.6, 0.5,0.6);
glVertex3f(0.6, 0.5,0.2);
glVertex3f( 0.2,0.4,0.2);

/* bottom of cube*/
glVertex3f( 0.2,0.2,0.6);
glVertex3f(0.6,0.2,0.6);
glVertex3f(0.6,0.2,0.2);
glVertex3f( 0.2,0.2,0.2);

/* front of cube*/
glVertex3f( 0.2,0.2,0.6);
glVertex3f(0.2, 0.4,0.6);
glVertex3f(0.2,0.4,0.2);
glVertex3f( 0.2,0.2,0.2);

/* back of cube.*/
glVertex3f(0.6,0.2,0.6);
glVertex3f(0.6,0.5,0.6);
glVertex3f(0.6,0.5,0.2);
glVertex3f( 0.6,0.2,0.2);

/* left of cube*/
glVertex3f(0.2,0.2,0.6);
glVertex3f(0.6,0.2,0.6);
glVertex3f(0.6,0.5,0.6);
glVertex3f(0.2,0.4,0.6);

/* Right of cube */
glVertex3f(0.2,0.2,0.2);
glVertex3f( 0.6,0.2,0.2);
glVertex3f( 0.6,0.5,0.2);
glVertex3f( 0.2,0.4,0.2);
//********************************************************************
********
glVertex3f(0.7,0.65,0.6);
glVertex3f(0.7,0.65,0.2);
glVertex3f(1.7,0.65,0.2); //top cover
glVertex3f(1.7,0.65,0.6);
//***************************back guard******************************
glColor3f(r,g,b); /* Set The Color To Blue*/
glVertex3f( 1.8, 0.5,0.6);
glVertex3f(1.8, 0.5,0.2);
glVertex3f(2.1, 0.4, 0.2);
glVertex3f(2.1,0.4,0.6);

/* bottom of cube*/
glVertex3f( 2.1,0.2,0.6);
glVertex3f(2.1,0.2,0.2);
glVertex3f(1.8,0.2,0.6);
glVertex3f( 1.8,0.2,0.6);

/* back of cube.*/
glVertex3f(2.1,0.4,0.6);
glVertex3f(2.1,0.4,0.2);
glVertex3f(2.1,0.2,0.2);
glVertex3f(2.1,0.2,0.6);

/* left of cube*/
glVertex3f(1.8,0.2,0.2);
glVertex3f(1.8,0.5,0.2);
glVertex3f(2.1,0.4,0.2);
glVertex3f(2.1,0.2,0.2);

/* Right of cube */
glVertex3f(1.8,0.2,0.6);
glVertex3f(1.8,0.5,0.6);
glVertex3f(2.1,0.4,0.6);
glVertex3f(2.1,0.2,0.6);
//******************MIDDLE BODY************************************
glVertex3f( 0.6, 0.5,0.6);
glVertex3f(0.6, 0.2,0.6);
glVertex3f(1.8, 0.2, 0.6);
glVertex3f(1.8,0.5,0.6);

/* bottom of cube*/
glVertex3f( 0.6,0.2,0.6);
glVertex3f(0.6,0.2,0.2);
glVertex3f(1.8,0.2,0.2);
glVertex3f( 1.8,0.2,0.6);

/* back of cube.*/
glVertex3f(0.6,0.5,0.2);
glVertex3f(0.6,0.2,0.2);
glVertex3f(1.8,0.2,0.2);
glVertex3f(1.8,0.5,0.2);
//*********************ENTER WINDOW**********************************
glColor3f(0.3,0.3,0.3);
glVertex3f( 0.77, 0.63,0.2);
glVertex3f(0.75, 0.5,0.2); //quad front window
glVertex3f(1.2, 0.5, 0.2);
glVertex3f( 1.22,0.63,0.2);

glVertex3f(1.27,0.63,.2);
glVertex3f(1.25,0.5,0.2); //quad back window
glVertex3f(1.65,0.5,0.2);
glVertex3f(1.67,0.63,0.2);

glColor3f(r,g,b);
glVertex3f(0.7,0.65,0.2);
glVertex3f(0.7,0.5,.2); //first separation
glVertex3f(0.75,0.5,0.2);
glVertex3f(0.77,0.65,0.2);

glVertex3f(1.2,0.65,0.2);
glVertex3f(1.2,0.5,.2); //second separation
glVertex3f(1.25,0.5,0.2);
glVertex3f(1.27,0.65,0.2);

glVertex3f(1.65,0.65,0.2);
glVertex3f(1.65,0.5,.2); //3d separation
glVertex3f(1.7,0.5,0.2);
glVertex3f(1.7,0.65,0.2);

glVertex3f( 0.75, 0.65,0.2);


glVertex3f(0.75, 0.63,0.2); //line strip
glVertex3f(1.7, 0.63, 0.2);
glVertex3f( 1.7,0.65,0.2);

glVertex3f( 0.75, 0.65,0.6);


glVertex3f(0.75, 0.63,0.6); //line strip
glVertex3f(1.7, 0.63, 0.6);
glVertex3f( 1.7,0.65,0.6);

glColor3f(0.3,0.3,0.3);
glVertex3f( 0.77, 0.63,0.6);
glVertex3f(0.75, 0.5,0.6); //quad front window
glVertex3f(1.2, 0.5, 0.6);
glVertex3f( 1.22,0.63,0.6);

glVertex3f(1.27,0.63,.6);
glVertex3f(1.25,0.5,0.6); //quad back window
glVertex3f(1.65,0.5,0.6);
glVertex3f(1.67,0.63,0.6);

glColor3f(r,g,b);
glVertex3f(0.7,0.65,0.6);
glVertex3f(0.7,0.5,.6); //first separation
glVertex3f(0.75,0.5,0.6);
glVertex3f(0.77,0.65,0.6);

glVertex3f(1.2,0.65,0.6);
glVertex3f(1.2,0.5,.6); //second separation
glVertex3f(1.25,0.5,0.6);
glVertex3f(1.27,0.65,0.6);

glVertex3f(1.65,0.65,0.6);
glVertex3f(1.65,0.5,.6);
glVertex3f(1.7,0.5,0.6);
glVertex3f(1.7,0.65,0.6);
glEnd();

//**************************************************************
glBegin(GL_QUADS);

/* top of cube*/
glColor3f(0.3,0.3,0.3);
glVertex3f( 0.6, 0.5,0.6);
glVertex3f(0.6, 0.5,0.2); //quad front window
glVertex3f(0.7, 0.65, 0.2);
glVertex3f( 0.7,0.65,0.6);

glVertex3f(1.7,0.65,.6);
glVertex3f(1.7,0.65,0.2); //quad back window
glVertex3f(1.8,0.5,0.2);
glVertex3f(1.8,0.5,0.6);

//*****************************road and surrounding


development***********************************
if(flag1)
{
glPushMatrix();
glTranslatef(xw,0,0);
glColor3f(0,1,0);
glVertex3f(-100,0.1,-100);
glVertex3f(-100,0.1,0); //a green surroundings
glVertex3f(100,0.1,0);
glVertex3f(100,0.1,-100);

glColor3f(0.7,0.7,0.7);
glVertex3f(-100,0.1,0);
glVertex3f(-100,0.1,0.45); //a long road
glVertex3f(100,0.1,0.45);
glVertex3f(100,0.1,0);

glColor3f(1.0,0.75,0.0);
glVertex3f(-100,0.1,0.45); //a median
glVertex3f(-100,0.1,0.55);
glVertex3f(100,0.1,0.55);
glVertex3f(100,0.1,0.45);

glColor3f(0.7,0.7,0.7);
glVertex3f(-100,0.1,0.55);
glVertex3f(-100,0.1,1); //a long road
glVertex3f(100,0.1,1);
glVertex3f(100,0.1,0.55);

glColor3f(0,1,0);
glVertex3f(-100,0.1,1);
glVertex3f(-100,0.1,100); //a green surroundings
glVertex3f(100,0.1,100);
glVertex3f(100,0.1,1);
glPopMatrix();
}
glEnd();

if(wheelflag)
{
glPushMatrix();
glTranslatef(xw,0,0);
glColor3f(0.5,.2,0.3);
glBegin(GL_QUADS);
for(i=0;i<200;i+=0.2)
{
glVertex3f(-100+i,0,1);
glVertex3f(-99.9+i,0,1);
glVertex3f(-99.9+i,0.2,1);
glVertex3f(-100+i,0.2,1);
i+=0.5;
}
for(i=0;i<200;i+=0.2)
{
glVertex3f(-100+i,0,0);
glVertex3f(-99.9+i,0,0);
glVertex3f(-99.9+i,0.2,0);
glVertex3f(-100+i,0.2,0);
i+=0.5;
}
glEnd();
glPopMatrix();
}
//********************************************************************
*****************************
glBegin(GL_TRIANGLES); /* start drawing the cube.*/

/* top of cube*/
glColor3f(0.3,0.3,0.3);
glVertex3f( 0.6, 0.5,0.6);
glVertex3f( 0.7,0.65,0.6); //tri front window
glVertex3f(0.7,0.5,0.6);

glVertex3f( 0.6, 0.5,0.2);


glVertex3f( 0.7,0.65,0.2); //tri front window
glVertex3f(0.7,0.5,0.2);

glVertex3f( 1.7, 0.65,0.2);


glVertex3f( 1.8,0.5,0.2); //tri back window
glVertex3f( 1.7,0.5,0.2);

glVertex3f( 1.7, 0.65,0.6);


glVertex3f( 1.8,0.5,0.6); //tri back window
glVertex3f(1.7,0.5,0.6);

glEnd();
//************IGNITION SYSTEM**********************************
glPushMatrix();
glColor3f(0.7,0.7,0.7);
glTranslatef(1.65,0.2,0.3);
glRotatef(90.0,0,1,0);
gluCylinder(t,0.02,0.03,.5,10,10);
glPopMatrix();
//********************WHEEL*******************************************
**

glColor3f(0.7,0.7,0.7);
glPushMatrix();
glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
{
glVertex3f(0.6,0.2,0.62);

glVertex3f(0.6+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin((
(theta+angle)*3.14)/180))),0.62);
}
glEnd();

glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
{
glVertex3f(0.6,0.2,0.18);

glVertex3f(0.6+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin((
(theta+angle)*3.14)/180))),0.18);
}
glEnd();

glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
{
glVertex3f(1.7,0.2,0.18);

glVertex3f(1.7+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin((
(theta+angle)*3.14)/180))),0.18);
}
glEnd();

glBegin(GL_LINE_STRIP);
for(theta=0;theta<360;theta=theta+20)
{
glVertex3f(1.7,0.2,0.62);

glVertex3f(1.7+(0.08*(cos(((theta+angle)*3.14)/180))),0.2+(0.08*(sin((
(theta+angle)*3.14)/180))),0.62);
}
glEnd();
glTranslatef(0.6,0.2,0.6);
glColor3f(0,0,0);
glutSolidTorus(0.025,0.07,10,25);

glTranslatef(0,0,-0.4);
glutSolidTorus(0.025,0.07,10,25);

glTranslatef(1.1,0,0);
glutSolidTorus(0.025,0.07,10,25);

glTranslatef(0,0,0.4);
glutSolidTorus(0.025,0.07,10,25);
glPopMatrix();
//*************************************************************
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glutPostRedisplay();
glutSwapBuffers();
}
}

/* The function called whenever a "normal" key is pressed. */


void NormalKey(GLubyte key, GLint x, GLint y)
{
switch ( key ) {
case ESCAPE : printf("escape pressed. exit.\n");
glutDestroyWindow(window); /* Kill our window
*/
exit(0);
break;

case ' ':view=1;


DrawGLScene();
break;

case 'x': xangle += 5.0;


glutPostRedisplay();
break;

case 'X':xangle -= 5.0;


glutPostRedisplay();
break;

case 'y':
yangle += 5.0;
glutPostRedisplay();
break;

case 'Y':
yangle -= 5.0;
glutPostRedisplay();
break;

case 'z':
zangle += 5.0;
glutPostRedisplay();
break;

case 'Z':
zangle -= 5.0;
glutPostRedisplay();
break;

case 'u': /* Move up */


yt += 0.2;
glutPostRedisplay();
break;

case 'U':
yt -= 0.2; /* Move down */
glutPostRedisplay();
break;

case 'f': /* Move forward */


zt += 0.2;
glutPostRedisplay();
break;

case 'F':
zt -= 0.2; /* Move away */
glutPostRedisplay();
break;

case 's':zs+=.2;
glutPostRedisplay();
break;

case 'S':zs-=0.2;
glutPostRedisplay();
break;

case 'a':ys+=.2;
glutPostRedisplay();
break;

case 'A':ys-=0.2;
glutPostRedisplay();
break;
case 'q':xs+=.2;
glutPostRedisplay();
break;

case 'Q':xs-=0.2;
glutPostRedisplay();
break;

default:
break;
}

static void SpecialKeyFunc( int Key, int x, int y )


{
switch ( Key ) {
case GLUT_KEY_RIGHT:
if(!wheelflag)
xt += 0.2;
if(wheelflag)
{
angle+=5;
xw+=0.2;
}
glutPostRedisplay();
break;

case GLUT_KEY_LEFT:
if(!wheelflag)
xt -= 0.2;
if(wheelflag)
{
angle+=5;
xw-=0.2;
}
glutPostRedisplay();
break;
}
}

void myMenu(int id)


{
if (id==1)
{
flag1=0;
wheelflag=0;
glutPostRedisplay();

}
if(id ==2)
{
flag1=1;
flag2=0;
wheelflag=0;
xangle += 5.0;
glutPostRedisplay();
}
if(id==3)
{
flag2=1;
wheelflag=0;
xangle += 5.0;
glutPostRedisplay();
}
if (id==4)
{
wheelflag=1;
glutPostRedisplay();
}
if (id==5)
{
if(day)
{

if(light)
{
count++;
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
light=0;
}
else
{
count--;
light=1;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
glutPostRedisplay();
}
else
{

if(nml==0 && flag2==2)


{
flag2=0;
nml=1;
}
else
{
flag2=2;
nml=0;

aflag=0;
day=0;
glClearColor(0.1,0.1,0.1,0);
GLfloat fogcolour[4]={0.0,0.0,0.0,1.0};

glFogfv(GL_FOG_COLOR,fogcolour); /*
Define the fog colour */
glFogf(GL_FOG_DENSITY,0.5); /* How
dense */
glFogi(GL_FOG_MODE,GL_EXP); /*
exponential decay */
/* end */
glHint(GL_FOG_HINT, GL_FASTEST); /*
compute per vertex */
glEnable(GL_FOG);

glutPostRedisplay();
}
}

if(id==12)
{
aflag=1;
day=1;
glClearColor(1,1,1,1);
glDisable(GL_FOG);
glutPostRedisplay();
}

if(id==13)
{
aflag=0;
day=0;
flag2=2;
glClearColor(0.1,0.1,0.1,0);
GLfloat fogcolour[4]={0.0,0.0,0.0,1.0};

glFogfv(GL_FOG_COLOR,fogcolour); /* Define the fog


colour */
glFogf(GL_FOG_DENSITY,0.5); /* How dense */
glFogi(GL_FOG_MODE,GL_EXP); /* exponential decay
*/
/* end */
glHint(GL_FOG_HINT, GL_FASTEST); /* compute per vertex
*/
glEnable(GL_FOG);

glutPostRedisplay();
}
}

void colorMenu(int id)


{
if (id==6)
{
r=g=0;
b=1;
glutPostRedisplay();

}
if(id ==7)
{
r=0.8;
b=g=0;
glutPostRedisplay();
}
if(id==8)
{
g=1;
r=b=0;
glutPostRedisplay();
}
if (id==9)
{
r=b=g=0;
glutPostRedisplay();
}
if(id==10)
{
b=0;
r=g=1;
glutPostRedisplay();
}
if(id==11)
{
b=r=g=.7;
glutPostRedisplay();
}

void myreshape(int w,int h)


{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-
2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-
2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-
10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
//*************************** Main
***************************************************************

int main(int argc, char **argv)


{

/* Initialisation and window creation */

glutInit(&argc, argv); /* Initialize GLUT state. */

glutInitDisplayMode(GLUT_RGBA | /* RGB and Alpha */


GLUT_DOUBLE| /* double buffer */
GLUT_DEPTH); /* Z buffer (depth) */

glutInitWindowSize(Xsize,Ysize); /* set initial window size. */


glutInitWindowPosition(0,0); /* upper left corner of the
screen. */

glutCreateWindow("3D CAR ANIMATION"); /* Open a window with a title.


*/

/* Now register the various callback functions */

glutReshapeFunc(myreshape);
glutDisplayFunc(DrawGLScene); /* Function to do all our
OpenGL drawing. */
glutReshapeFunc(ReSizeGLScene);
glutKeyboardFunc(NormalKey); /*Normal key is pressed */
glutSpecialFunc( SpecialKeyFunc );
InitGL(Xsize,Ysize);
int submenu=glutCreateMenu(colorMenu);
glutAddMenuEntry("blue", 6);
glutAddMenuEntry("red", 7);
glutAddMenuEntry("green",8);
glutAddMenuEntry("black",9);
glutAddMenuEntry("yellow",10);
glutAddMenuEntry("grey",11);
glutCreateMenu(myMenu);
glutAddMenuEntry("car model mode", 1);
glutAddMenuEntry("car driving mode", 2);
glutAddMenuEntry("fog effect",3);
glutAddMenuEntry("wheel effect",4);
glutAddMenuEntry("toggle light",5);
glutAddSubMenu("car colors",submenu);
glutAddMenuEntry("daymode",12);
glutAddMenuEntry("Night mode",13);
glutAttachMenu(GLUT_RIGHT_BUTTON);

/* Now drop into the event loop from which we never return */

glutMainLoop(); /* Start Event Processing


Engine. */
return 1;
}
Carro y señales de trafico
#include<stdio.h>
#include<GL/glut.h>

void bus();
void road();
void signal();
void car();
void car2();
void mydisplay();
void display();
void frontsreen();
void drawstring();
void setFont();
void myMouse();
void update();
void control();
void helpscreen();
GLint a=300,b=-
300,flag=0,traffic_regulator=1,control_keyl,control_keyr;
GLfloat red=0,blue=1,green=.3;

GLfloat p=0,q=0,r=0;
//-----------
void *currentfont;

void setFont(void *font)


{
currentfont=font;
}

void drawstring(float x,float y,float z,char *string)


{
char *c;
glRasterPos3f(x,y,z);

for(c=string;*c!='\0';c++)
{ glColor3f(0.0,0.0,0.0);
glutBitmapCharacter(currentfont,*c);
}
}

void frontscreen(void)
{
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glClearColor(0.15,0.1,0.01,0);/*background for cover page*/
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
drawstring(450.0,700.0,0.0,"INSTITUTE OF TECHNOLOGY ");
glColor3f(0.7,0,1);
drawstring(330,650,0.0,"DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING");
glColor3f(1,0.5,0);
drawstring(530,600,0.0,"A MINI PROJECT ON");
glColor3f(1,0,0);
drawstring(360,500,0.0,"GRAPHICAL REPRESENTATION OF TRAFFIC SIGNALS");
glColor3f(1,0.5,0);
drawstring(200,400,0.0,"BY:");
glColor3f(1,1,1);
drawstring(100,300,0.0,"name");
glColor3f(1,1,1);
drawstring(100,240,0.0,"name");
glColor3f(1,0.5,0);
drawstring(980,400,0.0,"GUIDES:");
glColor3f(1,1,1);
drawstring(930,300,0.0,"name");
glColor3f(1,1,1);
drawstring(930,240,0.0," name");
glColor3f(1,0.1,1);
drawstring(543,100,0.0,"PRESS ENTER TO START");
glFlush();
}

void helpscreen()
{
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glClearColor(0,0,0,0);/*background for cover page*/
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
drawstring(550.0,700.0,0.0,"TIPS");
glColor3f(1,0,0);
drawstring(650.0,700.0,0.0,"AND");
glColor3f(0,0,1);
drawstring(750.0,700.0,0.0,"TRICKS");
glColor3f(0.5,0.1,0.2);
drawstring(350.0,640.0,0.0,"Stop the traffic (Red Light)
MOUSE LEFT CLICK");
glColor3f(0.5,0.1,0.3);
drawstring(350.0,540.0,0.0,"Yellow Signal
MOUSE RIGHT BUTTON (HOLD ON)");
glColor3f(0.5,0.1,0.4);
drawstring(350.0,440.0,0.0,"Green Signal
MOUSE RIGHT BUTTON (RELEASE)");
glColor3f(0.4,0.1,0.5);
drawstring(350.0,340.0,0.0,"Allow vehicles to MOVE left to right
PRESS 'L'");
glColor3f(0.5,0.1,0.6);
drawstring(350.0,240.0,0.0,"Allow vehicles to MOVE right to left
PRESS 'R'");
glColor3f(0.5,0.1,0.7);
drawstring(350.0,140.0,0.0,"Speed up the vehicles
PRESS 'S'");
glColor3f(0.5,0.1,0.8);
drawstring(350.0,90.0,0.0,"Help
PRESS 'H'");
glColor3f(0.5,0.1,0.9);
drawstring(350.0,40.0,0.0,"Escape
PRESS 'ENTER'");
glFlush();
}

void control()
{
if(control_keyl!='l'||control_keyr!='r')
{
if(control_keyl=='l')
a=a+6;
if(control_keyr=='r')
b=b-6;
}

void myKeyboard( unsigned char key, int x, int y )

{
switch(key)
{
case 13:
if(flag==1)
{
flag=2;
mydisplay();
}
if(flag==0) //Ascii of 'enter' key is 13
{
flag=1;mydisplay();
}

break;
case 'l':control_keyl=key;
p=0;q=0;r=1;
break;
case 'r':control_keyr=key;
p=0;q=0;r=1;
break;
case 's':mydisplay();
break;
case 'h':flag=1;mydisplay();
break;
default:break;
}

void myMouse(int button,int state,int x,int y)


{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
traffic_regulator=0;
p=1;
q=0;
r=0;
}

if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)


{
traffic_regulator=0;
p=0;
q=1;
r=0;
}
if(button==GLUT_RIGHT_BUTTON && state==GLUT_UP)
{
traffic_regulator=1;
p=0;
q=0;
r=1;
}
glutPostRedisplay();

void mydisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if(flag==0)
frontscreen ();
if(flag==1)
helpscreen();
if(flag==2)
display();
glutSwapBuffers();
}

void update(int value)


{
a=a-6;
b=b+6;
control();
/*making day to night*/
if(blue!=0&&green!=0)
{blue-=.004;green-=.004;
}

glutPostRedisplay();
}

void display(void)
{
if(traffic_regulator)
glutTimerFunc(50,update,0);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(red,green,blue,0);/*back ground for sky*/
road();
bus();
signal();
car();
car2();

glFlush();
}

void road()
{
glPushMatrix();
glScaled(40.0,40.0,0.0);
glColor3f(0.1,0.1,0.1);
glBegin(GL_POLYGON);
//straight road
glVertex2f(0,5);
glVertex2f(40,5);
glVertex2f(40,10);
glVertex2f(0,10);
glEnd();
//green edge
glBegin(GL_POLYGON);
glColor3f(0.1,0.2,0.1);
glVertex2f(0,5);
glVertex2f(40,5);
glVertex2f(40,4);
glVertex2f(0,4);
glEnd();
//cross road
glColor3f(0.1,0.1,0.1);
glBegin(GL_POLYGON);
glVertex2f(10,10);
glVertex2f(15,10);
glVertex2f(0,40);
glVertex2f(4,40);
glEnd();
glPopMatrix();
}

void signal()
{
glPushMatrix();
glScaled(40.0,40.0,0.0);
//stand
glColor3f(0.1,0.2,0.1);
glBegin(GL_POLYGON);
glVertex2f(15,7);
glVertex2f(15,8);
glVertex2f(18,8);
glVertex2f(18,7);
glEnd();
//pole
glBegin(GL_POLYGON);
glVertex2f(16,7);
glVertex2f(17,8);
glVertex2f(17,15);
glVertex2f(16,15);
glEnd();
//board
glBegin(GL_POLYGON);
glVertex2f(15.5,15);
glVertex2f(17.5,15);
glVertex2f(17.5,10);
glVertex2f(15.5,10);
glEnd();
//red
glColor3f(p,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(16,14.5);
glVertex2f(17,14.5);
glVertex2f(17,14);
glVertex2f(16,14);
glEnd();
//yellow
glColor3f(q,q,0.0);
glBegin(GL_POLYGON);
glVertex2f(16,13.5);
glVertex2f(17,13.5);
glVertex2f(17,13);
glVertex2f(16,13);
glEnd();
//green
glColor3f(0.0,r,0.0);
glBegin(GL_POLYGON);
glVertex2f(16,12.5);
glVertex2f(17,12.5);
glVertex2f(17,12);
glVertex2f(16,12);
glEnd();
glPopMatrix();
}

void bus()
{
glPushMatrix();
glTranslated(a,50.0,0.0);
glScaled(40.0,40.0,0.0);
glColor3f(0.5,0.0,0.0);
//bus out line
glBegin(GL_POLYGON);
glVertex2f(25,8);
glVertex2f(25,9.5);
glVertex2f(26,11);
glVertex2f(32,11);
glVertex2f(32,8);
glEnd();
//window frame
glColor3f(0,0.1,1);
glBegin(GL_POLYGON);
glVertex2f(26.1,9.5);
glVertex2f(26.1,10.5);
glVertex2f(31.8,10.5);
glVertex2f(31.8,9.5);
glEnd();
//Doors
glColor3f(0,0.8,1);
glBegin(GL_POLYGON);
glVertex2f(26.2,9);
glVertex2f(26.2,10.4);
glVertex2f(27.7,10.4);
glVertex2f(27.7,9);
glEnd();

glColor3f(1,1,1);
glBegin(GL_POLYGON);
glVertex2f(27,8.4);
glVertex2f(27,10.4);
glVertex2f(27.7,10.4);
glVertex2f(27.7,8.4);
glEnd();
//small windows
glColor3f(0,1,1);
glBegin(GL_POLYGON);
glVertex2f(27.8,9.6);
glVertex2f(27.8,10.4);
glVertex2f(29,10.4);
glVertex2f(29,9.6);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(29.1,9.6);
glVertex2f(29.1,10.4);
glVertex2f(30.2,10.4);
glVertex2f(30.2,9.6);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(30.3,9.6);
glVertex2f(30.3,10.4);
glVertex2f(31.7,10.4);
glVertex2f(31.7,9.6);
glEnd();

//driver window
glColor3f(0,0.8,1);
glBegin(GL_POLYGON);
glVertex2f(25,9.5);
glVertex2f(26,11);
glVertex2f(26,9.5);
glEnd();
glPopMatrix();
//tyre
glPushMatrix();//front tyre
glTranslated(a+970,320,0.0);
glScaled(20.0,20.0,0.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(3.0,2.5);
glVertex2f(3.0,2.6);
glVertex2f(3.15,3.1);
glVertex2f(3.2,3.2);
glVertex2f(3.3,3.35);
glVertex2f(3.4,3.4);
glVertex2f(3.5,3.45);
glVertex2f(3.6,3.55);
glVertex2f(3.7,3.6);
glVertex2f(3.8,3.63);
glVertex2f(4.0,3.65);
glVertex2f(4.2,3.7);
glVertex2f(4.4,3.7);
glVertex2f(4.6,3.65);
glVertex2f(4.8,3.55);
glVertex2f(5.0,3.45);
glVertex2f(5.1,3.4);
glVertex2f(5.2,3.25);
glVertex2f(5.3,3.2);
glVertex2f(5.4,3.0);
glVertex2f(5.5,2.5);

glVertex2f(5.45,2.15);
glVertex2f(5.4,1.9);
glVertex2f(5.35,1.8);
glVertex2f(5.2,1.6);
glVertex2f(5.0,1.5);
glVertex2f(4.9,1.4);
glVertex2f(4.7,1.3);
glVertex2f(4.6,1.27);
glVertex2f(4.4,1.25);
glVertex2f(4.0,1.25);
glVertex2f(3.9,1.3);
glVertex2f(3.75,1.35);
glVertex2f(3.6,1.4);
glVertex2f(3.45,1.55);
glVertex2f(3.3,1.7);
glVertex2f(3.2,1.8);
glVertex2f(3.1,2.2);
glEnd();
glPopMatrix();

glPushMatrix();//back tyre
glTranslated(a+1140,320,0.0);
glScaled(20.0,20.0,0.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(3.0,2.5);
glVertex2f(3.0,2.6);
glVertex2f(3.15,3.1);
glVertex2f(3.2,3.2);
glVertex2f(3.3,3.35);
glVertex2f(3.4,3.4);
glVertex2f(3.5,3.45);
glVertex2f(3.6,3.55);
glVertex2f(3.7,3.6);
glVertex2f(3.8,3.63);
glVertex2f(4.0,3.65);
glVertex2f(4.2,3.7);
glVertex2f(4.4,3.7);
glVertex2f(4.6,3.65);
glVertex2f(4.8,3.55);
glVertex2f(5.0,3.45);
glVertex2f(5.1,3.4);
glVertex2f(5.2,3.25);
glVertex2f(5.3,3.2);
glVertex2f(5.4,3.0);
glVertex2f(5.5,2.5);

glVertex2f(5.45,2.15);
glVertex2f(5.4,1.9);
glVertex2f(5.35,1.8);
glVertex2f(5.2,1.6);
glVertex2f(5.0,1.5);
glVertex2f(4.9,1.4);
glVertex2f(4.7,1.3);
glVertex2f(4.6,1.27);
glVertex2f(4.4,1.25);
glVertex2f(4.0,1.25);
glVertex2f(3.9,1.3);
glVertex2f(3.75,1.35);
glVertex2f(3.6,1.4);
glVertex2f(3.45,1.55);
glVertex2f(3.3,1.7);
glVertex2f(3.2,1.8);
glVertex2f(3.1,2.2);
glEnd();
glPopMatrix();
}

void car()
{
glPushMatrix(); //making color for outer line
glTranslated(b,190.0,0.0);
glScaled(20.0,20.0,0.0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(2.5,2.5);
glVertex2f(3.0,3.5);
glVertex2f(3.5,3.75);
glVertex2f(4.0,4.0);
glVertex2f(4.5,4.0);
glVertex2f(5.0,3.75);
glVertex2f(5.5,3.5);
glVertex2f(5.75,3.0);
glVertex2f(6.0,2.5);
glVertex2f(16.5,2.5);
glVertex2f(16.75,3.0);
glVertex2f(17.0,3.5);
glVertex2f(17.5,3.75);
glVertex2f(18.0,4.0);
glVertex2f(18.5,4.0);
glVertex2f(19.0,3.75);
glVertex2f(19.5,3.5);
glVertex2f(19.75,3.0);
glVertex2f(20.0,2.5);
glVertex2f(21.0,2.5);
glVertex2f(21.0,4.0);
glVertex2f(21.5,4.0);
glVertex2f(21.0,4.5);
glVertex2f(20.0,5.0);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.5);
glVertex2f(13.0,6.0);
glVertex2f(12.0,6.5);
glVertex2f(11.0,7.0);
glVertex2f(6.0,7.0);
glVertex2f(5.0,6.5);
glVertex2f(4.5,6.25);
glVertex2f(4.25,6.0);
glVertex2f(4.0,5.75);
glVertex2f(3.5,5.5);
glVertex2f(3.0,5.5);
glVertex2f(1.9,5.45);
glVertex2f(1.8,5.4);
glVertex2f(1.7,5.35);
glVertex2f(1.6,5.3);
glVertex2f(1.5,5.25);
glVertex2f(1.4,5.15);
glVertex2f(1.3,5.0);
glVertex2f(1.2,4.85);
glVertex2f(1.1,4.7);
glVertex2f(1.0,4.3);
glVertex2f(1.0,3.2);
glVertex2f(1.1,3.05);
glVertex2f(1.2,2.9);
glVertex2f(1.3,2.9);
glVertex2f(1.4,2.75);
glVertex2f(1.5,2.65);
glVertex2f(1.6,2.6);
glVertex2f(1.7,2.55);
glVertex2f(1.8,2.5);
glVertex2f(1.9,2.45);
glVertex2f(2.0,2.5);
glEnd();

glColor3f(1.0,1.0,1.0); //color for outer window


glBegin(GL_POLYGON);
glVertex2f(5.0,5.0);
glVertex2f(14.0,5.0);
glVertex2f(11.5,6.5);
glVertex2f(10.5,6.75);
glVertex2f(7.0,6.75);
glEnd();

glColor3f(0.0,0.0,0.0); //making outer line for car


glBegin(GL_LINE_LOOP);
glVertex2f(2.5,2.5);
glVertex2f(3.0,3.5);
glVertex2f(3.5,3.75);
glVertex2f(4.0,4.0);
glVertex2f(4.5,4.0);
glVertex2f(5.0,3.75);
glVertex2f(5.5,3.5);
glVertex2f(5.75,3.0);
glVertex2f(6.0,2.5);
glVertex2f(16.5,2.5);
glVertex2f(16.75,3.0);
glVertex2f(17.0,3.5);
glVertex2f(17.5,3.75);
glVertex2f(18.0,4.0);
glVertex2f(18.5,4.0);
glVertex2f(19.0,3.75);
glVertex2f(19.5,3.5);
glVertex2f(19.75,3.0);
glVertex2f(20.0,2.5);
glVertex2f(21.0,2.5);
glVertex2f(21.0,4.0);
glVertex2f(21.5,4.0);
glVertex2f(21.0,4.5);
glVertex2f(20.0,5.0);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.5);
glVertex2f(13.0,6.0);
glVertex2f(12.0,6.5);
glVertex2f(11.0,7.0);
glVertex2f(6.0,7.0);
glVertex2f(5.0,6.5);
glVertex2f(4.5,6.25);
glVertex2f(4.25,6.0);
glVertex2f(4.0,5.75);
glVertex2f(3.5,5.5);
glVertex2f(3.0,5.5);
glVertex2f(1.9,5.45);
glVertex2f(1.8,5.4);
glVertex2f(1.7,5.35);
glVertex2f(1.6,5.3);
glVertex2f(1.5,5.25);
glVertex2f(1.4,5.15);
glVertex2f(1.3,5.0);
glVertex2f(1.2,4.85);
glVertex2f(1.1,4.7);
glVertex2f(1.0,4.3);
glVertex2f(1.0,3.2);
glVertex2f(1.1,3.05);
glVertex2f(1.2,2.9);
glVertex2f(1.3,2.9);
glVertex2f(1.4,2.75);
glVertex2f(1.5,2.65);
glVertex2f(1.6,2.6);
glVertex2f(1.7,2.55);
glVertex2f(1.8,2.5);
glVertex2f(1.9,2.45);
glVertex2f(2.0,2.5);
glEnd();

glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP); //outer line for design a car
glVertex2f(8.0,3.0);
glVertex2f(16.0,3.0);
glVertex2f(16.5,3.5);
glVertex2f(17.0,4.0);
glVertex2f(16.5,4.25);
glVertex2f(16.0,4.5);
glVertex2f(15.0,4.5);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.0);
glVertex2f(11.5,6.5);
glVertex2f(10.5,6.75);
glVertex2f(7.0,6.75);
glVertex2f(5.0,5.0);
glVertex2f(7.0,5.0);
glVertex2f(6.5,4.5);
glEnd();

glBegin(GL_LINES); //connecting outer line


glVertex2d(7.0,5.0);
glVertex2d(15.0,5.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,4.0);
glVertex2d(17.0,4.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,3.5);
glVertex2d(16.5,3.5);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,5.0);
glVertex2d(14.0,3.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(12.0,5.0);
glVertex2d(12.0,6.2);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(7.0,5.0);
glVertex2d(7.0,6.7);
glEnd();

glBegin(GL_POLYGON); //drawing a back tyre


glVertex2f(3.0,2.5);
glVertex2f(3.0,2.6);
glVertex2f(3.15,3.1);
glVertex2f(3.2,3.2);
glVertex2f(3.3,3.35);
glVertex2f(3.4,3.4);
glVertex2f(3.5,3.45);
glVertex2f(3.6,3.55);
glVertex2f(3.7,3.6);
glVertex2f(3.8,3.63);
glVertex2f(4.0,3.65);
glVertex2f(4.2,3.7);
glVertex2f(4.4,3.7);
glVertex2f(4.6,3.65);
glVertex2f(4.8,3.55);
glVertex2f(5.0,3.45);
glVertex2f(5.1,3.4);
glVertex2f(5.2,3.25);
glVertex2f(5.3,3.2);
glVertex2f(5.4,3.0);
glVertex2f(5.5,2.5);

glVertex2f(5.45,2.15);
glVertex2f(5.4,1.9);
glVertex2f(5.35,1.8);
glVertex2f(5.2,1.6);
glVertex2f(5.0,1.5);
glVertex2f(4.9,1.4);
glVertex2f(4.7,1.3);
glVertex2f(4.6,1.27);
glVertex2f(4.4,1.25);
glVertex2f(4.0,1.25);
glVertex2f(3.9,1.3);
glVertex2f(3.75,1.35);
glVertex2f(3.6,1.4);
glVertex2f(3.45,1.55);
glVertex2f(3.3,1.7);
glVertex2f(3.2,1.8);
glVertex2f(3.1,2.2);
glEnd();

glBegin(GL_POLYGON); //drawing front tyre


glVertex2f(17.0,2.5);
glVertex2f(17.0,2.6);
glVertex2f(17.15,3.1);
glVertex2f(17.2,3.2);
glVertex2f(17.3,3.35);
glVertex2f(17.4,3.4);
glVertex2f(17.5,3.45);
glVertex2f(17.6,3.55);
glVertex2f(17.7,3.6);
glVertex2f(17.8,3.63);
glVertex2f(18.0,3.65);
glVertex2f(18.2,3.7);
glVertex2f(18.4,3.7);
glVertex2f(18.6,3.65);
glVertex2f(18.8,3.55);
glVertex2f(19.0,3.45);
glVertex2f(19.1,3.4);
glVertex2f(19.2,3.25);
glVertex2f(19.3,3.2);
glVertex2f(19.4,3.0);

glVertex2f(19.5,2.5);
glVertex2f(19.45,2.15);
glVertex2f(19.4,1.9);
glVertex2f(19.35,1.8);
glVertex2f(19.2,1.6);
glVertex2f(19.0,1.5);
glVertex2f(18.9,1.4);
glVertex2f(18.7,1.3);
glVertex2f(18.6,1.27);
glVertex2f(18.4,1.25);
glVertex2f(18.0,1.25);
glVertex2f(17.9,1.3);
glVertex2f(17.75,1.35);
glVertex2f(17.6,1.4);
glVertex2f(17.45,1.55);
glVertex2f(17.3,1.7);
glVertex2f(17.2,1.8);
glVertex2f(17.1,2.2);
glEnd();
glPopMatrix();
}
void car2()
{
glPushMatrix(); //making color for outer line
glTranslated(b-2000,190.0,0.0);
glScaled(20.0,20.0,0.0);
glColor3f(1.0,1.0,0.4);
glBegin(GL_POLYGON);
glVertex2f(2.5,2.5);
glVertex2f(3.0,3.5);
glVertex2f(3.5,3.75);
glVertex2f(4.0,4.0);
glVertex2f(4.5,4.0);
glVertex2f(5.0,3.75);
glVertex2f(5.5,3.5);
glVertex2f(5.75,3.0);
glVertex2f(6.0,2.5);
glVertex2f(16.5,2.5);
glVertex2f(16.75,3.0);
glVertex2f(17.0,3.5);
glVertex2f(17.5,3.75);
glVertex2f(18.0,4.0);
glVertex2f(18.5,4.0);
glVertex2f(19.0,3.75);
glVertex2f(19.5,3.5);
glVertex2f(19.75,3.0);
glVertex2f(20.0,2.5);
glVertex2f(21.0,2.5);
glVertex2f(21.0,4.0);
glVertex2f(21.5,4.0);
glVertex2f(21.0,4.5);
glVertex2f(20.0,5.0);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.5);
glVertex2f(13.0,6.0);
glVertex2f(12.0,6.5);
glVertex2f(11.0,7.0);
glVertex2f(6.0,7.0);
glVertex2f(5.0,6.5);
glVertex2f(4.5,6.25);
glVertex2f(4.25,6.0);
glVertex2f(4.0,5.75);
glVertex2f(3.5,5.5);
glVertex2f(3.0,5.5);
glVertex2f(1.9,5.45);
glVertex2f(1.8,5.4);
glVertex2f(1.7,5.35);
glVertex2f(1.6,5.3);
glVertex2f(1.5,5.25);
glVertex2f(1.4,5.15);
glVertex2f(1.3,5.0);
glVertex2f(1.2,4.85);
glVertex2f(1.1,4.7);
glVertex2f(1.0,4.3);
glVertex2f(1.0,3.2);
glVertex2f(1.1,3.05);
glVertex2f(1.2,2.9);
glVertex2f(1.3,2.9);
glVertex2f(1.4,2.75);
glVertex2f(1.5,2.65);
glVertex2f(1.6,2.6);
glVertex2f(1.7,2.55);
glVertex2f(1.8,2.5);
glVertex2f(1.9,2.45);
glVertex2f(2.0,2.5);
glEnd();

glColor3f(1.0,1.0,1.0); //color for outer window


glBegin(GL_POLYGON);
glVertex2f(5.0,5.0);
glVertex2f(14.0,5.0);
glVertex2f(11.5,6.5);
glVertex2f(10.5,6.75);
glVertex2f(7.0,6.75);
glEnd();

glColor3f(0.0,0.0,0.0); //making outer line for car


glBegin(GL_LINE_LOOP);
glVertex2f(2.5,2.5);
glVertex2f(3.0,3.5);
glVertex2f(3.5,3.75);
glVertex2f(4.0,4.0);
glVertex2f(4.5,4.0);
glVertex2f(5.0,3.75);
glVertex2f(5.5,3.5);
glVertex2f(5.75,3.0);
glVertex2f(6.0,2.5);
glVertex2f(16.5,2.5);
glVertex2f(16.75,3.0);
glVertex2f(17.0,3.5);
glVertex2f(17.5,3.75);
glVertex2f(18.0,4.0);
glVertex2f(18.5,4.0);
glVertex2f(19.0,3.75);
glVertex2f(19.5,3.5);
glVertex2f(19.75,3.0);
glVertex2f(20.0,2.5);
glVertex2f(21.0,2.5);
glVertex2f(21.0,4.0);
glVertex2f(21.5,4.0);
glVertex2f(21.0,4.5);
glVertex2f(20.0,5.0);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.5);
glVertex2f(13.0,6.0);
glVertex2f(12.0,6.5);
glVertex2f(11.0,7.0);
glVertex2f(6.0,7.0);
glVertex2f(5.0,6.5);
glVertex2f(4.5,6.25);
glVertex2f(4.25,6.0);
glVertex2f(4.0,5.75);
glVertex2f(3.5,5.5);
glVertex2f(3.0,5.5);
glVertex2f(1.9,5.45);
glVertex2f(1.8,5.4);
glVertex2f(1.7,5.35);
glVertex2f(1.6,5.3);
glVertex2f(1.5,5.25);
glVertex2f(1.4,5.15);
glVertex2f(1.3,5.0);
glVertex2f(1.2,4.85);
glVertex2f(1.1,4.7);
glVertex2f(1.0,4.3);
glVertex2f(1.0,3.2);
glVertex2f(1.1,3.05);
glVertex2f(1.2,2.9);
glVertex2f(1.3,2.9);
glVertex2f(1.4,2.75);
glVertex2f(1.5,2.65);
glVertex2f(1.6,2.6);
glVertex2f(1.7,2.55);
glVertex2f(1.8,2.5);
glVertex2f(1.9,2.45);
glVertex2f(2.0,2.5);
glEnd();

glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP); //outer line for design a car
glVertex2f(8.0,3.0);
glVertex2f(16.0,3.0);
glVertex2f(16.5,3.5);
glVertex2f(17.0,4.0);
glVertex2f(16.5,4.25);
glVertex2f(16.0,4.5);
glVertex2f(15.0,4.5);
glVertex2f(15.0,5.0);
glVertex2f(14.0,5.0);
glVertex2f(11.5,6.5);
glVertex2f(10.5,6.75);
glVertex2f(7.0,6.75);
glVertex2f(5.0,5.0);
glVertex2f(7.0,5.0);
glVertex2f(6.5,4.5);
glEnd();

glBegin(GL_LINES); //connecting outer line


glVertex2d(7.0,5.0);
glVertex2d(15.0,5.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,4.0);
glVertex2d(17.0,4.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,3.5);
glVertex2d(16.5,3.5);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(15.0,5.0);
glVertex2d(14.0,3.0);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(12.0,5.0);
glVertex2d(12.0,6.2);
glEnd();

glColor3f(0.0,0.0,0.0); //connecting outer line


glBegin(GL_LINES);
glVertex2d(7.0,5.0);
glVertex2d(7.0,6.7);
glEnd();

glBegin(GL_POLYGON); //drawing a back tyre


glVertex2f(3.0,2.5);
glVertex2f(3.0,2.6);
glVertex2f(3.15,3.1);
glVertex2f(3.2,3.2);
glVertex2f(3.3,3.35);
glVertex2f(3.4,3.4);
glVertex2f(3.5,3.45);
glVertex2f(3.6,3.55);
glVertex2f(3.7,3.6);
glVertex2f(3.8,3.63);
glVertex2f(4.0,3.65);
glVertex2f(4.2,3.7);
glVertex2f(4.4,3.7);
glVertex2f(4.6,3.65);
glVertex2f(4.8,3.55);
glVertex2f(5.0,3.45);
glVertex2f(5.1,3.4);
glVertex2f(5.2,3.25);
glVertex2f(5.3,3.2);
glVertex2f(5.4,3.0);
glVertex2f(5.5,2.5);

glVertex2f(5.45,2.15);
glVertex2f(5.4,1.9);
glVertex2f(5.35,1.8);
glVertex2f(5.2,1.6);
glVertex2f(5.0,1.5);
glVertex2f(4.9,1.4);
glVertex2f(4.7,1.3);
glVertex2f(4.6,1.27);
glVertex2f(4.4,1.25);
glVertex2f(4.0,1.25);
glVertex2f(3.9,1.3);
glVertex2f(3.75,1.35);
glVertex2f(3.6,1.4);
glVertex2f(3.45,1.55);
glVertex2f(3.3,1.7);
glVertex2f(3.2,1.8);
glVertex2f(3.1,2.2);
glEnd();

glBegin(GL_POLYGON); //drawing front tyre


glVertex2f(17.0,2.5);
glVertex2f(17.0,2.6);
glVertex2f(17.15,3.1);
glVertex2f(17.2,3.2);
glVertex2f(17.3,3.35);
glVertex2f(17.4,3.4);
glVertex2f(17.5,3.45);
glVertex2f(17.6,3.55);
glVertex2f(17.7,3.6);
glVertex2f(17.8,3.63);
glVertex2f(18.0,3.65);
glVertex2f(18.2,3.7);
glVertex2f(18.4,3.7);
glVertex2f(18.6,3.65);
glVertex2f(18.8,3.55);
glVertex2f(19.0,3.45);
glVertex2f(19.1,3.4);
glVertex2f(19.2,3.25);
glVertex2f(19.3,3.2);
glVertex2f(19.4,3.0);

glVertex2f(19.5,2.5);
glVertex2f(19.45,2.15);
glVertex2f(19.4,1.9);
glVertex2f(19.35,1.8);
glVertex2f(19.2,1.6);
glVertex2f(19.0,1.5);
glVertex2f(18.9,1.4);
glVertex2f(18.7,1.3);
glVertex2f(18.6,1.27);
glVertex2f(18.4,1.25);
glVertex2f(18.0,1.25);
glVertex2f(17.9,1.3);
glVertex2f(17.75,1.35);
glVertex2f(17.6,1.4);
glVertex2f(17.45,1.55);
glVertex2f(17.3,1.7);
glVertex2f(17.2,1.8);
glVertex2f(17.1,2.2);
glEnd();
glPopMatrix();
}

void myinit()
{

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,1346.0,0.0,728.0);
}

void main(int argc, char* argv[])


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1346,728);
glutInitWindowPosition(0,0);
glutCreateWindow("Traffic signal");

/*call back functions*/


glutDisplayFunc(mydisplay);
glutKeyboardFunc(myKeyboard);
glutMouseFunc(myMouse);

myinit();
glutMainLoop();
}
Carro 2da dimensión y movimiento
#include <GL/glut.h>
GLfloat anguloCuboX = 0.0f;
GLfloat anguloCuboY = 0.0f;
GLfloat anguloEsfera = 0.0f;

GLint ancho=900;
GLint alto=600;
int ang =0 ;
int movx = 0;
int hazPerspectiva = 0;
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(hazPerspectiva)
gluPerspective(60.0f,(GLfloat)width/(GLfloat)height, 1.0f, 20.0f);

else
glOrtho(-4,4, -4, 4, 1, 10);
glMatrixMode(GL_MODELVIEW);
ancho = width;
alto = height;
}
void Piso(void)
{
glColor3f(1.0f, 0.0f, 1.0f);
glScalef(1.5f,0.0f,1.5f);
glBegin(GL_QUADS); //cara abajo
glVertex3f( 10.0f,-10.0f, -10.0f);
glVertex3f( 10.0f,-10.0f, 10.0f);
glVertex3f(-10.0f,-10.0f, 10.0f);
glVertex3f(-10.0f,-10.0f, -10.0f);
glEnd();
}
void carro(void)
{
glColor3f(0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON); //carro
glVertex3f(-1.5f,0.5f, -2.0f);
glVertex3f( 3.5f,0.5f, -2.0f);
glVertex3f(3.5f,2.0f, -2.0f);
glVertex3f(2.0f,2.0f, -2.0f);
glVertex3f(1.f,3.0f, -2.0f);
glVertex3f(-0.5f,3.0f, -2.0f);
glVertex3f(-1.5f,2.0f, -2.0f);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(15, 1.0f, 0.0f, 0.0f);
glRotatef(15, 0.0f, 1.0f, 0.0f);
Piso();

//dibuja 2da rueda


glLoadIdentity();

glColor3f(0.98f, 0.04f, 0.7f);


glTranslatef(movx,0.0,0.0);
carro();
glLoadIdentity();

glTranslatef(-.5f,0.5f,-1.0f);
glColor3f(0.98f, 0.04f, 0.7f);
glRotatef(ang,1.0,0.0,0.0);
glTranslatef(movx,0.0,0.0);
glutSolidSphere(0.5f, 16, 16);
glLoadIdentity();

glTranslatef(3.0f,0.5f,-1.0f);
glColor3f(0.98f, 0.04f, 0.7f);
glRotatef(ang,1.0,0.0,0.0);
glTranslatef(movx,0.0,0.0);
glutSolidSphere(0.5f, 16, 16);
glLoadIdentity();
glFlush();
glutSwapBuffers();
}
void init()
{
glClearColor(0,0,0,0);
glEnable(GL_DEPTH_TEST);
ancho = 600;
alto = 900;
}
void idle()
{
display();
}
void specialkeyevent( int key, int Xx, int Yy )
{
switch ( key ) {

case GLUT_KEY_LEFT:
movx-=1;
ang-=1;
display();
break;
case GLUT_KEY_RIGHT:
movx+=1;
ang-=1;
display();
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(ancho, alto);
glutCreateWindow("Carrito Movimiento");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutSpecialFunc(specialkeyevent );
glutMainLoop();
return 0;
}
Carrito 2D
#include <GL/glut.h>
float posicion;
int grados;
void reshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 5, -5, 5, 0.1f, 20);
glTranslatef(-3.5f, -2.0f, -15.0f);
glMatrixMode(GL_MODELVIEW);
}

void dibujar_tornillos(){
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.0f,0.0f, 1.0f);
glPushMatrix();
glTranslatef(-0.5f,0.0f, 0.0f);
glutSolidSphere(0.1f,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5f,0.0f, 0.0f);
glutSolidSphere(0.1f,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f,0.4f, 0.0f);
glutSolidSphere(0.1f,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2f,-0.5f, 0.0f);
glutSolidSphere(0.1f,20,20);
glPopMatrix();
glPushMatrix();
glTranslatef(0.2f,-0.5f, 0.0f);
glutSolidSphere(0.1f,20,20);
glPopMatrix();
glPopMatrix();
}
void dibujar_rueda_y_tornillos_delante(){
glPushMatrix();
glColor3f(1.0f,0.0f,0.5f);
glTranslatef(1.2f,0.0f, 0.0f);
glRotatef(grados, 0.0f, 0.0f, 1.0f);
glutSolidSphere(1.0f,30,30);
dibujar_tornillos();
glPopMatrix();
}
void dibujar_rueda_y_tornillos_detras(){
glPushMatrix();
glColor3f(1.0f,0.0f,0.5f);
glTranslatef(5.8f,0.0f, 3.0f);
glRotatef(grados, 0.0f, 0.0f, 1.0f);
glutSolidSphere(1.0f,30,30);
dibujar_tornillos();
glPopMatrix();
}

void dibujar_ventanas(){
glPushMatrix();
glColor3f(0.0f,0.0f,0.5f);
glTranslatef(1.1f,2.1f, 0.01f);
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.8f, 0.0f, 0.0f);
glVertex3f(0.8f, 0.8f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glBegin(GL_QUADS);
glVertex3f(0.8f, 0.0f, 0.0f);
glVertex3f(1.7f, 0.0f, 0.0f);
glVertex3f(1.7f, 0.8f, 0.0f);
glVertex3f(0.8f, 0.8f, 0.0f);
glEnd();
glTranslatef(1.9f,0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.9f, 0.0f, 0.0f);
glVertex3f(0.9f, 0.8f, 0.0f);
glVertex3f(0.0f, 0.8f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(0.9f, 0.0f, 0.0f);
glVertex3f(1.7f, 0.0f, 0.0f);
glVertex3f(0.9f, 0.8f, 0.0f);
glEnd();
glPopMatrix();
}
void techo(){
glPushMatrix();
glTranslatef(1.0f,2.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
glBegin(GL_QUADS);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(3.0f, 0.0f, 0.0f);
glVertex3f(3.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex3f(3.0f, 0.0f, 0.0f);
glVertex3f(4.0f, 0.0f, 0.0f);
glVertex3f(3.0f, 1.0f, 0.0f);
glEnd();
glPopMatrix();
}
dibujar_cuerpo_coche(){
glColor3f(0.0,1.0,0.0);
glTranslatef(posicion,0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(7.0f, 0.0f, 0.0f);
glVertex3f(7.0f, 2.0f, 0.0f);
glVertex3f (0.0f, 2.0f, 0.0f);
glEnd();
}
void display(){
GLfloat angulo;
int i;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
dibujar_cuerpo_coche();
techo();
dibujar_ventanas();
dibujar_rueda_y_tornillos_delante();
dibujar_rueda_y_tornillos_detras();
glutSwapBuffers();
}
void init(){
glClearColor(0,0,0,0);
posicion=0;
grados=0;
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(500, 500);
glutCreateWindow("cARRITO RUN RUN");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

You might also like