GraphicsApi.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #include "GraphicsApi.h"
  2. #include "Fenster.h"
  3. #include "Bild.h"
  4. #include "TexturModel.h"
  5. #include "Textur.h"
  6. #include "Shader.h"
  7. #include "Globals.h"
  8. #include "UIVertexShader.h"
  9. #include "UIPixelShader.h"
  10. #include "TexturList.h"
  11. #include "Kam3D.h"
  12. #include "DLLRegister.h"
  13. #include "Welt3D.h"
  14. #include "Model2D.h"
  15. #include "DXBuffer.h"
  16. #include <d3d11.h>
  17. #include <d3d9.h>
  18. using namespace Framework;
  19. GraphicsApi::GraphicsApi( GraphicApiType typ )
  20. {
  21. this->typ = typ;
  22. fenster = 0;
  23. backBufferSize = Vec2<int>( 0, 0 );
  24. fullScreen = 0;
  25. ref = 1;
  26. }
  27. GraphicsApi::~GraphicsApi()
  28. {
  29. if( fenster )
  30. fenster->release();
  31. }
  32. void GraphicsApi::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  33. {
  34. if( this->fenster )
  35. this->fenster->release();
  36. this->fenster = fenster;
  37. this->backBufferSize = backBufferSize;
  38. this->fullScreen = fullScreen;
  39. }
  40. void GraphicsApi::setBackBufferSize( Vec2< int > size )
  41. {
  42. backBufferSize = size;
  43. update();
  44. }
  45. void GraphicsApi::setFullScreen( bool fullScreen )
  46. {
  47. this->fullScreen = fullScreen;
  48. update();
  49. }
  50. void GraphicsApi::beginFrame( bool fill2D, bool fill3D, int fillColor )
  51. {}
  52. void GraphicsApi::renderKamera( Kam3D * zKamera )
  53. {}
  54. Textur *GraphicsApi::createOrGetTextur( const char *name, Bild * b )
  55. {
  56. if( b )
  57. b->release();
  58. return 0;
  59. }
  60. GraphicApiType GraphicsApi::getTyp() const
  61. {
  62. return typ;
  63. }
  64. Vec2< int > GraphicsApi::getBackBufferSize() const
  65. {
  66. return backBufferSize;
  67. }
  68. bool GraphicsApi::isFullScreen() const
  69. {
  70. return fullScreen;
  71. }
  72. GraphicsApi *GraphicsApi::getThis()
  73. {
  74. ref++;
  75. return this;
  76. }
  77. GraphicsApi *GraphicsApi::release()
  78. {
  79. if( !--ref )
  80. delete this;
  81. return 0;
  82. }
  83. DirectX9::DirectX9()
  84. : GraphicsApi( DIRECTX9 ),
  85. pDirect3D( 0 ),
  86. pDevice( 0 ),
  87. pBackBuffer( 0 ),
  88. backRect( new D3DLOCKED_RECT() )
  89. {
  90. uiBild = new Bild( 1 );
  91. }
  92. DirectX9::~DirectX9()
  93. {
  94. backRect->pBits = NULL;
  95. delete backRect;
  96. if( pBackBuffer )
  97. {
  98. pBackBuffer->Release();
  99. pBackBuffer = NULL;
  100. }
  101. if( pDevice )
  102. {
  103. pDevice->Release();
  104. pDevice = NULL;
  105. }
  106. if( pDirect3D )
  107. {
  108. pDirect3D->Release();
  109. pDirect3D = NULL;
  110. }
  111. uiBild->release();
  112. }
  113. typedef IDirect3D9 *( *D3D9CreateFunction )( UINT );
  114. void DirectX9::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  115. {
  116. if( pDirect3D )
  117. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  118. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  119. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d9.dll", "d3d9.dll" );
  120. if( !dll )
  121. {
  122. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  123. return;
  124. }
  125. D3D9CreateFunction direct3DCreate9 = (D3D9CreateFunction)GetProcAddress( dll, "Direct3DCreate9" );
  126. if( !direct3DCreate9 )
  127. {
  128. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt Direct3DCreate9 fon DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  129. return;
  130. }
  131. pDirect3D = direct3DCreate9( D3D_SDK_VERSION );
  132. D3DPRESENT_PARAMETERS d3dpp;
  133. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  134. d3dpp.Windowed = !fullScreen;
  135. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  136. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  137. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  138. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  139. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  140. if( !backBufferSize.x || !backBufferSize.y )
  141. backBufferSize = fenster->getKörperGröße();
  142. d3dpp.BackBufferHeight = backBufferSize.y;
  143. d3dpp.BackBufferWidth = backBufferSize.x;
  144. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  145. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  146. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  147. if( result != S_OK )
  148. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  149. if( pDevice )
  150. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  151. if( result != S_OK )
  152. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  153. }
  154. void DirectX9::update()
  155. {
  156. if( !pDirect3D )
  157. return;
  158. backRect->pBits = NULL;
  159. if( pBackBuffer )
  160. {
  161. pBackBuffer->Release();
  162. pBackBuffer = NULL;
  163. }
  164. if( pDevice )
  165. {
  166. pDevice->Release();
  167. pDevice = NULL;
  168. }
  169. D3DPRESENT_PARAMETERS d3dpp;
  170. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  171. d3dpp.Windowed = !fullScreen;
  172. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  173. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  174. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  175. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  176. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  177. if( !backBufferSize.x || !backBufferSize.y )
  178. backBufferSize = fenster->getKörperGröße();
  179. d3dpp.BackBufferHeight = backBufferSize.y;
  180. d3dpp.BackBufferWidth = backBufferSize.x;
  181. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  182. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  183. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  184. if( result != S_OK )
  185. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  186. if( pDevice )
  187. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  188. if( result != S_OK )
  189. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  190. }
  191. void DirectX9::beginFrame( bool fill2D, bool fill3D, int fillColor )
  192. {
  193. if( fill2D )
  194. uiBild->setFarbe( fillColor );
  195. }
  196. void DirectX9::presentFrame()
  197. {
  198. if( !uiBild->getBuffer() )
  199. return;
  200. HRESULT result;
  201. result = pBackBuffer->LockRect( backRect, 0, 0 );
  202. if( result != S_OK )
  203. {
  204. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  205. update();
  206. }
  207. // kopieren zum Bildschrirm
  208. int *bgBuff = uiBild->getBuffer();
  209. int tmpBr = sizeof( D3DCOLOR ) * uiBild->getBreite();
  210. for( int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight(); ++y, pitch += backRect->Pitch, bry += uiBild->getBreite() )
  211. memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr );
  212. // Beende Bild
  213. result = pBackBuffer->UnlockRect();
  214. if( result != S_OK )
  215. {
  216. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  217. update();
  218. }
  219. if( result != S_OK )
  220. {
  221. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  222. update();
  223. }
  224. result = pDevice->Present( 0, 0, 0, 0 );
  225. if( result != S_OK )
  226. {
  227. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  228. update();
  229. }
  230. }
  231. Bild *DirectX9::zUIRenderBild() const
  232. {
  233. return uiBild;
  234. }
  235. DirectX11::DirectX11()
  236. : GraphicsApi( DIRECTX11 ),
  237. d3d11Device( 0 ),
  238. d3d11Context( 0 ),
  239. d3d11SpawChain( 0 ),
  240. uiTextur( 0 ),
  241. vertexShader( 0 ),
  242. pixelShader( 0 ),
  243. sampleState( 0 ),
  244. rtview( 0 ),
  245. dsView( 0 ),
  246. depthStencilBuffer( 0 ),
  247. depthStencilState( 0 ),
  248. depthDisabledStencilState( 0 ),
  249. blendStateAlphaBlend( 0 ),
  250. vp( 0 ),
  251. texturModel( new TexturModel() ),
  252. texturRegister( new TexturList() ),
  253. texturRS( 0 ),
  254. meshRS( 0 ),
  255. defaultTextur( 0 ),
  256. diffuseLights( 0 ),
  257. pointLights( 0 ),
  258. vertexBuffer( 0 ),
  259. indexBuffer( 0 )
  260. {}
  261. DirectX11::~DirectX11()
  262. {
  263. if( vertexBuffer )
  264. vertexBuffer->release();
  265. if( indexBuffer )
  266. indexBuffer->release();
  267. if( diffuseLights )
  268. diffuseLights->release();
  269. if( pointLights )
  270. pointLights->release();
  271. if( defaultTextur )
  272. defaultTextur->release();
  273. if( texturRS )
  274. texturRS->Release();
  275. if( meshRS )
  276. meshRS->Release();
  277. texturModel->release();
  278. texturRegister->release();
  279. if( blendStateAlphaBlend )
  280. {
  281. blendStateAlphaBlend->Release();
  282. blendStateAlphaBlend = NULL;
  283. }
  284. if( uiTextur )
  285. {
  286. uiTextur->release();
  287. uiTextur = NULL;
  288. }
  289. if( sampleState )
  290. {
  291. sampleState->Release();
  292. sampleState = NULL;
  293. }
  294. if( pixelShader )
  295. {
  296. pixelShader->release();
  297. pixelShader = NULL;
  298. }
  299. if( vertexShader )
  300. {
  301. vertexShader->release();
  302. vertexShader = NULL;
  303. }
  304. if( depthDisabledStencilState )
  305. {
  306. depthDisabledStencilState->Release();
  307. depthDisabledStencilState = NULL;
  308. }
  309. delete vp;
  310. vp = 0;
  311. if( dsView )
  312. {
  313. dsView->Release();
  314. dsView = NULL;
  315. }
  316. if( depthStencilState )
  317. {
  318. depthStencilState->Release();
  319. depthStencilState = NULL;
  320. }
  321. if( depthStencilBuffer )
  322. {
  323. depthStencilBuffer->Release();
  324. depthStencilBuffer = NULL;
  325. }
  326. if( rtview )
  327. {
  328. rtview->Release();
  329. rtview = NULL;
  330. }
  331. if( d3d11SpawChain )
  332. {
  333. d3d11SpawChain->Release();
  334. d3d11SpawChain = NULL;
  335. }
  336. if( d3d11Device )
  337. {
  338. d3d11Device->Release();
  339. d3d11Device = NULL;
  340. }
  341. if( d3d11Context )
  342. {
  343. d3d11Context->Release();
  344. d3d11Context = NULL;
  345. }
  346. }
  347. typedef HRESULT( *D3D11CreateDeviceAndSwapChainFunction )( IDXGIAdapter *, D3D_DRIVER_TYPE,
  348. HMODULE, UINT, const D3D_FEATURE_LEVEL *,
  349. UINT, UINT, const DXGI_SWAP_CHAIN_DESC *,
  350. IDXGISwapChain * *, ID3D11Device * *,
  351. D3D_FEATURE_LEVEL *, ID3D11DeviceContext * * );
  352. void DirectX11::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  353. {
  354. if( d3d11Device )
  355. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  356. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  357. //--------------------------------------------------------------------
  358. // Create Device
  359. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d11.dll", "d3d11.dll" );
  360. if( !dll )
  361. {
  362. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht gefunden werden." ), MB_ICONERROR );
  363. return;
  364. }
  365. D3D11CreateDeviceAndSwapChainFunction createDeviceAndSwapChain = (D3D11CreateDeviceAndSwapChainFunction)GetProcAddress( dll, "D3D11CreateDeviceAndSwapChain" );
  366. if( !createDeviceAndSwapChain )
  367. {
  368. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt D3D11CreateDeviceAndSwapChain fon DirectX 11 konnte nicht gefunden werden." ), MB_ICONERROR );
  369. return;
  370. }
  371. // create a struct to hold information about the swap chain
  372. DXGI_SWAP_CHAIN_DESC scd;
  373. // clear out the struct for use
  374. ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
  375. // fill the swap chain description struct
  376. scd.BufferCount = 1; // one back buffer
  377. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  378. scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
  379. scd.SampleDesc.Count = 1;
  380. // Set the scan line ordering and scaling to unspecified.
  381. scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  382. scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  383. scd.Windowed = !fullScreen;
  384. if( !backBufferSize.x || !backBufferSize.y )
  385. backBufferSize = fenster ? fenster->getKörperGröße() : Punkt( 0, 0 );
  386. scd.BufferDesc.Width = backBufferSize.x;
  387. scd.BufferDesc.Height = backBufferSize.y; // windowed/full-screen mode
  388. scd.BufferDesc.RefreshRate.Denominator = 1;
  389. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  390. // Discard the back buffer contents after presenting.
  391. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  392. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  393. D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
  394. // create a device, device context and swap chain using the information in the scd struct
  395. UINT flag = 0;
  396. #ifdef _DEBUG
  397. flag |= D3D11_CREATE_DEVICE_DEBUG;
  398. #endif
  399. HRESULT result = createDeviceAndSwapChain( NULL,
  400. D3D_DRIVER_TYPE_HARDWARE,
  401. NULL,
  402. flag,
  403. &featureLevel,
  404. 1,
  405. D3D11_SDK_VERSION,
  406. &scd,
  407. &d3d11SpawChain,
  408. &d3d11Device,
  409. &support,
  410. &d3d11Context );
  411. if( result != S_OK )
  412. {
  413. std::cout << "ERROR: D3D11CreateDeviceAndSwapChain returned " << result << "\n";
  414. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  415. return;
  416. }
  417. ID3D11Texture2D *backBufferPtr;
  418. // Get the pointer to the back buffer.
  419. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID *)& backBufferPtr );
  420. if( result != S_OK )
  421. {
  422. std::cout << "ERROR: d3d11SpawChain->GetBuffer returned " << result << "\n";
  423. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  424. return;
  425. }
  426. // Create the render target view with the back buffer pointer.
  427. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
  428. if( result != S_OK )
  429. {
  430. std::cout << "ERROR: d3d11Device->CreateRenderTargetView returned " << result << "\n";
  431. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  432. return;
  433. }
  434. // Release pointer to the back buffer as we no longer need it.
  435. backBufferPtr->Release();
  436. // Initialize the description of the depth buffer.
  437. D3D11_TEXTURE2D_DESC depthBufferDesc;
  438. ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
  439. // Set up the description of the depth buffer.
  440. depthBufferDesc.Width = backBufferSize.x;
  441. depthBufferDesc.Height = backBufferSize.y;
  442. depthBufferDesc.MipLevels = 1;
  443. depthBufferDesc.ArraySize = 1;
  444. depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  445. depthBufferDesc.SampleDesc.Count = 1;
  446. depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  447. depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  448. // Create the texture for the depth buffer using the filled out description.
  449. result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
  450. if( result != S_OK )
  451. {
  452. std::cout << "ERROR: d3d11Device->CreateTexture2D returned " << result << "\n";
  453. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  454. return;
  455. }
  456. // Initialize the description of the stencil state.
  457. D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  458. ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
  459. // Set up the description of the stencil state.
  460. depthStencilDesc.DepthEnable = true;
  461. depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  462. depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
  463. depthStencilDesc.StencilEnable = true;
  464. depthStencilDesc.StencilReadMask = 0xFF;
  465. depthStencilDesc.StencilWriteMask = 0xFF;
  466. // Stencil operations if pixel is front-facing.
  467. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  468. depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  469. depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  470. depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  471. // Stencil operations if pixel is back-facing.
  472. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  473. depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  474. depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  475. depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  476. // Create the depth stencil state.
  477. result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
  478. if( result != S_OK )
  479. {
  480. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  481. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  482. return;
  483. }
  484. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  485. // Initialize the depth stencil view.
  486. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  487. ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
  488. // Set up the depth stencil view description.
  489. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  490. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  491. // Create the depth stencil view.
  492. result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
  493. if( result != S_OK )
  494. {
  495. std::cout << "ERROR: d3d11Device->CreateDepthStencilView returned " << result << "\n";
  496. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  497. return;
  498. }
  499. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  500. vp = new D3D11_VIEWPORT();
  501. memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
  502. vp->Width = (float)backBufferSize.x;
  503. vp->Height = (float)backBufferSize.y;
  504. vp->MinDepth = 0.0f;
  505. vp->MaxDepth = 1.0f;
  506. vp->TopLeftX = 0.0f;
  507. vp->TopLeftY = 0.0f;
  508. d3d11Context->RSSetViewports( 1, vp );
  509. D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
  510. // Clear the second depth stencil state before setting the parameters.
  511. ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
  512. // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
  513. // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
  514. depthDisabledStencilDesc.DepthEnable = false;
  515. depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  516. depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  517. depthDisabledStencilDesc.StencilEnable = true;
  518. depthDisabledStencilDesc.StencilReadMask = 0xFF;
  519. depthDisabledStencilDesc.StencilWriteMask = 0xFF;
  520. depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  521. depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  522. depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  523. depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  524. depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  525. depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  526. depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  527. depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  528. // Create the state using the device.
  529. result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
  530. if( result != S_OK )
  531. {
  532. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  533. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  534. return;
  535. }
  536. //-------------------------------------------------
  537. // Shaders
  538. vertexShader = new DX11VertexShader( d3d11Device, d3d11Context );
  539. vertexShader->setCompiledByteArray( (unsigned char *)UIVertexShader, sizeof( UIVertexShader ) );
  540. pixelShader = new DX11PixelShader( d3d11Device, d3d11Context );
  541. pixelShader->setCompiledByteArray( (unsigned char *)UIPixelShader, sizeof( UIPixelShader ) );
  542. D3D11_INPUT_ELEMENT_DESC polygonLayout[ 4 ];
  543. // Create the vertex input layout description.
  544. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
  545. polygonLayout[ 0 ].SemanticName = "POSITION";
  546. polygonLayout[ 0 ].SemanticIndex = 0;
  547. polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  548. polygonLayout[ 0 ].InputSlot = 0;
  549. polygonLayout[ 0 ].AlignedByteOffset = 0;
  550. polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  551. polygonLayout[ 0 ].InstanceDataStepRate = 0;
  552. polygonLayout[ 1 ].SemanticName = "TEXCOORD";
  553. polygonLayout[ 1 ].SemanticIndex = 0;
  554. polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
  555. polygonLayout[ 1 ].InputSlot = 0;
  556. polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  557. polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  558. polygonLayout[ 1 ].InstanceDataStepRate = 0;
  559. polygonLayout[ 2 ].SemanticName = "NORMAL";
  560. polygonLayout[ 2 ].SemanticIndex = 0;
  561. polygonLayout[ 2 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  562. polygonLayout[ 2 ].InputSlot = 0;
  563. polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  564. polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  565. polygonLayout[ 2 ].InstanceDataStepRate = 0;
  566. polygonLayout[ 3 ].SemanticName = "KNOCHEN_ID";
  567. polygonLayout[ 3 ].SemanticIndex = 0;
  568. polygonLayout[ 3 ].Format = DXGI_FORMAT_R32_UINT;
  569. polygonLayout[ 3 ].InputSlot = 0;
  570. polygonLayout[ 3 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  571. polygonLayout[ 3 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  572. polygonLayout[ 3 ].InstanceDataStepRate = 0;
  573. vertexShader->erstelleInputLayout( polygonLayout, 4 );
  574. vertexShader->erstelleConstBuffer( sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 ); // matrizen für skelett annimationen
  575. vertexShader->erstelleConstBuffer( sizeof( Mat4< float > ) * 2, 1 ); // View and Projection Matrix
  576. pixelShader->erstelleConstBuffer( sizeof( float ) * 3, 0 ); // Kamera Position
  577. pixelShader->erstelleConstBuffer( sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model
  578. pixelShader->erstelleConstBuffer( sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model
  579. // TODO: Remove Following Test Code
  580. int lc[] = { 1, 6 };
  581. pixelShader->füllConstBuffer( (char *)lc, 2, sizeof( int ) * 2 );
  582. // Create a texture sampler state description.
  583. D3D11_SAMPLER_DESC samplerDesc;
  584. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  585. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  586. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  587. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  588. samplerDesc.MipLODBias = 0.0f;
  589. samplerDesc.MaxAnisotropy = 1;
  590. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  591. samplerDesc.BorderColor[ 0 ] = 0;
  592. samplerDesc.BorderColor[ 1 ] = 0;
  593. samplerDesc.BorderColor[ 2 ] = 0;
  594. samplerDesc.BorderColor[ 3 ] = 0;
  595. samplerDesc.MinLOD = 0;
  596. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  597. // Create the texture sampler state.
  598. result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
  599. if( result != S_OK )
  600. {
  601. std::cout << "ERROR: d3d11Device->CreateSamplerState returned " << result << "\n";
  602. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  603. return;
  604. }
  605. //---------------------------------------------------------------
  606. // Framework Backbuffer Texture
  607. Bild *renderB = new Bild( 1 );
  608. renderB->setAlpha3D( 1 );
  609. renderB->neuBild( backBufferSize.x, backBufferSize.y, 0 );
  610. uiTextur = createOrGetTextur( "_f_Render_Bild", renderB );
  611. texturModel->setSize( backBufferSize );
  612. texturModel->setTextur( uiTextur->getThis() );
  613. D3D11_BLEND_DESC blendState;
  614. ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
  615. blendState.AlphaToCoverageEnable = false;
  616. blendState.IndependentBlendEnable = false;
  617. blendState.RenderTarget[ 0 ].BlendEnable = true;
  618. blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  619. blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  620. blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
  621. blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
  622. blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
  623. blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  624. blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  625. d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
  626. d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
  627. // Setup Render Objekt
  628. vertexShader->benutzeShader();
  629. d3d11Context->PSSetSamplers( 0, 1, &sampleState );
  630. pixelShader->benutzeShader();
  631. D3D11_RASTERIZER_DESC rasterDesc;
  632. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  633. rasterDesc.AntialiasedLineEnable = false;
  634. rasterDesc.CullMode = D3D11_CULL_BACK;
  635. rasterDesc.DepthBiasClamp = 0.0f;
  636. rasterDesc.DepthClipEnable = true;
  637. rasterDesc.FillMode = D3D11_FILL_SOLID;
  638. rasterDesc.FrontCounterClockwise = false;
  639. rasterDesc.MultisampleEnable = false;
  640. rasterDesc.ScissorEnable = false;
  641. rasterDesc.SlopeScaledDepthBias = 0.0f;
  642. d3d11Device->CreateRasterizerState( &rasterDesc, &texturRS );
  643. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  644. rasterDesc.AntialiasedLineEnable = false;
  645. rasterDesc.CullMode = D3D11_CULL_BACK;
  646. rasterDesc.DepthBiasClamp = 0.0f;
  647. rasterDesc.DepthClipEnable = true;
  648. rasterDesc.FillMode = D3D11_FILL_WIREFRAME;
  649. rasterDesc.FrontCounterClockwise = false;
  650. rasterDesc.MultisampleEnable = false;
  651. rasterDesc.ScissorEnable = false;
  652. rasterDesc.SlopeScaledDepthBias = 0.0f;
  653. d3d11Device->CreateRasterizerState( &rasterDesc, &meshRS );
  654. d3d11Context->RSSetState( texturRS );
  655. Bild *b = new Bild();
  656. b->neuBild( 10, 10, 0xFFFFFFFF );
  657. defaultTextur = createOrGetTextur( "_default_textur", b );
  658. vertexBuffer = new DX11Buffer( sizeof( Vertex3D ), d3d11Device, d3d11Context, D3D11_BIND_VERTEX_BUFFER );
  659. indexBuffer = new DX11Buffer( sizeof( int ), d3d11Device, d3d11Context, D3D11_BIND_INDEX_BUFFER );
  660. DiffuseLight dl[ 1 ];
  661. dl[ 0 ].direction = Vec3< float >( -0.5f, -0.5f, -0.5f ).normalize();
  662. dl[ 0 ].color = Vec3<float>( 1.f, 0.f, 0.f );
  663. diffuseLights = new DX11StructuredBuffer( sizeof( DiffuseLight ), d3d11Device, d3d11Context );
  664. diffuseLights->setData( dl );
  665. diffuseLights->setLength( sizeof( dl ) );
  666. diffuseLights->copieren();
  667. PointLight pl[ 6 ];
  668. pl[ 0 ].position = Vec3< float >( 0, 130, 0 );
  669. pl[ 0 ].color = Vec3< float >( 1.f, 1.f, 0.f );
  670. pl[ 0 ].radius = 100;
  671. pl[ 1 ].position = Vec3< float >( 150, 130, 0 );
  672. pl[ 1 ].color = Vec3< float >( 0.f, 1.f, 0.f );
  673. pl[ 1 ].radius = 100;
  674. pl[ 2 ].position = Vec3< float >( 150, 130, 150 );
  675. pl[ 2 ].color = Vec3< float >( 0.f, 0.f, 1.f );
  676. pl[ 2 ].radius = 100;
  677. pl[ 3 ].position = Vec3< float >( -150, 130, 0 );
  678. pl[ 3 ].color = Vec3< float >( 1.f, 0.f, 1.f );
  679. pl[ 3 ].radius = 100;
  680. pl[ 4 ].position = Vec3< float >( 0, 130, 150 );
  681. pl[ 4 ].color = Vec3< float >( 0.f, 1.f, 1.f );
  682. pl[ 4 ].radius = 100;
  683. pl[ 5 ].position = Vec3< float >( -150, 130, 150 );
  684. pl[ 5 ].color = Vec3< float >( 1.f, 0.f, 0.f );
  685. pl[ 5 ].radius = 100;
  686. pointLights = new DX11StructuredBuffer( sizeof( PointLight ), d3d11Device, d3d11Context );
  687. pointLights->setData( pl );
  688. pointLights->setLength( sizeof( pl ) * 6 );
  689. pointLights->copieren();
  690. }
  691. void DirectX11::update()
  692. {
  693. if( vertexBuffer )
  694. vertexBuffer = (DX11Buffer *)vertexBuffer->release();
  695. if( indexBuffer )
  696. indexBuffer = (DX11Buffer *)indexBuffer->release();
  697. if( texturRS )
  698. {
  699. texturRS->Release();
  700. texturRS = NULL;
  701. }
  702. if( meshRS )
  703. {
  704. meshRS->Release();
  705. meshRS = NULL;
  706. }
  707. texturRegister->leeren();
  708. if( defaultTextur )
  709. defaultTextur = defaultTextur->release();
  710. if( blendStateAlphaBlend )
  711. {
  712. blendStateAlphaBlend->Release();
  713. blendStateAlphaBlend = NULL;
  714. }
  715. if( uiTextur )
  716. {
  717. uiTextur->release();
  718. uiTextur = NULL;
  719. }
  720. if( sampleState )
  721. {
  722. sampleState->Release();
  723. sampleState = NULL;
  724. }
  725. if( pixelShader )
  726. {
  727. pixelShader->release();
  728. pixelShader = NULL;
  729. }
  730. if( vertexShader )
  731. {
  732. vertexShader->release();
  733. vertexShader = NULL;
  734. }
  735. if( depthDisabledStencilState )
  736. {
  737. depthDisabledStencilState->Release();
  738. depthDisabledStencilState = NULL;
  739. }
  740. delete vp;
  741. vp = 0;
  742. if( dsView )
  743. {
  744. dsView->Release();
  745. dsView = NULL;
  746. }
  747. if( depthStencilState )
  748. {
  749. depthStencilState->Release();
  750. depthStencilState = NULL;
  751. }
  752. if( depthStencilBuffer )
  753. {
  754. depthStencilBuffer->Release();
  755. depthStencilBuffer = NULL;
  756. }
  757. if( rtview )
  758. {
  759. rtview->Release();
  760. rtview = NULL;
  761. }
  762. if( d3d11SpawChain )
  763. {
  764. d3d11SpawChain->Release();
  765. d3d11SpawChain = NULL;
  766. }
  767. if( d3d11Device )
  768. {
  769. d3d11Device->Release();
  770. d3d11Device = NULL;
  771. }
  772. if( d3d11Context )
  773. {
  774. d3d11Context->Release();
  775. d3d11Context = NULL;
  776. }
  777. initialize( fenster->getThis(), backBufferSize, fullScreen );
  778. }
  779. void DirectX11::beginFrame( bool fill2D, bool fill3D, int fillColor )
  780. {
  781. if( fill2D )
  782. uiTextur->zBild()->setFarbe( fillColor );
  783. if( fill3D )
  784. {
  785. float color[ 4 ];
  786. // Setup the color to clear the buffer.
  787. color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
  788. color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
  789. color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
  790. color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
  791. d3d11Context->ClearRenderTargetView( rtview, color );
  792. // Clear the depth buffer.
  793. d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
  794. }
  795. // Bind the render target view and depth stencil buffer to the output render pipeline.
  796. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  797. // Set the depth stencil state.
  798. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  799. }
  800. void DirectX11::renderObject( Model3D * zObj )
  801. {
  802. vertexBuffer->setData( (void *)zObj->zVertexBuffer() );
  803. vertexBuffer->setLength( sizeof( Vertex3D ) * zObj->getVertexAnzahl() );
  804. vertexBuffer->copieren();
  805. Mat4< float > trans = Mat4< float >::identity();
  806. int anz = zObj->errechneMatrizen( trans, matrixBuffer );
  807. if( vertexShader )
  808. vertexShader->füllConstBuffer( (char *)matrixBuffer, 0, sizeof( Mat4< float > ) * anz );
  809. float matirialBuffer[ 3 ]; // light factors (phong model)
  810. matirialBuffer[ 0 ] = zObj->getAmbientFactor();
  811. matirialBuffer[ 1 ] = zObj->getDiffusFactor();
  812. matirialBuffer[ 2 ] = zObj->getSpecularFactor();
  813. if( pixelShader )
  814. pixelShader->füllConstBuffer( (char *)matirialBuffer, 1, sizeof( float ) * 3 );
  815. unsigned int offset = 0;
  816. unsigned int es = (unsigned)vertexBuffer->getElementLength();
  817. ID3D11Buffer * vBuffer = vertexBuffer->zBuffer();
  818. d3d11Context->IASetVertexBuffers( 0, 1, &vBuffer, &es, &offset );
  819. Model3DTextur * zTextur = zObj->zTextur();
  820. int ind = 0;
  821. for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
  822. {
  823. indexBuffer->setData( i->indexList );
  824. indexBuffer->setLength( sizeof( int ) * i->indexAnz );
  825. indexBuffer->copieren();
  826. Textur *t = zTextur->zPolygonTextur( ind );
  827. if( t &&t->brauchtUpdate() )
  828. t->updateTextur();
  829. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  830. if( indexBuffer->getElementLength() == 2 )
  831. f = DXGI_FORMAT_R16_UINT;
  832. if( indexBuffer->getElementLength() == 1 )
  833. f = DXGI_FORMAT_R8_UINT;
  834. d3d11Context->IASetIndexBuffer( indexBuffer->zBuffer(), f, 0 );
  835. d3d11Context->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  836. if( t )
  837. {
  838. ID3D11ShaderResourceView *v[ 3 ];
  839. v[ 0 ] = *(DX11Textur *)t;
  840. v[ 1 ] = *diffuseLights;
  841. v[ 2 ] = *pointLights;
  842. d3d11Context->PSSetShaderResources( 0, 3, v );
  843. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  844. }
  845. else
  846. {
  847. d3d11Context->RSSetState( meshRS );
  848. ID3D11ShaderResourceView *v[ 3 ];
  849. v[ 0 ] = *(DX11Textur *)defaultTextur;
  850. v[ 1 ] = *diffuseLights;
  851. v[ 2 ] = *pointLights;
  852. d3d11Context->PSSetShaderResources( 0, 3, v );
  853. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  854. d3d11Context->RSSetState( texturRS );
  855. }
  856. ind++;
  857. }
  858. }
  859. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  860. // pos: Der Mittelpunkt der Kugel
  861. // radius: Der Radius der Kugel
  862. // dist: Einen Zeiger auf einen float, in dem das quadrat des Abstands zur Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und der Zeiger nicht 0 ist
  863. bool DirectX11::isInFrustrum( const Vec3< float > & pos, float radius, float *dist ) const
  864. {
  865. for( int i = 0; i < 6; i++ )
  866. {
  867. if( frustrum[ i ] * pos + radius < 0 )
  868. return 0;
  869. }
  870. if( dist )
  871. * dist = kamPos.abstand( pos );
  872. return 1;
  873. }
  874. void DirectX11::renderKamera( Kam3D * zKamera )
  875. {
  876. d3d11Context->RSSetViewports( 1, (D3D11_VIEWPORT *)zKamera->zViewPort() );
  877. Mat4< float > tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  878. frustrum[ 0 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 0 ][ 0 ];
  879. frustrum[ 0 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 0 ][ 1 ];
  880. frustrum[ 0 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 0 ][ 2 ];
  881. frustrum[ 0 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 0 ][ 3 ];
  882. frustrum[ 1 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 0 ][ 0 ];
  883. frustrum[ 1 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 0 ][ 1 ];
  884. frustrum[ 1 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 0 ][ 2 ];
  885. frustrum[ 1 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 0 ][ 3 ];
  886. frustrum[ 2 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 1 ][ 0 ];
  887. frustrum[ 2 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 1 ][ 1 ];
  888. frustrum[ 2 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 1 ][ 2 ];
  889. frustrum[ 2 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 1 ][ 3 ];
  890. frustrum[ 3 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 1 ][ 0 ];
  891. frustrum[ 3 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 1 ][ 1 ];
  892. frustrum[ 3 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 1 ][ 2 ];
  893. frustrum[ 3 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 1 ][ 3 ];
  894. frustrum[ 4 ].x = tmp.elements[ 2 ][ 0 ];
  895. frustrum[ 4 ].y = tmp.elements[ 2 ][ 1 ];
  896. frustrum[ 4 ].z = tmp.elements[ 2 ][ 2 ];
  897. frustrum[ 4 ].w = tmp.elements[ 2 ][ 3 ];
  898. frustrum[ 5 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 2 ][ 0 ];
  899. frustrum[ 5 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 2 ][ 1 ];
  900. frustrum[ 5 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 2 ][ 2 ];
  901. frustrum[ 5 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 2 ][ 3 ];
  902. for( int i = 0; i < 6; i++ )
  903. frustrum[ i ].normalize();
  904. viewAndProj[ 0 ] = zKamera->getViewMatrix();
  905. viewAndProj[ 1 ] = zKamera->getProjectionMatrix();
  906. kamPos = zKamera->getWorldPosition();
  907. if( vertexShader )
  908. vertexShader->füllConstBuffer( (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  909. if( pixelShader )
  910. pixelShader->füllConstBuffer( (char *)& kamPos, 0, sizeof( float ) * 3 );
  911. Welt3D * w = zKamera->zWelt();
  912. w->lock();
  913. int alphaAnzahl = 0;
  914. int maxDist = 0;
  915. int minDist = 0x7FFFFFFF;
  916. for( auto obj = w->getMembers(); obj; obj++ )
  917. {
  918. float dist;
  919. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  920. {
  921. if( (int)dist > maxDist )
  922. maxDist = (int)dist;
  923. if( minDist < (int)dist )
  924. minDist = (int)dist;
  925. if( obj->hatAlpha() )
  926. alphaAnzahl++;
  927. else
  928. renderObject( obj._ );
  929. }
  930. }
  931. maxDist++;
  932. if( alphaAnzahl )
  933. {
  934. int size = maxDist - minDist;
  935. int *index = new int[ size ];
  936. Model3D **sorted = new Model3D * [ size * alphaAnzahl ];
  937. for( auto obj = w->getMembers(); obj; obj++ )
  938. {
  939. float dist;
  940. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  941. {
  942. if( obj->hatAlpha() )
  943. {
  944. int pos = (int)dist - minDist;
  945. sorted[ pos * alphaAnzahl + index[ pos ]++ ] = obj._;
  946. }
  947. }
  948. }
  949. for( int i = 0; i < size; i++ )
  950. {
  951. for( int j = 0; j < index[ i ]; j++ )
  952. {
  953. renderObject( sorted[ i * alphaAnzahl + j ] );
  954. }
  955. }
  956. delete[] index;
  957. delete[] sorted;
  958. }
  959. w->unlock();
  960. }
  961. void DirectX11::presentFrame()
  962. {
  963. // Set the depth stencil state.
  964. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
  965. uiTextur->updateTextur();
  966. d3d11Context->RSSetViewports( 1, vp );
  967. float screenAspect = (float)backBufferSize.x / (float)backBufferSize.y;
  968. Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
  969. viewAndProj[ 0 ] = view;
  970. viewAndProj[ 1 ] = view.projektion( (float)PI / 4.0f, screenAspect, 0.1f, 10000.f );
  971. kamPos = Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f );
  972. if( vertexShader )
  973. vertexShader->füllConstBuffer( (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  974. if( pixelShader )
  975. pixelShader->füllConstBuffer( (char *)& kamPos, 0, sizeof( float ) * 3 );
  976. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  977. renderObject( texturModel );
  978. HRESULT result = d3d11SpawChain->Present( 0, 0 );
  979. if( !SUCCEEDED( result ) )
  980. {
  981. update();
  982. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  983. }
  984. }
  985. Bild *DirectX11::zUIRenderBild() const
  986. {
  987. return uiTextur->zBild();
  988. }
  989. Textur *DirectX11::createOrGetTextur( const char *name, Bild * b )
  990. {
  991. if( !d3d11Device )
  992. {
  993. if( b )
  994. b->release();
  995. return 0;
  996. }
  997. if( texturRegister->hatTextur( name ) )
  998. {
  999. Textur *ret = texturRegister->getTextur( name );
  1000. if( b )
  1001. ret->setBildZ( b );
  1002. return ret;
  1003. }
  1004. Textur *ret = new DX11Textur( d3d11Device, d3d11Context );
  1005. if( b )
  1006. ret->setBildZ( b );
  1007. texturRegister->addTextur( ret->getThis(), name );
  1008. return ret;
  1009. }