Bike Code
Bike Code
Bike Code
***************************************/
#include<GL/glut.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159
#define NUM_SPOKES 5
#define SPOKE_ANGLE 72
/*****************************************
****************************************/
/*****************************************
******************************************/
/*******************************
********************************/
GLfloat camx,camy,camz;
GLfloat anglex,angley,anglez;
/****************************
int prevx,prevy;
GLenum Mouse;
/**************************
* variables
***************************/
GLfloat xpos,zpos,direction;
Abs(GLfloat);
GLfloat degrees(GLfloat);
GLfloat radians(GLfloat);
/*************************************
*************************************/
/**************************************
**************************************/
//#include "bike.h"
/******************************************
* A
* 1 ========== 2
* /\ | B
* / \ /5
* E/ \D /
* / \ /C
* / \/
* 3 ==========/ 4
* F
* 1 = 212,82
* 2 = 368,82
* 5 = 369,94
* 3 = 112,220
* 4 = 249,232
*
* 214 = 73 //angle between 2,1 & 4
* 124 = 55
* 142 = 52
* 143 = 73
* 134 = 50
* 431 = 57
****************************************/
GLUquadricObj *cylinder;
cylinder=gluNewQuadric(); glPushMatrix();
glTranslatef(0.0f,0.0f,0.0f);
gluCylinder(cylinder,radius,radius,length,15,5);
glPopMatrix();
{ glPushMatrix();
glRotatef(90.0f,0.0f,1.0f,0.0f);
ZCylinder(radius,length);
glPopMatrix();
updateScene()
if (-INC_SPEED < speed && speed < INC_SPEED) return; //"speed<INC_SPEED" because if again
accelerated, the bike stops moving if we put speed>INC_SPEED since speed value crosses INC_SPEED
xDelta = speed*cos(radians(direction + steering)); //To find out the new position of the bike once it starts
moving
this calculated new position to the variable xpos & zpos zpos -= zDelta;
sin_steering = sin(radians(steering)); //Angle calculated for rotation of our bike towards the direction of
handle rotation cos_steering = cos(radians(steering));
rotation = atan2(speed * sin_steering, BIKE_LENGTH + speed * cos_steering); //atan2(); used mostly to convert
from rectangular (x,y) to polar (r,0) direction = degrees(angleSum(radians(direction),rotation)); }
// angleSum(a,b) = (a+b) MOD 2*PI
{ a +=
b;
if (a < 0) return a+2*PI; //If returned angle is towards left and lesser than its threshold value,
increment its value to zero & do nothing
else if (a > 2*PI) return a-2*PI; //If returned angle is towards right and greater than its threshold
value, decrement its value to zero & do nothing
else return a; //If nothing, return the radians equivalent of angle 'a' }
/************************************************
* this.
* PLANE!
************************************************/ void
drawFrame()
{ glColor3f(1.0f,0.0f,0.0
f);
/********************************
frame.
*********************************/
glPushMatrix();
/******************************
*******************************/
glPushMatrix();
/***************************
* green color
***************************/
glColor3f(0.0f,1.0f,0.0f);
/**************************
* GEAR
***************************/
glPushMatrix();
glTranslatef(0.0f,0.0f,0.06f); glRotatef(-
2*pedalAngle,0.0f,0.0f,1.0f);
gear(0.08f,0.3f,0.03f,30,0.03f);
glPopMatrix();
/***************************
****************************/
glColor3f(1.0f,0.0f,0.0f);
glTranslatef(0.0f,0.0f,-0.2f);
ZCylinder(0.08f,0.32f); glPopMatrix();
/*****************************
*******************************/
glRotatef(RIGHT_ANGLE,0.0f,0.0f,1.0f);
XCylinder(ROD_RADIUS,RIGHT_ROD);
/*******************************
* the seat
*********************************/ glRotatef(MIDDLE_ANGLE-
RIGHT_ANGLE,0.0f,0.0f,1.0f);
XCylinder(ROD_RADIUS,MIDDLE_ROD);
/********************************
* new color
*********************************/
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(MIDDLE_ROD,0.0f,0.0f);
glRotatef(-MIDDLE_ANGLE,0.0f,0.0f,1.0f);
glScalef(0.3f,ROD_RADIUS,0.25f);
drawSeat(); /**********************
************************/
glColor3f(1.0f,0.0f,0.0f);
glPopMatrix();
/*********************************
* the frame.
*********************************/
/*********************************
* bike
**********************************/
glPushMatrix(); glRotatef(-
180.0f,0.0f,1.0f,0.0f);
XCylinder(ROD_RADIUS,BACK_CONNECTOR);
/***********************************
************************************/
glPushMatrix();
glTranslatef(0.5f,0.0f,WHEEL_OFFSET);
XCylinder(ROD_RADIUS,RADIUS_WHEEL+TUBE_WIDTH);
glPopMatrix(); glPushMatrix();
glTranslatef(0.5f,0.0f,-WHEEL_OFFSET);
XCylinder(ROD_RADIUS,RADIUS_WHEEL+TUBE_WIDTH);
glPopMatrix();
glPopMatrix();
/************************************
*************************************/
glPushMatrix();
glTranslatef(-(BACK_CONNECTOR+RADIUS_WHEEL+TUBE_WIDTH),0.0f,0.0f);
/********************************
**********************************/
glPushMatrix(); glRotatef(-
2*pedalAngle,0.0f,0.0f,1.0f);
drawTyre(); glColor3f(0.0f,1.0f,0.0f);
gear(0.03f,0.15f,0.03f,20,0.03f);
glColor3f(1.0f,0.0f,0.0f); glPopMatrix();
glRotatef(LEFT_ANGLE,0.0f,0.0f,1.0f);
/************************************
* Draw the two rods on the either
*************************************/
glPushMatrix(); glTranslatef(0.0f,0.0f,-
WHEEL_OFFSET);
XCylinder(ROD_RADIUS,WHEEL_LEN);
glPopMatrix(); glPushMatrix();
glTranslatef(0.0f,0.0f,WHEEL_OFFSET);
XCylinder(ROD_RADIUS,WHEEL_LEN);
glPopMatrix();
/*****************************
* same setup
******************************/
glTranslatef(WHEEL_LEN,0.0f,0.0f);
XCylinder(ROD_RADIUS,CRANK_ROD-1.0); //It is the rod which connects the handle bar and the
connector rods of the front wheel
glTranslatef(CRANK_ROD,0.0f,0.0f); glRotatef(-
LEFT_ANGLE,0.0f,0.0f,1.0f);
XCylinder(0.0000000001,TOP_LEN); //Its thickness has been reduced since it acts as a supporting rod,
hence it's not necessary to display it
/*******************************
********************************/
glPushMatrix();
glColor3f(0.5f,0.5f,0.5f);
glTranslatef(-0.6,-0.6f,0.0f);
XCylinder(GAS_TANK,1.0f);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f,0.5f,0.5f);
glTranslatef(0.6,0.5f,0.0f);
XCylinder(GAS_TANK,0.5f);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f,0.5f,0.5f);
glTranslatef(-3.2,-2.1f,0.3f);
glPopMatrix(); glPushMatrix();
glColor3f(0.5f,0.5f,0.5f); glTranslatef(-3.2,-
2.1f,0.3f); glRotatef(120.0,0.0f,0.0f,1.0f);
XCylinder(ROD_RADIUS+0.1f,0.5f); //Right upper
glPopMatrix(); glPushMatrix();
glColor3f(0.5f,0.5f,0.5f); glTranslatef(-3.2,-2.1f,-
0.3f);
glPopMatrix(); glPushMatrix();
glColor3f(0.5f,0.5f,0.5f); glTranslatef(-3.2,-2.1f,-0.3f);
glRotatef(120.0,0.0f,0.0f,1.0f);
glPopMatrix();
glColor3f(0.5f,0.5f,0.5f); glTranslatef(0.4,-0.8f,0.2f);
glRotatef(LEFT_ANGLE,0.0f,0.0f,1.0f);
glTranslatef(0.2,-0.8f,0.2f); glRotatef(LEFT_ANGLE-
50.0,0.0f,0.0f,1.0f); XCylinder(GAS_TANK,1.0f);
glColor3f(0.5f,0.5f,0.5f); glTranslatef(0.4,-0.8f,-
0.2f); glRotatef(LEFT_ANGLE,0.0f,0.0f,1.0f);
glTranslatef(0.2,-0.8f,-0.2f);
glRotatef(LEFT_ANGLE-50.0,0.0f,0.0f,1.0f);
glPopMatrix();
//Draw The Engine
glPushMatrix();
glColor3f(0.1,0.1,0.1); glTranslatef(-
0.4f,-1.2f,0.0f);
XCylinder(GAS_TANK,1.3f);
glPopMatrix();
/*****************************
* incline.
******************************/
glRotatef(-FRONT_INCLINE,0.0f,0.0f,1.0f);
/******************************
*******************************/
glPushMatrix();
glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
/******************************
*******************************/
glTranslatef(-0.3f,0.0f,0.0f);
/********************************
* We cannot use the incline
********************************/
glPushMatrix();
glRotatef(FRONT_INCLINE,0.0f,0.0f,1.0f);
glPushMatrix(); glTranslatef(-
0.6f,0.5f,-HANDLE_ROD/2);
ZCylinder(ROD_RADIUS,HANDLE_ROD);
glPopMatrix();
glPushMatrix(); glColor3f(1.0f,1.0f,0.0f);
glTranslatef(-0.6f,0.5f,-HANDLE_ROD/2); //Handle
ZCylinder(0.07f,HANDLE_ROD/4); //Rods
glTranslatef(0.0f,0.0f,HANDLE_ROD*3/4); //Are
ZCylinder(0.07f,HANDLE_ROD/4); //Drawn
glColor3f(1.0f,0.0f,0.0f); //Here
glPopMatrix(); glPopMatrix();
/*********************************
glPushMatrix();
/****************************
glTranslatef(-0.75,0.0,0.0);
XCylinder(ROD_RADIUS,FRONT_ROD);
/******************************
*******************************/
glTranslatef(CRANK_ROD,0.0f,0.0f); //Position set to the end of the main big connector rod in the
front to draw the front connector rods at that position glRotatef(CRANK_ANGLE,0.0f,0.0f,1.0f);
/*******************************
* wheel.
* to the connector.
********************************/
glPushMatrix();
glTranslatef(0.0f,0.0f,WHEEL_OFFSET-0.35);
XCylinder(ROD_RADIUS,CRANK_RODS);
glPopMatrix(); glPushMatrix();
glTranslatef(0.0f,0.0f,-WHEEL_OFFSET+0.35);
XCylinder(ROD_RADIUS,CRANK_RODS);
glPopMatrix();
/********************************
*********************************/
glTranslatef(CRANK_RODS,0.0f,0.0f);
glRotatef(-2*pedalAngle,0.0f,0.0f,1.0f);
glPopMatrix();
glPushMatrix();
glColor3f(1.0,1.0,0.0);
glRotatef(360.0,1.0,0.0,0.0);
glTranslatef(1.0,2.6,0.0);
glutSolidSphere(0.2,160.0,180.0);
glPopMatrix();
glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.3f,0.0f);
glRotatef(180.0,1.0,0.0,0.0);
glTranslatef(-1.0,-3.5f,0.0f);
glutSolidSphere(0.4,160.0,180.0);
glPopMatrix();
steering/2.0,1.0f,0.0f,0.0f); glColor3f(1.0f,1.0f,1.0f);
glRotatef(180.0,1.0,0.0,0.0); glTranslatef(-0.6,-3.6f,-
0.15f); glutSolidSphere(0.05,160.0,180.0); //Right
glPushMatrix();
glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f); glRotatef(180.0,1.0,0.0,0.0);
glTranslatef(-0.6,-3.6f,0.15f);
glPopMatrix();
glPushMatrix();
glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
glRotatef(180.0,1.0,0.0,0.0); glTranslatef(-0.6,-
3.6f,-0.15f); glutSolidSphere(0.025,160.0,180.0);
glPushMatrix(); glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f); glRotatef(180.0,1.0,0.0,0.0);
glTranslatef(-0.6,-3.6f,0.15f);
glPopMatrix();
glRotatef(-steering/2.0,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.3f,0.0f); glRotatef(-
270.0,-1.0,0.0,0.0); glTranslatef(-
0.6,0.0f,3.4f);
glutSolidCone(0.1,0.2,50.0,50.0);
glPopMatrix();
glTranslatef(-0.1,3.3f,-0.6f);
XCylinder(ROD_RADIUS,0.25f);
glPopMatrix();
glPushMatrix();
glRotatef(-steering/4.0,1.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,0.0f);
glRotatef(80.0,0.0,0.0,1.0);
glTranslatef(1.5,1.6,0.0);
XCylinder(GAS_TANK+0.15,1.5);
glPopMatrix();
glRotatef(-steering/4.0,1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glRotatef(150.0,0.0,0.0,1.0);
glTranslatef(1.8,-2.0,-0.6);
glPopMatrix();
glPushMatrix();
glRotatef(-steering/4.0,1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glRotatef(150.0,0.0,0.0,1.0); glTranslatef(1.8,-
2.0,0.6);
glPopMatrix();
glPushMatrix();
glRotatef(-steering/4.0,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.3f,0.0f);
glRotatef(180.0,0.0,0.0,1.0); glTranslatef(-0.3,-2.6,-
0.6);
glPopMatrix();
glPushMatrix();
glRotatef(-steering/4.0,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.3f,0.0f);
glRotatef(180.0,0.0,0.0,1.0); glTranslatef(-0.3,-
2.6,0.6);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,1.0f);
glRotatef(180.0,0.0,0.0,1.0);
glTranslatef(0.3,-1.6,0.6);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,1.0f);
glRotatef(180.0,0.0,0.0,1.0);
glTranslatef(0.3,-1.6,-0.6);
glPushMatrix();
glColor3f(0.5f,0.3f,0.0f);
glRotatef(100.0,0.0,0.0,1.0);
glTranslatef(0.0,0.0,-0.6);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f,0.3f,0.0f);
glRotatef(100.0,0.0,0.0,1.0);
glTranslatef(0.0,0.0,0.6);
glPopMatrix();
/*
*/
GLint i;
3.14159264;
r0 = inner_radius; r1 =
outer_radius - tooth_depth/2.0; r2
= outer_radius + tooth_depth/2.0;
glNormal3f( 0.0, 0.0, 1.0 ); //For flat shading,glNormal3f() has to be called for each face
/* draw front face */ glBegin( GL_QUAD_STRIP ); //For indicating
glEnd();
glBegin( GL_QUADS );
for (i=0;i<teeth;i++)
glEnd();
glBegin( GL_QUAD_STRIP );
for (i=0;i<=teeth;i++) { angle = i * 2.0*pi / teeth; glVertex3f(
glEnd(); /
* draw back
sides of teeth
*/
glBegin( GL
_QUADS );
for (i=0;i<teeth;i++)
glEnd();
glBegin( GL_QUAD_STRIP );
for (i=0;i<teeth;i++)
u = r1*cos(angle+3*da) - r2*cos(angle+2*da); v =
glEnd();
glShadeModel( GL_SMOOTH );
glBegin( GL_QUAD_STRIP );
for (i=0;i<=teeth;i++) { angle = i * 2.0*pi / teeth;
glEnd();
/******************************************
******************************************/
void drawChain()
GLfloat depth;
glColor3f(0.0f,1.0f,0.0f);
glEnable(GL_LINE_STIPPLE); mode=(mode+1)%2;
if(mode==0 && speed>0) glLineStipple(1,0x1c47); //Used ASCII value for situating on the teeth
exactly while bike is at rest else if(mode==1 && speed>0) glLineStipple(1,0x00FF); //Used
ASCII value for situating on the teeth exactly while bike is moving
glBegin(GL_LINES);
for(depth=0.06f;depth<=0.12f;depth+=0.01f) {
glVertex3f(-1.6f,0.15f,ROD_RADIUS);
glVertex3f(0.0f,0.3f,depth);
glVertex3f(-1.6f,-0.15f,ROD_RADIUS);
glVertex3f(0.0f,-0.3f,depth);
GL capabilities
/*********************************
**********************************/
glBegin(GL_POLYGON);
glEnd();
/**********************************
* Draw the bottom base part of the
* seat
***********************************/
glBegin(GL_POLYGON);
glEnd();
/**********************
**********************/
glBegin(GL_QUADS);
glVertex3f(1.0f,1.0f,-0.3f);
glVertex3f(1.0f,1.0f,0.3f); glVertex3f(1.0f,-
1.0f,0.3f); glVertex3f(1.0f,-1.0f,-0.3f);
glVertex3f(1.0f,1.0f,0.3f); glVertex3f(-
0.1f,1.0f,0.5f); glVertex3f(-0.1f,-1.0f,0.5f);
glVertex3f(1.0f,-1.0f,0.3f); glVertex3f(1.0f,1.0f,-
0.1f,-1.0f,-0.5f); glVertex3f(1.0f,-1.0f,-0.3f);
glVertex3f(-0.1f,1.0f,0.5f); glVertex3f(-
0.5f,1.0f,1.0f); glVertex3f(-0.5f,-1.0f,1.0f);
glVertex3f(-0.1f,-1.0f,0.5f);
glVertex3f(-0.1f,1.0f,-0.5f); glVertex3f(-
0.5f,1.0f,-1.0f); glVertex3f(-0.5f,-1.0f,-1.0f);
glVertex3f(-0.1f,-1.0f,-0.5f);
glVertex3f(-0.5f,1.0f,1.0f); glVertex3f(-
1.0f,1.0f,1.0f); glVertex3f(-1.0f,-1.0f,1.0f);
glVertex3f(-0.5f,-1.0f,1.0f);
glVertex3f(-0.5f,1.0f,-1.0f); glVertex3f(-
1.0f,1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-0.5f,-1.0f,-1.0f);
glVertex3f(-1.0f,1.0f,1.0f); glVertex3f(-
1.0f,1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();
{ glColor3f(0.0f,0.0f,1.0
f);
/***************************
* of the frame.
****************************/
/*****************************
* to the viewer
*****************************/
glPushMatrix();
glTranslatef(0.0f,0.0f,0.105f);
glTranslatef(0.25f,0.0f,0.0f);
/*************************
*************************/
glPushMatrix();
glScalef(0.5f,0.1f,0.1f);
glutSolidCube(1.0f); glPopMatrix();
/************************
************************/ glPushMatrix();
glTranslatef(0.25f,0.0f,0.15f);
glScalef(0.2f,0.02f,0.3f); glutSolidCube(1.0f);
glPopMatrix();
glPopMatrix();
/*******************************
*******************************/
glPushMatrix(); glTranslatef(0.0f,0.0f,-
0.105f); glTranslatef(0.25f,0.0f,0.0f);
/***************************
* rod
***************************/
glPushMatrix();
glScalef(0.5f,0.1f,0.1f);
glutSolidCube(1.0f); glPopMatrix();
/****************************
* the pedal
*****************************/
glPushMatrix();
glTranslatef(0.25f,0.0f,-0.15f);
glScalef(0.2f,0.02f,0.3f);
glutSolidCube(1.0f); glPopMatrix();
glPopMatrix();
glColor3f(1.0f,0.0f,0.0f);
}
void drawTyre(void)
{ in
t i;
glColor3f(1.0f,1.0f,1.0f);
glutSolidTorus(-0.17,0.29,100,100); //
glColor3f(1.0f,1.0f,1.0f);
glutSolidTorus(-0.05f,-0.75f,100,100);
glColor3f(1.0f,1.0f,0.5f);
glPushMatrix();
glTranslatef(0.0f,0.0f,-0.06f);
ZCylinder(0.02f,0.12f); glPopMatrix();
glutSolidTorus(0.02f,0.02f,3,20);
glColor3f(1.0f,1.0f,1.0f);
for(i=0;i<NUM_SPOKES;++i)
glPushMatrix();
glRotatef(i*SPOKE_ANGLE,0.0f,0.0f,1.0f);
glBegin(GL_LINES);
glVertex3f(0.0f,0.02f,0.0f);
glVertex3f(0.0f,0.86f,0.0f); glEnd();
glPopMatrix();
glColor3f(0.0f,0.0f,0.0f);
glutSolidTorus(TUBE_WIDTH,RADIUS_WHEEL,10,30);
glColor3f(1.0f,0.0f,0.0f);
void init()
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={100.0};
GLfloat light_directional[]={1.0,1.0,1.0,1.0};
GLfloat light_positional[]={1.0,1.0,1.0,1.0};
GLfloat light_diffuse[]={1.0,1.0,1.0};
reset();
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0,GL_POSITION,light_directional);
glLightfv(GL_LIGHT0,GL_AMBIENT,light_diffuse);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glColorMaterial(GL_FRONT,GL_DIFFUSE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
GLfloat i;
glColor3f(0.8f,0.4f,0.3f);
/************************************
************************************/
glBegin(GL_LINES); for(i=-100.0f ;
i<100.0f ; i += 1.0f)
glVertex3f(-100.0f,-RADIUS_WHEEL,i);
glVertex3f( 100.0f,-RADIUS_WHEEL,i);
glVertex3f(i,-RADIUS_WHEEL,-100.0f);
glVertex3f(i,-RADIUS_WHEEL,100.0f);
glEnd();
}
void display_bike(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_NORMALIZE);
glPushMatrix();
/*******************************
* remaining scene
*******************************/
glRotatef(angley,1.0f,0.0f,0.0f);
glRotatef(anglex,0.0f,1.0f,0.0f);
glRotatef(anglez,0.0f,0.0f,1.0f);
/***********************
* Start rendering
* the scene of
* the bike
**********************/
landmarks();
/****************************
****************************/
glPushMatrix();
glTranslatef(xpos,0.0f,zpos);
glRotatef(direction,0.0f,1.0f,0.0f);
drawFrame();
drawChain(); glPopMatrix();
glPopMatrix(); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); gluLookAt(camx,camy,camz,
camx,1.0,0.0,0.0,1.0,1.0);
glutSwapBuffers();
/************************
* value of a given
* float number
************************/
GLfloat Abs(GLfloat a)
{ if(a <
0.0f)
return -a;
else
return a;
/************************
* degrees
************************/
GLfloat degrees(GLfloat a)
{ return
a*180.0f/PI;
/************************
* radians
************************/
GLfloat radians(GLfloat a)
{ return
a*PI/180.0f;
/*************************
***************************/
void idle(void)
{ updateScene();
glutPostRedisplay();
}
void special(int key,int x,int y)
{ switch(ke
y)
case GLUT_KEY_UP:
camz -= 0.1f;
break;
case GLUT_KEY_DOWN:
camz += 0.1f;
break;
case GLUT_KEY_LEFT:
camx -= 0.1f;
break;
case GLUT_KEY_RIGHT:
camx += 0.1f;
break;
glutPostRedisplay();
/*****************************
*****************************/
void reset()
{ glColor3f(1.0f,1.0f,1.0f);
anglex=angley=anglez=0.0f;
pedalAngle=steering=0.0f;
Mouse=GLUT_UP;
pedalAngle=speed=steering=0.0f;
camx=camy=0.0f;
camz=5.0f; xpos=zpos=0.0f;
direction=0.0f; glPushMatrix();
glColor3f(0.5f,0.3f,0.0f);
glRotatef(100.0,0.0,0.0,1.0);
glTranslatef(-1.0,0.0,-0.6);
XCylinder(GAS_TANK-0.1,2.7);
glPopMatrix();
GLfloat r=0.0f,g=0.0f;
switch(key)
{ case
'r': case
'R':
reset();
break;
case 's':
case 'S':
glutDisplayFunc(operations_window);
break;
'C':
glutDisplayFunc(display_bike);
break;
case '1':
steering += INC_STEERING;
break;
case '2':
steering -= INC_STEERING;
break;
case '+':
speed += INC_SPEED;
break;
case '-':
speed -= INC_SPEED;
break;
case 27:
exit(1);
/********************************
* Where is my Bike?
*********************************/
/*********************************
* When you rotate the handle the
immediately.
speed too!!
************************************/
/***********************
* conditions ;>
***********************/
pedalAngle += speed;
pedalAngle = 0.0f;
pedalAngle -= 360.0f;
/******************
* Go! Display
*******************/
glutPostRedisplay();
} void mouse(int button,int state,int x,int
y) { switch(button)
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
Mouse=GLUT_DOWN;
prevx=x;
prevy=y;
if(state==GLUT_UP)
Mouse=GLUT_UP;
break;
case GLUT_RIGHT_BUTTON:
/* DO NOTHING */
break;
glutPostRedisplay();
/* DO NOTHING */
{
if(Mouse==GLUT_DOWN)
if(anglex < 0)
anglex+=360.0;
if(angley < 0)
angley+=360.0;
if(anglez < 0)
anglez += 360.0;
anglex-=360.0; if(angley
anglez-=360.0;
else
Mouse=GLUT_UP;
prevx=x;
prevy=y;
glutPostRedisplay();
}
void reshape(int w,int h)
{ glViewport(0,0,(GLsizei)w,
(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); gluLookAt(camx,camy,camz,
0.0,0.0,0.0, 0.0,1.0,0.0);
{ int len,
i;
glRasterPos3f(x,y,z);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, string[i]);
void welcome_window()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glFlush();
} void
operations_window()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glClearColor(0,0,0,0);
LEFT - USE 'LEFT ARROW'"); bitmap_output(-1.25,-0.4,0.50,"9. MOVE RIGHT - USE 'RIGHT ARROW'");
glutSwapBuffers();
glFlush();
{ glutInit(&argc,arg
v);
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB); glutInitWindowPosition(100,100);
glutInitWindowSize(WIN_WIDTH,WIN_HEIGHT);
glutCreateWindow("Bike");
init();
glutDisplayFunc(welcome_window);
glutReshapeFunc(reshape); glutIdleFunc(idle);
glutSpecialFunc(special);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutPassiveMotionFunc(passive);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
glutMainLoop();