DX11GraphicsApi.cpp 34 KB

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