Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<GLU.h>
void *currentfont;
int flag=0;
const GLfloat
const GLfloat
const GLfloat
const GLfloat
const GLfloat
const GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
PI=3.141f;
TWO_PI=2*PI;
RAD_TO_DEG=180.0/PI;
INNER_RADIUS=90.0;
TRACK_WIDTH=20.0;
TRACK_MIDDLE=INNER_RADIUS+0.5*TRACK_WIDTH;
grass[]={0.0f,0.8f,0.1f};
road[]={0.2f,0.2f,0.2f};
car_body[]={0.2f,0.0f,0.9f};
car_axle[]={0.4f,0.4f,0.4f};
car_wheel[]={1.0,0.0,0.0};
house_wall[]={0.7f,0.4f,0.2f};
house_roof[]={0.8f,0.0,0.2f};
fog[4]={0.7f,0.7f,0.7f,1.0f};
enum
{
DISTANT,INSIDE,OUTSIDE,DRIVER,HOUSE,OTHER,BESIDE,BALLOON,HELICOPTER,AUTO
}
view=DISTANT;
GLUquadricObj *p;
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat
car_direction=0.0;
car_x_pos=100.0;
car_y_pos=0.0;
car_z_pos=0.0;
height=5.0;
zoom=50.0;
mouse_x=0.0;
mouse_y=0.0;
GLint win_width=400;
GLint win_height=300;
bool movie_mode=false;
long clock=0;
long next_switch_time=0;
bool fog_enabled=false;
GLfloat fog_density=0.01f;
GLfloat bumpiness=0.0;
glPushMatrix();
glColor3fv(car_axle);
glTranslatef(3.0,-3.0,2.0);
glRotatef(90.0,-1.0,0.0,0.0);
gluCylinder(p,0.5,0.5,6.0,10,1);
glColor3fv(car_wheel);
gluDisk(p,0.0,2.0,25,5);
glTranslatef(0.0,6.0,0.0);
gluDisk(p,0.0,1.0,25,5);
glPopMatrix();
glPushMatrix();
glTranslatef(9.0,-2.0,1.0);
glRotatef(90.0,-1.0,0.0,0.0);
glColor3fv(car_axle);
gluCylinder(p,0.3f,0.3f,4.0,10,1);
glColor3fv(car_wheel);
gluDisk(p,0.0,1.0,20,5);
glTranslatef(0.0,0.0,4.0);
gluDisk(p,0.0,1.0,20,5);
glPopMatrix();
void draw_scenery()
{
glColor3fv(grass);
gluDisk(p,0.0,INNER_RADIUS,50,5);
glColor3fv(road);
gluDisk(p,INNER_RADIUS,INNER_RADIUS+TRACK_WIDTH,50,5);
draw_house(-20.0,50.0,5.0);
draw_house(0.0,70.0,10.0);
draw_house(20.0,-10.0,8.0);
draw_house(40.0,120.0,10.0);
draw_house(-30.0,-50.0,7.0);
draw_house(10.0,-60.0,10.0);
draw_house(-20.0,75.0,8.0);
draw_house(-40.0,140.0,10.0);
}
void set_viewpoint()
{
switch(view)
{
case DISTANT:
gluLookAt(250.0,0.0,20.0*height,0.0,0.0,0.0,0.0,0.0,1.0);
draw_scenery();
glTranslatef(car_x_pos,car_y_pos,car_z_pos);
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
draw_car();
break;
case INSIDE:
gluLookAt(85.0,0.0,height,car_x_pos,car_y_pos,0.0,0.0,0.0,1.0);
draw_scenery();
glTranslatef(car_x_pos,car_y_pos,car_z_pos);
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
draw_car();
break;
case OUTSIDE:
gluLookAt(115.0,0.0,height,car_x_pos,car_y_pos,0.0,0.0,0.0,1.0);
draw_scenery();
glTranslatef(car_x_pos,car_y_pos,car_z_pos);
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
draw_car();
break;
case DRIVER:
gluLookAt(2.0,0.0,height,12.0,0.0,2.0,0.0,0.0,1.0);
draw_car();
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
glTranslatef(-car_x_pos,-car_y_pos,car_z_pos);
draw_scenery();
break;
case HOUSE:
glRotatef(RAD_TO_DEG*car_direction,0.0,-1.0,0.0);
gluLookAt(2.0,0.0,height,40.0-car_x_pos,120.0-car_y_pos,car_z_pos,0.0,0
.0,1.0);
draw_car();
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
glTranslatef(-car_x_pos,-car_y_pos,car_z_pos);
draw_scenery();
break;
case OTHER:
gluLookAt(25.0,5.0,height,0.0,0.0,3.0+car_z_pos,0.0,0.0,1.0);
draw_car();
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,1.0);
glTranslatef(-car_x_pos,-car_y_pos,0.0);
draw_scenery();
break;
case BESIDE:
gluLookAt(5.0,15.0,height,5.0,0.0,3.0+car_z_pos,0.0,0.0,1.0);
draw_car();
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,1.0);
glTranslatef(-car_x_pos,-car_y_pos,0.0);
draw_scenery();
break;
case BALLOON:
gluLookAt(150.0,75.0,250.0,200.0*mouse_x,200.0*mouse_y,0.0,0.0,0.0,1.0);
draw_scenery();
glTranslatef(car_x_pos,car_y_pos,car_z_pos);
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
draw_car();
break;
case HELICOPTER:
gluLookAt(200.0*mouse_x,200.0*mouse_y,200.0,0.0,0.0,0.0,0.0,0.0,1.0);
draw_scenery();
glTranslatef(car_x_pos,car_y_pos,car_z_pos);
glRotatef(RAD_TO_DEG*car_direction,0.0,0.0,-1.0);
draw_car();
break;
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
set_viewpoint();
glutSwapBuffers();
}
void set_projection()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(zoom,GLfloat(win_width)/GLfloat(win_height),1.0,500.0);
view=OTHER;
break;
case 6:
view=BESIDE;
break;
}
}
}
glutPostRedisplay();
}
void mouse(int x,int y)
{
mouse_x=(2.0*x)/win_width-1.0;
mouse_y=(2.0*y)/win_width-1.0;
}
void set_fog_density(GLfloat density)
{
fog_density=density;
cout<<"Fog density:"<<fog_density<<endl;
glFogf(GL_FOG_DENSITY,fog_density);
}
void keyboard(unsigned char key, int x,int y)
{
switch(key)
{
case'b':
if(bumpiness=0.0)
{
cout<<"Bumpy road! Use ',' and '>' to change bumpiness."<<endl;
bumpiness=0.1f;
}
else
{
cout<<"smooth road again!"<<endl;
bumpiness=0.0;
}
break;
case'<': bumpiness/=2.0;
break;
case'>': bumpiness*=2.0;
break;
case'f':
if(fog_enabled)
{
glDisable(GL_FOG);
cout<<"it's a clear day!"<<endl;
fog_enabled=false;
}
else
{
glEnable(GL_FOG);
cout<<"It's foggy:use +/- to change fog density."<<endl;
set_fog_density(0.01f);
fog_enabled=true;
}
break;
case'+':
set_fog_density(2.0*fog_density);
break;
case'-':
set_fog_density(0.5f*fog_density);
break;
case 27:
exit(0);
}
}
void keys(int key,int x,int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
zoom*=1.2f;
cout<<"zoom angle="<<zoom<<endl;
set_projection();
break;
case GLUT_KEY_RIGHT:
zoom/=1.2f;
cout<<"zoom angle="<<zoom<<endl;
set_projection();
break;
case GLUT_KEY_UP:
height+=1.0f;
cout<<"height="<<height<<endl;
break;
case GLUT_KEY_DOWN:
height-=1.0f;
cout<<"height="<<height<<endl;
break;
case GLUT_KEY_F1:
movie_mode=false;
view=DISTANT;
cout<<"Distant,fixed camera projection."<<endl;
break;
case GLUT_KEY_F2:
movie_mode=false;
cout<<"view from a panning camera inside the track."<<endl;
view=INSIDE;
break;
case GLUT_KEY_F3:
movie_mode=false;
cout<<"view from a panning camera outside the track."<<endl;
view=OUTSIDE;
break;
case GLUT_KEY_F4:
movie_mode=false;
view=DRIVER;
height=6.0;
zoom=75.0;
set_projection();
cout<<"view from the driver's seat."<<endl;
break;
case GLUT_KEY_F5:
movie_mode=false;
view=HOUSE;
cout<<"looking at a house while driving around."<<endl;
break;
case GLUT_KEY_F6:
movie_mode=false;
view=OTHER;
cout<<"looking back from the car in front."<<endl;
break;
case GLUT_KEY_F7:
movie_mode=false;
view=BESIDE;
cout<<"view from another car."<<endl;
break;
case GLUT_KEY_F8:
movie_mode=false;
view=BALLOON;
cout<<"view from a baloon: use mouse to change viewing direction"<<endl;
break;
case GLUT_KEY_F9:
movie_mode=false;
view=HELICOPTER;
cout<<"View from a helicopter: move mouse to fly around"<<endl;
break;
case GLUT_KEY_F10:
movie_mode=true;
clock=0;
next_switch_time=0;
cout<<"MOVIE MODE:views change at random intervals"<<endl;
break;
case GLUT_KEY_END:
exit(0);
}
}
void frontscreen(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
drawstring(20.0,90.0,0.0,"NAME OF THE COLLEGE ");
glColor3f(0.7,0,1);
drawstring(21,82,0.0,"DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING");
glColor3f(1,0.5,0);
drawstring(38,70,0.0,"A MINI PROJECT ON");
glColor3f(1,0,0);
drawstring(40,60,0.0,"PROJECT TITLE");
glColor3f(1,0.5,0);
drawstring(20,50,0.0,"BY:");
glColor3f(0.5,0,0.5);
drawstring(10,40,0.0,"NAME FIRST
(USN)");
drawstring(10,34,0.0,"NAME SECOND
(USN)");
glColor3f(1,0.5,0);
drawstring(68,50,0.0,"GUIDES:");
glColor3f(0.5,0.2,0.2);
drawstring(63,40,0.0,"GUIDE NAME FIRST");
drawstring(63,34,0.0,"GUIDE NAME SECOND");
glColor3f(1,0.1,1);
drawstring(32,10,0.0,"PRESS ENTER TO START");
glFlush();
}
void myKeyboardFunc( unsigned char key, int x, int y )
{
switch(key)
{
case 13:if(flag==0) //Ascii of 'enter' key is 13
flag=1;
break;
}
mydisplay();
}
void mydisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
if(flag==0)
frontscreen ();
if(flag==1)
display();
}