In this tutorial, I will show you how to dram 3D house animation using C++ and OpenGL in the command line. The complete source code of 3D House Animation software is given below.
C++ OpenGL 3D House Animation Code
code.cpp
#include <iostream> #include <stdlib.h> #ifdef __APPLE__ #include <OpenGL/OpenGL.h> #include <GLUT/glut.h> #else #include <GL/glut.h> #endif using namespace std; void handleKeypress(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); } } void initRendering() { glEnable(GL_DEPTH_TEST); } void handleResize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double) w / (double) h, 1.0, 200.0); } float _angle = 45.0f; float _cameraAngle = 0.0f; void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); glTranslatef(-1.0f, -1.5f, -2.0f); glPopMatrix(); glPushMatrix(); glTranslatef(1.0f, 1.0f, 0.0f); glRotatef(_angle, 0.0f, 1.0f, 0.0f); glScalef(1.0f, 1.0f, 1.0f); glColor3f(1.0, 0.25, 1.0); glColor3f (.5, 0.5, .25); /* main rec */ glClear (GL_COLOR_BUFFER_BIT); glColor3f (.5, .75, .35); glBegin(GL_POLYGON); glVertex3f (0.2, 0.1, 0.0); glVertex3f (0.9, 0.1, 0.0); glVertex3f (0.9, 0.575, 0.0); glVertex3f (0.2, 0.575, 0.0); glEnd(); /*back rec*/ glColor3f (.5, .75, .35); glBegin(GL_POLYGON); glVertex3f (0.2, 0.1, 0.5); glVertex3f (0.9, 0.1, 0.5); glVertex3f (0.9, 0.575, 0.5); glVertex3f (0.2, 0.575, 0.5); glEnd(); /* left rec */ glColor3f (.75, 0.75, .25); glBegin(GL_POLYGON); glVertex3f (0.2, 0.1, 0.5); glVertex3f (0.2, 0.1, 0.0); glVertex3f (0.2, 0.575, 0.0); glVertex3f (0.2, 0.575, 0.5); glEnd(); /* right rec */ glColor3f (.75, 0.75, .25); glBegin(GL_POLYGON); glVertex3f (0.9, 0.1, 0.5); glVertex3f (0.9, 0.1, 0.0); glVertex3f (0.9, 0.575, 0.0); glVertex3f (0.9, 0.575, 0.5); glEnd(); /* left tri */ glColor3f (.5, 0.5, .25); glBegin(GL_TRIANGLES); glVertex3f (0.9, 0.575, 0.0); glVertex3f (0.9, 0.575, 0.5); glVertex3f (0.9, 0.8, 0.25); glEnd(); /* right tri */ glColor3f (.5, 0.5, .25); glBegin(GL_TRIANGLES); glVertex3f (0.2, 0.575, 0.0); glVertex3f (0.2, 0.575, 0.5); glVertex3f (0.2, 0.8, 0.25); glEnd(); /* roof */ glColor3f (.55, 0.35, .2); glBegin(GL_POLYGON); glVertex3f (0.2, 0.575, 0.0); glVertex3f (0.9, 0.575, 0.0); glVertex3f (0.9, 0.8, 0.25); glVertex3f (0.2, 0.8, 0.25); glEnd(); /*back roof */ glColor3f (.55, 0.35, .2); glBegin(GL_POLYGON); glVertex3f (0.2, 0.575, 0.5); glVertex3f (0.9, 0.575, 0.5); glVertex3f (0.9, 0.8, 0.25); glVertex3f (0.2, 0.8, 0.25); glEnd(); /* door */ glColor3f (.15, 0.2, .3); glBegin(GL_POLYGON); glVertex3f (0.47, 0.105, 0.0); glVertex3f (0.65, 0.105, 0.0); glVertex3f (0.65, 0.46, 0.0); glVertex3f (0.47, 0.46, 0.0); glEnd(); /* window 1 */ glColor3f (.3, 0.2, .1); glBegin(GL_POLYGON); glVertex3f (0.72, 0.25, 0.0); glVertex3f (0.83, 0.25, 0.0); glVertex3f (0.83, 0.4, 0.0); glVertex3f (0.72, 0.4, 0.0); glEnd(); /* window 2 */ glColor3f (.3, 0.2, .1); glBegin(GL_POLYGON); glVertex3f (0.27, 0.25, 0.0); glVertex3f (0.38, 0.25, 0.0); glVertex3f (0.38, 0.4, 0.0); glVertex3f (0.27, 0.4, 0.0); glEnd(); glFlush (); glPopMatrix(); glutSwapBuffers(); } void update(int value) { _angle += 1.0f; if (_angle > 360) { _angle -= 360; } glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char * * argv) { glutInit( & argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(1000, 800); glutCreateWindow("3D House Rotate"); initRendering(); glutDisplayFunc(drawScene); glutKeyboardFunc(handleKeypress); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }