This toturial was written for me by Imran Khan (email@example.com,
My First OpenGL Program:
In this section you will learn how to start programming with OpenGL and how to display a basic openGL window of any resolution,size.
Now you can use OpenGL libraries in your Program.
Windows programmers are familiar with first headerfile ' window.h ' use for windows programming. Next 3 header files are for openGL Libraries.
First 2 variables are related 2 openGL and last 2 are related to windows programming. First we declare a Rendering Context 'hRC' which will links OpenGL to device context. To Draw anything on a window you have to create Device context ( 2nd line).so 'hRC' is rendering context and 'hDC' is device context. Next variable holds the Window handle and next varible holds the instance of the window we will create. Last 2 variables are boolean type ,first variable tells that whether the window is active or not, second variable tells us that whether the window is fullscreen or not. By default these values are set to TRUE.
Now we will Initialize OpenGL. This method will be called after the creation of window.
In the first line we enables the smooth shading.Smooth shading blends colors nicely across a polygon, and smoothes out lighting. Next we describe the background color of the window.g1ClearColor method got 4 arguments. First is red,2nd is green and 3rd is blue,4th one is a alpha value.You can create different colors by mixing up these values But the range of these values are from 0.0f to 1.0f. More the value more brighter will be the color.Then we set the prespective view. Next we return true , i.e opengl initializse correctly. If any error occur u can return false too.
We have initialize OpenGL.Now we will write a function which will draw to the screen.
First line clearing the screen to the color we made in the previous method and clear the depth buffer.Second line reset the scene. Then we return True that our method deosn't got any error.
Now we will define an method which is use to resize the scene according to the width and height of the Window.This method is not compulsory but it is better to use it.
First we reset the viewport according to width and height of the window.glMatrixMode(GL_PROJECTION) Select The Projection Matrix. The perspective matrix isresponsible for adding perspective to our scene. glLoadIdentity() is use to reset the Projection Matrix. glMatrixMode(GL_MODELVIEW) select the modelview matrix. The modelview matrix is where our object information is stored. Then we reset the modelview matrix.
Now we will define a method which will be called whenever u want to kill this openGL window.It will release the
Rendering Context, Device Context and Window Handle.
First we check the ' fullscreen' flag whether it is True or not. If it is true , it means that the window is fullscreen. We then switch to window screen so that we can destroy it.Then we try to destroy the Rendering Object , first we check whether rendering object exist or not .If it exist it will try to detach the hRC from the hDC.If it fails to destroy it will show a message. Then we try to destroy Rendering Context.If it fails it will show a error message.In the same manner we try to destroy Device Context,Window,Unregistered the window class.
Here comes the main and the most important part of the code. It will Create OpenGL window.
' CreateGLWindow ' has 5 arguments:
First we define some variables. First define a variable to store the Pixel format .wc hold Window Class structure.dwExStyle and dwStyle will store the Window Style Information. dwExStyle for Extended Style and dwStyle for Normal Style. Next 5 variables are use to define the boundries of the window. Then we assign the value of fullscreenflag to fullscreen ( which we have define above). Next we grab an instance for our Window, then we define the Window Class ( windows programmer familiar with this ). Then we try to register the class .If we fail an error message will be shown.
Next part of the function...
First we check whether the window is fullscreen or not.If it is then we set fullscreen mode.Then we define a Device mode( DEVMODE dmScreenSettings ) and then clear room to store our video (memset) .Then We set the width, height and bits that we want the screen to switch to.Then we check whether the fullmode is set successfully .If it is not it will show the error message "The Requested Fullscreen Mode Is Not Supported ...". with "Yes/NO" option. If you press No, Program will exit( in the else code ).If you press Yes. It will assign FLASE to FULLSCREEN flag , i.e now it will show Window.
Next we again check " if (fullscreen) ". Whether the mode is still fullscreen. If it is fullscreen then it will choose the extended window style,Hides the Cursor.If it is a window instead of fullscreen mode, we'll add WS_EX_WINDOWEDGE to the extended style. This gives the window a more 3D look. For style we'll use WS_OVERLAPPEDWINDOW instead of WS_POPUP. WS_OVERLAPPEDWINDOW creates a window with a title bar, sizing border, window menu, and minimize / maximize buttons.Last line adjust our window depending on what style of window we are creating.It is not usefull in Fullscreen Mode.
Next Part of the function...
Now we will create a typical Window ( Windows programmers are familiar with this, so no explaination required for this code). Just one thing want to describe is this that we include the styles WS_CLIPSIBLINGS and WS_CLIPCHILDREN along with the style of window we've decided to use.WS_CLIPSIBLINGS and WS_CLIPCHILDREN are both REQUIRED for OpenGL to work properly. These styles prevent other windows from drawing over or into our OpenGL Window..In this section of code we check whether we create the window successfully or not. If not the we call out KillGLwindow method ( which we have define above ). Then we show a error Message and then return false.
Next Part of the function....
This part of code describes a Pixel Format. We choose a format that supports OpenGL and double buffering,RGBA (red, green, blue, alpha channel),Set Color Depth and set up a 16bit Z-Buffer other arguments are not important or not useful here.
Next Part of the Code...
In this section of code we try to get Device Context,find a pixel format that matches the one we described above,setting the pixel format,get a Rendering Context.If we are succeed in all these things then we will make the Rendering Context active.
Next Part of the Code....
Now we will show the window, set it to be the foreground window (giving it more priority) and then set the focus to that window. Then we'll call ReSizeGLScene passing the screen width and height to set up our perspective OpenGL screen.
Now finally we will write Window Method which will deal with the message.
This Proc deals with the messages like, Is window active, Is keypressed,Is the windowsize changed by the user,Is the Window close.( Windows programmers already know this ).
Now we will describe the Main function From which our application start running.
In this Section of Code First we will show a message box so that user can choose whether he want windowed style or fullscreen.If it press 'No' , it will make the fullscreen flag to Flase. and call the CreateGLWindow method , which we have define above. Then we read Message By using PeekMessage().
|Correspondence about this page|
This site may have errors. Don't use for critical systems.