Skip to content

Instantly share code, notes, and snippets.

@ssi-anik
Last active August 29, 2015 14:26
Show Gist options
  • Save ssi-anik/5c5d917c2b3726fd9508 to your computer and use it in GitHub Desktop.
Save ssi-anik/5c5d917c2b3726fd9508 to your computer and use it in GitHub Desktop.
This program will animate a car, move up, down with up and down arrow key. Pause animation pressing button p. closes window pressing ESCAPE. And moves fast and slow using right and left arrow button
/*
3 ------------------ 4
/ \
/ \
/ \
1 --------------- 2 5 ------------------ 6
| |
| Car Demo |
| |
8 ----------------------------------------------------------------- 7
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define PI 3.14159
#define ESCAPE 27
#define PAUSE 112
#define MANUAL 109
#define LEFT_ARROW_KEY GLUT_KEY_LEFT
#define RIGHT_ARROW_KEY GLUT_KEY_RIGHT
#define UP_ARROW_KEY GLUT_KEY_UP
#define DOWN_ARROW_KEY GLUT_KEY_DOWN
int current_window_width = 0;
int current_window_height = 0;
int window = 0;
int starting_point_x = 0;
int starting_point_y = 0;
int moveSpeed = 500;
bool stepForword = true;
bool isPaused = false;
float rbgaConverter(int colorInDecimal){
return (float) colorInDecimal / 255;
}
void keyboard(unsigned char key, int x, int y){
switch (key) {
case ESCAPE:
glutDestroyWindow(window);
exit(0);
break;
case PAUSE:
isPaused = !isPaused;
default:
break;
}
}
void specialKeyboard(int key, int x, int y){
int car_width = 80;
int extra_space = 10;
switch (key) {
case UP_ARROW_KEY:
starting_point_y += 20;
if(starting_point_y >= current_window_height - 80)
starting_point_y = current_window_height - 80;
printf("Up button: ");
break;
case DOWN_ARROW_KEY:
starting_point_y -= 20;
if(starting_point_y < car_width + extra_space)
starting_point_y = car_width + extra_space;
printf("Down button: ");
break;
case RIGHT_ARROW_KEY: // speed up by decresing the waiting time
moveSpeed -= 30;
if(moveSpeed < 0)
moveSpeed = 0;
break;
case LEFT_ARROW_KEY: // slower speed by incrementing the waiting time
moveSpeed += 30;
break;
default:
break;
}
printf("%d\n", starting_point_y);
}
void init( void ){
glClearColor(0.15, 0.75, 1.0, 1.0);
glColor3f( 1.0f, 1.0f, 1.0f );
glPointSize( 4.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, current_window_width, 0.0, current_window_height);
}
void build_car(){
int point_one_y = starting_point_y + 0, //(current_window_height - 80),
point_two_y = point_one_y, //(current_window_height - 80),
point_three_y = point_two_y + 40, //(current_window_height - 40),
point_four_y = point_three_y, //(current_window_height - 40),
point_five_y = point_four_y - 40, //(current_window_height - 80),
point_six_y = point_five_y, //(current_window_height - 80),
point_seven_y = point_six_y - 30, //(current_window_height - 110),
point_eight_y = point_seven_y; //(current_window_height - 110);
int point_one_x = starting_point_x + 0,
point_two_x = point_one_x + 60,
point_three_x = point_two_x + 55,
point_four_x = point_three_x + 60,
point_five_x = point_four_x + 55,
point_six_x = point_five_x + 60,
point_seven_x = point_six_x + 0,
point_eight_x = point_one_x + 0;
glLineWidth(4.0);
glBegin(GL_LINES);
glColor3f(rbgaConverter(48), rbgaConverter(98), rbgaConverter(127));
glVertex2d(point_one_x, point_one_y);
glVertex2d(point_two_x, point_two_y);
glVertex2d( point_two_x, point_two_y);
glVertex2d( point_three_x, point_three_y);
glVertex2d( point_three_x, point_three_y);
glVertex2d( point_four_x, point_four_y);
glVertex2d( point_four_x, point_four_y);
glVertex2d( point_five_x, point_five_y);
glVertex2d( point_five_x, point_five_y);
glVertex2d( point_six_x, point_six_y);
glVertex2d( point_six_x, point_six_y);
glVertex2d( point_seven_x, point_seven_y);
glVertex2d( point_seven_x, point_seven_y);
glVertex2d( point_eight_x, point_eight_y);
glVertex2d( point_eight_x, point_eight_y);
glVertex2d( point_one_x, point_one_y);
glEnd();
}
void build_wheel(){
float left_wheel_x,
left_wheel_y,
right_wheel_x,
right_wheel_y;
float radius = 20.0f;
int left_wheel_center_point_x = starting_point_x + 60,
left_wheel_center_point_y = starting_point_y - 30,
right_wheel_center_point_x = starting_point_x + 230,
right_wheel_center_point_y = starting_point_y - 30;
glBegin(GL_TRIANGLE_FAN);
glColor3f(rbgaConverter(49),rbgaConverter(83), rbgaConverter(89));
left_wheel_x = (float) left_wheel_center_point_x + radius * cos(359 * PI/180.0f);
left_wheel_y = (float) left_wheel_center_point_y + radius * sin(359 * PI/180.0f);
for(int j = 0; j < 360; j++){
glVertex2f(left_wheel_x,left_wheel_y);
left_wheel_x = (float) left_wheel_center_point_x + radius * cos(j * PI/180.0f);
left_wheel_y = (float) left_wheel_center_point_y + radius * sin(j * PI/180.0f);
glVertex2f(left_wheel_x,left_wheel_y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glColor3f(rbgaConverter(49),rbgaConverter(83), rbgaConverter(89));
right_wheel_x = (float) right_wheel_center_point_x + radius * cos(359 * PI/180.0f);
right_wheel_y = (float) right_wheel_center_point_y + radius * sin(359 * PI/180.0f);
for(int j = 0; j < 360; ++j){
glVertex2f(right_wheel_x, right_wheel_y);
right_wheel_x = (float) right_wheel_center_point_x + radius * cos(j * PI/180.0f);
right_wheel_y = (float) right_wheel_center_point_y + radius * sin(j * PI/180.0f);
glVertex2f(right_wheel_x, right_wheel_y);
}
glEnd();
}
void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
build_car();
build_wheel();
glutSwapBuffers();
}
void Time(int value){
printf("State: %s\n", isPaused ? "Paused" : "Running");
if(isPaused){
glutTimerFunc(0, Time, 10);
} else{
if(starting_point_x + 320 >= current_window_width) stepForword = false;
else if(starting_point_x < 20) stepForword = true;
if(stepForword) starting_point_x += 20;
else starting_point_x -= 20;
glutPostRedisplay();
printf("Wait for: %d miliseconds\n", moveSpeed);
glutTimerFunc(moveSpeed, Time, 10);
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB );
int max_window_width = glutGet(GLUT_SCREEN_WIDTH);
int max_window_height = glutGet(GLUT_SCREEN_HEIGHT);
current_window_width = max_window_width - 15;
current_window_height = max_window_height - 100;
starting_point_x = 20;
starting_point_y = current_window_height - 80;
glutInitWindowSize(current_window_width, current_window_height);
glutInitWindowPosition(0, 0);
window = glutCreateWindow("Car animation");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeyboard);
glutTimerFunc(0, Time, 10);
init();
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment