Tuesday, 16 August 2016

Program 5: Write a program to create a scene using OpenGL illumination functions.

PROGRAM 5: LIGHT
·         Header File
·         Render func
·         Handle resize
·         Display
·         Timer
·         Main func
1.      Header File.(gl/glut.h , math.h, windows.h) // REFER 2d 
       link:http://simplifiedopengl.blogspot.in/2016/08/program-3-write-program-to-implement-2d.html
2.      Initialise rendering function for the 2d object// REFER 2d and include the below mentioned effects for 3d
glEnable(GL_COLOR_MATERIAL);
            glEnable(GL_LIGHTING); //Enable lighting
            glEnable(GL_LIGHT0); //Enable light #0
            glEnable(GL_NORMALIZE); //Automatically normalize normals
3.      Create Handle Resize // REFER 2d
4.       Display Function // REFER 2d
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
       GLfloat light_ambient[] = {1.0, 1.0,1.0,1.0};//producing the color
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
      GLfloat lightPos0[] = {0.0f, 0.0f, 1.0f, 0.0f}; //Positioned at (4, 0, 8)
      glLightfv(GL_LIGHT0, GL_DIFFUSE, light_ambient);
      glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
    //************************
      //OBJ
//Right face (x = 1.0f), Left face (x = -1.0f), Back face (z = -1.0f), Front face  (z = 1.0f), Bottom face (y = -1.0f), top face(z=1.0f)
glutSwapBuffers();
}
5.      Timer
6.      Main Function





SAMPLE PROGRAM
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
using namespace std;
//Initializes 3D rendering
void initRendering() {
            glEnable(GL_DEPTH_TEST);
            glEnable(GL_COLOR_MATERIAL);
            glEnable(GL_LIGHTING); //Enable lighting
            glEnable(GL_LIGHT0); //Enable light #0
            glEnable(GL_NORMALIZE); //Automatically normalize normals
}
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 = 0.0f;
//Draws the 3D scene
void drawScene() {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glTranslatef(0.0f, 0.0f, -5.0f);
            //Add ambient light
            GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
             GLfloat light_ambient[] = {1.0, 1.0,1.0,1.0};//producing the color
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
            GLfloat lightPos0[] = {0.0f, 0.0f, 1.0f, 0.0f}; //Positioned at (4, 0, 8)
            glLightfv(GL_LIGHT0, GL_DIFFUSE, light_ambient);
            glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
            glRotatef(_angle, 1.0f,0.0f, 0.0f);
    glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//light blue
    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
      // Top face (y = 1.0f)
      // Define vertices in counter-clockwise (CCW) order with normal pointing out
      glColor3f(0.0f, 1.0f, 0.0f);     // Green
      glVertex3f( 1.0f, 1.0f, -1.0f);
      glVertex3f(-1.0f, 1.0f, -1.0f);
      glVertex3f(-1.0f, 1.0f,  1.0f);
      glVertex3f( 1.0f, 1.0f,  1.0f);

      // Bottom face (y = -1.0f)
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
      glVertex3f( 1.0f, -1.0f,  1.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f( 1.0f, -1.0f, -1.0f);

      // Front face  (z = 1.0f)
      glColor3f(1.0f, 0.0f, 0.0f);     // Red
      glVertex3f( 1.0f,  1.0f, 1.0f);
      glVertex3f(-1.0f,  1.0f, 1.0f);
      glVertex3f(-1.0f, -1.0f, 1.0f);
      glVertex3f( 1.0f, -1.0f, 1.0f);

      // Back face (z = -1.0f)
      glColor3f(1.0f, 1.0f, 1.0f);     // Yellow
      glVertex3f( 1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f);
      glVertex3f( 1.0f,  1.0f, -1.0f);

      // Left face (x = -1.0f)
      glColor3f(0.0f, 0.0f, 1.0f);     // Blue
      glVertex3f(-1.0f,  1.0f,  1.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f);

      // Right face (x = 1.0f)
      glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
      glVertex3f(1.0f,  1.0f, -1.0f);
      glVertex3f(1.0f,  1.0f,  1.0f);
      glVertex3f(1.0f, -1.0f,  1.0f);
      glVertex3f(1.0f, -1.0f, -1.0f);
    glEnd();  // End of drawing color-cube
            glutSwapBuffers();
            glFlush();
}

void update(int value) {
            _angle += 1.5f;
            if (_angle > 360) {
                        _angle -= 360;
            }
            glutPostRedisplay();
            glutTimerFunc(25, update,0);
}

int main(int argc, char** argv) {
            //Initialize GLUT
            glutInit(&argc, argv);
            glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
            glutInitWindowSize(400, 400);
            glutCreateWindow("Lighting");
            initRendering();
            glutDisplayFunc(drawScene);
            glutReshapeFunc(handleResize);
            glutTimerFunc(25, update, 0); //Add a timer
            glutMainLoop();
            return 0;
}










Program 4: Write a program to implement 3D Geometric Transformations.

PROGRAM 4: 3D
·         Header File
·         Render func
·         Handle resize
·         Display
·         Timer
·         Main func

1.      Header File.(gl/glut.h , math.h, windows.h) // REFER 2d link: http://simplifiedopengl.blogspot.in/2016/08/program-3-write-program-to-implement-2d.html
2.      Initialise rendering function for the 2d object// REFER 2d
3.      Create Handle Resize // REFER 2d
4.      Display Function // REFER 2d
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
    //************************
      //OBJ
//Right face (x = 1.0f), Left face (x = -1.0f), Back face (z = -1.0f), Front face  (z = 1.0f), Bottom face (y = -1.0f), top face(z=1.0f)
glutSwapBuffers();
}
5.      Timer
6.      Main Function




