PDA

View Full Version : Open gl and c++ builder 5



gioa001
03-10-2001, 03:50 AM
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 <vcl.h>
#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 <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <gl\gl.h>
#include <gl\glu.h>
//---------------------------------------------------------------------------
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

Moz
03-10-2001, 03:58 AM
www.bytamin-c.com (http://www.bytamin-c.com) has an article on how to set up OpenGL in C++Builder.

Cardinal
03-11-2001, 09:05 PM
try using
hDC = GetDC(Handle);
instead of
hDC = GetDC(Form1->Handle);

http://www.opengl.org/discussion_boards/ubb/smile.gif

Machiavelly
03-11-2001, 11:48 PM
See if your SetPixelFormatDescriptor is working properly it may not.

Machiavelly
03-11-2001, 11:50 PM
See if your SetPixelFormatDescriptor is working properly it may not.

feritf
03-15-2001, 10:58 AM
use this function to initialize OpenGL and
PixelDescriptor is below.

GLvoid initializeGL()
{
/*
int width,height;
GLfloat aspect;
ghWnd=Form1->Handle;
ghDC = GetDC(ghWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (0);
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetClientRect(ghWnd, &rect);
glClearIndex( (GLfloat)WHITE_INDEX);
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
glMatrixMode( GL_PROJECTION );
width=rect.right;
height=rect.bottom;
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
*/
INITIALIZED =1111;
int width,height;
ghWnd=Form1->Handle;
ghDC = GetDC(ghWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (0);
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetWindowRect(ghWnd,&rect);
width=rect.right;
height=rect.bottom;
glClearColor(1.0,1.0,1.0,1.0);
glClearIndex((GLfloat)WHITE_INDEX);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
GLfloat aspect;
glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / (GLfloat)height;
vangle=1.0;
gluPerspective( vangle, aspect, -10.0, 10.0 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
}


HWND ghWnd;
HDC ghDC;
HGLRC ghRC;
RECT rect;
CScene m_Scene;
float m_newMatrix[16];
float vangle;

BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;

ppfd = &pfd;

ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |

PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;

pixelformat = ChoosePixelFormat(hdc, ppfd);

if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}

if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)

{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}

return TRUE;
}

feritf
03-15-2001, 11:03 AM
Above codes are from my project so ignore some lines.Sorry I have no time to arrange them for you.

Good luck!