Thursday, September 17, 2015

download computer graphics open gl 3d house with car mini project


3D HOUSE WITH CAR



Screen shots







House design


Car design




Here is the code........


#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<GL/glut.h>
static int value=0;
float red=0.88235f,green=0.67843f ,blue=0.2f;
GLfloat vertices[][3]={{0.0,0.05,-0.3},{0.0,0.05,-1.05},{0.01,0.35,-1.05},{0.01,0.35,-0.3},  //car lower left
 {0.0,0.05,-0.3},{0.05,0.35,-0.3},{0.39,0.35,-0.3},{0.4,0.05,-0.3}, //car lower center
 {0.4,0.05,-0.3},{0.4,0.05,-1.05},{0.39,0.35,-1.05},{0.39,0.35,-0.3}, //car lower right
 {0.0,0.05,-1.05},{0.01,0.35,-1.05},{0.39,0.35,-1.05},{0.4,0.05,-1.05},//car lower back
 {-0.5,0.5,1.0}};
GLfloat carvert[][3]={{0.0,0.05,-0.3},{0.0,0.05,-1.05},{0.01,0.35,-1.05},{0.01,0.35,-0.3},  //car lower left
 {0.0,0.05,-0.3},{0.01,0.35,-0.3},{0.39,0.35,-0.3},{0.4,0.05,-0.3}, //car lower front
 {0.4,0.05,-0.3},{0.4,0.05,-1.05},{0.39,0.35,-1.05},{0.39,0.35,-0.3}, //car lower right
 {0.0,0.05,-1.05},{0.01,0.35,-1.05},{0.39,0.35,-1.05},{0.4,0.05,-1.05},//car lower back
 {0.01,0.35,-0.3},{0.01,0.35,-0.55},{0.39,0.35,-0.55},{0.39,0.35,-0.3},//car lower top
 {0.01,0.35,-0.55},{0.01,0.35,-1.05},{0.03,0.55,-1.03},{0.03,0.55,-0.57},//car upper left
 {0.01,0.35,-0.55},{0.03,0.55,-0.57},{0.37,0.55,-0.57},{0.39,0.35,-0.55},//car upper front
 {0.39,0.35,-0.55},{0.39,0.35,-1.05},{0.37,0.55,-1.03},{0.37,0.55,-0.57},//car upper right
 {0.01,0.35,-1.05},{0.03,0.55,-1.03},{0.37,0.55,-1.03},{0.39,0.35,-1.05},//car upper back
 {0.03,0.55,-0.57},{0.03,0.55,-1.03},{0.37,0.55,-1.03},{0.37,0.55,-0.57},//car upper top
 {0.0075,0.375,-0.75},{0.0075,0.375,-1.025},{0.025,0.525,-1.01},{0.025,0.525,-0.75},//car left window
 {0.0075,0.375,-0.575},{0.0075,0.375,-0.70},{0.025,0.525,-0.70},{0.025,0.525,-0.585},//car right window
 {0.35,0.125,-0.285},{0.05,0.125,-0.285},{0.065,0.3,-0.285},{0.34,0.3,-0.285}, //car lower front inter
 };
