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:
| unit RendererD3D;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Direct3D8, d3dx8, ExtCtrls, ComCtrls;
const Pi180 = Pi/180.0; Fovy = Pi/4; ViewDist = 15; ViewHeight = 5; Delta = 0.01; CubeCount = 36;
type TMyVertex = record x,y,z : single; color : dword; end;
TCube = array [0..CubeCount-1] of TMyVertex;
const D3D8T_CUSTOMVERTEX =D3DFVF_XYZ or D3DFVF_DIFFUSE;
NormCube : TCube = ( (x :-1.0; y :-1.0; z :-1.0; color : $000000FF), (x :-1.0; y : 1.0; z :-1.0; color : $000000FF), (x : 1.0; y : 1.0; z :-1.0; color : $000000FF), (x : 1.0; y : 1.0; z :-1.0; color : $000000FF), (x : 1.0; y :-1.0; z :-1.0; color : $000000FF), (x :-1.0; y :-1.0; z :-1.0; color : $000000FF),
(x :-1.0; y :-1.0; z : 1.0; color : $00FF00FF), (x :-1.0; y : 1.0; z : 1.0; color : $00FF00FF), (x :-1.0; y : 1.0; z :-1.0; color : $00FF00FF), (x :-1.0; y : 1.0; z :-1.0; color : $00FF00FF), (x :-1.0; y :-1.0; z :-1.0; color : $00FF00FF), (x :-1.0; y :-1.0; z : 1.0; color : $00FF00FF),
(x : 1.0; y :-1.0; z : 1.0; color : $0000FF00), (x : 1.0; y : 1.0; z : 1.0; color : $0000FF00), (x :-1.0; y : 1.0; z : 1.0; color : $0000FF00), (x :-1.0; y : 1.0; z : 1.0; color : $0000FF00), (x :-1.0; y :-1.0; z : 1.0; color : $0000FF00), (x : 1.0; y :-1.0; z : 1.0; color : $0000FF00),
(x : 1.0; y :-1.0; z :-1.0; color : $0000FFFF), (x : 1.0; y : 1.0; z :-1.0; color : $0000FFFF), (x : 1.0; y : 1.0; z : 1.0; color : $0000FFFF), (x : 1.0; y : 1.0; z : 1.0; color : $0000FFFF), (x : 1.0; y :-1.0; z : 1.0; color : $0000FFFF), (x : 1.0; y :-1.0; z :-1.0; color : $0000FFFF),
(x :-1.0; y : 1.0; z :-1.0; color : $00FF0000), (x :-1.0; y : 1.0; z : 1.0; color : $00FF0000), (x : 1.0; y : 1.0; z : 1.0; color : $00FF0000), (x : 1.0; y : 1.0; z : 1.0; color : $00FF0000), (x : 1.0; y : 1.0; z :-1.0; color : $00FF0000), (x :-1.0; y : 1.0; z :-1.0; color : $00FF0000),
(x : 1.0; y :-1.0; z :-1.0; color : $00FFFF00), (x : 1.0; y :-1.0; z : 1.0; color : $00FFFF00), (x :-1.0; y :-1.0; z : 1.0; color : $00FFFF00), (x :-1.0; y :-1.0; z : 1.0; color : $00FFFF00), (x :-1.0; y :-1.0; z :-1.0; color : $00FFFF00), (x : 1.0; y :-1.0; z :-1.0; color : $00FFFF00));
type TRendererD3D = class procedure StartScene(Handle: HWND); procedure StopScene; private lpd3d : IDIRECT3D8; lpd3ddevice : IDirect3DDevice8; D3dDevCaps : TD3DCaps8;
CubeVB : IDirect3DVertexBuffer8;
RotY : single ;
HwVertexProcess, FullScreen, Animate : boolean;
FFont: TFont; FDXFont: ID3DXFont;
procedure D3DInit(Handle: HWND); procedure D3DShutdown; procedure D3DInitScene; procedure D3DKillScene; procedure D3DRender; procedure MyIdleHandler (Sender: TObject; var Done: Boolean); public constructor Create; end;
implementation
constructor TRendererD3D.Create; begin Self.FFont := TFont.Create; with Self.FFont do begin Style := [fsBold]; Size := 10; Name := 'Arial'; end;
lpd3d:=nil; lpd3ddevice:=nil; CubeVB:=nil;
RotY:=0; Animate:=false; Application.OnIdle:= MyIdleHandler; end;
procedure TRendererD3D.D3DInit(Handle: HWND); var hr : HRESULT; d3dpp : TD3DPRESENTPARAMETERS; d3ddm : TD3DDISPLAYMODE; vp : integer; begin lpd3d:=Direct3DCreate8(D3D_SDK_VERSION); if(lpd3d=nil) then raise Exception.Create('Fehler beim Erstellen von Direct3D!');
ZeroMemory(@d3dpp,sizeof(d3dpp)); with d3dpp do begin SwapEffect:=D3DSWAPEFFECT_DISCARD; hDeviceWindow:=Handle; EnableAutoDepthStencil := TRUE; AutoDepthStencilFormat := D3DFMT_D16;
Windowed:=not FullScreen;
hr:=lpd3d.GetAdapterDisplayMode(D3DADAPTER_DEFAULT,d3ddm); if failed(hr) then raise Exception.Create('Fehler beim Ermitteln des Dislaymodes'); BackBufferFormat := d3ddm.Format;
end;
hr:=lpd3d.GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,D3dDevCaps); if FAILED(hr) then raise Exception.Create('Fehler beim Abfragen der DevCaps'); HwVertexProcess:=D3dDevCaps.DevCaps and D3DDEVCAPS_HWTRANSFORMANDLIGHT <> 0;
if HwVertexProcess then vp:=D3DCREATE_HARDWARE_VERTEXPROCESSING else vp:=D3DCREATE_SOFTWARE_VERTEXPROCESSING;
hr:=lpd3d.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Handle, vp, d3dpp, lpd3ddevice); if FAILED(hr) then raise Exception.Create('Fehler beim Erzeugen des 3D-Device');
end;
procedure TRendererD3D.D3DShutdown; begin if assigned(lpd3ddevice) then lpd3ddevice:=nil; if assigned(lpd3d) then lpd3d:=nil; end;
procedure TRendererD3D.D3DInitScene; var hr : HRESULT; vbVertices : pByte; ProjMatrix : TD3DXMATRIX;
begin HR := D3DXCreateFont(Self.lpd3ddevice, Self.FFont.Handle, Self.FDXFont); if HR <> S_OK then raise Exception.Create('Fehler beim Erzeugen des Font.');
if assigned(lpd3ddevice) then with lpd3ddevice do begin hr:=CreateVertexBuffer (sizeof(TCube), D3DUSAGE_WRITEONLY, D3D8T_CUSTOMVERTEX, D3DPOOL_MANAGED, CubeVB); if FAILED(hr) then raise Exception.Create('Fehler beim Erstellen des Vertex Buffers');
with CubeVB do begin hr:=Lock(0, 0, vbVertices, 0); if FAILED(hr) then raise Exception.Create('Fehler beim Locken des Vertex-Buffers'); Move(NormCube,vbVertices^,SizeOf(TCube)); Unlock; end;
SetRenderState(D3DRS_LIGHTING,0); SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
D3DXMatrixPerspectiveFovLH(ProjMatrix, Fovy, 640/480, 1.0, 1000.0); SetTransform(D3DTS_PROJECTION,ProjMatrix ); end; Animate:=true; end;
procedure TRendererD3D.D3DKillScene; begin CubeVB:=nil; end;
procedure TRendererD3D.D3DRender; var WorldMatrix, ViewMatrix, ProjMatrix, TempMatrix : TD3DXMATRIX; WorldPoint : TD3DXVector3; ScrnPoint : TD3DXVector4; TXTRect : TRect; begin RotY:=RotY+Delta; if assigned(lpd3ddevice) then with lpd3ddevice do begin Clear(0, nil, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1, 0 );
if SUCCEEDED(BeginScene) then begin
D3DXMatrixLookAtLH (ViewMatrix,D3DXVECTOR3(ViewDist*sin(Pi180*RotY), ViewHeight, ViewDist*cos(Pi180*RotY)), D3DXVECTOR3(0.0,0.0,0.0), D3DXVECTOR3(0.0,1.0,0.0));
SetTransform(D3DTS_VIEW,ViewMatrix);
SetVertexShader(D3D8T_CUSTOMVERTEX);
SetStreamSource(0,CubeVB,sizeof(TMyVertex));
SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW); D3DXMatrixTranslation(WorldMatrix,0,0,0); SetTransform(D3DTS_WORLD,WorldMatrix);
DrawPrimitive(D3DPT_TRIANGLELIST,0,12); EndScene; end;
GetTransform(D3DTS_WORLD, WorldMatrix); GetTransform(D3DTS_PROJECTION, ProjMatrix); GetTransform(D3DTS_VIEW, ViewMatrix);
D3DXMatrixMultiply(TempMatrix, WorldMatrix, ViewMatrix); D3DXMatrixMultiply(TempMatrix, TempMatrix, ProjMatrix);
WorldPoint.x := NormCube[1].x; WorldPoint.y := NormCube[1].y; WorldPoint.z := NormCube[1].z; D3DXVec3Transform(ScrnPoint, WorldPoint, TempMatrix);
TxtRect := Rect(Round(ScrnPoint.x), Round(ScrnPoint.y), 0, 0); Self.FDXFont.DrawTextA('Hallo', -1, TxtRect, DT_CALCRECT, 0); Self.FDXFont.DrawTextA('Hallo', -1, TxtRect, DT_LEFT, $FFFFFFFF);
SetRenderState(D3DRS_ALPHATESTENABLE, 1); SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
Present(nil,nil,0,nil); end; end;
procedure TRendererD3D.StartScene(Handle: HWND); begin D3DInit(Handle); D3DInitScene; D3DRender; end;
procedure TRendererD3D.StopScene; begin D3DKillScene; D3DShutdown; end;
procedure TRendererD3D.MyIdleHandler (Sender: TObject; var Done: Boolean); begin if Animate then D3DRender; Done:=false; end;
end. |