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 .  

Friday 3 February 2017

Basic OpenGl program

This is  OpenGL c++ basic program. Make sure your OpenGL completely configured in visual studio.

Open visual studio create a new program.



Select visual C++ and click on Empty Project . Edit name of your project and click Ok, It will create a new project.



Now in solution explorer right click on Source Files > Add > New Item


Select C++ File (.cpp) and click Add.

Now the Coding parts begins.

#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

I will explain code in my next post.

Now Run you project . It will Show this output


If this output is showed on your screen your OpenGL is configured in visual studio. check my previous post to configure your visual studio and OpenGL.

Configure OpenGL with Visual Studio

There is a lot of problems  during configuring OpenGl  in visual studio. So in this post i will show you how to configure your OpenGl in visual studio.

Things you need



click link to download.

After download save this folder on desktop for easy access.
Make sure you download the right files.

In some windows files extension( .dll .lib) are hidden by default to show the file extension you can do this.





There is alot of methods available to configure OpenGl in Visual Studio but i will Show you best method. This is one time process no need to repeat for every new project. 

Step 1  

Copy files ( glut.lib and glut32.lib ) and paste it in your C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib  folder.
if your  Microsoft Visual Studio 14.0 is not available then paste it to Microsoft Visual Studio 13.0 or lower version (12.0) VC\lib directory.


Step 2

Copy file ( glut.h ) and paste it in your C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include folder.




Step 3

Copy files ( glut.dll and glut32.dll ) and paste it in your C:\Windows\System32 folder.




Step 4

This is last step.
Copy files ( glut.dll and glut32.dll ) and paste it in your C:\Windows\SysWOW64 folder.



Now your OpenGL is configured in Visual Studio. See my next post to create a basic OpenGL program . Thanks.