Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.

htm

Using OpenGL with Visual C++
The OpenGL is a graphics library which provides the interface to your graphics hardware. It was first released by SGI systems. SGI is producer of marvelous graphics workstations. They have been the king in the graphics field. They released a line of graphics workstations and server in the market. These workstations were loaded with IRIS/UNIX operating system. They designed the powerful graphics library, OpenGL, to exploit the power of the specialized proprietary graphics hardware. The best thing about the OpenGL is that it is hardware independent library. It does not require any special hardware to perform its functions. If your computer is equipped with special graphics accelerator hardware, it will use it otherwise the required functionality is emulated by software. With the popularity and the power of the OpenGL, it becomes mandatory to provide it on other operating systems platform. Now the powerful OpenGL library is available on Windows platform also. It comes free of cost with the Microsoft Visual C++ pack. The MSDN supports the help files related to OpenGL. The OpenGL can be found on your system at the following location if the Microsoft Visual C++ is installed on your system. The include file path may be: C:/Program Files/Microsoft Visual Studio/VC98/Include/GL This contains three files GL.h GLAUX.h GLU.h Yes, believe me, just three include files, not more than that!!! The library file path may be: C:/Program Files/Microsoft Visual Studio/VC98/lib It contains three OpenGL related libraries GLAUX.lib GLU32.lib OPENGL32.lib

Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

That’s it. If you have such files on your system, you can start programming in OpenGL. As I wrote earlier, OpenGL is a library, so you must include the header file at appropriate places in your program like other libraries and include OpenGL library during linking otherwise you will get link time error messages. Features of OpenGL The following functionalities are provided by the OpenGL. Drawing Primitive Geometric Objects All the primitive drawing objects like triangle strip, quad strip, triangle supported.

line, polygon, fan etc. are

Viewing and Modeling Transformations The orthographic , perspective projection, rotation, scaling, translation etc. supported by the library. Display Lists To store a series of OpenGL Commands and execute at a later time. It helps in building the animation, movies etc. Color Color and model.

shading

technique

to

describe

the

graphical

Lighting Light model for ambient light, light supported by the library. Blending, Antialiasing, Fog These features let you generated scene.

specular

light,

diffuse

create

a

realistic

computer

Drawing Pixels, Bitmaps, Fonts, Images Representation and display of 2D data as pixels, bitmaps or images. Texture mapping Mapping technique objects.

to

wrap

1D

or

2D

texture

on

the

3D

Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

Framebuffer Description and control of all the buffers. It helps in generation of industry standard animations. Evaluators & NURBS Advanced technique for 2D-3D curves and surfaces. Selection and Feedback It allows the selection of 3D object on 3D screen using the mouse. The OpenGL e-book can be searched on the Internet for free download. If you could not find it please contact me for a free copy at support@saturnsoftmills.com

So far, we have talked a lot about the OpenGL. Now it is time to talk about its interface with the Visual C++. This is simple but most important step before you start programming in OpenGL. The one by one steps are given below, follow them and enjoy. 1. Create a New MFC based application (the way you wish). 2. Go to Project->.Setting. Click on the “link” tab. Specify the name of all the OpenGL library in “Object/library Modules”. Press OK. 3. Open your project view class cpp file. Search for the function named “OnInitialUpdate”. If it is not found, go to view->ClassWizard and add it. Add the following code inside the OnInitialUpdate function. void CYourPrjView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class RECT rect; cdc = (CDC*)GetDC(); // Getting the client rect GetClientRect(&rect);

Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

//setting pixel format for GL compatibility PIXELFORMATDESCRIPTOR pfd = { sizeof (PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version number PFD_DRAW_TO_WINDOW | // Flags PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, // RGBA pixel values 24, // 24-bit color 0, 0, 0, 0, 0, 0, // Don't care about these 0, 0, // No alpha buffer 0, 0, 0, 0, 0, // No accumulation buffer 32, // 32-bit depth buffer 0, // No stencil buffer 0, // No auxiliary buffers PFD_MAIN_PLANE, // Layer type 0, // Reserved (must be 0) 0, 0, 0 // No layer masks }; int nPixelFormat = ChoosePixelFormat(cdc->m_hDC, &pfd); SetPixelFormat(cdc->m_hDC, nPixelFormat, &pfd); hrc = wglCreateContext(cdc->m_hDC); // define hrc in your .h file wglMakeCurrent(cdc->m_hDC,hrc); // Setting the gl drawing environment glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,1.0,1.0,2000.0); glViewport(rect.left,rect.top,rect.right,rect.bottom); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0.0,0.0,0.0,1.0); }

Saturn Software Mills Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

4. Declare the HGLRC hrc variable in the YourPrjView.h file. 5. Start programming in OpenGL, dude. Go and write some code in OnDraw function, Compile, link and Run.

It is so simple to use OpenGL in Visual C++ environment. If you are expert programmer and have good knowledge of OpenGL, you can produce your own Jurassic Park. You can be next successful Producer and Director in Hollywood.

Sign up to vote on this title
UsefulNot useful