Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  ghkyt
Language: C/C++
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
typedef float point[3];

static GLdouble viewer[]={0.0,0.0,5.0};
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
static GLfloat red[] =
  {0.9, 0.0, 0.0, 1.0};
  static GLfloat glass[] =
  {0.0, 0.8, 0.8, 1.0};
  static GLfloat yellow[] =
  {1.1, 1.0, 0.0, 1.0};
  static GLfloat blue[] =
  {0.0, 0.0, 1.0, 1.0};
  static GLfloat black[] =
  {0.0, 0.0, 0.0, 1.0};
  static GLfloat silver[] =
  {0.7, 0.7, 0.7, 1.0};
  static GLfloat backgrnd[] =
  {0.340, 1.0, 0.0, 1.0};
static GLfloat white[] =  {1.0, 1.0, 1.0, 1.0};
//vertices for leftside of car end vertices
point l[60]={
     {-1.1,-0.2,-0.25},
     {-1.1,0.0,-0.25},
     {-1.0,0.1,-0.25},
     {-0.65,0.19,-0.25},
     {-0.52,0.4,-0.25},
     {0.1,0.4,-0.25},
     {0.4,0.18,-0.25},
     {1.1,-0.02,-0.25},
     {1.1,-0.2,-0.25},
     {0.65,-0.25,-0.25},
     {0.6,-0.15,-0.25},
     {0.45,-0.1,-0.25},
     {0.3,-0.15,-0.25},
     {0.25,-0.25,-0.25},
     {-0.45,-0.25,-0.25},
     {-0.5,-0.15,-0.25},
     {-0.65,-0.1,-0.25},
     {-0.8,-0.15,-0.25},
     {-0.85,-0.25,-0.25},
     {-1.1,-0.2,-0.25},
};
//vertices for rightside of car end vertices
point r[60]={
     {-1.1,-0.2,0.25},
     {-1.1,0.0,0.25},
     {-1.0,0.1,0.25},
     {-0.65,0.19,0.25},
     {-0.52,0.4,0.25},
     {0.1,0.4,0.25},
     {0.4,0.18,0.25},
     {1.1,-0.02,0.25},
     {1.1,-0.2,0.25},
     {0.65,-0.25,0.25},
     {0.6,-0.15,0.25},
     {0.45,-0.1,0.25},
     {0.3,-0.15,0.25},
     {0.25,-0.25,0.25},
     {-0.45,-0.25,0.25},
     {-0.5,-0.15,0.25},
     {-0.65,-0.1,0.25},
     {-0.8,-0.15,0.25},
     {-0.85,-0.25,0.25},
     {-1.1,-0.2,0.25},
};

//vertices for leftside front door
point lfdoor[]={{-0.15,0.4,0.25},
     {0.1,0.4,0.25},
     {0.4,0.18,0.25},
     {0.45,-0.1,0.25},
     {0.3,-0.15,0.25},
     {0.25,-0.25,0.25},
     {-0.15,-0.25,0.25}
};

//vertices for rightside front door
point rfdoor[]={{-0.15,0.4,-0.25},
     {0.1,0.4,-0.25},
     {0.4,0.18,-0.25},
     {0.45,-0.1,-0.25},
     {0.3,-0.15,-0.25},
     {0.25,-0.25,-0.25},
     {-0.15,-0.25,-0.25}
};
//vertices for leftside back door
point lbdoor[]={{-0.15,0.4,-0.25},
{-0.15,-0.25,-0.25},
{-0.5,-0.25,-0.25},
{-0.5,-0.15,-0.25},
{-0.65,-0.1,-0.25},
{-0.65,0.19,-0.25},
{-0.52,0.4,-0.25},
};

//vertices for rightside back door
point rbdoor[]={{-0.15,0.4,0.25},
{-0.15,-0.25,0.25},
{-0.5,-0.25,0.25},
{-0.5,-0.15,0.25},
{-0.65,-0.1,0.25},
{-0.65,0.19,0.25},
{-0.52,0.4,0.25},
};

