/* racegl.c race track, crude car */ #include #include #include static GLfloat track[100][6][3]; static GLfloat car[3] = {5.0, 0.0, 0.0}; static GLfloat lane = 0.6; static GLfloat theta[3] = {-65.0, 10.0, 30.0}; static int n = 1; static int m = 6; static int width=600; static int height=600; static double Pi = 3.14159265358979323846; static double r = 5.0; /* turn radius */ static double lw = 0.3; /* lane width */ static double hs = 10.0; /* half straightaway */ static double incline = 0.1; /* turn incline */ static int wire = 0; static double move = 0.005; void nexti(int i, int j, float dx, float dy, float dz) { track[i][j][0] = track[i-1][j][0] + dx; track[i][j][1] = track[i-1][j][1] + dy; track[i][j][2] = track[i-1][j][2] + dz; } /* end nexti */ void nextj(int i, int j, float dx, float dy, float dz) { track[i][j][0] = track[i][j-1][0] + dx; track[i][j][1] = track[i][j-1][1] + dy; track[i][j][2] = track[i][j-1][2] + dz; } /* end nextj */ void build_track() { int i, j, k; double ang, dang=Pi/24.0; int nang = 23; /* minus 1 */ float dx, dy, dz, rt; float x, y, z; i=0; j=0; track[i][j][0] = -r; /* x */ track[i][j][1] = -hs; /* y */ track[i][j][2] = 0.0; /* z left side botton */ for(j=1; j0.0 && cy>=-hs && cy<=hs) /* going straight, +y */ { car[1] = car[1] + move; if(car[0]>r+(m-1)*lw) car[0] = r+(m-1)*lw; if(car[0]=-hs && cy<=hs) /* going straight, -y */ { car[1] = car[1] - move; if(car[0]<-(r+(m-1)*lw)) car[0] = -(r+(m-1)*lw); if(car[0]>-r) car[0]=-r; } if(cy>hs) /* on top */ { dx = cx; dy = cy-hs; rt = sqrt(dx*dx+dy*dy); dx = dx/rt; dy = dy/rt; car[0] = car[0] - move*dy; car[1] = car[1] + move*dx; } if(cy<-hs) /* on bottom */ { dx = cx; dy = cy+hs; rt = sqrt(dx*dx+dy*dy); dx = dx/rt; dy = dy/rt; car[0] = car[0] - move*dy; car[1] = car[1] + move*dx; } glutPostRedisplay(); } /* end drive */ void init() { /* set clear color to white */ glClearColor (1.0, 1.0, 1.0, 0.0); /* set fill color to black */ glColor3f(0.0, 0.0, 0.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0); glMatrixMode (GL_MODELVIEW); glViewport(0, 0, width, height); build_track(); car[0] = car[0] + lane; } /* end init */ int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize(width, height); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutIdleFunc(drive); glutKeyboardFunc(keyboard); glutSpecialFunc(special); /* glEnable(GL_DEPTH_TEST); */ init(); glutMainLoop(); return 0; } /* end main of racegl.c */