DX11GraphicsApi.cpp 39 KB

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