SAMPLE PROGRAM: 4 3D
//header file same as all
GLfloat angleCube = 0.0f;     // Rotational angle for cube [NEW]
void initRendering() {
   glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
}
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);
}
void display() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
   glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix
   glLoadIdentity();                 // Reset the model-view matrix
   glTranslatef(-1.5f, 0.0f, -6.0f);  // Move right and into the screen
   glRotatef(angleCube, 0.0f, 1.0f, 1.0f);  // Rotate about (1,1,1)-axis [NEW]
   glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads

      glColor3f(0.0f, 1.0f, 0.0f);     // Green
      glVertex3f( 1.0f, 1.0f, -1.0f);
      glVertex3f(-1.0f, 1.0f, -1.0f);
      glVertex3f(-1.0f, 1.0f,  1.0f);
      glVertex3f( 1.0f, 1.0f,  1.0f);

      // Bottom face (y = -1.0f)
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
      glVertex3f( 1.0f, -1.0f,  1.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f( 1.0f, -1.0f, -1.0f);

      // Front face  (z = 1.0f)
      glColor3f(1.0f, 0.0f, 0.0f);     // Red
      glVertex3f( 1.0f,  1.0f, 1.0f);
      glVertex3f(-1.0f,  1.0f, 1.0f);
      glVertex3f(-1.0f, -1.0f, 1.0f);
      glVertex3f( 1.0f, -1.0f, 1.0f);

      // Back face (z = -1.0f)
      glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
      glVertex3f( 1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f);
      glVertex3f( 1.0f,  1.0f, -1.0f);

      // Left face (x = -1.0f)
      glColor3f(0.0f, 0.0f, 1.0f);     // Blue
      glVertex3f(-1.0f,  1.0f,  1.0f);
      glVertex3f(-1.0f,  1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f, -1.0f);
      glVertex3f(-1.0f, -1.0f,  1.0f);

      // Right face (x = 1.0f)
      glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
      glVertex3f(1.0f,  1.0f, -1.0f);
      glVertex3f(1.0f,  1.0f,  1.0f);
      glVertex3f(1.0f, -1.0f,  1.0f);
      glVertex3f(1.0f, -1.0f, -1.0f);
   glEnd();  // End of drawing color-cube
    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
    angleCube -= 0.15f;
}
void update(int value) {
    angleCube+=2.0f;
    if(angleCube>360.f)
    {
    angleCube-=360;
    }
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}
int main(int argc, char** argv) {
   glutInit(&argc, argv);            // Initialize GLUT
   glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
   glutInitWindowSize(640, 480);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
   glutCreateWindow("PROGRAM4");          // Create window with the given title
   glutDisplayFunc(display);       // Register callback handler for window re-paint event
   glutReshapeFunc(handleResize);       // Register callback handler for window re-size event
   initRendering();                       // Our own OpenGL initialization
   glutTimerFunc(0, update, 0);     // First timer call immediately [NEW]
   glutMainLoop();                 // Enter the infinite event-processing loop
   return 0;
}


Program 3: Write a program to implement 2D Geometric Transformations.

PROGRAM 3: 2D transformations
·         Header File
·         Render func
·         Handle resize
·         Display
·         Timer
·         Main func

1.      Header File.(gl/glut.h , math.h, windows.h)
2.      Initialise rendering function for the 2d object
void initRendering()
{         
glEnable(GL_DEPTH_TEST);
}
3.      Create Handle Resize
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);
}
4.      Display Function
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
    //************************
    glPushMatrix();
    glScalef(2,2,2);
    //OBJ
      glPopMatrix();
    glutSwapBuffers();
}
5.      Timer
void update(int value)
{
       _angle+=2.0f;
      if(_angle>360.f)
      {
                  _angle-=360;
      }
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}
6.      Main Function
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("rotate");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25,update,0);
    glutMainLoop();
    return 0;
}
SAMPLE PROGRAM:PROGRAM 3
//header file and include math.h as well
#include<math.h>
float _angle=45.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);
}
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
    //************************
    glPushMatrix();
    glScalef(2,2,2);
    //OBJ
    glPopMatrix();
    glutSwapBuffers();
}
void update(int value)
{
    _angle+=2.0f;
    if(_angle>360.f)
    {
    _angle-=360;
    }
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("rotate");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25,update,0);
    glutMainLoop();
    return 0;

}

Program 2: Write a program to draw objects using Line-Attribute, Fill-Area Attribute, Color functions of OpenGL.

PROGRAM 2: (Line, fill, color attributes)
1.      Header File : same as the previous prog. link: http://simplifiedopengl.blogspot.in/2016/08/simplified-output-primitive-function.html
2.      Display Function
//LINE ATTRIBUTES: line colour, stipple, width.
    GLint factor=1;
    GLushort pattern=0x00ff;
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(factor,pattern);
//then create the object.
glDisable(GL_LINE_STIPPLE);

//COLOR FUNCTION: gradient color and solid color.
Gradient: Give, glColor3f(0.0,0.0,1.0); before every vertex;
Solid: glColor3f(0.0,0.0,1.0); before the object creation.

//POLYGON FILL
//declare: GLubyte mask[128]; outside the display function
GLubyte Lines[] = {
    0x00, 0x00, 0x00, 0x00,
    0xff, 0xff, 0xff, 0xff,
    };
    glEnable(GL_POLYGON_STIPPLE);
     glPolygonStipple(Lines);
//draw polygon
    glDisable(GL_POLYGON_STIPPLE);

3.      Main function same as the firs program.











SAMPLE: PROGRAM 2
//header file: windows and glut
GLubyte mask[128];
void display()
{
    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    //LINE ATRRIBUTES:
    GLint factor=1;
    GLushort pattern=0x00ff;
    glEnable(GL_LINE_STIPPLE);
    glLineStipple(factor,pattern);
    glColor3f(1.0,1.0,1.0); //solid line color
    glBegin(GL_LINES);
    glVertex2f(0.0,0.0);
    glVertex2f(0.40,0.0);
    glLineWidth(10.0f);// line attribute
    glEnd();
    glDisable(GL_LINE_STIPPLE);
    //COLOR FUNCTION: gradient
    glBegin(GL_LINES);
    glColor3f(0.0,0.0,1.0); //blue
    glVertex2f(0.0,0.10);
    glColor3f(1.0,1.0,0.0);//yellow
    glVertex2f(0.40,0.10);
    glEnd();
    //Fill area:POLYGON FILL
    GLubyte Lines[] = {
    0x00, 0x00, 0x00, 0x00,
    0xff, 0xff, 0xff, 0xff,
    };
    glEnable(GL_POLYGON_STIPPLE);
     glPolygonStipple(Lines);
    glColor4f(1.0f, 0.0f, 0.0f, 0.0f);//red
    glBegin(GL_POLYGON);
    glVertex2f(-0.44,0.45);
    glVertex2f(-0.57,0.45);
    glVertex2f(-0.65,0.10);
    glVertex2f(-0.90,-0.60);
    glVertex2f(-0.10,-0.60);
    glEnd();
    glDisable(GL_POLYGON_STIPPLE);
    glFlush();

}

int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Prog");
    glutInitWindowSize(320,320);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}