DX11GraphicsApi.cpp 33 KB

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