GraphicsApi.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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. void DirectX11::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  348. {
  349. if( d3d11Device )
  350. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  351. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  352. //--------------------------------------------------------------------
  353. // Create Device
  354. // create a struct to hold information about the swap chain
  355. DXGI_SWAP_CHAIN_DESC scd;
  356. // clear out the struct for use
  357. ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
  358. // fill the swap chain description struct
  359. scd.BufferCount = 1; // one back buffer
  360. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  361. scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
  362. scd.SampleDesc.Count = 1;
  363. // Set the scan line ordering and scaling to unspecified.
  364. scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  365. scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  366. scd.Windowed = !fullScreen;
  367. if( !backBufferSize.x || !backBufferSize.y )
  368. backBufferSize = fenster ? fenster->getKörperGröße() : Punkt( 0, 0 );
  369. scd.BufferDesc.Width = backBufferSize.x;
  370. scd.BufferDesc.Height = backBufferSize.y; // windowed/full-screen mode
  371. scd.BufferDesc.RefreshRate.Denominator = 1;
  372. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  373. // Discard the back buffer contents after presenting.
  374. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  375. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  376. D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
  377. // create a device, device context and swap chain using the information in the scd struct
  378. UINT flag = 0;
  379. #ifdef _DEBUG
  380. flag |= D3D11_CREATE_DEVICE_DEBUG;
  381. #endif
  382. HRESULT result = D3D11CreateDeviceAndSwapChain( NULL,
  383. D3D_DRIVER_TYPE_HARDWARE,
  384. NULL,
  385. flag,
  386. &featureLevel,
  387. 1,
  388. D3D11_SDK_VERSION,
  389. &scd,
  390. &d3d11SpawChain,
  391. &d3d11Device,
  392. &support,
  393. &d3d11Context );
  394. if( result != S_OK )
  395. {
  396. std::cout << "ERROR: D3D11CreateDeviceAndSwapChain returned " << result << "\n";
  397. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  398. return;
  399. }
  400. ID3D11Texture2D *backBufferPtr;
  401. // Get the pointer to the back buffer.
  402. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID *)& backBufferPtr );
  403. if( result != S_OK )
  404. {
  405. std::cout << "ERROR: d3d11SpawChain->GetBuffer returned " << result << "\n";
  406. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  407. return;
  408. }
  409. // Create the render target view with the back buffer pointer.
  410. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
  411. if( result != S_OK )
  412. {
  413. std::cout << "ERROR: d3d11Device->CreateRenderTargetView returned " << result << "\n";
  414. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  415. return;
  416. }
  417. // Release pointer to the back buffer as we no longer need it.
  418. backBufferPtr->Release();
  419. // Initialize the description of the depth buffer.
  420. D3D11_TEXTURE2D_DESC depthBufferDesc;
  421. ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
  422. // Set up the description of the depth buffer.
  423. depthBufferDesc.Width = backBufferSize.x;
  424. depthBufferDesc.Height = backBufferSize.y;
  425. depthBufferDesc.MipLevels = 1;
  426. depthBufferDesc.ArraySize = 1;
  427. depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  428. depthBufferDesc.SampleDesc.Count = 1;
  429. depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  430. depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  431. // Create the texture for the depth buffer using the filled out description.
  432. result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
  433. if( result != S_OK )
  434. {
  435. std::cout << "ERROR: d3d11Device->CreateTexture2D returned " << result << "\n";
  436. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  437. return;
  438. }
  439. // Initialize the description of the stencil state.
  440. D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  441. ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
  442. // Set up the description of the stencil state.
  443. depthStencilDesc.DepthEnable = true;
  444. depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  445. depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
  446. depthStencilDesc.StencilEnable = true;
  447. depthStencilDesc.StencilReadMask = 0xFF;
  448. depthStencilDesc.StencilWriteMask = 0xFF;
  449. // Stencil operations if pixel is front-facing.
  450. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  451. depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  452. depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  453. depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  454. // Stencil operations if pixel is back-facing.
  455. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  456. depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  457. depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  458. depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  459. // Create the depth stencil state.
  460. result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
  461. if( result != S_OK )
  462. {
  463. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  464. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  465. return;
  466. }
  467. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  468. // Initialize the depth stencil view.
  469. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  470. ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
  471. // Set up the depth stencil view description.
  472. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  473. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  474. // Create the depth stencil view.
  475. result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
  476. if( result != S_OK )
  477. {
  478. std::cout << "ERROR: d3d11Device->CreateDepthStencilView returned " << result << "\n";
  479. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  480. return;
  481. }
  482. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  483. vp = new D3D11_VIEWPORT();
  484. memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
  485. vp->Width = (float)backBufferSize.x;
  486. vp->Height = (float)backBufferSize.y;
  487. vp->MinDepth = 0.0f;
  488. vp->MaxDepth = 1.0f;
  489. vp->TopLeftX = 0.0f;
  490. vp->TopLeftY = 0.0f;
  491. d3d11Context->RSSetViewports( 1, vp );
  492. D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
  493. // Clear the second depth stencil state before setting the parameters.
  494. ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
  495. // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
  496. // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
  497. depthDisabledStencilDesc.DepthEnable = false;
  498. depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  499. depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  500. depthDisabledStencilDesc.StencilEnable = true;
  501. depthDisabledStencilDesc.StencilReadMask = 0xFF;
  502. depthDisabledStencilDesc.StencilWriteMask = 0xFF;
  503. depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  504. depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  505. depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  506. depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  507. depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  508. depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  509. depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  510. depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  511. // Create the state using the device.
  512. result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
  513. if( result != S_OK )
  514. {
  515. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  516. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  517. return;
  518. }
  519. //-------------------------------------------------
  520. // Shaders
  521. vertexShader = new VertexShader();
  522. vertexShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIVertexShader, sizeof( UIVertexShader ) );
  523. pixelShader = new PixelShader();
  524. pixelShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIPixelShader, sizeof( UIPixelShader ) );
  525. D3D11_INPUT_ELEMENT_DESC polygonLayout[ 4 ];
  526. // Create the vertex input layout description.
  527. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
  528. polygonLayout[ 0 ].SemanticName = "POSITION";
  529. polygonLayout[ 0 ].SemanticIndex = 0;
  530. polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  531. polygonLayout[ 0 ].InputSlot = 0;
  532. polygonLayout[ 0 ].AlignedByteOffset = 0;
  533. polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  534. polygonLayout[ 0 ].InstanceDataStepRate = 0;
  535. polygonLayout[ 1 ].SemanticName = "TEXCOORD";
  536. polygonLayout[ 1 ].SemanticIndex = 0;
  537. polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
  538. polygonLayout[ 1 ].InputSlot = 0;
  539. polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  540. polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  541. polygonLayout[ 1 ].InstanceDataStepRate = 0;
  542. polygonLayout[ 2 ].SemanticName = "NORMAL";
  543. polygonLayout[ 2 ].SemanticIndex = 0;
  544. polygonLayout[ 2 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  545. polygonLayout[ 2 ].InputSlot = 0;
  546. polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  547. polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  548. polygonLayout[ 2 ].InstanceDataStepRate = 0;
  549. polygonLayout[ 3 ].SemanticName = "KNOCHEN_ID";
  550. polygonLayout[ 3 ].SemanticIndex = 0;
  551. polygonLayout[ 3 ].Format = DXGI_FORMAT_R32_UINT;
  552. polygonLayout[ 3 ].InputSlot = 0;
  553. polygonLayout[ 3 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  554. polygonLayout[ 3 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  555. polygonLayout[ 3 ].InstanceDataStepRate = 0;
  556. vertexShader->erstelleInputLayout( d3d11Device, polygonLayout, 4 );
  557. vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 ); // matrizen für skelett annimationen
  558. vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * 2, 1 ); // View and Projection Matrix
  559. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 0 ); // Kamera Position
  560. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model
  561. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model
  562. // TODO: Remove Following Test Code
  563. int lc[] = { 0, 0 };
  564. pixelShader->füllConstBuffer( d3d11Context, (char *)lc, 2, sizeof( int ) * 2 );
  565. // Create a texture sampler state description.
  566. D3D11_SAMPLER_DESC samplerDesc;
  567. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  568. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  569. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  570. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  571. samplerDesc.MipLODBias = 0.0f;
  572. samplerDesc.MaxAnisotropy = 1;
  573. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  574. samplerDesc.BorderColor[ 0 ] = 0;
  575. samplerDesc.BorderColor[ 1 ] = 0;
  576. samplerDesc.BorderColor[ 2 ] = 0;
  577. samplerDesc.BorderColor[ 3 ] = 0;
  578. samplerDesc.MinLOD = 0;
  579. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  580. // Create the texture sampler state.
  581. result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
  582. if( result != S_OK )
  583. {
  584. std::cout << "ERROR: d3d11Device->CreateSamplerState returned " << result << "\n";
  585. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  586. return;
  587. }
  588. //---------------------------------------------------------------
  589. // Framework Backbuffer Texture
  590. Bild *renderB = new Bild( 1 );
  591. renderB->setAlpha3D( 1 );
  592. renderB->neuBild( backBufferSize.x, backBufferSize.y, 0 );
  593. uiTextur = createOrGetTextur( "_f_Render_Bild", renderB );
  594. texturModel->setSize( backBufferSize );
  595. texturModel->setTextur( uiTextur->getThis() );
  596. D3D11_BLEND_DESC blendState;
  597. ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
  598. blendState.AlphaToCoverageEnable = false;
  599. blendState.IndependentBlendEnable = false;
  600. blendState.RenderTarget[ 0 ].BlendEnable = true;
  601. blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  602. blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  603. blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
  604. blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
  605. blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
  606. blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  607. blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  608. d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
  609. d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
  610. // Setup Render Objekt
  611. vertexShader->benutzeShader( d3d11Context );
  612. d3d11Context->PSSetSamplers( 0, 1, &sampleState );
  613. pixelShader->benutzeShader( d3d11Context );
  614. D3D11_RASTERIZER_DESC rasterDesc;
  615. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  616. rasterDesc.AntialiasedLineEnable = false;
  617. rasterDesc.CullMode = D3D11_CULL_BACK;
  618. rasterDesc.DepthBiasClamp = 0.0f;
  619. rasterDesc.DepthClipEnable = true;
  620. rasterDesc.FillMode = D3D11_FILL_SOLID;
  621. rasterDesc.FrontCounterClockwise = false;
  622. rasterDesc.MultisampleEnable = false;
  623. rasterDesc.ScissorEnable = false;
  624. rasterDesc.SlopeScaledDepthBias = 0.0f;
  625. d3d11Device->CreateRasterizerState( &rasterDesc, &texturRS );
  626. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  627. rasterDesc.AntialiasedLineEnable = false;
  628. rasterDesc.CullMode = D3D11_CULL_BACK;
  629. rasterDesc.DepthBiasClamp = 0.0f;
  630. rasterDesc.DepthClipEnable = true;
  631. rasterDesc.FillMode = D3D11_FILL_WIREFRAME;
  632. rasterDesc.FrontCounterClockwise = false;
  633. rasterDesc.MultisampleEnable = false;
  634. rasterDesc.ScissorEnable = false;
  635. rasterDesc.SlopeScaledDepthBias = 0.0f;
  636. d3d11Device->CreateRasterizerState( &rasterDesc, &meshRS );
  637. d3d11Context->RSSetState( texturRS );
  638. Bild * b = new Bild();
  639. b->neuBild( 10, 10, 0xFFFFFFFF );
  640. defaultTextur = createOrGetTextur( "_default_textur", b );
  641. vertexBuffer = new DX11Buffer( sizeof( Vertex3D ), d3d11Device, d3d11Context, D3D11_BIND_VERTEX_BUFFER );
  642. indexBuffer = new DX11Buffer( sizeof( int ), d3d11Device, d3d11Context, D3D11_BIND_INDEX_BUFFER );
  643. DiffuseLight dl[ 1 ];
  644. dl[ 0 ].direction = Vec3< float >( -0.5f, -0.5f, -0.5f ).normalize();
  645. dl[ 0 ].color = Vec3<float>( 1.f, 0.f, 0.f );
  646. diffuseLights = new DX11StructuredBuffer( sizeof( DiffuseLight ), d3d11Device, d3d11Context );
  647. diffuseLights->setData( dl );
  648. diffuseLights->setLength( sizeof( dl ) );
  649. diffuseLights->copieren();
  650. PointLight pl[ 6 ];
  651. pl[ 0 ].position = Vec3< float >( 0, 130, 0 );
  652. pl[ 0 ].color = Vec3< float >( 1.f, 1.f, 0.f );
  653. pl[ 0 ].radius = 100;
  654. pl[ 1 ].position = Vec3< float >( 150, 130, 0 );
  655. pl[ 1 ].color = Vec3< float >( 0.f, 1.f, 0.f );
  656. pl[ 1 ].radius = 100;
  657. pl[ 2 ].position = Vec3< float >( 150, 130, 150 );
  658. pl[ 2 ].color = Vec3< float >( 0.f, 0.f, 1.f );
  659. pl[ 2 ].radius = 100;
  660. pl[ 3 ].position = Vec3< float >( -150, 130, 0 );
  661. pl[ 3 ].color = Vec3< float >( 1.f, 0.f, 1.f );
  662. pl[ 3 ].radius = 100;
  663. pl[ 4 ].position = Vec3< float >( 0, 130, 150 );
  664. pl[ 4 ].color = Vec3< float >( 0.f, 1.f, 1.f );
  665. pl[ 4 ].radius = 100;
  666. pl[ 5 ].position = Vec3< float >( -150, 130, 150 );
  667. pl[ 5 ].color = Vec3< float >( 1.f, 0.f, 0.f );
  668. pl[ 5 ].radius = 100;
  669. pointLights = new DX11StructuredBuffer( sizeof( PointLight ), d3d11Device, d3d11Context );
  670. pointLights->setData( pl );
  671. pointLights->setLength( sizeof( pl ) * 6 );
  672. pointLights->copieren();
  673. }
  674. void DirectX11::update()
  675. {
  676. if( vertexBuffer )
  677. vertexBuffer = (DX11Buffer *)vertexBuffer->release();
  678. if( indexBuffer )
  679. indexBuffer = (DX11Buffer *)indexBuffer->release();
  680. if( texturRS )
  681. {
  682. texturRS->Release();
  683. texturRS = NULL;
  684. }
  685. if( meshRS )
  686. {
  687. meshRS->Release();
  688. meshRS = NULL;
  689. }
  690. texturRegister->leeren();
  691. if( defaultTextur )
  692. defaultTextur = defaultTextur->release();
  693. if( blendStateAlphaBlend )
  694. {
  695. blendStateAlphaBlend->Release();
  696. blendStateAlphaBlend = NULL;
  697. }
  698. if( uiTextur )
  699. {
  700. uiTextur->release();
  701. uiTextur = NULL;
  702. }
  703. if( sampleState )
  704. {
  705. sampleState->Release();
  706. sampleState = NULL;
  707. }
  708. if( pixelShader )
  709. {
  710. pixelShader->release();
  711. pixelShader = NULL;
  712. }
  713. if( vertexShader )
  714. {
  715. vertexShader->release();
  716. vertexShader = NULL;
  717. }
  718. if( depthDisabledStencilState )
  719. {
  720. depthDisabledStencilState->Release();
  721. depthDisabledStencilState = NULL;
  722. }
  723. delete vp;
  724. vp = 0;
  725. if( dsView )
  726. {
  727. dsView->Release();
  728. dsView = NULL;
  729. }
  730. if( depthStencilState )
  731. {
  732. depthStencilState->Release();
  733. depthStencilState = NULL;
  734. }
  735. if( depthStencilBuffer )
  736. {
  737. depthStencilBuffer->Release();
  738. depthStencilBuffer = NULL;
  739. }
  740. if( rtview )
  741. {
  742. rtview->Release();
  743. rtview = NULL;
  744. }
  745. if( d3d11SpawChain )
  746. {
  747. d3d11SpawChain->Release();
  748. d3d11SpawChain = NULL;
  749. }
  750. if( d3d11Device )
  751. {
  752. d3d11Device->Release();
  753. d3d11Device = NULL;
  754. }
  755. if( d3d11Context )
  756. {
  757. d3d11Context->Release();
  758. d3d11Context = NULL;
  759. }
  760. initialize( fenster->getThis(), backBufferSize, fullScreen );
  761. }
  762. void DirectX11::beginFrame( bool fill2D, bool fill3D, int fillColor )
  763. {
  764. if( fill2D )
  765. uiTextur->zBild()->setFarbe( fillColor );
  766. if( fill3D )
  767. {
  768. float color[ 4 ];
  769. // Setup the color to clear the buffer.
  770. color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
  771. color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
  772. color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
  773. color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
  774. d3d11Context->ClearRenderTargetView( rtview, color );
  775. // Clear the depth buffer.
  776. d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
  777. }
  778. // Bind the render target view and depth stencil buffer to the output render pipeline.
  779. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  780. // Set the depth stencil state.
  781. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  782. }
  783. void DirectX11::renderObject( Model3D * zObj )
  784. {
  785. vertexBuffer->setData( (void *)zObj->zVertexBuffer() );
  786. vertexBuffer->setLength( sizeof( Vertex3D ) * zObj->getVertexAnzahl() );
  787. vertexBuffer->copieren();
  788. Mat4< float > trans = Mat4< float >::identity();
  789. int anz = zObj->errechneMatrizen( trans, matrixBuffer );
  790. if( vertexShader )
  791. vertexShader->füllConstBuffer( d3d11Context, (char *)matrixBuffer, 0, sizeof( Mat4< float > ) * anz );
  792. float matirialBuffer[ 3 ]; // light factors (phong model)
  793. matirialBuffer[ 0 ] = zObj->getAmbientFactor();
  794. matirialBuffer[ 1 ] = zObj->getDiffusFactor();
  795. matirialBuffer[ 2 ] = zObj->getSpecularFactor();
  796. if( pixelShader )
  797. pixelShader->füllConstBuffer( d3d11Context, (char *)matirialBuffer, 1, sizeof( float ) * 3 );
  798. unsigned int offset = 0;
  799. unsigned int es = (unsigned)vertexBuffer->getElementLength();
  800. ID3D11Buffer * vBuffer = vertexBuffer->zBuffer();
  801. d3d11Context->IASetVertexBuffers( 0, 1, &vBuffer, &es, &offset );
  802. Model3DTextur * zTextur = zObj->zTextur();
  803. int ind = 0;
  804. for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
  805. {
  806. indexBuffer->setData( i->indexList );
  807. indexBuffer->setLength( sizeof( int ) * i->indexAnz );
  808. indexBuffer->copieren();
  809. Textur *t = zTextur->zPolygonTextur( ind );
  810. if( t &&t->brauchtUpdate() )
  811. t->updateTextur();
  812. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  813. if( indexBuffer->getElementLength() == 2 )
  814. f = DXGI_FORMAT_R16_UINT;
  815. if( indexBuffer->getElementLength() == 1 )
  816. f = DXGI_FORMAT_R8_UINT;
  817. d3d11Context->IASetIndexBuffer( indexBuffer->zBuffer(), f, 0 );
  818. d3d11Context->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  819. if( t )
  820. {
  821. ID3D11ShaderResourceView *v[ 3 ];
  822. v[ 0 ] = *(DX11Textur *)t;
  823. v[ 1 ] = *diffuseLights;
  824. v[ 2 ] = *pointLights;
  825. d3d11Context->PSSetShaderResources( 0, 3, v );
  826. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  827. }
  828. else
  829. {
  830. d3d11Context->RSSetState( meshRS );
  831. ID3D11ShaderResourceView *v[ 3 ];
  832. v[ 0 ] = *(DX11Textur *)defaultTextur;
  833. v[ 1 ] = *diffuseLights;
  834. v[ 2 ] = *pointLights;
  835. d3d11Context->PSSetShaderResources( 0, 3, v );
  836. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  837. d3d11Context->RSSetState( texturRS );
  838. }
  839. ind++;
  840. }
  841. }
  842. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  843. // pos: Der Mittelpunkt der Kugel
  844. // radius: Der Radius der Kugel
  845. // 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
  846. bool DirectX11::isInFrustrum( const Vec3< float > & pos, float radius, float *dist ) const
  847. {
  848. for( int i = 0; i < 6; i++ )
  849. {
  850. if( frustrum[ i ] * pos + radius < 0 )
  851. return 0;
  852. }
  853. if( dist )
  854. * dist = kamPos.abstand( pos );
  855. return 1;
  856. }
  857. void DirectX11::renderKamera( Kam3D * zKamera )
  858. {
  859. d3d11Context->RSSetViewports( 1, (D3D11_VIEWPORT *)zKamera->zViewPort() );
  860. Mat4< float > tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  861. frustrum[ 0 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 0 ][ 0 ];
  862. frustrum[ 0 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 0 ][ 1 ];
  863. frustrum[ 0 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 0 ][ 2 ];
  864. frustrum[ 0 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 0 ][ 3 ];
  865. frustrum[ 1 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 0 ][ 0 ];
  866. frustrum[ 1 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 0 ][ 1 ];
  867. frustrum[ 1 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 0 ][ 2 ];
  868. frustrum[ 1 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 0 ][ 3 ];
  869. frustrum[ 2 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 1 ][ 0 ];
  870. frustrum[ 2 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 1 ][ 1 ];
  871. frustrum[ 2 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 1 ][ 2 ];
  872. frustrum[ 2 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 1 ][ 3 ];
  873. frustrum[ 3 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 1 ][ 0 ];
  874. frustrum[ 3 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 1 ][ 1 ];
  875. frustrum[ 3 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 1 ][ 2 ];
  876. frustrum[ 3 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 1 ][ 3 ];
  877. frustrum[ 4 ].x = tmp.elements[ 2 ][ 0 ];
  878. frustrum[ 4 ].y = tmp.elements[ 2 ][ 1 ];
  879. frustrum[ 4 ].z = tmp.elements[ 2 ][ 2 ];
  880. frustrum[ 4 ].w = tmp.elements[ 2 ][ 3 ];
  881. frustrum[ 5 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 2 ][ 0 ];
  882. frustrum[ 5 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 2 ][ 1 ];
  883. frustrum[ 5 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 2 ][ 2 ];
  884. frustrum[ 5 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 2 ][ 3 ];
  885. for( int i = 0; i < 6; i++ )
  886. frustrum[ i ].normalize();
  887. viewAndProj[ 0 ] = zKamera->getViewMatrix();
  888. viewAndProj[ 1 ] = zKamera->getProjectionMatrix();
  889. kamPos = zKamera->getWorldPosition();
  890. if( vertexShader )
  891. vertexShader->füllConstBuffer( d3d11Context, (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  892. if( pixelShader )
  893. pixelShader->füllConstBuffer( d3d11Context, (char *)& kamPos, 0, sizeof( float ) * 3 );
  894. Welt3D * w = zKamera->zWelt();
  895. w->lock();
  896. int alphaAnzahl = 0;
  897. int maxDist = 0;
  898. int minDist = 0x7FFFFFFF;
  899. for( auto obj = w->getMembers(); obj; obj++ )
  900. {
  901. float dist;
  902. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  903. {
  904. if( (int)dist > maxDist )
  905. maxDist = (int)dist;
  906. if( minDist < (int)dist )
  907. minDist = (int)dist;
  908. if( obj->hatAlpha() )
  909. alphaAnzahl++;
  910. else
  911. renderObject( obj._ );
  912. }
  913. }
  914. maxDist++;
  915. if( alphaAnzahl )
  916. {
  917. int size = maxDist - minDist;
  918. int *index = new int[ size ];
  919. Model3D **sorted = new Model3D * [ size * alphaAnzahl ];
  920. for( auto obj = w->getMembers(); obj; obj++ )
  921. {
  922. float dist;
  923. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  924. {
  925. if( obj->hatAlpha() )
  926. {
  927. int pos = (int)dist - minDist;
  928. sorted[ pos * alphaAnzahl + index[ pos ]++ ] = obj._;
  929. }
  930. }
  931. }
  932. for( int i = 0; i < size; i++ )
  933. {
  934. for( int j = 0; j < index[ i ]; j++ )
  935. {
  936. renderObject( sorted[ i * alphaAnzahl + j ] );
  937. }
  938. }
  939. delete[] index;
  940. delete[] sorted;
  941. }
  942. w->unlock();
  943. }
  944. void DirectX11::presentFrame()
  945. {
  946. // Set the depth stencil state.
  947. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
  948. uiTextur->updateTextur();
  949. d3d11Context->RSSetViewports( 1, vp );
  950. float screenAspect = (float)backBufferSize.x / (float)backBufferSize.y;
  951. Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
  952. viewAndProj[ 0 ] = view;
  953. viewAndProj[ 1 ] = view.projektion( (float)PI / 4.0f, screenAspect, 0.1f, 10000.f );
  954. kamPos = Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f );
  955. if( vertexShader )
  956. vertexShader->füllConstBuffer( d3d11Context, (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  957. if( pixelShader )
  958. pixelShader->füllConstBuffer( d3d11Context, (char *)& kamPos, 0, sizeof( float ) * 3 );
  959. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  960. renderObject( texturModel );
  961. HRESULT result = d3d11SpawChain->Present( 0, 0 );
  962. if( !SUCCEEDED( result ) )
  963. {
  964. update();
  965. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  966. }
  967. }
  968. Bild *DirectX11::zUIRenderBild() const
  969. {
  970. return uiTextur->zBild();
  971. }
  972. Textur *DirectX11::createOrGetTextur( const char *name, Bild * b )
  973. {
  974. if( !d3d11Device )
  975. {
  976. if( b )
  977. b->release();
  978. return 0;
  979. }
  980. if( texturRegister->hatTextur( name ) )
  981. {
  982. Textur *ret = texturRegister->getTextur( name );
  983. if( b )
  984. ret->setBildZ( b );
  985. return ret;
  986. }
  987. Textur *ret = new DX11Textur( d3d11Device, d3d11Context );
  988. if( b )
  989. ret->setBildZ( b );
  990. texturRegister->addTextur( ret->getThis(), name );
  991. return ret;
  992. }