//vertices for front left window
point flwin[]={{-0.13,0.38,-0.25},
{0.08,0.38,-0.25},
{0.38,0.14,-0.25},
{-0.13,0.14,-0.25}
};

//vertices for front right window
point frwin[]={{-0.13,0.38,0.25},
{0.08,0.38,0.25},
{0.38,0.14,0.25},
{-0.13,0.14,0.25}
};

//vertices for back left window
point blwin[]={{-0.17,0.38,-0.25},
{-0.17,0.14,-0.25},
{-0.63,0.14,-0.25},
{-0.49,0.38,-0.25}
};

//vertices for back right window
point brwin[]={{-0.17,0.38,0.25},
{-0.17,0.14,0.25},
{-0.63,0.14,0.25},
{-0.49,0.38,0.25}
};

void draw_body()
{
     //draw front right door
     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red );
     glColor3f(0.0,0.0,1.0);
     glBegin(GL_POLYGON);
     int i;
     for(i=0;i<7;i++)
     {
          glVertex3fv(rfdoor[i]);
     }
     glEnd();

     //draw front left door
     glColor3f(0.0,0.0,1.0);
     glBegin(GL_POLYGON);
          glVertex3fv(lfdoor[0]);
glVertex3fv(lfdoor[1]);
glVertex3fv(lfdoor[2]);
glVertex3fv(lfdoor[3]);
glVertex3fv(lfdoor[4]);
glVertex3fv(lfdoor[5]);
glVertex3fv(lfdoor[6]);

     glEnd();
glBegin(GL_POLYGON);
          glVertex3fv(lbdoor[0]);
     glVertex3fv(lbdoor[1]);
     glVertex3fv(lbdoor[2]);
     glVertex3fv(lbdoor[3]);
     glVertex3fv(lbdoor[4]);
     glVertex3fv(lbdoor[5]);
     glVertex3fv(lbdoor[6]);
     glEnd();
     glBegin(GL_POLYGON);
          glVertex3fv(rbdoor[0]);
          glVertex3fv(rbdoor[1]);
          glVertex3fv(rbdoor[2]);
          glVertex3fv(rbdoor[3]);
          glVertex3fv(rbdoor[4]);
          glVertex3fv(rbdoor[5]);
          glVertex3fv(rbdoor[6]);

     glEnd();

     glPopMatrix();
     glBegin(GL_POLYGON);
          glVertex3fv(l[0]);
          glVertex3fv(r[0]);
          glVertex3fv(r[1]);
          glVertex3fv(l[1]);
     glEnd();

            glBegin(GL_POLYGON);
          glVertex3fv(l[1]);
          glVertex3fv(r[1]);
          glVertex3fv(r[2]);
          glVertex3fv(l[2]);
     glEnd();
             glBegin(GL_POLYGON);
          glVertex3fv(l[2]);
          glVertex3fv(r[2]);
          glVertex3fv(r[3]);
          glVertex3fv(l[3]);
     glEnd();

          glBegin(GL_POLYGON);
          glVertex3fv(l[4]);
          glVertex3fv(r[4]);
          glVertex3fv(r[5]);
          glVertex3fv(l[5]);
     glEnd();

             glBegin(GL_POLYGON);
          glVertex3fv(l[6]);
          glVertex3fv(r[6]);
          glVertex3fv(r[7]);
          glVertex3fv(l[7]);
     glEnd();

              glBegin(GL_POLYGON);
          glVertex3fv(l[8]);
          glVertex3fv(r[8]);
          glVertex3fv(r[9]);
          glVertex3fv(l[9]);
     glEnd();
 glBegin(GL_POLYGON);
          glVertex3fv(l[9]);
          glVertex3fv(r[9]);
          glVertex3fv(r[10]);
          glVertex3fv(l[10]);
 glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[10]);
          glVertex3fv(r[10]);
          glVertex3fv(r[11]);
          glVertex3fv(l[11]);
     glEnd();

  glBegin(GL_POLYGON);
          glVertex3fv(l[11]);
          glVertex3fv(r[11]);
          glVertex3fv(r[12]);
          glVertex3fv(l[12]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[12]);
          glVertex3fv(r[12]);
          glVertex3fv(r[13]);
          glVertex3fv(l[13]);
     glEnd();
