BCB and OpenGL...

Hi… I’m trying to create an OpenGL App. with Borland C++Builder 6.0. As far as I read I get to these:

Unit1.cpp

 #include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
        Application->OnIdle = IdleLoop;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdleLoop(TObject*, bool& done)
{
     RenderGLScene();
     SwapBuffers(hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RenderGLScene()
{
        glRectf(-5.0, 5.0, 5.0, -5.0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    hdc = GetDC(Handle);
    SetPixelFormatDescriptor();
    hrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc, hrc);
    SetupRC();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetupRC()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    ReleaseDC(Handle,hdc);
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hrc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetPixelFormatDescriptor()
{
        PIXELFORMATDESCRIPTOR pfd;
        ZeroMemory( &pfd, sizeof( pfd ) );
        pfd.nSize = sizeof( pfd );
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
              PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
        int iFormat = ChoosePixelFormat( hdc, &pfd );
        SetPixelFormat( hdc, iFormat, &pfd );
}

Unit1.h

 #include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <gl/gl.h>
#include <gl/glu.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
private:
    HDC hdc;
    HGLRC hrc;
    int PixelFormat;
public:
    __fastcall TForm1(TComponent* Owner);
    void __fastcall IdleLoop(TObject*, bool&);
    void __fastcall RenderGLScene();
    void __fastcall SetPixelFormatDescriptor();
    void __fastcall SetupRC();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;  

but only the window appears… not the OpenGL and the rect it has to draw… can anyone help me solv the problem… Thanks beforehand :slight_smile:

What’s the result? A white window? Then it might just work.

With the default transformation matrix setup OpenGL shows the contents of the unit cube range [-1.0, +1.0] in all three axes.

Your clear color is white and the default color is white as well and your rect is too big.

Try to replace the functions with the following code:
glClearColor(0.0f, 0.0f, 0.5f, 0.0f); // dark blue
and the TForm1::RenderGLScene() should contain
glClear(GL_COLOR_BUFFER_BIT); // Belongs into the redraw function
glColor3f(1.0f, 0.0f, 0.0f); // red
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);

That should render a bright red rectangle in the middle of a dark blue background.

just the same result… The window is not blue or white… it’s just with it default colour( foggy white )…

Can you post your code to reflect the suggested modifications?

Incidentally, you should be checking for errors/failure during the initialization process. Those function calls (wgl/PFD related) are not guaranteed to succeed. Please check the return values to be sure all is well.

Add in the FormCreate function, before you call SetupRC(), the lines:

glViewport(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(float)ClientWidth,0.0,(float)ClientHeight,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
  

Did you try running the sample OpenGL demo that comes with the C++ Builder installation in the Samples directory?

the BCB example works perfectly and fits… 10x :slight_smile: