Skip to content

Instantly share code, notes, and snippets.

@thenasim
Last active September 19, 2020 17:08
Show Gist options
  • Save thenasim/807048ee94fcd78e31bcdc22c1b83ee4 to your computer and use it in GitHub Desktop.
Save thenasim/807048ee94fcd78e31bcdc22c1b83ee4 to your computer and use it in GitHub Desktop.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <math.h>
#include <ctime>
#include <cstdlib>
#include <iostream>
#define PI 3.1416
/* GLOBAL VARIABLES */
GLint i, j, k;
GLfloat spin = 0.0;
GLfloat car_speed = -110.0;
GLfloat car_speed2 = -150.0;
GLfloat car_speed3 = -250.0;
GLfloat moon_spin_x = 750, moon_spin_y = 50;
GLfloat sky_red = 0.05, sky_blue = 0.05, sky_green = 0.15;
int sky_condition = 1;
const int total_star = 50;
int random_star[total_star*2];
int getRandom(int min, int max) {
return (rand() % max) + min;
}
void genrateRandom() {
for (int i = 0; i < total_star*2; i=i+2) {
random_star[i] = getRandom(30,970);
random_star[i+1] = getRandom(300,470);
}
}
void circle(GLdouble rad) {
GLint points = 50;
GLdouble delTheta = (2.0 * PI) / (GLdouble)points;
GLdouble theta = 0.0;
glBegin(GL_POLYGON);
for( i = 0; i <=50; i++, theta += delTheta ) {
glVertex2f(rad * cos(theta), rad * sin(theta));
}
glEnd();
}
/* CAR START */
void Car_Model() {
/* CAR */
// CAR BODY
glBegin(GL_POLYGON);
glColor3f(0.411, 0.23, 0.836);
glVertex2i(15, 35);
glVertex2i(0, 23);
glVertex2i(0, 10);
glVertex2i(80, 10);
glVertex2i(80, 23);
glVertex2i(60, 23);
glVertex2i(50, 35);
glEnd();
// CAR WHEEL 1
glColor3f(0.45, 0.0, 0.25);
glPushMatrix();
glTranslatef(13,12,0);
circle(7);
glPopMatrix();
// CAR WHEEL 2
glColor3f(0.45, 0.0, 0.25);
glPushMatrix();
glTranslatef(63,12,0);
circle(7);
glPopMatrix();
}
/* CAR END */
/* ROAD STRIPE START */
void roadStripe() {
glBegin(GL_POLYGON);
glColor3f(0.4,0.4,0.4);
glVertex2i(0, 29);
glVertex2i(0, 31);
glVertex2i(20, 31);
glVertex2i(20, 29);
glEnd();
}
/* ROAD STRIPE END */
/* FIELD START */
void field() {
glBegin(GL_POLYGON);
// glColor3f(1.0, .693, 0.1);
glColor3f(0.05, 0.35, 0.15);
glVertex2i(0, 50);
glVertex2i(0, 70);
glVertex2i(1000, 70);
glVertex2i(1000, 50);
glEnd();
// ROAD
glBegin(GL_POLYGON);
glColor3f(0.10,0.11,0.11);
glVertex2i(0, 0);
glVertex2i(0, 60);
glVertex2i(1000, 60);
glVertex2i(1000, 0);
glEnd();
// ROAD STRIPES
roadStripe();
for (int i = 45; i < 1050; i=i+45) {
glPushMatrix();
glTranslatef(i,0,0);
roadStripe();
glPopMatrix();
}
// Car Model 1
glPushMatrix();
glTranslatef(car_speed,20,0);
Car_Model();
glPopMatrix();
// Car Model 2
glPushMatrix();
glTranslatef(car_speed2,0,0);
Car_Model();
glPopMatrix();
// Car Model 3
glPushMatrix();
glTranslatef(car_speed3,0,0);
Car_Model();
glPopMatrix();
}
/* FIELD END */
/* TREE START */
void Tree_Model_One() {
glPushMatrix();
glTranslatef(110,110,0);
circle(15);
glPopMatrix();
glPushMatrix();
glTranslatef(110,100,0);
circle(15);
glPopMatrix();
glBegin(GL_POLYGON);
glColor3f(0.38, 0.21, 0.26);
glVertex2f(109, 70);
glVertex2f(109, 90);
glVertex2f(111, 90);
glVertex2f(111, 70);
glEnd();
}
void Tree_Model_Two() {
glPushMatrix();
glTranslatef(130,130,0);
circle(5);
glPopMatrix();
glPushMatrix();
glTranslatef(125,126,0);
circle(5);
glPopMatrix();
glPushMatrix();
glTranslatef(135,126,0);
circle(5);
glPopMatrix();
glPushMatrix();
glTranslatef(130,125,0);
circle(5);
glPopMatrix();
glBegin(GL_POLYGON);
glColor3f(0.38, 0.21, 0.26);
glVertex2f(129, 110);
glVertex2f(129, 124);
glVertex2f(131, 124);
glVertex2f(131, 110);
glEnd();
}
void Tree_Model_Three() {
glBegin(GL_POLYGON);
glVertex2f(125, 123);
glVertex2f(133, 145);
glVertex2f(141, 123);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.38, 0.21, 0.26);
glVertex2f(132, 110);
glVertex2f(132, 124);
glVertex2f(134, 124);
glVertex2f(134, 110);
glEnd();
}
void Tree_One() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(0,0,0);
Tree_Model_One();
glPopMatrix();
}
void Tree_Two() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(540,0,0);
Tree_Model_One();
glPopMatrix();
}
void Tree_Three() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(750,0,0);
Tree_Model_One();
glPopMatrix();
}
void Tree_Four() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(292,0,0);
Tree_Model_One();
glPopMatrix();
}
void Tree_Five() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(30,-40,0);
Tree_Model_Two();
glPopMatrix();
}
void Tree_Six() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(50,-40,0);
Tree_Model_Two();
glPopMatrix();
}
void Tree_Seven() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(322,-40,0);
Tree_Model_Two();
glPopMatrix();
}
void Tree_Eight() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(350,-40,0);
Tree_Model_Two();
glPopMatrix();
}
void Tree_Nine() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(780,-40,0);
Tree_Model_Two();
glPopMatrix();
}
void Tree_Ten() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(90,-40,0);
Tree_Model_Three();
glPopMatrix();
}
void Tree_Eleven() {
glColor3f(0.13, 0.37, 0.31); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(125,-40,0);
Tree_Model_Three();
glPopMatrix();
}
void Tree_Twelve() {
glColor3f(0.0, 0.5, 0.1); // greenish
// glColor3f(1, 0.5, 0.0);
glPushMatrix();
glTranslatef(408,-40,0);
Tree_Model_Three();
glPopMatrix();
}
/* TREE END */
/* HILL START */
void hill_big() {
// Hill
glBegin(GL_POLYGON);
glColor3f(0.938, 0.21, 0.626);
glVertex2i(70, 70);
glVertex2i(200, 225);
glVertex2i(330, 70);
glEnd();
// Hill_Snow
glBegin(GL_POLYGON);
glColor3f(1.25, .724, .730);
glVertex2i(200, 225);
glVertex2i(230, 190);
glVertex2i(220, 180);
glVertex2i(200, 190);
glVertex2i(190, 180);
glVertex2i(170, 190);
glEnd();
}
void hill_small() {
// Hill_Small
glBegin(GL_POLYGON);
glColor3f(0.911, 0.23, 0.26);
glVertex2i(250, 100);
glVertex2i(310, 175);
glVertex2i(370, 100);
glEnd();
// Hill_Small_Snow
glBegin(GL_POLYGON);
glColor3f(1.25, .724, .730);
glVertex2i(290, 150);
glVertex2i(310, 175);
glVertex2i(330, 150);
glVertex2i(325, 140);
glVertex2i(310, 150);
glVertex2i(300, 140);
glEnd();
}
void Hill_Big_One() {
glPushMatrix();
glTranslatef(0,0,0);
hill_big();
glPopMatrix();
}
void Hill_Big_Two() {
glPushMatrix();
glTranslatef(550,-20,0);
hill_big();
glPopMatrix();
}
void Hill_Small_One() {
glPushMatrix();
glTranslatef(10,0,0);
hill_small();
glPopMatrix();
}
/* HILL END */
/* TILLA START */
void Tilla_One_Model() {
// Tilla
glBegin(GL_POLYGON);
glColor3f(0.05, 0.3, 0.15);
glVertex2i(125, 70);
glVertex2i(150, 80);
glVertex2i(160, 90);
glVertex2i(170, 90);
glVertex2i(180, 100);
glVertex2i(190, 105);
glVertex2i(200, 108);
glVertex2i(300, 110);
glVertex2i(300, 70);
glEnd();
}
void Tilla_Four() {
glColor3f(0.0, 0.0, 0.0);
glPushMatrix();
glTranslatef(380,0,0);
Tilla_One_Model();
glPopMatrix();
}
/* TILLA END */
/* HOUSE START */
void house() {
// House_Roof
glBegin(GL_POLYGON);
glColor3f(.3, 0.0, 0.0);
glVertex2i(285, 105);
glVertex2i(285, 130);
glVertex2i(380, 115);
glVertex2i(380, 105);
glEnd();
// House_Roof_Shadow
glBegin(GL_POLYGON);
glColor3f(.3, 0.0, 0.0);
glVertex2i(285, 105);
glVertex2i(285, 120);
glVertex2i(380, 105);
glVertex2i(380, 105);
glEnd();
// House_Fence
glBegin(GL_POLYGON);
glColor3f(.955, 1.0, 1.0);
glVertex2i(290, 70);
glVertex2i(290, 104);
glVertex2i(375, 104);
glVertex2i(375, 70);
glEnd();
// House_Fence_Shadow
glBegin(GL_POLYGON);
glColor3f(1, 0.924, 0.930);
glVertex2i(310, 70);
glVertex2i(350, 104);
glVertex2i(375, 104);
glVertex2i(375, 70);
glEnd();
// House_Door
glBegin(GL_POLYGON);
glColor3f(0.88, 0.21, 0.826);
glVertex2i(330, 70);
glVertex2i(330, 100);
glVertex2i(350, 100);
glVertex2i(350, 70);
glEnd();
// House_Window1
glBegin(GL_POLYGON);
glColor3f(1, 0.0, 0.0);
glVertex2i(295, 75);
glVertex2i(295, 90);
glVertex2i(310, 90);
glVertex2i(310, 75);
glEnd();
// House_Window2
glBegin(GL_POLYGON);
glColor3f(1, 0.0, 0.0);
glVertex2i(312, 75);
glVertex2i(312, 90);
glVertex2i(327, 90);
glVertex2i(327, 75);
glEnd();
// House_Window3
glBegin(GL_POLYGON);
glColor3f(1, 0.0, 0.0);
glVertex2i(355, 75);
glVertex2i(355, 90);
glVertex2i(370, 90);
glVertex2i(370, 75);
glEnd();
// House_Small_Roof
glBegin(GL_POLYGON);
glColor3f(.3, 0.0, 0.0);
glVertex2i(250, 90);
glVertex2i(257, 104);
glVertex2i(290, 104);
glVertex2i(290, 90);
glEnd();
// House_Small_Fence
glBegin(GL_POLYGON);
glColor3f(.9555, .9724, .930);
glVertex2i(255, 70);
glVertex2i(255, 90);
glVertex2i(290, 90);
glVertex2i(290, 70);
glEnd();
// House_Small_Door
glBegin(GL_POLYGON);
glColor3f(0.411, 0.23, 0.836);
glVertex2i(260, 70);
glVertex2i(260, 80);
glVertex2i(285, 80);
glVertex2i(285, 70);
glEnd();
}
void house_one() {
glPushMatrix();
glTranslatef(0,0,0);
house();
glPopMatrix();
}
void house_two() {
glPushMatrix();
glTranslatef(450,0,0);
house();
glPopMatrix();
}
void house_three() {
glPushMatrix();
glTranslatef(320, 37,0);
house();
glPopMatrix();
}
/* HOUSE END */
/* WINDMILL START */
void Windmill_Stand_Model() {
glColor3f(0.738, 0.741, 0.736);
glBegin(GL_POLYGON);
glVertex2i(375, 100);
glVertex2i(380, 240);
glVertex2i(384, 240);
glVertex2i(390, 100);
glEnd();
}
void Windmill_Blade() {
///Blade_One
glPushMatrix();
glRotatef(spin,0,0,90);
glBegin(GL_POLYGON);
glVertex2i(-5, 0);
glVertex2i(-85, -36);
glVertex2i(-83, -37);
glVertex2i(-3, -8);
glEnd();
glPopMatrix();
///Blade_Two
glPushMatrix();
glRotatef(spin,0,0,90);
glBegin(GL_POLYGON);
glVertex2i(0, 5);
glVertex2i(45, 70);
glVertex2i(50, 73);
glVertex2i(5, 0);
glEnd();
glPopMatrix();
///Blade_Three
glPushMatrix();
glRotatef(spin,0,0,90);
glBegin(GL_POLYGON);
glVertex2i(68, -78);
glVertex2i(0,0);
glVertex2i(5, 5);
glVertex2i(70, -77);
glEnd();
glPopMatrix();
}
void Windmill() {
// Windmill_Stand
glColor3f(0.838, 0.841, 0.836);
glPushMatrix();
Windmill_Stand_Model();
glPopMatrix();
// Windmill_Motor
glColor3f(0.911, 0.923, 0.936);
glPushMatrix();
glTranslatef(380,250,0);
circle(10);
glPopMatrix();
// Windmill_Rotary_Blades
glColor3f(0.911, 0.923, 0.936);
glPushMatrix();
glTranslatef(380,251,0);
Windmill_Blade();
glPopMatrix();
}
void Windmill_One() {
glColor3f(0.911, 0.923, 0.936);
glPushMatrix();
glTranslatef(0,-40,0);
Windmill();
glPopMatrix();
}
void Windmill_Two() {
glColor3f(0.11, 0.23, 0.36);
glPushMatrix();
glTranslatef(508,-70,0);
Windmill();
glPopMatrix();
}
void Windmill_Three() {
glColor3f(0.11, 0.23, 0.36);
glPushMatrix();
glTranslatef(108,-90,0);
Windmill();
glPopMatrix();
}
/* WINDMILL END */
/* SKY START */
void Sky() {
glBegin(GL_POLYGON);
glColor3f(sky_red, sky_blue, sky_green);
glVertex2f(0, 0);
glVertex2f(1000, 0);
glVertex2f(1000, 500);
glVertex2f(0,500);
glEnd();
}
/* SKY END */
/* STAR START */
void Star() {
for (int i = 0; i < total_star*2; i=i+2) {
glColor3f(0.6, 0.6, 0.6);
glPushMatrix();
glTranslatef(random_star[i], random_star[i+1], 0);
circle(1);
glPopMatrix();
}
}
/* STAR END */
/* MOON START */
void Moon() {
glColor3f(0.7, 0.7, 0.7);
glPushMatrix();
glTranslatef(moon_spin_x, moon_spin_y, 0);
circle(20);
glPopMatrix();
}
void Moon_Animate() {
moon_spin_x -= 0.01;
moon_spin_y += 0.02;
if (moon_spin_y >= 550) {
moon_spin_x = 750;
moon_spin_y = 50;
}
}
/* MOON END */
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// DRAW
Sky();
Moon();
Star();
Windmill_Three();
Hill_Big_One();
Tilla_Four();
house_three();
Hill_Big_Two();
Hill_Small_One();
Windmill_One();
Windmill_Two();
house_one();
house_two();
Tree_One();
Tree_Two();
Tree_Three();
Tree_Four();
Tree_Five();
Tree_Six();
Tree_Seven();
Tree_Eight();
Tree_Nine();
Tree_Ten();
Tree_Eleven();
Tree_Twelve();
field();
glFlush();
}
/* ANIMATION FUNCTION */
void SpinWindmill() {
spin += 0.1;
car_speed += 0.12;
car_speed2 += 0.10;
car_speed3 += 0.08;
/* Sky Color Change Start */
/*sky_red += (0.00003 * sky_condition);
sky_blue += (0.00003 * sky_condition);
sky_green += (0.00003 * sky_condition);
if (sky_red >= 0.9 || sky_blue >= 0.9 || sky_green >= 0.9) {
sky_condition = -1;
}
if (sky_red <= 0.1 || sky_blue <= 0.1 || sky_green <= 0.1) {
sky_condition = 1;
}*/
/* Sky Color Change End */
if (car_speed > 1100) {
car_speed = -110;
}
if (car_speed2 > 1100) {
car_speed2 = -150;
}
if (car_speed3 > 1100) {
car_speed3 = -250;
}
Moon_Animate();
glutPostRedisplay();
}
void init() {
glClearColor(0.184314f, 0.184314f, 0.309804f,0.0f);
genrateRandom();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1000.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1920, 1900);
glutCreateWindow("Night Village Recreate");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
// Animation
glutIdleFunc(SpinWindmill);
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment