Social Icons

Featured Posts

Wednesday, 16 July 2014

Open GL Program to draw various primitives

#include<GL/gl.h>
#include<GL/glut.h>

int flag=0;
void display()
{
glClearColor(0.0,0.0,1.0,0.0);
   glClear(GL_COLOR_BUFFER_BIT );
   if(flag==0)
   {
     glBegin(GL_TRIANGLES);
     glColor3f(1.0,0.0,0.0);
     glVertex2f(0.0,0.4);
     glVertex2f(-0.4,-0.4);
     glVertex2f(0.4,-0.4);
     glEnd();
   }
   else if(flag==1)
   {
     glBegin(GL_QUADS);
     glColor3f(0.0,1.0,0.0);
     glVertex2f(0.2,0.2);
     glVertex2f(-0.2,0.2);
     glVertex2f(-0.2,-0.2);
     glVertex2f(0.2,-0.2);
     glEnd();
   }
   else if(flag==2)
   {
     glBegin(GL_POLYGON);
     glColor3f(1.0,1.0,0.0);
     float cx=0.0,cy=0.0,r=0.5,theta;
     int num_segments=40,i;
     for(i=0;i<num_segments;i++)
     {
        theta=(2*3.14*i)/num_segments;
        float x=r * cosf(theta);
        float y=r * sinf(theta);
        glVertex2f(cx+x,cy+y);                       
     }
     glEnd();
   }
   glFlush();
}

void processNormalKeys(unsigned char key,int x,int y)
{
     if(key=='Q' || key=='q')
     {
          flag=1;
     }
     else if(key=='t' || key=='T')
     {
          flag=0;
     }
     else
     {
         flag=2;
     }
     glutPostRedisplay();
}
void processSpecialKeys(int key, int x, int y)
 {
   
    if(key== GLUT_KEY_F1)
    {
             flag=2;
             }
             glutPostRedisplay();
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (150, 150);
   glutCreateWindow ("Primitives");
   glutDisplayFunc(display);
   glutKeyboardFunc(processNormalKeys);
   glutSpecialFunc(processSpecialKeys);
   glutMainLoop();
   return 0;
}



                                                         OUTPUT

                               

Open GL Program to perform Keyboard Events

#include<GL/gl.h>
#include<GL/glut.h>

int flag=0;
void display()
{
     glClearColor(0.0,0.0,1.0,0.0);
   glClear(GL_COLOR_BUFFER_BIT );
   if(flag==0)
   {
     glBegin(GL_TRIANGLES);
     glColor3f(1.0,0.0,0.0);
     glVertex2f(0.0,0.4);
     glVertex2f(-0.4,-0.4);
     glVertex2f(0.4,-0.4);
     glEnd();
   }
   else if(flag==1)
   {
     glBegin(GL_QUADS);
     glColor3f(0.0,1.0,0.0);
     glVertex2f(0.2,0.2);
     glVertex2f(-0.2,0.2);
     glVertex2f(-0.2,-0.2);
     glVertex2f(0.2,-0.2);
     glEnd();
   }
   else if(flag==2)
   {
     glBegin(GL_POLYGON);
     glColor3f(1.0,1.0,0.0);
     float cx=0.0,cy=0.0,r=0.5,theta;
     int num_segments=40,i;
     for(i=0;i<num_segments;i++)
     {
        theta=(2*3.14*i)/num_segments;
        float x=r * cosf(theta);
        float y=r * sinf(theta);
        glVertex2f(cx+x,cy+y);                       
     }
     glEnd();
   }
   glFlush();
}

void processNormalKeys(unsigned char key,int x,int y)
{
     if(key=='Q' || key=='q')
     {
          flag=1;
     }
     else if(key=='t' || key=='T')
     {
          flag=0;
     }
     else
     {
         flag=2;
     }
     glutPostRedisplay();
}
void processSpecialKeys(int key, int x, int y)
 {
   
    if(key== GLUT_KEY_F1)
    {
             flag=2;
             }
             glutPostRedisplay();
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (150, 150);
   glutCreateWindow ("KeyBoard Events");
   glutDisplayFunc(display);
   glutKeyboardFunc(processNormalKeys);
   glutSpecialFunc(processSpecialKeys);
   glutMainLoop();
   return 0;
}





                                                    OUTPUT

                          

Open GL Program to display a Clock


#include<GL/glut.h>

float theta = 0.0, thetar;
int flag=0;
void mydisplay(void)
{
   
     float cx=-0.0,cy=0.0, r=1;
     int num_segments=80;
     int i;
     glClear(GL_COLOR_BUFFER_BIT);
     glBegin(GL_LINES);
    thetar=theta*(3.14/180);
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(0.0, 0.0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(-sin(thetar), cos(thetar));
       
     glEnd();
 
     glBegin(GL_LINE_LOOP);
     for(i = 0; i<num_segments; i++)
     {
        float theta1;
        theta1 = (2.0 * 3.1415926 * (i)) / (num_segments);
        float x = r * cosf(theta1);
        float y = r * sinf(theta1);
        glVertex2f(x + cx, y + cy);
     }
     glEnd();
     glutSwapBuffers();
}

void spinDisplay(void)
{
  theta -= 0.1;
  if (theta < 360.0)
      theta += 360.0;
  glutPostRedisplay();
}


int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(150, 150);
  glutInitWindowSize(500,500);
  glutCreateWindow("Clock");
  glutDisplayFunc(mydisplay);
  glutIdleFunc(spinDisplay);
  glutMainLoop();
  return 0;
}



                                                        OUTPUT

                                            