glBegin(GL_POLYGON);
          glVertex3fv(l[13]);
          glVertex3fv(r[13]);
          glVertex3fv(r[14]);
          glVertex3fv(l[14]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[14]);
          glVertex3fv(r[14]);
          glVertex3fv(r[15]);
          glVertex3fv(l[15]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[15]);
          glVertex3fv(r[15]);
          glVertex3fv(r[16]);
          glVertex3fv(l[16]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[16]);
          glVertex3fv(r[16]);
          glVertex3fv(r[17]);
          glVertex3fv(l[17]);
     glEnd();
glBegin(GL_POLYGON);
          glVertex3fv(l[17]);
          glVertex3fv(r[17]);
          glVertex3fv(r[18]);
          glVertex3fv(l[18]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[18]);
          glVertex3fv(r[18]);
          glVertex3fv(r[19]);
          glVertex3fv(l[19]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[6]);
          glVertex3fv(l[7]);
          glVertex3fv(l[8]);
          glVertex3fv(l[9]);
          glVertex3fv(l[10]);
          glVertex3fv(l[11]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(r[6]);
          glVertex3fv(r[7]);
          glVertex3fv(r[8]);
          glVertex3fv(r[9]);
          glVertex3fv(r[10]);
          glVertex3fv(r[11]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(r[0]);
          glVertex3fv(r[1]);
          glVertex3fv(r[2]);
          glVertex3fv(r[3]);
          glVertex3fv(r[16]);
          glVertex3fv(r[17]);
          glVertex3fv(r[18]);
     glEnd();

glBegin(GL_POLYGON);
          glVertex3fv(l[0]);
          glVertex3fv(l[1]);
          glVertex3fv(l[2]);
          glVertex3fv(l[3]);
          glVertex3fv(l[16]);
          glVertex3fv(l[17]);
          glVertex3fv(l[18]);
     glEnd();
}

void draw_head_light()
{

     glBegin(GL_POLYGON);
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, black);
          glVertex3fv(l[7]);
          glVertex3fv(r[7]);
          glVertex3fv(r[8]);
          glVertex3fv(l[8]);
     glEnd();

glPushMatrix();
     glTranslated(1.09,-0.12,-0.11);
     glScaled(0.3,0.8,1.1);
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glass);
     glutSolidSphere (0.05, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(1.087,-0.12,-0.17);
     glScaled(0.4,0.9,1.1);
     glutSolidSphere (0.05, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(1.09,-0.12,0.11);
     glScaled(0.3,0.8,1.1);
     glutSolidSphere (0.05, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(1.087,-0.12,0.17);
     glScaled(0.4,0.9,1.1);
     glutSolidSphere (0.05, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
     glTranslated(-1.08,-0.12,0.17);
     glutSolidSphere (0.03, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
     glTranslated(-1.08,-0.12,-0.17);
     glutSolidSphere (0.03, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, yellow);
     glTranslated(-1.08,-0.07,-0.17);
     glutSolidSphere (0.03, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-1.08,-0.07,0.17);
     glutSolidSphere (0.03, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(1.1,-0.17,0.17);
     glutSolidSphere (0.02, 15, 15);
     glPopMatrix();

     glPushMatrix();
     glTranslated(1.1,-0.17,-0.17);
     glutSolidSphere (0.02, 15, 15);
     glPopMatrix();

}

void draw_wheel()
{


     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, black);
     glTranslated(0.45,-0.25,0.25);
     glutSolidTorus(0.05,0.08,25,40);
     glPopMatrix();


     glPushMatrix();
     glTranslated(0.45,-0.25,-0.25);
     glutSolidTorus(0.05,0.08,25,40);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-1.13,-0.06,0.0);
     glRotated(90.0,0,1,0);
     glutSolidTorus(0.04,0.08,25,40);
     glPopMatrix();


     glPushMatrix();
     glTranslated(-0.65,-0.25,0.25);
     glutSolidTorus(0.05,0.08,25,40);
     glPopMatrix();


     glPushMatrix();
     glTranslated(-0.65,-0.25,-0.25);
     glutSolidTorus(0.05,0.08,25,40);
     glPopMatrix();

     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, silver);
    glTranslated(0.45,-0.25,0.25);
     glutSolidSphere(0.06,30,10);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-1.13,-0.06,0.0);
     glRotated(90.0,0,1,0);
     glutSolidSphere(0.05,30,10);
     glPopMatrix();

     glPushMatrix();
     glTranslated(0.45,-0.25,-0.25);
     glutSolidSphere(0.06,30,10);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-0.65,-0.25,0.25);
     glutSolidSphere(0.06,30,10);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-0.65,-0.25,-0.25);
     glutSolidSphere(0.06,30,10);
     glPopMatrix();

}

