Gwynnn, sorry my code was inconsistent (the variable names), actually it should not have compiled for you.
Anyhow, the following is correct.
( I think this is as simple as it can get)
The best thing for this code just create a new regular c++ builder application.
NOT a console application, that is not the way you will be using C++ Builder.
Cut and paste this code into your app.
You need to create the OnCreate event and the OnPaint event then just add the code in there.
The only function you need to create manually is
the SetPixelFormatDescriptor function.
in the header file;
#include <gl\gl.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormPaint(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
HDC hdc; // device context
HGLRC hrc; //rendering context
void SetPixelFormatDescriptor();
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Now the source code file;
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include “Unit1.h”
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource “*.dfm”
TForm1 Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::SetPixelFormatDescriptor()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
//use the default bit planes
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
int PixelFormat = ChoosePixelFormat(hdc, &pfd); // choose and set the
SetPixelFormat(hdc, PixelFormat, &pfd); // appropriate pixelformat
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
hdc = GetDC(Handle); // get device context from main window
SetPixelFormatDescriptor();
hrc = wglCreateContext(hdc); // use device context to create
// a rendering context
if(hrc == NULL)
ShowMessage(“Creating Rendering Context failed.”);
wglMakeCurrent(hdc,hrc); // tell windows to use hdc and rdc
if(wglMakeCurrent(hdc, hrc) == false)
ShowMessage(“MakeCurrent of rendering context failed.”);
}
void __fastcall TForm1::FormPaint(TObject *Sender)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);//black background
glColor3f(1.0,1.0,0.0); //draw yellow
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,10.0,0.0,10.0,-1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(2.0,4.0,0.0);
glVertex3f(8.0,4.0,0.0);
glVertex3f(8.0,6.0,0.0);
glVertex3f(2.0,6.0,0.0);
glEnd();
glFlush();
SwapBuffers(hdc); // make it visible
}