Open GL Program to create a Circle

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void display()
{
float cx=0.0,cy=0.0, r=0.5;
int num_segments=50;
int i;
glClearColor(0.0,0.6,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
for(i = 0; i < num_segments; i++)
{
float angle;
angle = (2.0 * 3.1415926 * (i)) / (num_segments);
float x = r * cosf(angle);
float y = r * sinf(angle);
glVertex2f(x + cx, y + cy);
}
glEnd();
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE| GLUT_RGB );
glutInitWindowSize (500, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Circle");
glutDisplayFunc(display);
glutMainLoop();
}




                                                         OUTPUT

                                


Open GL Program to perform various Mouse Events

#include<GL/gl.h>
#include<GL/glut.h>
void display()
{
   glClear(GL_COLOR_BUFFER_BIT );
   glBegin(GL_TRIANGLES);
   glVertex2f(0.0,0.4);
   glVertex2f(-0.4,-0.4);
   glVertex2f(0.4,-0.4);
   glEnd();
   glFlush();
}

void processMouse(int button,int state,int x,int y)
{
     if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
     {
       glColor3f(1.0,0.0,0.0);
     }
     else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
     {
          glColor3f(0.0,1.0,0.0);
     }
     else
     {
          glColor3f(1.0,1.0,0.0);
     }
     glutPostRedisplay();
}

void processActiveMotionMouse(int x,int y)
{
     if(x<300)
     {
       glColor3f(1.0,0.0,0.0);
     }
     else if(x>300)
     {
          glColor3f(0.0,1.0,0.0);
     }
     else
     {
          glColor3f(1.0,1.0,0.0);
     }
     glutPostRedisplay();
}
void processPassiveMotionMouse(int x,int y)
{
     if(x<300)
     {
       glColor3f(1.0,0.0,0.0);
     }
     else if(x>300)
     {
          glColor3f(0.0,1.0,0.0);
     }
     else
     {
          glColor3f(1.0,1.0,0.0);
     }
     glutPostRedisplay();
}
void processEntryMouse(int state)
{
     if(state==GLUT_LEFT)
     {
       glColor3f(1.0,0.0,0.0);
     }
     else
     {
          glColor3f(1.0,1.0,0.0);
     }
     glutPostRedisplay();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (150, 150);
   glutCreateWindow ("Mouse Events");
   glutDisplayFunc(display);
   glutMouseFunc(processMouse);
   //glutMotionFunc(processActiveMotionMouse);
   //glutPassiveMotionFunc(processPassiveMotionMouse);
   //glutEntryFunc(processEntryMouse);
   glutMainLoop();
   return 0;
}




                                                    OUTPUT

                             

Friday, 4 July 2014

Tutorial of OpenGL

What Is OpenGL?

OpenGl: - OpenGL (Open Graphics Library) is a cross-language, multi-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a Graphics processing unit (GPU), to achieve hardware-accelerated rendering.
OpenGL was developed by Silicon Graphics Inc. (SGI) from 1991 and released in January 1992 and is widely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation, and video games. OpenGL is managed by the non-profit technology consortium Khronos Group.
                                                                                             
Version
OpenGL 1.1
Release Date: March 4, 1997

OpenGL 1.2
Release Date: March 16, 1998

OpenGL 1.2.1
Release Date: October 14, 1998

OpenGL 1.3
Release Date: August 14, 2001

OpenGL 1.4
Release date: July 24, 2002

OpenGL 1.5
Release date: July 24, 2002

OpenGL 2.0
Release Date: September 7, 2004

OpenGL 2.1
Release Date: July 2, 2006

OpenGL 3.0
Release Date: August 11, 2008

OpenGL 3.1
Release Date: March 24, 2009

OpenGL 3.2
Release Date: August 3, 2009

OpenGL 3.3
Release Date: March 11, 2010

OpenGL 4.0
Release Date: March 11, 2010

OpenGL 4.1
Release Date: July 26, 2010

OpenGL 4.2
Release Date: August 8, 2011

OpenGL 4.3
Release Date: August 6, 2012

OpenGL 4.4
Release Date: July 22, 2013





Required
• Knowledge in C\C++
• Dev C++ : a freeware for C/C++
http://www.bloodshed.net/devcpp.html
• Glut Dev Pack


Setting OpenGL Dev Pack

1. To use Open GL install the correct version of glut DevPack go to http://www.nigels.com/glt/devpak: download glut.3.7.6+.DevPak to a local folder
2. In Dev C++ open the package manager: Tools->Package Manager
3. Install the glut package: Package->Install Package
4. Browse to the local location where the package was downloaded to follow the installation wizard
5. The glut package should now appear in the main pane
6. Exit package manager


Setting Project for OpenGL

1. To compile, build and run using Dev C++: make a new project: File -> New -> Project and choose empty project and and C++ project is selected.
2. Create the new project in the folder with the source file.
3. Add the .cpp file to the project: Project -> add to project: select the .cpp file
4. Define the linker options: Project -> Project options
5. In the Parameters tab window add the following line in the Linker pane: -/glut32 -/glu32 -/opengl32 -/winmm -/gdi32
6. click ok
7. Compile and run the project:
8. Execute -> Compile & run


Various Pieces

gl: The basic libraries.
glu: Advanced implementation of OpenGL.
          Higher level function calls/commands. i.e.
          gluSphere.
glut: Generic platform independent library.



Hello World.cpp

#include <gl/glut.h>
void display( )
{
// nothing to see here, please move along
}
int main(int argc, char* argv[])
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 50, 50 );
glutInitWindowSize( 200, 200 );
glutCreateWindow( "Hello World" );
glutDisplayFunc( display );
glutMainLoop( );
return 0;
}



Functions

void glutInit()
      to initialize the GLUT library, the variables should be the same in main()

void glutInitDisplayMode(),
void glutInitWindowsSize(),
void glutWindowPosition()

     define the type of window, its size, and its position (detail in OpenGL Utility ToolKit)

int glutCreateWindow(“Hello World”)
     creates a window on the screen with the given title. This function returns an integer that can be used to refer to the window in multiwindow environments

void glutMainLoop()
     causes the program to enter an event-processing loop. Should be in the end in the main function


Display Function

void display( )
{
// Clear the background before drawing
glClear( GL_COLOR_BUFFER_BIT );
// Actual code to do the drawing
glBegin( GL_POLYGON );
glVertex2f( -0.5, -0.5);
glVertex2f( -0.5, 0.5);
glVertex2f( 0.5, 0.5);
glVertex2f( 0.5, -0.5);
glEnd();
// Ensure all drawing commands are executed
glFlush( );
}



Procedure to draw a rectangle

Clearing the window
          glClear(GL_COLOR_BUFFER_BIT);
Drawing the rectangle
          Must starts with glBegin and ends with glEnd. Drawing the rectangle must be in between them
Flushing GL buffers
          Force to render or display the rectangle on the windows using glFlush functions
glFlush();



What is entity, geometry and attributes

Entity: lines, polygon, triangle
Geometry: coordinates of the vertices
Attribute: color, thickness



Data Types

character         C-language type             OpenGL type definition
     b                   signed char                         GLbyte
     s                   short                                   GLshort
     i                    int                                      GLint, GLsizei
     f                    float                                   GLfloat, GLclampf
     d                   double                                GLdouble, GLclampd
     ub                 unsigned char                     GLubyte, GLboolean
     us                 unsigned short                    GLushort
     ui                  unsigned int                       GLuint, GLenum, GLbitfield
                          void                                    GLvoid



Coordinate System in OpenGL

void glVertex{234}{sifd}(TYPE x, TYPE y, …)
void glVertex{234}{sifd}v(TYPE *v)

specifies the location in space of the vertex with type of coordinate.
If v presence, the coordinate must be declared as array

glVertex2f(2.0, 2.0);
glVertex2fv(p); // p[0] = 2.0, p[1] = 2.0

The 4th coord is homogenous specifically used during transformation



CS in OpenGL and Window

glutinitwindowside() sets the windows size in pixels.

CS in OpenGL is according to Cartesian CS in plane XY. By default the origin is in the center of the window and bottom left is (-1,-1) and top right is (1,1).


200 x 200 pixel and 500 x 500 pixel will have the same bottom left (-1,-1) and top right (1,1)


gluOrtho2D sets the new bottom left and top right point.


gluOrtho2D(GLdouble minx, GLdouble maxx , GLdouble miny, GLdouble maxy) will set the new clipping area. This is place after glutDisplayFunc() function

glutDisplayFunc( display );
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);




Color in OpenGL

void glColor3{bsifd ubusui}{v} (TYPE r, TYPE g, TYPE b);
void glColor4{bsifd ubusui}{v} (TYPE r, TYPE g, TYPE b, TYPE a);

a is alpha, to measure opacity; 1.0 is opaque and 0.0 is
transparent
(1.0, 1.0, 1.0): white
(1.0, 0.0, 0.0): bright red



Coloring the polygon
                                                                                                                  
glColor3f(1.0,1.0,0.0);
// Actual code to do the drawing
glBegin( GL_POLYGON );
glVertex2f( -0.5, -0.5);
glVertex2f( -0.5, 0.5);
glVertex2f( 0.5, 0.5);
glVertex2f( 0.5, -0.5);
glEnd();


glBegin( GL_POLYGON );
glColor3f(1.0,1.0,1.0);
glVertex2f( -0.5, -0.5);
glColor3f(1.0,0.0,0.0);
glVertex2f( -0.5, 0.5);
glColor3f(0.0,1.0,0.0);
glVertex2f( 0.5, 0.5);
glColor3f(0.0,0.0,1.0);
glVertex2f( 0.5, -0.5);
glEnd();
Thickness Attribute

Point: the size of the point
void glPointSize(GLfloat pixel_size )
     glPoint Size( 5.0);
          *this function must be before glBegin

Line: the width of the line
void glLineWidth(GLfloat pixel_width)
     glLineWidth( 5.0);



Primitive Points and Lines























Filled Primitives



Window related functions

void glutDestroyWindow(int id)
Destroys top-level window id

void glutSetWindow(int id)
Set the current to the window with identifier id

int glutCreateSubWindow(int parent, int x, int y,int width, int height)
Create subwindows to specified id windows

void glutPostWindowRedisplay(int winid)
Post a redisplay for window winid



Example


Circle can be drawn using connected lines. The lines
are drawn using the following formula
x = r cos (θ) y = r sin (θ)
As the angle gets smaller, the connected lines will
form a circle. Therefore, the task is to draw a circle
using this angle θ = 10o.