GLfloat vertic[][3]={{-1.5,0.0,1.5},{-1.5,0.0,-1.5},{1.5,0.0,-1.5},{1.5,0.0,1.5},   //ground  3
{0.5,0.0,-0.3},{0.5,0.0,-1.4},{1.4,0.0,-1.4},{1.4,0.0,-0.3},   //house 1 7
{0.5,0.0,-0.3},{0.5,0.0,-1.4},{0.5,1.0,-1.4},{0.5,1.0,-0.3}, //house 2 11
{0.5,0.0,-0.3},{0.5,1.0,-0.3},{1.4,1.0,-0.3},{1.4,0.0,-0.3},   //house 3 15 
{1.4,0.0,-1.4},{1.4,1.0,-1.4},{1.4,1.0,-0.3},{1.4,0.0,-0.3}, //house 4 19
{0.5,0.0,-1.4},{0.5,1.0,-1.4},{1.4,1.0,-1.4},{1.4,0.0,-1.4},  //house 5  23  
{0.5,1.0,-1.4},{0.5,1.0,-0.3},{0.5,1.75,-0.85}, //house left roof     26
{1.4,1.0,-0.3},{0.5,1.0,-0.3},{0.5,1.75,-0.85},{1.4,1.75,-0.85},  //house front roof  30
{1.4,1.0,-0.3},{1.4,1.0,-1.4},{1.4,1.75,-0.85}, //house right roof                 33 
{1.4,1.0,-1.4},{0.5,1.0,-1.4},{0.5,1.75,-0.85},{1.4,1.75,-0.85},  //house back roof   37
{1.1,0.0,-0.275},{0.8,0.0,-0.275},{0.8,0.7,-0.275},{1.1,0.7,-0.275},  //house front door 41
{0.75,0.35,-0.275},{0.55,0.35,-0.275},{0.55,0.7,-0.275},{0.75,0.7,-0.275},  //house front left window 45
{1.35,0.35,-0.275},{1.15,0.35,-0.275},{1.15,0.7,-0.275},{1.35,0.7,-0.275},  //house front right window 49
{1.5,0.025,1.5},{-1.6,0.025,1.5},{-1.6,0.025,0.75},{1.5,0.025,0.75},  //road 53
{-0.3,0.04,1.2},{-0.8,0.04,1.2},{-0.8,0.04,1.05},{-0.3,0.04,1.05},  //road strip 57
{0.7,0.04,1.2},{0.2,0.04,1.2},{0.2,0.04,1.05},{0.7,0.04,1.05},  //road strip 61
{1.5,0.04,1.2},{1.2,0.04,1.2},{1.2,0.04,1.05},{1.5,0.04,1.05},  //road strip 65
{-1.3,0.04,1.2},{-1.6,0.04,1.2},{-1.6,0.04,1.05},{-1.3,0.04,1.05},  //road strip 69
{-0.3,0.03,0.0},{-1.2,0.03,0.0},{-1.2,0.03,-1.05},{-0.3,0.03,-1.05},  //road strip 73
{-0.3,0.0,-0.1},{-0.3,0.5,-0.1},{-0.4,0.25,-0.1},{-0.4,0.0,-0.1},  //pool carrier 77
{-0.3,0.0,-0.3},{-0.3,0.5,-0.3},{-0.4,0.25,-0.3},{-0.4,0.0,-0.3},  //pool carrier 81
};
GLfloat colors[][3]={{1.0,1.0,1.0},{0.0,0.0,0.0},{0.89019,0.38823,0.03921},{0.57254,0.44705,0.01176},{0.88235,0.0,0.03921},{0.00784,0.54509,0.0},
{0.4588,0.6784,0.2509},{0.88235,0.67843,0.2},{0.35294117,0.3568627,0.3647059},{0.0,0.0,1.0}}; //0.white,1.black,2.orange,3.yellowish,4.red,5.green,6.car,7.orange,8.grey
GLfloat viewer[3]={0.0,0.0,0.5};
void menu(int);
void sun()
{
 //glTranslatef(-1.0,1.5,-1.0);
 //glutSolidSphere(0.3,30,30;
 glTranslatef(0.5,1.7,-2.5);
 glutSolidSphere(0.3,30,30);
}

void square(int a,int b,int c,int d) //may not be used 
{
 glBegin(GL_POLYGON);
 glVertex3fv(vertices[a]);
 glVertex3fv(vertices[b]);
 glVertex3fv(vertices[c]);
 glVertex3fv(vertices[d]);
 glEnd();
}
void squarv(int a,int b,int c,int d)
{
 glBegin(GL_POLYGON);
 glVertex3fv(vertic[a]);
 glVertex3fv(vertic[b]);
 glVertex3fv(vertic[c]);
 glVertex3fv(vertic[d]);
 glEnd();
 }
 void squarc(int a,int b,int c,int d)
{
 glBegin(GL_POLYGON);
 glVertex3fv(carvert[a]);
 glVertex3fv(carvert[b]);
 glVertex3fv(carvert[c]);
 glVertex3fv(carvert[d]);
 glEnd();
 }

void triangle(int a,int b,int c)
{
 glBegin(GL_POLYGON);
 glVertex3fv(vertic[a]);
 glVertex3fv(vertic[b]);
 glVertex3fv(vertic[c]);
 glEnd();
}

