Created
July 31, 2015 03:59
-
-
Save ssi-anik/21db2f13220055de3299 to your computer and use it in GitHub Desktop.
Animated house, programmed in C++. Used openGl. Tree animates its branches. A man walk through the front road. Have fun playing with the code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <string.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 | |
#define ANGLE 0.3 | |
#define SCALE 0.9 | |
#define RAND 0.06 | |
int current_window_width = 0; | |
int current_window_height = 0; | |
int window = 0; | |
int moveSpeed = 500; | |
int human_head_center_x = 50; | |
int human_head_center_y = 180; | |
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; | |
char title[100]; | |
sprintf(title, "Animated house / %s", isPaused ? "Paused" : "Running"); | |
glutSetWindowTitle(title); | |
default: | |
break; | |
} | |
} | |
void specialKeyboard(int key, int x, int y){ | |
switch (key) { | |
case UP_ARROW_KEY: | |
break; | |
case DOWN_ARROW_KEY: | |
break; | |
case RIGHT_ARROW_KEY: | |
break; | |
case LEFT_ARROW_KEY: | |
break; | |
default: | |
break; | |
} | |
} | |
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 draw_polygoan(int numberOfRows, float points[][2], float colorCode[]){ | |
glBegin(GL_POLYGON); | |
glColor3fv(colorCode); | |
for(int i = 0 ; i < numberOfRows; ++i){ | |
glVertex2fv(points[i]); | |
} | |
glEnd(); | |
} | |
void draw_lines(int numberOfRows, float points[][2], float colorCode[], float lineWidth){ | |
lineWidth = lineWidth == 0 ? 1 : lineWidth; | |
glLineWidth(lineWidth); | |
glBegin(GL_LINES); | |
glColor3fv(colorCode); | |
for(int i = 0; i < numberOfRows; ++i){ | |
glVertex2fv(points[i]); | |
if( i+1 == numberOfRows) glVertex2fv(points[0]); | |
else glVertex2fv(points[i+1]); | |
} | |
glEnd(); | |
} | |
void build_house(){ | |
float top_left_shade_colorCode[] = { rbgaConverter( 97), rbgaConverter(178), rbgaConverter(110) }, | |
top_right_shade_colorCode[] = { rbgaConverter( 73), rbgaConverter(204), rbgaConverter( 80) }, | |
base_left_colorCode[] = { rbgaConverter( 30), rbgaConverter(117), rbgaConverter(178) }, | |
base_mid_colorCode[] = { rbgaConverter( 73), rbgaConverter(150), rbgaConverter(204) }, | |
window_space_colorCode[] = { rbgaConverter(201), rbgaConverter(245), rbgaConverter(204) }, | |
window_lid_colorCode[] = { rbgaConverter( 48), rbgaConverter( 76), rbgaConverter( 50) }, | |
window_rod_colorCode[] = { rbgaConverter( 48), rbgaConverter( 76), rbgaConverter( 50) }, | |
door_outline_colorCode[] = { rbgaConverter( 71), rbgaConverter( 76), rbgaConverter( 69) }, | |
door_inline_colorCode[] = { rbgaConverter(179), rbgaConverter(167), rbgaConverter(204) }, | |
door_collapsable_colorCode[] = { rbgaConverter( 60), rbgaConverter( 52), rbgaConverter( 76) }; | |
float top_left_shade_points[][2] = { { 140, 570}, {260, 470}, { 20, 470} }, // counter-clockwise | |
top_mid_shade_points[][2] = { { 140, 570}, {560, 570}, {680, 470}, {260, 470} }, // counter-clockwise | |
center_left_base_points[][2] = { { 20, 470}, {260, 470}, {260, 220}, { 20, 220} }, // counter-clockwise | |
center_mid_base_points[][2] = { { 260, 470}, {680, 470}, {680, 220}, {260, 220} }, // counter-clockwise | |
left_window_points[][2] = { { 80, 375}, {160, 375}, {160, 295}, { 80, 295} }, // counter-clockwise | |
left_window_on_left_lid_points[][2] = { { 80, 375}, { 60, 355}, { 60, 275}, { 80, 295} }, // anti-clockwise | |
left_window_on_right_lid_points[][2] = { { 140, 355}, {160, 375}, {160, 295}, {140, 275} }, // counter-clockwise | |
left_window_rod_points[][2] = { { 80, 375}, {160, 295}, {160, 375}, { 80, 295} }, // counter-clockwise | |
base_window_left_points[][2] = { { 290, 375}, {370, 375}, {370, 295}, {290, 295} }, // counter-clockwise | |
base_window_left_on_left_lid_points[][2] = { { 290, 375}, {310, 355}, {310, 275}, {290, 295} }, // counter-clockwise | |
base_window_left_on_right_lid_points[][2] = { { 370, 375}, {390, 355}, {390, 275}, {370, 295} }, // counter-clockwise | |
base_window_left_rod_points[][2] = { { 290, 375}, {370, 295}, {370, 375}, {290, 295} }, // counter-clockwise | |
base_window_right_points[][2] = { { 570, 375}, {650, 375}, {650, 295}, {570, 295} }, // counter-clockwise | |
base_window_right_on_left_lid_points[][2] = { { 570, 375}, {590, 355}, {590, 275}, {570, 295} }, // anti-clockwise | |
base_window_right_on_right_lid_points[][2] = { { 650, 375}, {670, 355}, {670, 275}, {650, 295} }, // counter-clockwise | |
base_window_right_rod_points[][2] = { { 570, 375}, {650, 295}, {650, 375}, {570, 295} }, // counter-clockwise | |
door_outer_line_points[][2] = { { 430, 420}, {530, 420}, {530, 220}, {430, 220} }, | |
door_inline_points[][2] = { { 440, 410}, {520, 410}, {520, 220}, {440, 220} }, | |
door_collapsable_points[][2] = { { 480, 410}, {480, 220} }; | |
draw_polygoan(3, top_left_shade_points, top_left_shade_colorCode); | |
draw_polygoan(4, top_mid_shade_points, top_right_shade_colorCode); | |
draw_polygoan(4, center_left_base_points, base_left_colorCode); | |
draw_polygoan(4, center_mid_base_points, base_mid_colorCode); | |
draw_polygoan(4, left_window_points, window_space_colorCode); | |
draw_lines (4, left_window_rod_points, window_rod_colorCode, 3); | |
draw_polygoan(4, left_window_on_left_lid_points, window_lid_colorCode); | |
draw_polygoan(4, left_window_on_right_lid_points, window_lid_colorCode); | |
draw_polygoan(4, base_window_left_points, window_space_colorCode); | |
draw_lines (4, base_window_left_rod_points, window_rod_colorCode, 3); | |
draw_polygoan(4, base_window_left_on_left_lid_points, window_lid_colorCode); | |
draw_polygoan(4, base_window_left_on_right_lid_points, window_lid_colorCode); | |
draw_polygoan(4, base_window_right_points, window_space_colorCode); | |
draw_lines (4, base_window_right_rod_points, window_rod_colorCode, 3); | |
draw_polygoan(4, base_window_right_on_left_lid_points, window_lid_colorCode); | |
draw_polygoan(4, base_window_right_on_right_lid_points, window_lid_colorCode); | |
draw_polygoan(4, door_outer_line_points, door_outline_colorCode); | |
draw_polygoan(4, door_inline_points, door_inline_colorCode); | |
draw_lines (2, door_collapsable_points, door_collapsable_colorCode, 5); | |
} | |
void build_road_zebra_crossing(){ | |
int distance = 20; | |
glBegin(GL_LINES); | |
glColor3f(rbgaConverter(92), rbgaConverter(127), rbgaConverter(120)); | |
for(int i = 0; i <= current_window_width - distance; i += distance){ | |
glVertex2f(i, 50); | |
glVertex2f(i + distance, 200); | |
} | |
glEnd(); | |
} | |
void build_road(){ | |
float colorCode[] = {1, 1, 1}; | |
float upperRoadLine[][2] = { {0, 200}, {current_window_width, 200}}, | |
downRoadLine[][2] = { {0, 50}, {current_window_width, 50}}; | |
draw_lines(2, upperRoadLine, colorCode, 2); | |
draw_lines(2, downRoadLine, colorCode, 2); | |
build_road_zebra_crossing(); | |
} | |
void build_human_head(){ | |
float radius = 15.0f; | |
glBegin(GL_TRIANGLE_FAN); | |
glColor3f(rbgaConverter(0),rbgaConverter(73), rbgaConverter(105)); | |
float x = (float) human_head_center_x + radius * cos(359 * PI/180.0f); | |
float y = (float) human_head_center_y + radius * sin(359 * PI/180.0f); | |
for(int j = 0; j < 360; j++){ | |
glVertex2f(x, y); | |
x = (float) human_head_center_x + radius * cos(j * PI/180.0f); | |
y = (float) human_head_center_y + radius * sin(j * PI/180.0f); | |
glVertex2f(x, y); | |
} | |
glEnd(); | |
} | |
void build_human_hands(){ | |
float left_hand[][2] = { { human_head_center_x, human_head_center_y - 15 }, { human_head_center_x - 25, human_head_center_y - 45} }; | |
float right_hand[][2] = { { human_head_center_x, human_head_center_y - 15 }, { human_head_center_x + 25, human_head_center_y - 45} }; | |
float colorCode[] = { rbgaConverter(0), rbgaConverter(73), rbgaConverter(105) }; | |
draw_lines(2, left_hand, colorCode, 4); | |
draw_lines(2, right_hand, colorCode, 4); | |
} | |
void build_human_body(){ | |
float colorCode[] = { rbgaConverter(0),rbgaConverter(73), rbgaConverter(105)}; | |
float bodyLine[][2] = { { human_head_center_x, human_head_center_y + 15}, {human_head_center_x, human_head_center_y - 80}}; | |
draw_lines(2, bodyLine, colorCode, 4); | |
} | |
void build_human_legs(){ | |
float left_leg[][2] = { { human_head_center_x, human_head_center_y - 80 }, { human_head_center_x - 25, human_head_center_y - 120} }; | |
float right_leg[][2] = { { human_head_center_x, human_head_center_y - 80 }, { human_head_center_x + 25, human_head_center_y - 120} }; | |
float colorCode[] = { rbgaConverter(0), rbgaConverter(73), rbgaConverter(105)}; | |
draw_lines(2, left_leg, colorCode, 4); | |
draw_lines(2, right_leg, colorCode, 4); | |
} | |
void build_human(){ | |
build_human_head(); | |
build_human_hands(); | |
build_human_body(); | |
build_human_legs(); | |
} | |
float random(float r) { | |
return (2 * r * rand()) / RAND_MAX - r; | |
} | |
void build_tree(float x1, float y1, float length1, float angle1, int depth){ | |
if (depth > 0) { | |
float x2 = x1 + length1 * cos(angle1); | |
float y2 = y1 + length1 * sin(angle1); | |
glColor3f(rbgaConverter(3), rbgaConverter(130), rbgaConverter(19)); | |
glBegin(GL_LINES); | |
glVertex2f(x1, y1); | |
glVertex2f(x2, y2); | |
glEnd(); | |
length1 = 15; | |
float length2 = length1 * (SCALE + random(RAND)); | |
float angle2 = angle1 + ANGLE + random(RAND); | |
build_tree(x2, y2, length2, angle2, depth-1); | |
length2 = length1 * (SCALE + random(RAND)); | |
angle2 = angle1 - ANGLE + random(RAND); | |
build_tree(x2, y2, length2, angle2, depth-1); | |
} | |
} | |
void display( void ) { | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
build_house(); | |
build_road(); | |
build_human(); | |
build_tree(800, 220, 180, 1.59, 8); | |
glutSwapBuffers(); | |
} | |
void Time(int value){ | |
printf("State: %s\n", isPaused ? "Paused" : "Running"); | |
if(isPaused){ | |
glutTimerFunc(0, Time, 10); | |
} else{ | |
if(human_head_center_x > current_window_width - 45) stepForword = false; | |
else if(human_head_center_x < 50 ) stepForword = true; | |
if(stepForword){ | |
human_head_center_x += 20; | |
human_head_center_y = 180; | |
} | |
else{ | |
human_head_center_x -= 20; | |
human_head_center_y = 250; | |
} | |
glutPostRedisplay(); | |
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; | |
glutInitWindowSize(current_window_width, current_window_height); | |
glutInitWindowPosition(0, 0); | |
window = glutCreateWindow("Animated house / Running"); | |
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