Pages

Saturday 4 February 2017

Understanding Basic OpenGL Program

In my Previous Post I write a basic OpenGL program in visual studio . In this Post i will explain the OpenGL code .

#include <windows.h>   // use as needed for your system
#include <glut.h>   //graphic libarary toolkit

//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(0.0, 1.0, 0.0, 0.0);      // set the bg color to a bright green
glColor3f(0.0f, 0.0f, 0.0f);           // set the drawing color to black
glPointSize(4.0);             //set the point size to 4 by 4 pixels
glMatrixMode(GL_PROJECTION);        // set up appropriate matrices- to be explained
glLoadIdentity();                   // to be explained
gluOrtho2D(0.0, 640.0, 0.0, 480.0); // to be explained
}

//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
// the redraw function
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);     // clear the screen


glFlush(); // send all output to display

}

//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char **argv)
{
glutInit(&argc, argv);          // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize(640, 480);     // set the window size
glutInitWindowPosition(100, 150); // set the window position on the screen
glutCreateWindow("OpenGL Basic Program"); // open the screen window(with its exciting title)
glutDisplayFunc(myDisplay);     // register the redraw function
myInit();
glutMainLoop();       // go into a perpetual loop
}

Download Source Code

Starting from header file 

 #include <glut.h>

This line include the OpenGL library in you project . To use GL functions you need to include this library in each project . 

Main( )

void main(int argc, char **argv)
{
glutInit(&argc, argv);          // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize(640, 480);     // set the window size
glutInitWindowPosition(100, 150); // set the window position on the screen
glutCreateWindow("OpenGL Basic Program"); // open the screen window(with its exciting title)
glutDisplayFunc(myDisplay);     // register the redraw function
myInit();
glutMainLoop();       // go into a perpetual loop
}

This is the main method of your program. your program is starting from here. This is the basic program so i am explain basic things. as we proceed i will explain more deeply.

 glutInit(&argc, argv); 

 glutInit will initialize the toolkit needed for your system to run OpenGL program and should be called before any OpenGL funtions.

glutInitWindowSize(640, 480);

This function will create a screen for you x-axis 640 (Width) and y-axis 480 (Height). you can change values and see the change in window size. 

glutInitWindowPosition(100, 150);  

This function is used to set the window position on the screen . takes two parameters X and Y . 
In this case  X will be left 100 means you want margin of 100 pixels from left and 150 from top. 

glutCreateWindow("OpenGL Basic Program");

This function is used to set the title of the Screen , and display on the top of the screen . 

glutDisplayFunc(myDisplay);

This function is get called each time you want to draw something on screen . OpenGL is Event Driven ( when something happen ) ( mouse click etc) This function call function inside it named as myDisplay . Every thing you want to draw  will be coded inside this myDisplay function .

myInit();

 The initialize  function of OpenGL it will initialize basic screen things to display the screen . like background color foreground color ( text color ) . and size of the text ( thickness ) etc.

glutMainLoop();

This function puts the program in infinite loop . OpenGL is event driven, program will wait for event
to occur and then display graphics according to event which is called .

void myInit(void)
{
glClearColor(0.0, 1.0, 0.0, 0.0);      // set the bg color to a bright green
glColor3f(0.0f, 0.0f, 0.0f);           // set the drawing color to black
glPointSize(4.0);             //set the point size to 4 by 4 pixels
glMatrixMode(GL_PROJECTION);        // set up appropriate matrices- to be explained
glLoadIdentity();                   // to be explained
gluOrtho2D(0.0, 640.0, 0.0, 480.0); // to be explained
}


  • glClearColor(0.0, 1.0, 0.0, 0.0);  This function takes arguments these arguments are colors makes  RGB value and display that color . this function takes values from 0-1 . RGB(0,0,0) is white and RGB(1,1,1) is black. 
  • glColor3f(0.0f, 0.0f, 0.0f);  This function change the (foreground) font color of your drawing objects (lines point etc).
  • glPointSize(4.0); This function set the thickness of your drawings. 



void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);     // clear the screen


glFlush(); // send all output to display

}

This function is called each time you run you program . you can write code in it to draw some points or shape or call other function in it .

glClear(GL_COLOR_BUFFER_BIT); 

This function is used to clear screen .

Now Run you project . It will Show this output


This is basic program to create a screen. Follow my next post to learn more with OpenGL .  

No comments:

Post a Comment