void car()
{

glColor3f(red,green,blue);  //car lower left
 squarc(0,1,2,3);
 glColor3f(red,green,blue);  //car lower front
 squarc(4,5,6,7);
 glColor3f(red,green,blue);  //car lower right
 squarc(8,9,10,11);
 glColor3f(red,green,blue);  //car lower back
 squarc(12,13,14,15);
glColor3f(red,green,blue);  //car lower top
 squarc(16,17,18,19);
 glColor3f(red,green,blue);  //car upper left
 squarc(20,21,22,23);
 glColor3fv(colors[1]);  //car upper front
 squarc(24,25,26,27);
 glColor3f(red,green,blue);  //car upper right
 squarc(28,29,30,31);
 glColor3fv(colors[1]);  //car upper back
 squarc(32,33,34,35);
 glColor3f(red,green,blue);  //car upper top
 squarc(36,37,38,39);
 glColor3fv(colors[1]);  //car left window
 squarc(40,41,42,43);
 glColor3fv(colors[1]);  //car right window
 squarc(44,45,46,47);
 glColor3fv(colors[8]);  //car lower inter
 squarc(48,49,50,51);
glColor3fv(colors[2]); //house smoke outlet
glPushMatrix();
//glutSolidSphere(0.5,10,10);
glColor3fv(colors[1]);
glTranslatef(0.0,0.15,-0.425); 
glScalef(0.075,0.3,0.2);
glutSolidSphere(0.5,20,20); //left front tyre
//glutSolidTeapot(1.0);
glPopMatrix();
glPushMatrix();
glColor3fv(colors[1]);
glTranslatef(0.0,0.15,-0.90); 
glScalef(0.075,0.3,0.2);
glutSolidSphere(0.5,20,20); //left back tyre
glPopMatrix();
glPushMatrix();
glColor3fv(colors[1]);
glTranslatef(0.4,0.15,-0.425); 
glScalef(0.075,0.3,0.2);
glutSolidSphere(0.5,20,20); //right front tyre
glPopMatrix();
glPushMatrix();
glColor3fv(colors[1]);
glTranslatef(0.4,0.15,-0.90); 
glScalef(0.075,0.3,0.2);
glutSolidSphere(0.5,20,20); //right back tyre
glPopMatrix();

glPushMatrix();
glTranslatef(0.09,0.21,-0.275); 
glScalef(0.125,0.15,0.02);
glutSolidSphere(0.4,20,20); //left headlight
glPopMatrix();
glPushMatrix();
glTranslatef(0.31,0.21,-0.275); 
glScalef(0.125,0.15,0.02);
glutSolidSphere(0.4,20,20); //right headlight
glPopMatrix();

//glFlush();
//sglutPostRedisplay(); 
}


