Open gl and c++ builder 5
I''m a beginner in opengl and c++builder, i try these code but thy don''t work, could you thake a look and sey me what i have forgot?
Tanks for your help.
Unit1.cpp
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RenderGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glFlush();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
hdc = GetDC(Form1->Handle);
SetPixelFormatDescriptor();
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
SetupRC();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
RenderGLScene();
SwapBuffers(hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ReleaseDC(hwnd,hdc);
wglMakeCurrent(hdc, NULL);
wglDeleteContext(hrc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetPixelFormatDescriptor()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
PixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, PixelFormat, &pfd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
GLfloat nRange = 200.f;
if(ClientHeight == 0)
ClientHeight = 1;
glViewport(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(ClientWidth <= ClientHeight)
glOrtho(-nRange, nRange, -nRange*ClientHeight/ClientWidth,
nRange*ClientHeight/ClientWidth, -nRange, nRange);
else
glOrtho(-nRange*ClientWidth/ClientHeight, nRange*ClientWidth/ClientHeight,
-nRange, nRange, -nRange, nRange);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------------
Unit1.h
#include
#include
#include <forms.hpp>
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
private: // User declarations
HDC hdc;
HGLRC hrc;
int PixelFormat;
HWND hwnd;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall RenderGLScene();
void __fastcall SetPixelFormatDescriptor();
void __fastcall SetupRC();
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
1)
cut SwapBuffers(hdc); from SetupRC() and paste it at the bottom of RenderGLScene()
2)if you clear the depth buffer, then you will maybe use it
with glEnable(GL_DEPTH_TEST); in your init func.
3) most important comes here: where is your "mainloop"?
either you use a timer(represents the loop) and put
RenderGLScene in there, or you can do the following:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnIdle = RenderGLScene();
}
cut SwapBuffers(hdc); from SetupRC() and paste it at the bottom of RenderGLScene()
2)if you clear the depth buffer, then you will maybe use it
with glEnable(GL_DEPTH_TEST); in your init func.
3) most important comes here: where is your "mainloop"?
either you use a timer(represents the loop) and put
RenderGLScene in there, or you can do the following:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnIdle = RenderGLScene();
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement