1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474:
| unit template_main;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OpenGL12, Geometry, StdCtrls, ExtCtrls, template_Fullscreen;
const FarClipping = 1000.0; NearClipping = 1.0;
HelpText : Array[0..4] of String = ('H - Zeige Hilfe', 'F - Zeige Framerate + Debug Infos', 'R - Zeige Renderer Info', 'V - zw. Vollbild/Fenster wechseln', 'Maus Knopf - Kamera');
type TDemoForm = class(TForm) procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormResize(Sender: TObject); procedure ApplicationEventsIdle(Sender: TObject; var Done: Boolean); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormKeyPress(Sender: TObject; var Key: Char); private OpenGLInitialized : Boolean; RC : HGLRC; StartTick : Cardinal; Frames : LongInt; XRotation, YRotation : Single; MouseX, MouseY : LongInt; ShowFPS : Boolean; ShowRendererInfo : Boolean; ShowHelp : Boolean; FontBase : TGLuint; FrameText : String; procedure PrintText(const Text : String); public end;
var DemoForm: TDemoForm;
implementation
{$R *.DFM}
procedure TDemoForm.FormCreate(Sender: TObject); begin OpenGLInitialized := False; if not LoadOpenGL then Halt(100) end;
procedure TDemoForm.FormShow(Sender: TObject); procedure SetPixelFormat; var PixelFormat : TGLuint; PFD : pixelformatdescriptor; begin with pfd do begin nSize:= SizeOf( PIXELFORMATDESCRIPTOR ); nVersion:= 1; dwFlags:= PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER; iPixelType:= PFD_TYPE_RGBA; cColorBits:= 16; cRedBits:= 0; cRedShift:= 0; cGreenBits:= 0; cBlueBits:= 0; cBlueShift:= 0; cAlphaBits:= 0; cAlphaShift:= 0; cAccumBits:= 0; cAccumRedBits:= 0; cAccumGreenBits:= 0; cAccumBlueBits:= 0; cAccumAlphaBits:= 0; cDepthBits:= 16; cStencilBits:= 0; cAuxBuffers:= 0; iLayerType:= PFD_MAIN_PLANE; bReserved:= 0; dwLayerMask:= 0; dwVisibleMask:= 0; dwDamageMask:= 0 end; PixelFormat := ChoosePixelFormat(Canvas.Handle, @pfd); if (PixelFormat=0) then MessageDlg('Es konnte kein passendes PixelFormat gefunden werden.', mtError, [mbOk], 0); if (not Windows.SetPixelFormat(Canvas.Handle,PixelFormat,@pfd)) then MessageDlg('PixelFormat konnte nicht gesetzt werden.', mtError, [mbOk], 0) end;
procedure StartGl; begin RC := wglCreateContext(Canvas.Handle); if (RC=0) then begin MessageDlg('Rendering Context kann nicht erstellt werden.', mtError, [mbOk], 0); Halt(100) end;
if (not wglMakeCurrent(Canvas.Handle, RC)) then begin MessageDlg('Rendering Context kann nicht aktiviert werden.', mtError, [mbOk], 0); Halt(100) end end;
procedure SetupGL; begin glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glShadeModel(GL_SMOOTH) end;
procedure BuildFont; var font : HFONT; begin FontBase := glGenLists(96);
font := CreateFont(16, 0, 0, 0, FW_MEDIUM, 0, 0, 0, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE or DEFAULT_PITCH, 'MS Sans Serif'); SelectObject(Canvas.Handle, font);
wglUseFontBitmaps(Canvas.Handle, 32, 96, FontBase);
DeleteObject(font) end;
begin RC := 0;
SetPixelFormat; StartGL;
ClearExtensions; ReadExtensions;
SetupGL; BuildFont;
Frames := 0; StartTick := GetTickCount; YRotation := 0; XRotation := 0; ShowFPS := True; ShowHelp := True; ShowRendererInfo := False;
Application.OnIdle := ApplicationEventsIdle;
OpenGLInitialized := True end;
procedure TDemoForm.FormDestroy(Sender: TObject); begin glDeleteLists(FontBase, 96); OpenGLInitialized := False; if RC<>0 then begin if (not wglMakeCurrent(Canvas.Handle,0)) then MessageDlg('Rendering Context konnte nicht abgewählt werden.', mtError, [mbOk], 0); if (not wglDeleteContext(RC)) then MessageDlg('Rendering Context konnte nicht freigegeben werden.', mtError, [mbOk], 0) end; RC:=0 end;
procedure TDemoForm.FormResize(Sender: TObject); begin glViewport(0, 0, ClientWidth, ClientHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0,ClientWidth/ClientHeight,NearClipping,FarClipping); glMatrixMode(GL_MODELVIEW); glLoadIdentity end;
procedure TDemoForm.ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
procedure ShowText; var Line : LongInt;
procedure LineOut(const X : LongInt; Text : String); begin glRasterPos2i(X, (Line)*16); PrintText(Text); Inc(Line) end;
var I : Integer; begin Line := 1; glPushAttrib(GL_DEPTH_BUFFER_BIT or GL_ENABLE_BIT); glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
glOrtho(0.0, ClientWidth, ClientHeight, 0, -1, 1);
glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
if ShowHelp then begin LineOut(5, 'Hilfe:'); for i := Low(HelpText) to High(HelpText) do LineOut(5, HelpText[i]) end;
if ShowRendererInfo then begin LineOut(5, 'Info:'); LineOut(5, 'Renderer: '+ glGetString(GL_RENDERER)); LineOut(5, 'Vendor: '+ glGetString(GL_VENDOR)); LineOut(5, 'Version: '+ glGetString(GL_VERSION)) end;
if ShowFPS then begin glColor3f(1.0, 0.0, 0.0); glRasterPos2i(5, ClientHeight-16); PrintText(FrameText) end;
glMatrixMode(GL_PROJECTION); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glPopMatrix();
glPopAttrib() end;
var Error : LongInt; begin if not OpenGLInitialized then Exit; Done := False;
glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT); glLoadIdentity;
glTranslatef(0.0, 0.0, - 4); glRotatef(XRotation, 1, 0,0); glRotatef(YRotation, 0, 1,0);
glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glColor3f(0.0, 0.0, 1.0); glVertex3f(1.0, -1.0, 1.0); glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glColor3f(1.0, 0.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glColor3f(1.0, 0.0, 1.0); glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 0.0, 1.0); glVertex3f(1.0, -1.0, 1.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glColor3f(1.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd();
glBegin(GL_QUADS); glColor3f(1.0, 0.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(1.0, -1.0, -1.0); glColor3f(0.0, 0.0, 1.0); glVertex3f(1.0, -1.0, 1.0); glColor3f(1.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); glEnd();
ShowText;
Error := glgetError; if Error <> GL_NO_ERROR then begin Caption := gluErrorString(Error); Done := True; FlashWindow(Handle, True) end; Inc(Frames); if GetTickCount - StartTick >=500 then begin FrameText := Format('FPS: %f x:%f° y:%f°', [Frames/(GetTickCount - StartTick)*1000, XRotation, YRotation]); Frames := 0; StartTick := GetTickCount end; SwapBuffers(Canvas.Handle) end;
procedure TDemoForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin MouseCapture := True; MouseX := X; MouseY := Y end;
procedure TDemoForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if MouseCapture then begin YRotation := YRotation + ((MouseX - X) div 2); XRotation := XRotation + ((MouseY - Y) div 2); MouseX := X; MouseY := Y end end;
procedure TDemoForm.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin MouseCapture := False end;
procedure TDemoForm.FormKeyPress(Sender: TObject; var Key: Char); begin case UpCase(Key) of 'H': ShowHelp := not ShowHelp; 'F': ShowFPS := not ShowFPS; 'R': ShowRendererInfo := not ShowRendererInfo; 'V': begin SwichFullscreenMode; end end end;
{$Q-} procedure TDemoForm.PrintText(const Text: String); begin if (Text = '') then Exit; glPushAttrib(GL_LIST_BIT); glListBase(FontBase - 32); glCallLists(Length(text), GL_UNSIGNED_BYTE, PChar(Text)); glPopAttrib() end; {$Q+}
end. |