void house()
{

glColor3fv(colors[5]);  //ground base
 squarv(0,1,2,3);
 glColor3fv(colors[3]);
 glPushMatrix();
glTranslatef(0.0,-1.45,0.0);
//glScalef(2.0,0.2,0.2);
glutSolidCube(2.8);
glPopMatrix();

/*
glColor3fv(colors[2]); //x axis 
 glBegin(GL_LINES);
 glVertex3f(-1.5,0.0,0.0);
 glVertex3f(1.5,0.0,0.0);
 glEnd();

glColor3fv(colors[2]); //y axis 
 glBegin(GL_LINES);
 glVertex3f(0.0,0.0,-1.5);
 glVertex3f(0.0,0.0,1.5);
 glEnd();
 */
 glColor3fv(colors[2]);  //house base
squarv(4,5,6,7);
glColor3fv(colors[2]);  //house left wall
 squarv(8,9,10,11);
glColor3fv(colors[3]); //house center wall
 squarv(12,13,14,15);
 glColor3fv(colors[2]); //house right wall
squarv(16,17,18,19);
 glColor3fv(colors[3]); //house back wall
squarv(20,21,22,23);
 glColor3fv(colors[2]); //house left roof
triangle(24,25,26);
glColor3fv(colors[4]); //house front roof
squarv(27,28,29,30);
glColor3fv(colors[2]); //house right roof
triangle(31,32,33);
glColor3fv(colors[4]); //house back roof
squarv(34,35,36,37);
glColor3fv(colors[6]); //house front door
squarv(38,39,40,41);
glPushMatrix();
GLfloat mat_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f}; 
GLfloat mat_diffuse[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_specular[] = {1.0f,1.0f, 1.0f, 1.0f};
GLfloat mat_shininess[] = {100.0f};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel (GL_SMOOTH);
glEnable (GL_NORMALIZE);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess);
GLfloat lightIntensity[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat light_position[] = {1.4f, 2.0f, 1.4f, 0.0f};
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glLightfv (GL_LIGHT0, GL_DIFFUSE, lightIntensity);
//glColor3fv(colors[6]);
glNormal3fv(vertic[42]);
glNormal3fv(vertic[43]);
glNormal3fv(vertic[44]);
glNormal3fv(vertic[45]); //glutPostRedisplay() glPopMatrix();
//house left window
squarv(42,43,44,45);
//glColor3fv(colors[6]); //house right window
glNormal3fv(vertic[46]);
glNormal3fv(vertic[47]);
glNormal3fv(vertic[48]);
glNormal3fv(vertic[49]);
squarv(46,47,48,49);
glutPostRedisplay();
glPopMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glColor3fv(colors[0]); //house smoke outlet
glPushMatrix();
glTranslatef(0.7,1.375,-0.575);
glScalef(0.25,1.0,0.25);
glutSolidCube(0.5);
glPopMatrix();

glColor3fv(colors[1]); //road
squarv(50,51,52,53);
glColor3fv(colors[0]); //road strip2 from left to right
squarv(54,55,56,57);
glColor3fv(colors[0]); //road strip3
squarv(58,59,60,61);
glColor3fv(colors[0]); //road strip4
squarv(62,63,64,65);
glColor3fv(colors[0]); //road strip1
squarv(66,67,68,69);

glColor3fv(colors[3]); //tree trunk 1 from left
glPushMatrix();
glTranslatef(-1.3,0.5,-1.3);
glScalef(0.25,2.0,0.25);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix(); 
glColor3fv(colors[5]);
glTranslatef(-1.3,1.5,-1.3);
glScalef(0.5,0.5,0.5);
glutSolidSphere(1.0,20,20);
glPopMatrix();

glPushMatrix(); 
glColor3fv(colors[5]);
glTranslatef(-1.3,2.0,-1.3);
glScalef(0.5,0.5,0.5);
glutSolidSphere(0.7,20,20);
glPopMatrix();


glColor3fv(colors[3]); //tree trunk 2
glPushMatrix();
glTranslatef(-1.0,0.35,-1.3);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3fv(colors[5]);
glTranslatef(-1.0,0.7,-1.3);
glScalef(0.5,0.5,0.5);
glutSolidSphere(0.5,20,20);
glPopMatrix();

glColor3fv(colors[3]); //tree trunk 3
glPushMatrix();
glTranslatef(-0.4,0.35,-1.3);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3fv(colors[5]);
glTranslatef(-0.4,0.7,-1.3);
glScalef(0.5,0.5,0.5);
glutSolidSphere(0.5,20,20);
glPopMatrix();

glColor3fv(colors[3]); //tree trunk 4
glPushMatrix();
glTranslatef(0.2,0.35,-1.3);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
glPopMatrix();

glPushMatrix();
glColor3fv(colors[5]);
glTranslatef(0.2,0.7,-1.3);
glScalef(0.5,0.5,0.5);
glutSolidSphere(0.5,20,20);
glPopMatrix();

//SWIMMING POOL
glColor3fv(colors[0]); //swimmimg pool front
glPushMatrix();
glTranslatef(-0.75,0.0,0.0);
glScalef(2.0,0.2,0.2);
glutSolidCube(0.5);
glPopMatrix();
glColor3fv(colors[0]); //swimmimg pool left
glPushMatrix();
glTranslatef(-1.2,0.0,-0.525);
glScalef(0.2,0.2,2.0);
glutSolidCube(0.5);
glPopMatrix();
glColor3fv(colors[0]); //swimmimg pool back
glPushMatrix();
glTranslatef(-0.75,0.0,-1.05);
glScalef(2.0,0.2,0.2);
glutSolidCube(0.5);
glPopMatrix();
glColor3fv(colors[0]); //swimmimg pool right
glPushMatrix();
glTranslatef(-0.3,0.0,-0.525);
glScalef(0.2,0.2,2.0);
glutSolidCube(0.5);
glPopMatrix();

glColor3fv(colors[0]); //swimmimg pool stand
glPushMatrix();
glTranslatef(-0.3,0.0,-0.2);
glScalef(0.2,0.6,0.5);
glutSolidCube(0.5);
glPopMatrix();

glColor3fv(colors[1]); //swimmimg pool carrier
glPushMatrix();
glLineWidth(3.0);
glBegin(GL_LINE_LOOP);
//glPointSize(5.0);
 glVertex3fv(vertic[74]);
 glVertex3fv(vertic[75]);
 glVertex3fv(vertic[76]);
 glVertex3fv(vertic[77]);
 glEnd();
glBegin(GL_LINE_LOOP);
//glPointSize(5.0);
 glVertex3fv(vertic[78]);
 glVertex3fv(vertic[79]);
 glVertex3fv(vertic[80]);
 glVertex3fv(vertic[81]);
 glEnd();

 glPopMatrix();

glColor3fv(colors[9]); //water
squarv(70,71,72,73);
}
 GLfloat theta[]={0.0,40.0,0.0};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(theta[0],1.0,0.0,0.0);
                        glRotatef(theta[1],0.5,1.0,0.0);     
                         glRotatef(theta[2],0.0,0.0,1.0);
                         
 house();
