Created by Jijith Nadumuri at 27 Jul 2010 07:14 and updated at 07 Dec 2011 10:51

OpenGL (Open Graphics Library) is a 3D Graphics API, that is widely used along with DirectX (or the DirectXGraphics component of DirectX). As the name indicate it is 'open' or is not proprietary like DirectX (owned by Microsoft). OpenGL is better than DirectX if one need to develop cross platform graphics applications. Traditionally OpenGL is the favorite API of CAD developers and UI and HMI developers and DirectX is the favorite API of Game Developers, though both can be used vice versa. There is an audio API that complements OpenGL, named OpenAL (Open Audio Library) which is comparable to DirectSound component of DirectX. Together with OpenGL and OpenAL one can make Audio-Visual applications (including games) much like using DirectXGraphics and DirectSound.

Initializing OpenGL

Below are the steps needed for initializing OpenGL for a Windows application (that uses CWnd class in MFC or the window-handle HWND in case of Win32 SDK programming)

  1. Specify the size of the Window
  2. Specify the position of the Window
  3. Create the Window
  4. Inform OpenGL about the Window
  5. Provide OpenGL the Device Context of the Window
  6. Set the pixel format of the Device Context of the window and make it compatible with OpenGL
  7. Set the Rendering Context of the Window
  8. Set the OpenGL View Port
  9. Set the Depth Buffer
  10. Set the Projection Matrix
  11. Set the Perspective
  12. Set the ModelView Matrix
  13. Create some 3D object like a cube so that OpenGL can render something

All the above steps is to be placed in the initialization routine of the application

Rendering using OpenGL

Ensure that OpenGL is properly initialized and set up. Then call your animation functions and Render Functions. These functions should change some animation parameters, and invoke OpenGL render functions.

Pseudo Code

The above steps are best explained using a pseudo code. Assuming that you know how to encapsulate an API or a technology into a class and use it, below is a pseudo code sample to initialize and use OpenGL. For simplicity, OpenGL code is encapsulated into a class. Assume that the variable m_OpenGL represents an instance of this class.

For initialization these top level methods of the class can be used:-

m_OpenGL.SetWindow(TheWindow) Tell OpenGL about the window where it can render
m_OpenGL.SetLocation(WindowPosition_x, WindowPosition_y) Position the OpenGL window
m_OpenGL.SetSize(Window_width, Window_height) Size the OpenGL window
m_OpenGL.Initialize() Initialize OpenGL

Initialize steps

Let us see what the Initialize() function contains:-

IF any of the above steps fails stop and set m_bInitOpenGL = false

Creating window

Setting Device Context

Setting the right Pixel Format for DC

Setting the OpenGL Renderng Context

Setting the View Port

Setting the Depth Buffer

Set Projection Matrix

Set Perspective

Set Model View Matrix


Share:- Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License