我看書上的範例~用出來了~可是我也不太懂~
我把程式貼上來~懂的人可以解說一下嗎~
PS:舊的BCB的範例中,也有部份相同的程式碼~
非常感謝哦~
第一個Unit_OpenGL.cpp
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit_OpenGL.h" //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::mmInitOpenGLClick(TObject *Sender)
{
if(FglRC==NULL)
{
PIXELFORMATDESCRIPTOR pfd;
FhDC=GetDC(Handle);
pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion=1;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.dwFlags=PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.cColorBits=32;
pfd.cDepthBits=24;
int pixelFormat=ChoosePixelFormat(FhDC,&pfd);
if(pixelFormat==0)
{
ShowMessage("像點格式選取失敗!!");
return;
} int setPixelFmt=SetPixelFormat(FhDC,pixelFormat,&pfd); if(setPixelFmt==false)
{
ShowMessage("像點格式設定失敗!!");
return;
} FglRC=wglCreateContext(FhDC);
wglMakeCurrent(FhDC,FglRC); Label3->Caption=IntToStr((int)FhDC);
Label4->Caption=IntToStr((int)FglRC);
Label7->Caption=IntToStr(pixelFormat);
if(setPixelFmt)
Label8->Caption="true";
else
Label8->Caption="false"; glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); glViewport(0,0,ClientWidth,ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(ClientHeight==0)
gluPerspective(30,ClientHeight,1.0,100);
else
gluPerspective(30,ClientWidth/ClientHeight,1.0,100);
} }
//---------------------------------------------------------------------------
void __fastcall TForm1::mmDrawOpenGLClick(TObject *Sender)
{ if(FglRC==NULL)
{
mmInitOpenGLClick(Sender); //呼叫函數建立OpenGL的繒圖環境
} wglMakeCurrent(FhDC, FglRC);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST);
glLineWidth(15);
glPushMatrix();
glTranslatef(0.0, 0.0, -40);
glRotatef(20, 1.0, 0.0, 0.0);
glBegin(GL_LINES);
for(int lop=0; lop<12; lop )
{
glColor3f(0.0, 0.0, 0.1);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(1.0, lop/12, 0.2);
float x = cos((M_PI/6)*lop);
float y = sin((M_PI/6)*lop);
glVertex2f(x*10, y*10);
}
glEnd();
glPopMatrix();
glFlush();
SwapBuffers(FhDC); }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(FglRC != NULL)
wglDeleteContext(FglRC); if(FhDC != NULL)
ReleaseDC(Handle, FhDC);
}
//---------------------------------------------------------------------------
第二個 Unit_OpenGL.h
//--------------------------------------------------------------------------- #ifndef Unit_OpenGLH
#define Unit_OpenGLH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include
#include
#include <math.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TMenuItem *mmInitOpenGL;
TMenuItem *mmDrawOpenGL;
TLabel *Label3;
TLabel *Label1;
TLabel *Label2;
TLabel *Label4;
TLabel *Label5;
TLabel *Label7;
TLabel *Label6;
TLabel *Label8;
void __fastcall mmInitOpenGLClick(TObject *Sender);
void __fastcall mmDrawOpenGLClick(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
HDC FhDC;
HGLRC FglRC; public: // User declarations
__fastcall TForm1(TComponent* Owner); };
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
發表人 - 黑輪 於 2004/10/27 16:00:10