OpenGL Basic Code and Function | for Beginners - Full Tutorial Series



OpenGL Basic (2D) Code and Function | for Beginners

Today we will learn the General Structure of OpenGL  Programs and Some Basic Function componets in OpenGL for the graphics. 
If you new in OpenGL you can see, How To SetUp OpenGL Glut In Codeblocks

You Can Download Base Code form here.


General Structure of OpenGL  Programs: see this base code 2 times very carefully before continued to next.





From this Base Code we can see some basic function. If we understand the uses of this function OpenGL will be very easier for us, believe me.


So, Now we studied these functions and their utilities one by one.



Header Files

#include <windows.h>           }

#include <GL/gl.h>                }   →    #include <GL/glut.h>        
#include <GL/glu.h>              }


           Glut - openGL Utility Toolkit


* In addition, we will often need to iclude header fiels that are required by C++ code.


#include <stdio.h>

#include <stdlib.h>
#include <math.h> 



Display-Window Management Using GLUT

* glutInit(&argc, argv)


Scince we are using the OpenGL Utility Toolkit, our first step is to initialize GLUT.


* glutCreateWindows("Program Name");


Title Bar  now, we can state a display window is to be creayed on the screen with a given caption for the title bar.


* glutDisplayFunc(lineSegment);


We need to specify what the display windows is to contain.For this, we create a picture using OpenGL function and pass the picture definition to the GLUT routine glutDisplayFunc, which assigns our picture to the display window.


 * glutMainLoop();


But the display window is not yet on the screen. We need one more GLUT function to complite the window-processing operations.


* glutInitWindowPosition(50,100);


* glutInitWondowSize(400,300);





We can also set a number of other options for the display window, such as buffering and a choise of color of color modes, with the glutInitDisplayMode function.

* glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);


The above command specifies that a single refresh buffer is to be used for the display window and that we want to use the color mode which uses red,green and blue (RGB).


Background color for the display window:-

* glClearColor(1.0,1.0,1.0,0.0);
* glClearColor(  r  , g  , b  , a  );

The fourth parameter in tha glClearColor function is called the alpha value.
One use for the alpha value is as a "blending" parameter.
Wheb we activate the OpenGL blending operations, alpha values can be used to determine the resulting color for two overlapping object.

0.0 indicates a totally transparent object.
Alpha value of 1.0 indecates an apaque object.

* glClear(GL_COLOR_BUFFER_BIT);

Although the glClearColor command assign a color to the display womdow, but it does not put the display window om the screen.

To get the assigned window color displayed, we need to invoke the glClear(GL_COLOR_BUFFER_BIT); OpenGL function.



Object Color

* glColor(0.0,0.4,0.2);

The suffinx 3f on the glColor function indicates that we are specifying the three RGB color components using floating-point (f) values.

This function requires that the value be in the range form 0.0 to 1.0.



We can set the projection type (mode) and other viewing parameters that we need with the folloing two functions:

* glMatrixMode(GL_PROJECTION);

* glOrtho2D(0.0,200.0,0.0,150,0);

World-coordinate rectangle will e shown within the display window.
Anything outside this coordinate range will not be displayed.


Finally, we need to call the appropriate OpenGL routines to crate out line segment. 


glBegin(GL_LINES);
glVertex2i(180,15);
glVertex2i(10,145);
glEnd();

Now we are ready to put all the pieces together.
The following OpenGL program is organize into three functions.

* We place all initializations and related one-time parameter setting function init. 

* Our geometric description of the "picture" that we want to display is in function line Segment, which is the function that will be referenced by the GLUT function glutDisplayFunc.

* The main function contains the GLUT functions for setting up the display window and getting our line segment onto the screen.

Thank You

Previous Post
Next Post
Related Posts