void draw_window()
{
          //draw front left window

     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, glass);
     glBegin(GL_QUADS);
     int i;
     for(i=0;i<4;i++)
     {
          glVertex3fv(flwin[i]);
     }
     glEnd();

     //draw front right window

     glBegin(GL_QUADS);

          glVertex3fv(frwin[0]);
          glVertex3fv(frwin[1]);
          glVertex3fv(frwin[2]);
          glVertex3fv(frwin[3]);

     glEnd();

     //draw back left window

     glBegin(GL_POLYGON);


          glVertex3fv(blwin[0]);
          glVertex3fv(blwin[1]);
          glVertex3fv(blwin[2]);
          glVertex3fv(blwin[3]);

     glEnd();

     //draw back right window

     glBegin(GL_POLYGON);

          glVertex3fv(brwin[0]);
          glVertex3fv(brwin[1]);
          glVertex3fv(brwin[2]);
          glVertex3fv(brwin[3]);

     glEnd();

     //draw back mirror

     glBegin(GL_POLYGON);
          glVertex3fv(r[3]);
          glVertex3fv(r[4]);
          glVertex3fv(l[4]);
          glVertex3fv(l[3]);
     glEnd();

     //draw front mirror

     glBegin(GL_POLYGON);
          glVertex3fv(r[5]);
          glVertex3fv(r[6]);
          glVertex3fv(l[6]);
          glVertex3fv(l[5]);
     glEnd();


}

void draw_bumper()
{
     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, black);
     glTranslated(-1.1,-0.17,0);
     glRotated(90.0,1,0,0);
     glRotated(90.0,0,0,1);
     glScaled(2.3,0.2,0.5);
     glutSolidTorus(0.05,0.06,25,40);
     glPopMatrix();

     glPushMatrix();
     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, black);
     glTranslated(1.1,-0.17,0);
     glRotated(90.0,1,0,0);
     glRotated(90.0,0,0,1);
     glScaled(2.3,0.2,0.5);
     glutSolidTorus(0.05,0.06,25,40);
     glPopMatrix();


     glPushMatrix();
     glTranslated(-0.09,-0.1,0.28);
     glRotated(90.0,1,0,0);
     glScaled(4,0,0.3);
     glutSolidTorus(0.05,0.06,25,40);
     glPopMatrix();

     glPushMatrix();
     glTranslated(-0.09,-0.1,-0.28);
     glRotated(90.0,1,0,0);
     glScaled(4,0,0.3);
     glutSolidTorus(0.05,0.06,25,40);
     glPopMatrix();

}


