Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Introduction to OpenGL

 OpenGL is graphic software (API) which allows us to create 2D or 3D high quality images
and animations i.e. Main purpose of OpenGL is rendering (process of generating image
from a model or from scene). OpenGL is an API (Software Interface to the graphics
Hardware)

Graphic/image

 OpenGL is platform (OS) independent( Applications programs can run on windows and Linux) , Hardware
independent (will run on PC’s, workstation, super computer), Open source software (free).
 Uses any on the programming language like c,C++, java, Python for operating

Procedure of Generating Images in OpenGL


To construct a picture or image we need to give the geometrical description of the object which determines the
location of the object and shape of the object using the Cartesian coordinate reference frame.

Constructing a scene from multiple Objects


Step1: Define shapes of individual objects in a separate reference frame.
step2: Place the objects into appropriate location within a scene (only one) reference frame called world
coordinates
Structure of a OpenGL Program

Preprocessor Directives

display function definition


Init function definition

main function()
{
….

}
#include<GL/glut.h> //Preprocessor Directive

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(20);
glBegin(GL_POINTS);
glVertex2i(0,0);
glEnd();
glFlush();
}

void init()
{
glClearColor(1.0,1.0,1.0,0.0); //set the background color of the window
glColor3f(1.0,1.0,1.0); //set the background color of the window
//glPointSize(4.0); // point size default is 1.0
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0); //window origin //clipping window or normal window or viewing window
}

int main(int argc, char ** argv)


{
glutInit(&argc,argv); // initialize the GLUT and this function should be called before other GLUT functions.
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); It is used to specify refresh buffer and colour modes.
glutInitWindowSize(1000,1000);
glutInitWindowPosition(0,0); //It specifies the position of the window
glutCreateWindow("my first openGL program"); //used for creating the window along with that we can give the name of the program

glutDisplayFunc(display); This function gives picture definition to the display window. It is first and most important event call back function.

init();
glutMainLoop();
}

pre-processor Directives

GL, GLU, GLUT [GLX,AGL,WGL]: OpenGL can be accessed by application program directly through functions in
these 3 libraries.

Graphics Library (GL): Core library of OpenGL which contains basic functions/primitive functions. Functions in GL library
begins with gl
Graphics Library Utility (GLU) : contains functions for setting up viewing and projection matrices.
Graphics Library toolkit (GLUT): contains functions to manage window system. All GLUT functions start with prefix glut.
0)
Note: instead of including GL, GLU, GLUT separately we use GL/glut.h

To display our graphical object/output on the screen we need to use window (clipping window). Five
routines/functions are used to initialize window.

STEP-I SPECIFYING WINDOW CHARACTERISTICS [I.E SIZE, POSITION ETC].

i. glutIit(&argc, argv)

This function initialize the GLUT and this function should be called before other GLUT functions.
2. glutInitDisplayMode : It is used to specify refresh buffer and colour modes.

glutInitDisplayMode(GLUT_single|GLUT_RGB)

3. glutCreteWindow(char *string);

example glutCreateWindow("my first openGL program");

4. glutInitWindowPosition(int x,int y)

Displays window that we created will be in some default position and size. By using this function we can specify the
position/location of the window with reference to the display screen whose origin is at left top corner.

Example

glutInitWindowPosition(50,100)

5. glutInitWindowSize(int width,int height);

Example: glutInitWindowSize(400,300);

This function specfies size of the clipping window in pixels

STEP-II OBJECT SPECIFICATION AND CREATION INSIDE CLIPPING WINDOW.

Now we need to specify what object/image display window will contain:

For this create an object/image/picture using OpenGL functions and pass this picture definition as argument to the
glutDisplayFunc() i.e. glutDisplayFunc(function_name); //here function_name is diplay()

This function gives picture definition to the display window. It is first and most important event call back function.

Note: Whenever GLUT determines the window contents need to be redisplayed then glutDisplayFun() is executed.

glClearColor(r,g,b,alpha); alpha=0 it is transparent object, 1=opaque object. This glClearColor function assigns a
color to display window after glClear openGL function is called. i.e.

glClear(GL_COLOR_BUFFER_BIT); is called.

Here GL_COLOR_BUFFER_BIT is an openGL symbolic constant which specifies that it is the bit value in the
color buffer (refresh buffer) that is to be set to the values indicated in the glClearColor function

Representing 2D or 3D objects

Now we want to tell openGL that how we want to project our picture onto the display window [viewing]

viewing operation need the following 2 functions:


glMatrixMode(GL_PROJECTION);

glOrtho2d(L,R,B,T);

You might also like