Pages

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.

No comments:

Post a Comment