View Full Version : How to use OpenGL with Borland C++ 5?

01-10-2004, 07:18 AM
I did look at the tutorials on this subject that I found on this site, but nowhere does it state what libraries to add or even where to find them.

I did check the Project Manager and see tons of added files, but without some documentation, these tutorials are pretty much useless.

I am very familiar with OpenGL programming, but have only done so on the Mac. In that case, the libraries were clearly named, i.e., GLUT, OpenGL, etc. I cannot find anything like this on Windows.

I'd like to try and create a VERY simple GLUT app. Maybe just a window with a sphere. I cannot even do that. What am I missing? It cannot possibly be this hard to write an app in Windows, can it?


01-10-2004, 07:34 AM

I don't know about Borland 5, but I'm using C++ Builder 6. All that I've had to do is include the header files, and the IDE does the rest.

The libs for windows are usually opengl32, glu32, and glut.

Hope this helps.


01-10-2004, 02:39 PM
Sorry, I did not finish that. I'm using Borland C++ Builder 5.

I'm sure it would not be much different than 6. Any tips you can share? What do you add to your projects and what directories are they located in (assuming that vs 5 & 6 create the same directories...)?

01-11-2004, 07:03 AM
Go to http://nehe.gamedev.net and download the lesson tutorials in Borland C++ Builder format and look at the code. That is how I got started. Plus i think that the setup for Buidler comes with Examples and an example OpenGL App is one of them. See if you have that one.

01-11-2004, 07:10 AM
I've been using BCB5 for OpenGL development for more than a year now and basically there's nothing special about it.

The only library you'll need in any case is the Opengl32.lib from the Microsoft Platform SDK which is located in <install dir>\CBuilder5\lib\PSDK. CBuilder finds this one automatically, so there's nothing you have to do to make this work. All you need to do to use OpenGL commands is a simple #include <gl/gl.h> (and <gl/glu.h> if you want to use utils).

When it comes to constructing the rendering loop and doing the OpenGL setup, there are different ways. GLUT requires the least number of code lines but is the most troublesome. C++ Builder is built to be a RAD tool which means that it is tightly bound with the VCL (visual control library, something like MFC, but usable). Getting rid of the VCL in a BCB project is hard. If you don't need to get your app fully portable or absolutely can't have the ~3MB vcl-DLLs which have to be present, so your app will run, don't bother.

If you start with a standard project in BCB, there's 3 things you'll need to do:

(1) Include the gl.h/glu.h headers.
(2) Set up OpenGL. This includes the most Windows-specific stuff and works as follows:

(a) Set the PixelFormatDescriptor:
/* This gets the Device Context for your Window's handle. Handle is a property of the TWinControl class from which TForm descends. */
HDC hDC = GetDC(Handle);
/* Set up a PIXELFORMATDESCRIPTOR structure. The details of this struct are documented in the Windows Platform SDK (BCB-Menu Help->Windows SDK). */
/* Choose the PixelFormat. If the returned value is 0, an error has occured */
int PixelFormat = ChoosePixelFormat(hDC,&pfd);
/* SetPixelFormat returns TRUE or FALSE. */
BOOL res = SetPixelFormat(hDC, PixelFormat, &pfd);

(b) Create an OpenGL rendering context and make it current:
/* This creates an OpenGL rendering context and returns a handle to it. If it returns NULL, an error has occured. */
HGLRC hRC = wglCreateContext(hDC);
/* Now make the rendering context current. You have to do this before issuing ANY gl* command. */
res = wglMakeCurrent(hDC,hRC);

(c) Set up the projection matrix. This is already pure OpenGL stuff, Use either glOrtho or gluPerspective.

(3) Do the rendering:
For this you can use a timer or some other mechanism to call your rendering procedure. If you want to create a loop that uses all available resources to render as fast as possible a good idea is to overwrite the Application's OnIdle event. Every BCB project has a global variable called Application of type TApplication. To set up your own OnIdle event, do the following:

(a) Add a method of suitable type to your form like this:
void __fastcall MyForm::MyIdleEvent(System::TObject* Sender, bool &Done);

(b) Assign the event:
Application->OnIdle = MyIdleEvent;

All Done.
Whenever the Application is idle, your MyIdleEvent procedure will be called. There are two things I'd suggest to include. One ist the parameter &Done. If you set this to false, your application will not release control to other applications but will immediately check, if there's work to do and, if not, call your IdleEvent again. If you do this, however, I'd suggest adding the line Application->ProcessMessages(). If you don't, none of your application's messages will be processed.

All together a generously spaced imlementation of the above take somewhere between 80 and 100 lines including comments, procedure declarations and everything else.

One last word on GLUT. Until today I couldn't manage to cleanly include GLUT into my OpenGL application. When i need a fast way for simple spheres or the likes, I use quadrics from GLU.

hope this helps. It's a long read but the implementation is really simple, fast and straightforward.