void draw_car()
{

     draw_wheel();
     draw_window();
     draw_body();
     draw_head_light();
     draw_bumper();

}

void draw_background()
{

     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, backgrnd);
     glBegin(GL_POLYGON);
          glVertex3f(-16.90,14.60,-16.0);
          glVertex3f(16.90,14.60,-16.0);
          glVertex3f(16.90,-14.60,-16.0);
          glVertex3f(-16.90,-14.60,-16.0);
     glEnd();


}

void display()
{
     GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f}; // gray
     GLfloat mat_diffuse[] = {.5f, .5f, .5f, 1.0f};
     GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
     GLfloat mat_shininess[] = {50.0f};
     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);

     //set the light source properties
     GLfloat lightIntensity[] = {1.0f, 1.0f, 1.0f, 1.0f};
     GLfloat light_position[] = {0.0f, 6.0f, 3.0f, 0.0f};
     glLightfv (GL_LIGHT0, GL_POSITION, light_position);
     glLightfv (GL_LIGHT0, GL_DIFFUSE, lightIntensity);



    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     glMatrixMode(GL_MODELVIEW);


     glPushMatrix();
     glRotated(90,0.0,0.0,1.0);
     draw_background();

     glPopMatrix();

     glPushMatrix();
     gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.0);
//     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
     glRotatef(yRot, 0.0f, 1.0f, 0.0f);

     glTranslated(xRot,0.0,0.0);
     draw_car();

     glPopMatrix();

     glutSwapBuffers();
}



void myReshape(int w, int h)
{
     glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     gluPerspective( 60.0, (GLdouble)w/(GLdouble)h, 0.1, 40.0);
    glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     gluLookAt(0.0, 0.0, 2.0, /* eye at (0,0,20) */
                 0.0, 0.0, 0.0, /* lookat point */
                 0.0, 1.0, 0.0); /* up is in +ive y */
}



void SpecialKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP)
        xRot+= 0.10f;

    if(key == GLUT_KEY_DOWN)
        xRot -= 0.10f;

    if(key == GLUT_KEY_LEFT)
        yRot -= 5.0f;

    if(key == GLUT_KEY_RIGHT)
        yRot += 5.0f;

    if(key > 356.0f)
        xRot = 0.0f;

    if(key < -1.0f)
        xRot = 355.0f;

    if(key > 356.0f)
        yRot = 0.0f;

    if(key < -1.0f)
        yRot = 355.0f;

    // Refresh the Window
    glutPostRedisplay();
}


void myKeyboardFunc( unsigned char key, int x, int y )
{
     switch ( key )
     {
     case 'w':
          viewer[0]+=1.0;
          glutPostRedisplay();
          break;

     case 'W':
          viewer[0]-=1.0;
          glutPostRedisplay();
          break;

     case 's':
          viewer[1]+=1.0;
          glutPostRedisplay();
          break;

     case 'S':
          viewer[1]-=1.0;
          glutPostRedisplay();
          break;

     case 'x':
          viewer[2]+=1.0;
          glutPostRedisplay();
          break;

     case 'X':
          viewer[2]-=1.0;
          glutPostRedisplay();
          break;

          case 27:     // Escape key pressed ... quits.
          exit(1);
     }
}
 int main(int argc, char **argv)
{
     glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA);
    glutInitWindowSize(900, 600);
    glutCreateWindow("3D CAR");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
     glutSpecialFunc(SpecialKeys);
//     glutMouseFunc(myMouse);
     glutKeyboardFunc( myKeyboardFunc );
     glEnable (GL_LIGHTING);
     glEnable(GL_LIGHT0);
     glShadeModel (GL_SMOOTH);
     glEnable (GL_DEPTH_TEST);
     glEnable (GL_NORMALIZE);

     glClearColor(1.0, 1.0, 1.0, 1.0);
     glColor3f(0.0, 0.0, 0.0);
     glutMainLoop();
     return 0;
}
Comments: