Monday, December 5, 2011

Render a scene containing three subject(overlapping each other) i.e. Torus,Teapot and sphere in opengl.

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>

void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat direction[] = { 1.0, 1.0, 1.0, 0.0 };

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan);
  glMaterialfv(GL_FRONT, GL_SPECULAR, white);
  glMaterialf(GL_FRONT, GL_SHININESS, 50);

  glLightfv(GL_LIGHT0, GL_AMBIENT, black);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
  glLightfv(GL_LIGHT0, GL_SPECULAR, white);
  glLightfv(GL_LIGHT0, GL_POSITION, direction);

  glEnable(GL_LIGHTING);               
  glEnable(GL_LIGHT0);                 
  glEnable(GL_DEPTH_TEST);             

 

// Add a torus to the scene.
  glPushMatrix();
    glRotatef(-20.0, 1.0, 0.0, 0.0);
  glRotatef(90.0, 1.0, 0.0, 0.0);
  glutSolidTorus(0.275, 0.85, 16, 40);
  glPopMatrix();

GLfloat blue1[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat magenta1[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat red1[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat yellow1[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat direction2[] = { 1.0, 1.0, 1.0, 0.0 };

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red1);
  glMaterialfv(GL_FRONT, GL_SPECULAR, yellow1);
  glMaterialf(GL_FRONT, GL_SHININESS, 50);

  glLightfv(GL_LIGHT0, GL_AMBIENT, blue1);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, magenta1);
  glLightfv(GL_LIGHT0, GL_SPECULAR, yellow1);
  glLightfv(GL_LIGHT0, GL_POSITION, direction2);

  glEnable(GL_LIGHTING);               
  glEnable(GL_LIGHT0);                 
  glEnable(GL_DEPTH_TEST);             
  // Add a teapot to the scene.
  glPushMatrix();
  glTranslatef(0.75, -0.5, 0.0);
  glRotatef(45.0, 1.0, 0.0, 0.0);
  glutSolidTeapot(1);
  glPopMatrix();

  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat direction1[] = { 1.0, 1.0, 1.0, 0.0 };

  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
  glMaterialfv(GL_FRONT, GL_SPECULAR, yellow);
  glMaterialf(GL_FRONT, GL_SHININESS, 50);

  glLightfv(GL_LIGHT0, GL_AMBIENT, blue);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, magenta);
  glLightfv(GL_LIGHT0, GL_SPECULAR, yellow);
  glLightfv(GL_LIGHT0, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);               
  glEnable(GL_LIGHT0);                 
  glEnable(GL_DEPTH_TEST);             
  // Add a sphere to the scene.
  glPushMatrix();
  glTranslatef(0.75, 0.0, -1.0);
  glutSolidSphere(1.0, 30, 30);
  glPopMatrix();

  glPopMatrix();
  glFlush();
}


void reshape(GLint w, GLint h) {
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30, (GLfloat)w/(GLfloat)h,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}




int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Hidden Surface");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
}

No comments:

Post a Comment