glColor3fv(colors[7]);  
                         car();
if(value==0)
{
glColor3f(0.0,0.0,0.0);
glScalef(0.0,0.0,0.0);
sun();
}
if(value==1)
{
 glColor3f(1.0,0.5,0.0);
sun();
}
if(value==2)
{
glColor3f(1.0,1.0,1.0);
sun();
}
glFlush();
glutSwapBuffers();
//glutPostRedisplay();
}
void mykey1(unsigned char key, int x, int y) {

if(key=='X') viewer[0]+=1.0;
if(key=='x') viewer[0]-=.5;
if(key=='Y') viewer[1]+=.5;
if(key=='y') viewer[1]-=.5;
if(key=='Z') viewer[2]+=.5;
if(key=='z') viewer[2]-=.5;
if(key=='r') { red=0.933333;
green=0.2352;
blue=0.13725;
car();
}
if(key=='g') {red=0.48235;
green=0.733333;
blue=0.28627;
 car();}
if(key=='o') {red=0.96078;
green=0.50196;
blue=0.15294;
car();}
glutPostRedisplay();
if (key == 27)
exit(0);
}

void menu(int choice)
{
  switch (choice) {

  case 1:glClearColor(0.6,0.6,1.0,1.0);
// glColor3fv(colors[0]);
//sun();
value=1;
  // glutPostRedisplay();
    break;
  case 2:glClearColor(0.0,0.0,0.0,1.0);
        // glColor3fv(colors[1]);
//sun();
 value=2;
  // glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  //  glutSetWindowTitle("Two-sided lighting");
   //glutPostRedisplay();
    break;
  }
}
void processSpecialKeys(int key, int x, int y) {

switch(key) {
case GLUT_KEY_F1 :red=1.0;
green=0.0;
blue=0.0;
                                        
                                  car();
                                  break;       
case GLUT_KEY_F2 :red=0.0;
green=1.0;
blue=0.0;
                                  car();
                                   break;
case GLUT_KEY_F3 :red=0.0;
green=0.0;
blue=1.0;
car();
         break;
}
glutPostRedisplay();
}

void myreshape(int w,int h) 
 {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0 * (GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-20.0,20.0);
else
glOrtho(-2.0,2.0,-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-20.0,20.0);
glMatrixMode(GL_MODELVIEW);
             
}



int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("my house");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
       glutKeyboardFunc(mykey1);
                glutCreateMenu(menu);
                glutAddMenuEntry("Day", 1);
                glutAddMenuEntry("night", 2);
                glutAttachMenu(GLUT_RIGHT_BUTTON);        
glutDisplayFunc(display);
glutKeyboardFunc(mykey1);
                glutSpecialFunc(processSpecialKeys);                
                glutIdleFunc(display);
               
        glEnable (GL_DEPTH_TEST);
glutMainLoop();
              
       }



No comments:

Post a Comment