19,468
社区成员
发帖
与我相关
我的任务
分享
TForm1 *Form1;
HGLRC g_hrc = NULL;
HDC g_hdc = NULL;
int g_iFontList = 0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnIdle = IdleLoop;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdleLoop(TObject*, bool& done)
{
//TODO: Add your source code here
done = false;
RenderScene ();
SwapBuffers (g_hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
g_hdc = GetDC (Handle);
SetDCPixelFormat (g_hdc);
g_hrc = wglCreateContext (g_hdc);
wglMakeCurrent (g_hdc, g_hrc);
SetRC (g_hdc);
}
//---------------------------------------------------------------------------
// 设置pixel格式
void __fastcall TForm1::SetDCPixelFormat(HDC hdc)
{
//TODO: Add your source code here
int iPixelFormat;
PIXELFORMATDESCRIPTOR pfd = {
sizeof (pfd),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0, 0,
16,
0,
0,
0,
0,
0,
0,
0};
iPixelFormat = ChoosePixelFormat (g_hdc, &pfd);
SetPixelFormat (g_hdc, iPixelFormat, &pfd);
}
// -----------------------------------------------------------------------
void __fastcall TForm1::SetRC(HDC hdc)
{
//TODO: Add your source code here
HFONT hFont;
GLYPHMETRICSFLOAT agmf[128];
LOGFONT logfont;
logfont.lfHeight = -10;
logfont.lfWidth = 0;
logfont.lfEscapement = 0;
logfont.lfOrientation = 0;
logfont.lfWeight = FW_BOLD;
logfont.lfItalic = FALSE;
logfont.lfUnderline = FALSE;
logfont.lfStrikeOut = FALSE;
logfont.lfCharSet = ANSI_CHARSET;
logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logfont.lfQuality = DEFAULT_QUALITY;
logfont.lfPitchAndFamily = DEFAULT_PITCH;
lstrcpy (logfont.lfFaceName, "Arial");
hFont = CreateFontIndirect (&logfont);
SelectObject (g_hdc, hFont);
g_iFontList = glGenLists (128);
wglUseFontOutlines (g_hdc, 0, 128, g_iFontList, 0.0f, 0.5f,
WGL_FONT_POLYGONS, agmf);
DeleteObject (hFont);
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f);
glEnable (GL_DEPTH_TEST);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
wglMakeCurrent (g_hdc, NULL);
wglDeleteContext (g_hrc);
ReleaseDC (Handle, g_hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormResize(TObject *Sender)
{
ChangeSize (ClientWidth, ClientHeight);
}
#define ORTHO 0
void __fastcall TForm1::ChangeSize(int w, int h)
{
GLfloat nRange = 100.0;
w = ClientWidth;
h = ClientHeight;
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#if ORTHO
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
#else
//For perspective correction
GLfloat aspect = (GLfloat)w/(GLfloat)h;
gluPerspective(60.0f, aspect, 1.0, 400.0);
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
}
#define PI 3.1415f
GLfloat xRot = 0.0f, yRot = 0.0f;
#include "math.h"
#include "stdio.h"
//---------------------------------------------------------------------------
void __fastcall TForm1::RenderScene (void)
{
//TODO: Add your source code here
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat x, y, z, angle;
glPushMatrix ();
#if (ORTHO == 0)
glTranslatef (0.0f, 0.0f, -200.0f);
#endif
glRotatef (xRot, 1.0f, 0.0f, 0.0f);
glRotatef (yRot, 0.0f, 1.0f, 0.0f);
glBegin (GL_LINES);
glVertex3f (0.0f, 0.0f, -50.0f);
glVertex3f (50.0f, 0.0f, -50.0f);
glEnd ();
glBegin (GL_LINE_STRIP);
z = -50.0f;
GLfloat adi = 0.01f;
for (angle = 0.0f; angle <= (2.0f * PI) * 3.0f; angle += 0.1f)
{
x = 50.0f * sin(angle);
y = 50.0f * cos(angle);
glVertex3f (x, y, z);
z += 0.5f;
}
glEnd ();
glPopMatrix ();
char cBuff[200];
sprintf (cBuff, "xRot:%f yRot:%f", xRot, yRot);
Caption = cBuff;
}
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (Key == VK_UP)
{
xRot += 1.0f;
}
else if (Key == VK_DOWN)
{
xRot -= 1.0f;
}
else if (Key == VK_LEFT)
{
yRot += 1.0f;
}
else if (Key == VK_RIGHT)
{
yRot -= 1.0f;
}
}