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:
| unit x;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OpenGL12, AppEvnts, ExtCtrls;
type TForm1 = class(TForm) ApplicationEvents1: TApplicationEvents; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure InitGL; procedure DrawScene; procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); procedure DrawLandscape; procedure CheckKeys; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); Procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Timer1Timer(Sender: TObject); private public end;
procedure SetDCPixelFormat(Handle: HDC);
var Form1: TForm1; h_DC: hDC; hRC: HGLRC; Keys: Array[0..255] of Boolean; Rotate: Extended; RealFPS: Integer = 100; FPS: Integer; PosX, PosZ: Single;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin InitOpenGL; h_DC:=GetDC(Handle); SetDCPixelFormat(h_DC); hRC:=wglCreateContext(h_DC); wglMakeCurrent(h_DC,hRC); InitGL; Application.OnIdle := ApplicationEvents1Idle; end;
procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); begin DrawScene; Done:=False; end;
procedure TForm1.DrawScene; begin glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); glLoadIdentity;
CheckKeys; glRotate(Rotate,0,1,0); glTranslate(PosX,0,PosZ); DrawLandscape;
SwapBuffers(h_DC); end;
procedure TForm1.InitGL; begin glClearColor(0,0,0,0); glClearDepth(1); glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST);
glViewport(0,0,Width,Height); glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(45,Width/Height,1,100); glMatrixMode(GL_MODELVIEW); end;
procedure SetDCPixelFormat(Handle: HDC); var nPixelFormat: GLUint; const pfd: PIXELFORMATDESCRIPTOR = ( 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 ); begin nPixelFormat:=ChoosePixelFormat(h_DC, @pfd); SetPixelFormat(h_DC,nPixelFormat,@pfd); end;
procedure TForm1.DrawLandscape; var j, i: Integer; r, wi, w, x, y: Double; begin wi:=360 / 40; glColor3f(0.0,1.0,0.0); glBegin(GL_LINES); for i:=1 to 40 do begin w:=(PI / 180) * (wi * i - (wi / 2)); x:=(-5 / 2) + (5 / 2 + cos(w) * (5 + 5) / 2); y:=(5 / 2) + (-5 / 2 + sin(w) * (-5 - 5) / 2); glVertex3f(0.0,-0.4,0.0); glVertex3f(x,-0.4,y); end; glEnd; r:=0; for j:=1 to 11 do begin glBegin(GL_LINE_LOOP); for i:=1 to 40 do begin w:=(PI / 180) * (wi * i - (wi / 2)); x:=(-r / 2) + (r / 2 + cos(w) * (r + r) / 2); y:=(r / 2) + (-r / 2 + sin(w) * (-r - r) / 2); glVertex3f(x,-0.4,y); end; glEnd; r:=r + 0.5; end; end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin Keys[Key]:=true; end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin Keys[Key]:=false; end;
procedure TForm1.CheckKeys; begin if Keys[VK_LEFT] then begin Rotate:=Rotate - 1.0; if Rotate<0 then Rotate:=Rotate + (0.5 * (100 / FPS)); end; if Keys[VK_RIGHT] then begin Rotate:=Rotate + 1.0; if Rotate>360 then Rotate:=Rotate - (0.5 * (100 / FPS)); end; if Keys[VK_UP] then begin PosX:=PosX + Sin(-Rotate * (PI / 180)) * (0.1); PosZ:=PosZ + Cos(-Rotate * (PI / 180)) * (0.1); end; if Keys[VK_DOWN] then begin PosX:=PosX - Sin(-Rotate * (PI / 180)) * (0.1); PosZ:=PosZ - Cos(-Rotate * (PI / 180)) * (0.1); end;
end;
procedure TForm1.Timer1Timer(Sender: TObject); begin RealFPS:=FPS; FPS:=0; end;
end. |