B
Banz
Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
#include <vcl.h>
#include <gl/gl.h>
#pragma hdrstop
#include "stdlib.h"
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void CreateFont(void);
GLYPHMETRICSFLOAT pgmf[1];
int x,y,z;
void glDrawString(unsigned char *str)
{
HDC hDC=wglGetCurrentDC();
DWORD dwChar;
int ListNum;
for(size_t i=0;i<strlen((char *)str);i++)
{
if(IsDBCSLeadByte(str))
{dwChar=(DWORD)((str<<8)|str[i+1]);i++;}
else dwChar=str;
ListNum=glGenLists(1);
wglUseFontOutlines(hDC,dwChar,1,ListNum,0.0,0.1,WGL_FONT_POLYGONS,pgmf);
glCallList(ListNum);
glDeleteLists(ListNum,1);
}
}
void CreateFont(void)
{
HFONT HFont,HOldFont=0;
LOGFONT LogFont;
GetObject(Form1->Font->Handle,sizeof(LOGFONT),&LogFont);
HFont = CreateFontIndirect(&LogFont);
HOldFont=SelectObject (Form1->hDC, HFont);
DeleteObject(HOldFont);
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
hDC=GetDC(Handle); //茆取一个DC,TForm1.Handle中保存有Form的窗口句柄
SetDCPixelFormat(hDC); //调整该DC的象素格式
hRC=wglCreateContext(hDC); //用这种DC去创建一个RC
wglMakeCurrent(hDC,hRC); //指定当前DC、当前RC为hDC、hRC
glEnable(GL_COLOR_MATERIAL);
randomize();
glColor3f(random(255)/255.0,random(255)/255.0,random(255)/255.0);
CreateFont();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CreateParams(TCreateParams& Params)
{
TForm::CreateParams(Params); //调用原有函数预处理
Params.Style|=WS_CLIPCHILDREN|WS_CLIPSIBLINGS;//加上必要的属性
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetDCPixelFormat(HDC hDC)
{ //本函数用于调整DC的象素格式,如缓冲区、颜色数等
//先不深究,只要知道它的作用就行
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
1, // Version of this structure
PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
PFD_DOUBLEBUFFER, // Double buffered mode
PFD_TYPE_RGBA, // RGBA Color mode
24, // Want 24bit color
0,0,0,0,0,0, // Not used to select mode
0,0, // Not used to select mode
0,0,0,0,0, // Not used to select mode
32, // Size of depth buffer
0, // Not used to select mode
0, // Not used to select mode
PFD_MAIN_PLANE, // Draw in main plane
0, // Not used to select mode
0,0,0 }; // Not used to select mode
// Choose a pixel format that best matches that described in pfd
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// Set the pixel format for the device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
//---------------------------------------------------------------------------
void RenderScence(void)
{
//try to draw fonts.
byte cstr[]={"OpenGL测试"};
//glTranslatef(-1,-1,0);
glScalef(0.2,0.2,1);
glDrawString(cstr);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{ //该Form的绘制响应函数
glClearColor(0.5,0.7,0.9,1.0); //指定背景颜色(依次为RGBA)
glClear(GL_COLOR_BUFFER_BIT); //用背景色清窗口
glPushMatrix();
glRotatef(x,1.0,0.0,0.0);
glRotatef(y,0.0,1.0,0.0);
glRotatef(z,0.0,0.0,1.0);
RenderScence();
glPopMatrix();
SwapBuffers(hDC);
//切交缓冲区,这就是可以启动图形库处理流程并得相应结果的两条命令之一
//如果当前RC相联DC具有DoubleBuffer的PixelFormat则用本命令
//否则就是单缓冲DC,用glFlush(void)进行更新。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
wglMakeCurrent(NULL,NULL); //取消当前RC和当前DC
wglDeleteContext(hRC); //删除该RC
// DeleteObject(hDC); //删除Windows DC。!!交给VCL系统自动完成。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Timer1->Enabled=false;
x=y=z=0;
FormPaint(Sender);
fd->Execute();
Form1->Font=fd->Font;
CreateFont();
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// x=(x+random(10))%360;
// y=(y+random(10))%360;
// z=(z+random(5))%360;
FormPaint(Sender);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TFontDialog *fd;
TTimer *Timer1;
void __fastcall FormDestroy(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
private: // User declarations
void __fastcall CreateParams(TCreateParams &Params);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
HGLRC hRC;
HDC hDC;
void __fastcall SetDCPixelFormat(HDC hDC);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif