Przemo B. Guest
|
Posted: Tue Oct 28, 2003 4:49 pm Post subject: OpenGL on TPaintBox (long) |
|
|
Dear Programmers,
I would like creating a simple OpenGL program based on TPaintBox control
(drawing only solid filled rectangle). There are a lot of examples of OpenGL
but most of them using TForm->Canvas as a render context. So, I wrote
bellowed code and (as I supposed) does not work:
From header file:
class TForm1 : public TForm
{
__published: // IDE-managed Components
TPaintBox *PaintBox1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
private: // User declarations
HDC hCanvas;
HGLRC hRC;
int PixelFormat;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall SetPixelFormatDescriptor();
void __fastcall SetupColor();
};
From source file:
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
hCanvas = PaintBox1->Canvas->Handle;
SetPixelFormatDescriptor();
hRC = wglCreateContext(hCanvas);
wglMakeCurrent(hCanvas, hRC);
SetupColor();
}
void __fastcall TForm1::SetPixelFormatDescriptor()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
PixelFormat = ChoosePixelFormat(hCanvas, &pfd);
SetPixelFormat(hCanvas, PixelFormat, &pfd);
}
void __fastcall TForm1::SetupColor()
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void __fastcall TForm1::FormPaint(TObject *Sender)
{
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(10.0f, 10.0f, 50.0f, 50.0f);
glFlush();
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
wglMakeCurrent(hCanvas, NULL);
wglDeleteContext(hRC);
}
May I ask you for suggestions?
I will by very appreciate
Przemo
|
|