OpenGL program execution problems in C++ Builder

I am having trouble running OpenGL programs in C++ builder. When I run OpenGL programs from the C++ Builder IDE my machines suffers an assorted collection of problems.

I would first like to know if there is anyone else out there who has been encountering the same or similar problems, and then if so, do you know why they occur, and then can these problems be avoided.

At the moment I am simply running C++ Builder and using its editor and compilation futures, and then running the programs from the command line in a DOS console.

These are the most common problems I have encountered so far:

Execution fails:

Very often after I run an OpenGL program, it simply exits with an error. But there is no obvious pattern. Sometimes with the same program unchanged, the program executes fine with out error, and then at other times it fails to excite successfully as described above. Normally I can just reset the program and try to execute it a few times and eventually after a few tries of resetting and re-executing it finally executes with out error. The random behavior that is exhibited is what makes it so difficult to isolate a problem either in the code or in the configuration of C++ builder. This behavior occurs with any OpenGL Program I try to execute, my own code and other peoples, so I am safely presuming that there is no relationship between these phenomenon and my code.

The computer slows down, BADLY:

After I have been trying to execute OpenGL programs for a while, the entire computer seems to slow down, the hardware acceleration stops working, and the rest of Windows seems to slow down, with normal windows GUI graphics drawing its self in painfully slow steps. Again this behavior is reasonably random, it happens at some point after any ware from about 30 to as many as 100 execution attempts, and the execution attempts are also randomly successful as described above, with a success rate of about 20 – 50 %. Again this has happened with all the OpenGL programs I have tried, my own and other peoples.

Crashes, and Hangs:

On a few occasions C++ builder has simply crashed after I have tried to execute OpenGL programs. And on a few more rare occasions Windows has just hung after I have attempted to run OpenGL programs from C++ Builder. These crashed and hangs happen much more rarely than the other problems and could of coarse simply be crashes and hangs associated with the ‘know to be less than stable’ general operation of M$ Windows.

Possible cause:

Because of the random nature of the problems, I cannot easily identify any obvious causes. But I have noticed that the frequency of the problems increases by about 20% when I am working with OpenGL programs that make use of double-buffered windows. But this might be a side effect of the problems rather than a direct cause. Anyway I thought I’d just mention it just incase is gives anybody any new clues that they might be able to use to help me.

Compilation works fine:

Compilation works fine. I have not encountered any problems with compilation of OpenGL programs in C++ Builder. There are only problems associated with running OpenGL from the C++ Builder IDE. Also when I run programs from the command line in a DOS console, while C++ Builder is open and active, there are no problems either. C++ Builder does not mind if I try to run the programs elsewhere other than from with in it using its program execution facilities.

Help!

So does anybody have any ideas? I’m I being targeted by alien hackers with a twisted sense of humor or are there know problems between OpenGL and C++ Builder.

I have included my system specs below, as well as the source code of a simple OpenGL program, which suffers the offer mentioned problems on my machine. Perhaps there is something wrong with the code, which someone might be able to help me with.

I have also encountered these same problems on another totally different machine that never even had any 3D graphics card installed, which leads me to believe that the problems have nothing to do with my machine’s specs, but I include them here anyway for the sake of completeness, and problem solving efficiency.

CPU: AMD K7 Athlone 650
Main board: Gigabyte GA–71XE
RAM: 128 Meg
Video Card: ELSA ERAZOR III (RIVA TNT 2 based)

Program source code:

//---------------------------------------------------------------------------
//
// Test program
//
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Include files
//---------------------------------------------------------------------------

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>

#pragma hdrstop

//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------

#define SWAPBUFFERS SwapBuffers (ghDC)
#define WIDTH 400
#define HEIGHT 400

//---------------------------------------------------------------------------
// Gloabal variables
//---------------------------------------------------------------------------

char szAppName=“Test Program”; // Application name
HWND ghWnd; // Window handel
HDC ghDC; // Device context handle
HGLRC ghRC; // Rendering context handle
BOOL AppExitFlag=FALSE; // Application exit flag

//---------------------------------------------------------------------------
// Prototypes
//---------------------------------------------------------------------------

LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL InitPixelFormat (HDC);

GLvoid ResizeViewport (GLsizei Width, GLsizei Height);
GLvoid InitOpenGL (GLsizei Width, GLsizei Height);
GLvoid DisplayImage (GLvoid);
GLvoid MainLoopBody (GLvoid);

//---------------------------------------------------------------------------
// Win32 entry point
//---------------------------------------------------------------------------

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG Msg;
WNDCLASS WndClass;

// Register the frame class

WndClass.style         = 0;
WndClass.lpfnWndProc   = (WNDPROC) MainWndProc;
WndClass.cbClsExtra    = 0;
WndClass.cbWndExtra    = 0;
WndClass.hInstance     = hInstance;
WndClass.hIcon         = LoadIcon   (hInstance, szAppName);
WndClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
WndClass.lpszMenuName  = szAppName;
WndClass.lpszClassName = szAppName;

if (!RegisterClass (&WndClass))
    return FALSE;
// Create the frame

ghWnd = CreateWindow (
    szAppName,
    "Test Program",
        WS_OVERLAPPEDWINDOW |
        WS_CLIPSIBLINGS |
        WS_CLIPCHILDREN,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    WIDTH,
    HEIGHT,
    NULL,
    NULL,
    hInstance,
    NULL
);
// Make sure the window was created

if (!ghWnd)
    return FALSE;
// Show and update main window

ShowWindow   (ghWnd, nCmdShow);
UpdateWindow (ghWnd);
// Execute main program loop

while (!AppExitFlag)
{
    // Process all pending Win32 message

    while (PeekMessage (&Msg, NULL, 0,0, PM_NOREMOVE) == TRUE)
    {
        if (GetMessage (&Msg, NULL, 0,0))
        {
            TranslateMessage (&Msg);
            DispatchMessage  (&Msg);
        }
        else
        {
            return TRUE;
        }
    }

    // Process main loop procedure

    MainLoopBody();
}
// Exitprogram

return TRUE;

}

//---------------------------------------------------------------------------
// Main window procedure
//---------------------------------------------------------------------------

LONG WINAPI MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT Rect;

switch (uMsg)
{
    // On create window

    case WM_CREATE:

        ghDC = GetDC (hWnd);
        if (!InitPixelFormat(ghDC))
            PostQuitMessage (0);

        ghRC = wglCreateContext (ghDC);
        wglMakeCurrent (ghDC, ghRC);
        GetClientRect  (hWnd, &Rect);
        InitOpenGL     ((GLsizei) Rect.right, (GLsizei) Rect.bottom);

        break;
    // On window repaint

    case WM_PAINT:

        BeginPaint (hWnd, &ps);
        EndPaint   (hWnd, &ps);

        break;
    // On window resize

    case WM_SIZE:

        GetClientRect  (hWnd, &Rect);
        ResizeViewport ((GLsizei) Rect.right, (GLsizei) Rect.bottom);

        break;
    // On window close

    case WM_CLOSE:

        if (ghRC)
            wglDeleteContext (ghDC);

        if (ghDC)
            ReleaseDC (hWnd, ghDC);

        ghRC = 0;
        ghDC = 0;

        DestroyWindow (hWnd);

        break;
    // On window destroy

    case WM_DESTROY:

        if (ghRC)
            wglDeleteContext (ghRC);

        if (ghDC)
            ReleaseDC (hWnd, ghDC);

        ghRC = 0;
        ghDC = 0;

        break;
    // On keydown

    case WM_KEYDOWN:
        switch (wParam)
        {
            // Enter

            case VK_RETURN:
                break;
            // Escape

            case VK_ESCAPE:

                AppExitFlag = TRUE;

                break;
            // Left

            case VK_LEFT:
                break;
            // Right

            case VK_RIGHT:
                break;
            // Up arrow

            case VK_UP:
                break;
            // Down arrow

            case VK_DOWN:
                break;

        }
        break;
    default:
        lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
        break;
}

return lRet;

}

//---------------------------------------------------------------------------
// Initialize pixel format
//---------------------------------------------------------------------------

BOOL InitPixelFormat (HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd; // Pixel format descriptor
int pf; // Pixel format

pfd.nSize    = sizeof (PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags  = PFD_DRAW_TO_WINDOW |
               PFD_SUPPORT_OPENGL |
               PFD_DOUBLEBUFFER;

pfd.iPixelType   = PFD_TYPE_RGBA;
pfd.cColorBits   = 24;
pfd.cDepthBits   = 32;
pfd.cAccumBits   = 0;
pfd.cStencilBits = 0;

if ((pf = ChoosePixelFormat (hDC, &pfd)) == 0)
{
    MessageBox (NULL, "ChoosePixelFormat failed", "Error", MB_OK);
    return FALSE;
}

if (SetPixelFormat (hDC, pf, &pfd) == FALSE)
{
    MessageBox (NULL, "SetPixelFormat failed", "Error", MB_OK);
    return FALSE;
}

return TRUE;

}

//---------------------------------------------------------------------------
// Resize display viewport
//---------------------------------------------------------------------------

GLvoid ResizeViewport (GLsizei Width, GLsizei Height)
{
glViewport (0,0, Width, Height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (50.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

//---------------------------------------------------------------------------
// Initialize OpenGL
//---------------------------------------------------------------------------

GLvoid InitOpenGL (GLsizei Width, GLsizei Height)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

//---------------------------------------------------------------------------
// Main Program loop
//---------------------------------------------------------------------------

GLvoid MainLoopBody (GLvoid)
{
DisplayImage ();
}

//---------------------------------------------------------------------------
// Display a single OpenGL image frame
//---------------------------------------------------------------------------

GLvoid DisplayImage (GLvoid)
{
// Cube coordinates, formated as 6 sides of 4 3D verticies

static GLfloat v[6][4][3] =
{
    {{1.0, 1.0, -1.0},  {1.0, -1.0, -1.0}, {-1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0}},
    {{1.0, 1.0, 1.0},   {1.0, -1.0, 1.0},  {-1.0, -1.0, 1.0},  {-1.0, 1.0, 1.0}},
    {{1.0, 1.0, -1.0},  {1.0, 1.0, 1.0},   {1.0, -1.0, 1.0},   {1.0, -1.0, -1.0}},
    {{-1.0, 1.0, -1.0}, {-1.0, 1.0, 1.0},  {-1.0, -1.0, 1.0},  {-1.0, -1.0, -1.0}},
    {{1.0, 1.0, -1.0},  {1.0, 1.0, 1.0},   {-1.0, 1.0, 1.0},   {-1.0, 1.0, -1.0}},
    {{1.0, -1.0, -1.0}, {1.0, -1.0, 1.0},  {-1.0, -1.0, 1.0},  {-1.0, -1.0, -1.0}}
};

int f;
// Set colors

glClear   (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
// Set viewing transformtion

glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef  (1.0, 1.0, 1.0);
// Draw a wire from cube

for (f=0; f<6; f++)
{
    glBegin (GL_LINE_LOOP);
        glVertex3fv (v[f][0]);
        glVertex3fv (v[f][1]);
        glVertex3fv (v[f][2]);
        glVertex3fv (v[f][3]);
    glEnd();
}


// Display image

glFlush();

SWAPBUFFERS;

}

[This message has been edited by rohin (edited 12-06-2000).]

Check out this !!!

2.050 What compiler can I use?

OpenGL programs are typically written in C and C++. You can also program OpenGL from Delphi (a Pascal-like language), Basic, Fortran, Ada, and others.

Borland

Programming OpenGL with Borland compilers is the same as with any other compiler, with one exception: OpenGL apps can produce floating point exceptions at run time. To disable these harmless errors, add the following to your app before you call an OpenGL function:

_control87(MCW_EM, MCW_EM);
Borland users need to be aware that versions prior to 4.0 only support OpenGL 1.0 out of the box. Download the OpenGL SDK from Microsoft to use OpenGL v1.1, or v1.2 when it becomes available.

Use Borland’s implib utility to generate Borland-compatible .LIB export libraries from Microsoft-compatible .DLL libraries. If you accidently link with Microsoft-format .LIB files, you will receive a linker error like the following:

C:\BORLAND\BCC55\LIB\GLUT32.LIB’ contains invalis OMF record, type 0x21 (possibly COOF)
The bornews.borland.com Usenet news server has two newsgroups that pertain to graphics: borland.public.delphi.graphics and borland.public.cppbuilder.graphics.

The Borland Community is an online source of FAQs that address Borland compiler issues.

For information on how to use OpenGL through the commercial version of Borland C++ Builder, visit Scott Heiman’s Web page. For information on the free version, go here.

The book Delphi Developer’s Guide to OpenGL by Jon Jacobs is available. The author maintains a web page for this book.

Information on using OpenGL from Delphi can be found here and at the Delphi3D web page. Code and utilities for using OpenGL through Delphi are available.

Some Links !!
http://www.geocities.com/SiliconValley/Hills/6131/ http://www.opengl.org/news/Archives1997.html?builder#first_hit http://www.opengl.org/developers/faqs/technical/gettingstarted.htm?builder#f
irst_hit

I am pretty sure that Borland does not have any problems with OpenGL. The reason for the slowdowns can be that the memory on the graphic card is not released after some crashes.