Thursday 26 May 2016

7. Program, using OpenGL functions, to draw a simple shaded scene consisting of a tea pot on a table. Define suitably the position and properties of the light source along with the properties of the properties of the surfaces of the solid object used in the scene.

Description:

Lighting and shading play a very prominent role in the field of image formation process, there are various graphics shading models supported by openGL. In this program the programmer should develop a simple
Scene consisting of table and a teapot and demonstrate the different effects of lighting and shading.

Program:

#include<GL/glut.h>
void displaySolid(void)
{
            glClearColor(0.5,0.5,0.1,0.0);
            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(-100,100,-100,100,-100,100);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            //set properties of the surface material
            GLfloat mat_ambient[]={0.0f,1.0f,1.0f,1.0f};
            GLfloat mat_diffuse[]={1.0f,0.5f,1.0f,1.0f};
            GLfloat mat_specular[]={0.5f,0.5f,1.0f,1.0f};
            GLfloat mat_shininess[]={25.0f};
            glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
            glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
            glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
            glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

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

            glPushMatrix();
            glTranslated(0,30,0);
            glRotatef(35,1,0.5,0);
            //glScaled(1,8,1);
            glutSolidTeapot(10);
            //glutWireTeapot(10);
            glPopMatrix();
           
            GLfloat mat_ambient1[]={1.0f,0.0f,0.0f,1.0f};
            GLfloat mat_diffuse1[]={1.0f,1.0f,0.0f,1.0f};
            GLfloat mat_specular1[]={1.0f,1.0f,0.5f,1.0f};
            GLfloat mat_shininess1[]={25.0f};
            glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient1);
            glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse1);
            glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular1);
            glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess1);

            //set the light source properties
            GLfloat lightIntensity1[]={0.5f,0.5f,0.5f,1.0f};
            GLfloat light_position1[]={25.0f,50.0f,50.0f,1.0f};
            glLightfv(GL_LIGHT0, GL_POSITION,light_position1);
            glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity1);
            //top surface
            glPushMatrix();
            glTranslated(0,20,0);
            glRotatef(-80,1,0.5,0.8);
            //glRotatef(,0,0,1);
            glScalef(1.5,1.5,0.1);
            glutSolidCube(50);
            glPopMatrix();

            //First Leg
            glPushMatrix();
            glTranslated(-45,-10,-5);
            glRotatef(45,0,1,0);
            glScalef(0.4,5.5,0.4);
            glutSolidCube(10);
            glPopMatrix();

            //Second Leg
            glPushMatrix();
            glTranslated(-10,-25,5);
            glRotatef(45,0,1,0);
            glScalef(0.4,4.5,0.4);
            glutSolidCube(10);
            glPopMatrix();

            //Third Leg
            glPushMatrix();
            glTranslated(45,-5,-10);
            glRotatef(45,0,1,0);
            glScaled(0.4,5.5,0.4);
            glutSolidCube(10);
            glPopMatrix();

            //Fourth Leg
            glPushMatrix();
            glTranslated(10,5,-35);
            glRotatef(45,0,1,0);
            glScalef(0.4,6,0.4);
            glutSolidCube(10);
            glPopMatrix();

            glFlush();
}

void main(int argc, char *argv[])
{
            glutInit(&argc,argv);
            glutInitWindowSize(600,600);
            glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
            glutInitWindowPosition(10,10);
            glutInitWindowSize(500,500);
            glutCreateWindow("Simple shaded scene consisting of a tea pot on a table");
            glutDisplayFunc(displaySolid);
            glEnable(GL_LIGHTING); // enable Lighting
            glEnable(GL_LIGHT0);  // enable the light source
            glShadeModel(GL_SMOOTH); // set the shading model GL_FLAT or GL_SMOOTH
            glEnable(GL_DEPTH_TEST);
            glEnable(GL_NORMALIZE);
            glutMainLoop();

}

Thursday 5 May 2016

8. Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. Use OpenGL functions.

Description:

Perspective in the graphic arts is an approximate representation, on a flat surface, of an image as it is seen by the eye. The two most characteristic features of perspective are that objects are smaller as their distance from the observer increases and the objects appear larger as the viewer is very close to the object. The main objective of this program is to implement the concept of perspective viewing with the help of cube.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>
GLfloat v[8][3]={{-100,-100,100},{100,-100,100},{100,100,100},{-100,100,100},{-100,-100,-100}, {100,-100,-100},{100,100,-100},{-100,100,-100}};
GLfloat viewer[3]={ 0.0,0.0,500};

void drawcube(GLfloat *,GLfloat *,GLfloat *,GLfloat *);

void keys(unsigned char k,int x,int y)
{
            if(k=='x') viewer[0]-=10.0;
            if(k=='X')  viewer[0]+=10.0;
            if(k=='y') viewer[1]+=10.0;
            if(k=='Y') viewer[1]-=10.0;
            if(k=='z') viewer[2]+=10.0;
            if(k=='Z') viewer[2]-=10.0;
            glutPostRedisplay();
}

void display()
{
            glClearColor(0.0,0.0,0.0,0.0);
            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glFrustum(-200,200,-200,200,200,800);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            gluLookAt(viewer[0],viewer[1],viewer[2],0,0,0,1,0,0);        
            glColor3f(1.0,0.6,0.3);
            drawcube(v[0],v[1],v[2],v[3]);
            glColor3f(1.0,0.7,0.3);
            drawcube(v[1],v[5],v[6],v[2]);
            glColor3f(1.0,0.0,0.0);
            drawcube(v[3],v[2],v[6],v[7]);
            glColor3f(0.0,1.0,0.0);
            drawcube(v[4],v[5],v[1],v[0]);
            glColor3f(0.0,0.0,1.0);
            drawcube(v[7],v[6],v[5],v[4]);
            glColor3f(1.0,1.0,0.3);
            drawcube(v[3],v[7],v[4],v[0]);
            glFlush();
}

void drawcube(GLfloat *a,GLfloat *b,GLfloat *c,GLfloat *d)
{
            glBegin(GL_POLYGON);
                        //glColor3f(1.0,0.2,0.5);
                        glVertex3fv(a);
                        //glColor3f(0.9,0.1,0.8);
                        glVertex3fv(b);
                        //glColor3f(1.0,1.0,1.0);
                        glVertex3fv(c);
                        //glColor3f(0.7,0.8,0.6);
                        glVertex3fv(d);
            glEnd();
}

void main(int argc, char *argv[])
{
            glutInit(&argc,argv);
            glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
            glutInitWindowPosition(10,10);
            glutInitWindowSize(500,500);
            glutCreateWindow("Perspective View");
            glutDisplayFunc(display);
            glEnable(GL_DEPTH_TEST);
            glutKeyboardFunc(keys);  // Enable the keyboard function
            glutMainLoop();

}