Bildschirm.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. #include "Bildschirm.h"
  2. #include "Bild.h"
  3. #include "Fenster.h"
  4. #include "Text.h"
  5. #include "Zeichnung.h"
  6. #include "Globals.h"
  7. #include "Zeit.h"
  8. #include "ToolTip.h"
  9. #include "MausEreignis.h"
  10. #include <iostream>
  11. #include <DirectXMath.h>
  12. #include <D3Dcompiler.h>
  13. #include "Datei.h"
  14. #include "DefaultShader.h"
  15. #include "comdef.h"
  16. #include "Zeichnung3D.h"
  17. #include "Shader.h"
  18. #include "Kam3D.h"
  19. #include "Render3D.h"
  20. #include "Mat3.h"
  21. #include "DXBuffer.h"
  22. #include "Model3D.h"
  23. #include "Textur.h"
  24. #include "TexturModel.h"
  25. #include "TexturList.h"
  26. #include <d3d11.h>
  27. #include <d3d9.h>
  28. using namespace Framework;
  29. // Inhalt der Bildschirmklass aus Bildschirm.h
  30. // Konstruktor
  31. Bildschirm::Bildschirm( WFenster *f )
  32. : fenster( f ),
  33. renderB( new Bild( 1 ) ),
  34. ref( 1 ),
  35. members( new ZeichnungArray() ),
  36. füllFarbe( 0xFF000000 ),
  37. deckFarbe( 0 ),
  38. onTop( 0 ),
  39. renderOnTop( 0 ),
  40. renderZeichnungen( 1 ),
  41. vollbild( 0 ),
  42. rendering( 0 ),
  43. renderZeit( new ZeitMesser() ),
  44. backBufferGröße( 0, 0 ),
  45. tips( new RCArray< ToolTip >() ),
  46. tipAnzahl( 0 ),
  47. testRend( 1 ),
  48. füll( 1 ),
  49. rend( 0 )
  50. {
  51. InitializeCriticalSection( &cs );
  52. }
  53. // Destruktor
  54. Bildschirm::~Bildschirm()
  55. {
  56. lock();
  57. if( renderB )
  58. renderB->release();
  59. if( fenster )
  60. fenster->release();
  61. delete members;
  62. tipAnzahl = 0;
  63. tips->release();
  64. renderZeit->release();
  65. unlock();
  66. DeleteCriticalSection( &cs );
  67. }
  68. // nicht konstant
  69. void Bildschirm::lock()
  70. {
  71. EnterCriticalSection( &cs );
  72. }
  73. void Bildschirm::unlock()
  74. {
  75. LeaveCriticalSection( &cs );
  76. }
  77. void Bildschirm::setFüll( bool f )
  78. {
  79. füll = f;
  80. }
  81. void Bildschirm::setTestRend( bool tr ) // legt fest, ob vo rendern auf updates geprüft werden soll
  82. {
  83. testRend = tr;
  84. }
  85. void Bildschirm::setRenderZeichnungen( bool rO ) // legt fest, ob die Zeichnunge gerendert werden
  86. {
  87. lock();
  88. renderZeichnungen = rO;
  89. rend = 1;
  90. unlock();
  91. }
  92. void Bildschirm::setOnTop( bool onTop ) // legt fest, ob das onTop Zeichnung gerendert wid
  93. {
  94. renderOnTop = onTop;
  95. rend = 1;
  96. }
  97. void Bildschirm::setOnTopZeichnung( Zeichnung *obj ) // setzt das OnTop Zeichnung
  98. {
  99. lock();
  100. onTop = obj;
  101. rend = 1;
  102. unlock();
  103. }
  104. void Bildschirm::setdeckFarbe( int f ) // setzt die deckFarbe
  105. {
  106. deckFarbe = f;
  107. rend = 1;
  108. }
  109. void Bildschirm::addMember( Zeichnung *obj ) // Fügt ein Zeichnung hinzu
  110. {
  111. lock();
  112. members->addZeichnung( obj );
  113. members->updateIndex( 0 );
  114. rend = 1;
  115. unlock();
  116. }
  117. void Bildschirm::removeMember( Zeichnung *obj ) // Entfernt ein Zeichnung
  118. {
  119. lock();
  120. members->removeZeichnung( obj );
  121. members->updateIndex( 0 );
  122. rend = 1;
  123. unlock();
  124. }
  125. void Bildschirm::setFüllFarbe( int f ) // setzt die Fill Farbe
  126. {
  127. füllFarbe = f;
  128. rend = 1;
  129. }
  130. void Bildschirm::setVollbild( bool vollbild ) // setzt vollbild
  131. {
  132. lock();
  133. this->vollbild = vollbild;
  134. rend = 1;
  135. unlock();
  136. }
  137. void Bildschirm::tick( double tickval )
  138. {
  139. lock();
  140. if( !renderOnTop )
  141. {
  142. for( int i = 0; i < tipAnzahl; ++i )
  143. rend |= tips->z( i )->tick( tickval );
  144. rend |= members->tick( tickval );
  145. }
  146. else if( onTop )
  147. {
  148. rend |= onTop->tick( tickval );
  149. for( int i = 0; i < tipAnzahl; ++i )
  150. rend |= tips->z( i )->tick( tickval );
  151. }
  152. unlock();
  153. }
  154. void Bildschirm::setBackBufferGröße( int breite, int höhe ) // setzt die Größe des Backbuffers
  155. {
  156. lock();
  157. backBufferGröße.x = breite;
  158. backBufferGröße.y = höhe;
  159. rend = 1;
  160. unlock();
  161. }
  162. void Bildschirm::setBackBufferGröße( Punkt &größe )
  163. {
  164. lock();
  165. backBufferGröße = größe;
  166. rend = 1;
  167. unlock();
  168. }
  169. void Bildschirm::doMausEreignis( MausEreignis &me ) // sendet maus Ereignis
  170. {
  171. int fBr = backBufferGröße.x;
  172. int fHö = backBufferGröße.y;
  173. if( fenster )
  174. {
  175. fBr = fenster->getKörperBreite();
  176. fHö = fenster->getKörperHöhe();
  177. }
  178. me.mx = (int)( me.mx * backBufferGröße.x / (double)fBr + 0.5 );
  179. me.my = (int)( me.my * backBufferGröße.y / (double)fHö + 0.5 );
  180. lock();
  181. if( !renderOnTop )
  182. {
  183. for( int i = 0; i < tipAnzahl; ++i )
  184. tips->z( i )->doMausEreignis( me );
  185. members->sendMausAll( me );
  186. }
  187. else if( onTop )
  188. {
  189. onTop->doMausEreignis( me );
  190. for( int i = 0; i < tipAnzahl; ++i )
  191. tips->z( i )->doMausEreignis( me );
  192. }
  193. unlock();
  194. }
  195. void Bildschirm::doTastaturEreignis( TastaturEreignis &te ) // sendet tastatur Ereignis
  196. {
  197. lock();
  198. if( !renderOnTop )
  199. members->sendTastaturAll( te );
  200. else if( onTop )
  201. onTop->doTastaturEreignis( te );
  202. unlock();
  203. }
  204. void Bildschirm::addToolTip( ToolTip *tip ) // fügt ToolTip hinzu
  205. {
  206. lock();
  207. tips->add( tip, tipAnzahl );
  208. ++tipAnzahl;
  209. rend = 1;
  210. unlock();
  211. }
  212. bool Bildschirm::removeToolTip( ToolTip *zTip ) // entfernt ToolTip
  213. {
  214. lock();
  215. bool gefunden = 0;
  216. for( int i = 0; i < tipAnzahl; ++i )
  217. {
  218. ToolTip *tmp = tips->z( i );
  219. if( tmp == zTip )
  220. {
  221. tips->lösche( i );
  222. --tipAnzahl;
  223. gefunden = 1;
  224. rend = 1;
  225. break;
  226. }
  227. }
  228. unlock();
  229. return gefunden;
  230. }
  231. // constant
  232. Bild *Bildschirm::getRenderBild() const
  233. {
  234. return renderB->getThis();
  235. }
  236. Bild *Bildschirm::zRenderBild() const
  237. {
  238. return renderB;
  239. }
  240. ZeichnungArray *Bildschirm::getMembers() const // gibt die Zeichnunge zurück
  241. {
  242. return members;
  243. }
  244. int Bildschirm::getFüllFarbe() const // gibt die Füll Farbe zurück
  245. {
  246. return füllFarbe;
  247. }
  248. bool Bildschirm::istVolbild() const // gibt zurück, ob vollbild an ist
  249. {
  250. return vollbild;
  251. }
  252. const Punkt &Bildschirm::getBackBufferGröße() const // gibt die Größe des Backbuffers zurück
  253. {
  254. return backBufferGröße;
  255. }
  256. void Bildschirm::warteAufRendern() const // wartet auf die render Funktion
  257. {
  258. while( rendering )
  259. {
  260. if( !rendering )
  261. return;
  262. }
  263. }
  264. double Bildschirm::getRenderZeit() const // gibt zurück wie viele Sekunden das Rendern dauert
  265. {
  266. return renderZeit->getSekunden();
  267. }
  268. // Reference Counting
  269. Bildschirm *Bildschirm::getThis()
  270. {
  271. ++ref;
  272. return this;
  273. }
  274. Bildschirm *Bildschirm::release()
  275. {
  276. --ref;
  277. if( !ref )
  278. delete this;
  279. return 0;
  280. }
  281. int MonitorEnum( HMONITOR m, HDC dc, LPRECT r, LPARAM p )
  282. {
  283. Monitor *mon = new Monitor();
  284. mon->existiert = 1;
  285. mon->x = r->left;
  286. mon->y = r->top;
  287. mon->breite = r->right - r->left;
  288. mon->höhe = r->bottom - r->top;
  289. ( ( Array< Monitor* >* )p )->add( mon );
  290. return 1;
  291. }
  292. Monitor Framework::getMonitor( int id )
  293. {
  294. if( id < 0 )
  295. {
  296. Monitor m;
  297. m.existiert = 0;
  298. return m;
  299. }
  300. Array< Monitor* > *monitore = new Array< Monitor* >();
  301. EnumDisplayMonitors( 0, 0, (MONITORENUMPROC)MonitorEnum, (LPARAM)monitore );
  302. if( id >= monitore->getEintragAnzahl() )
  303. {
  304. for( int i = 0; monitore->get( i ); ++i )
  305. delete monitore->get( i );
  306. delete monitore;
  307. Monitor m;
  308. m.existiert = 0;
  309. return m;
  310. }
  311. Monitor m = *monitore->get( id );
  312. int anz = monitore->getEintragAnzahl();
  313. for( int i = 0; i < anz; ++i )
  314. delete monitore->get( i );
  315. delete monitore;
  316. return m;
  317. }
  318. // Bildschirm2D
  319. // Konstruktor
  320. Bildschirm2D::Bildschirm2D( WFenster *fenster )
  321. : Bildschirm( fenster ),
  322. pDirect3D( 0 ),
  323. pDevice( 0 ),
  324. pBackBuffer( 0 ),
  325. backRect( new D3DLOCKED_RECT() )
  326. {}
  327. // Destruktor
  328. Bildschirm2D::~Bildschirm2D()
  329. {
  330. cleanUpDirectX();
  331. delete backRect;
  332. }
  333. // private
  334. void Bildschirm2D::cleanUpDirectX()
  335. {
  336. backRect->pBits = NULL;
  337. if( pDevice )
  338. {
  339. pDevice->Release();
  340. pDevice = NULL;
  341. }
  342. if( pDirect3D )
  343. {
  344. pDirect3D->Release();
  345. pDirect3D = NULL;
  346. }
  347. if( pBackBuffer )
  348. {
  349. pBackBuffer->Release();
  350. pBackBuffer = NULL;
  351. }
  352. }
  353. // nicht constant
  354. void Bildschirm2D::update() // aktualisiert directX
  355. {
  356. lock();
  357. HRESULT result;
  358. cleanUpDirectX();
  359. pDirect3D = Direct3DCreate9( D3D_SDK_VERSION );
  360. D3DPRESENT_PARAMETERS d3dpp;
  361. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  362. d3dpp.Windowed = !vollbild;
  363. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  364. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  365. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  366. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  367. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  368. if( !backBufferGröße.x || !backBufferGröße.y )
  369. backBufferGröße = fenster->getKörperGröße();
  370. d3dpp.BackBufferHeight = backBufferGröße.y;
  371. d3dpp.BackBufferWidth = backBufferGröße.x;
  372. if( renderB )
  373. renderB->release();
  374. renderB = new Bild( 1 );
  375. renderB->neuBild( backBufferGröße.x, backBufferGröße.y, füllFarbe );
  376. result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  377. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  378. if( pDevice )
  379. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  380. rend = 1;
  381. unlock();
  382. }
  383. void Bildschirm2D::render() // Zeichnet das Bild
  384. {
  385. if( !rend && testRend )
  386. return;
  387. rendering = 1;
  388. int count = 0;
  389. if( renderB && pDevice )
  390. {
  391. lock();
  392. renderZeit->messungStart();
  393. if( füll )
  394. renderB->setFarbe( füllFarbe );
  395. if( renderZeichnungen )
  396. {
  397. if( renderOnTop && deckFarbe && ( deckFarbe & ( füllFarbe | 0xFF000000 ) ) == deckFarbe )
  398. {
  399. renderB->setAlpha( 255 - (unsigned char)( deckFarbe >> 24 ) );
  400. members->render( *renderB ); // zeichnen nach zwischenbuffer
  401. renderB->releaseAlpha();
  402. }
  403. else
  404. {
  405. members->render( *renderB ); // zeichnen nach zwischenbuffer
  406. if( renderOnTop && deckFarbe )
  407. renderB->alphaRegion( 0, 0, renderB->getBreite(), renderB->getHöhe(), deckFarbe );
  408. }
  409. for( int i = 0; i < tipAnzahl; ++i )
  410. tips->z( i )->render( *renderB );
  411. }
  412. if( renderOnTop && onTop )
  413. onTop->render( *renderB );
  414. Bild *tmp = renderB->getThis();
  415. unlock();
  416. // Beginne Bild
  417. HRESULT result;
  418. if( !füllFarbe )
  419. result = pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 0.0f, 0 );
  420. result = pBackBuffer->LockRect( backRect, 0, 0 );
  421. // kopieren zum Bildschrirm
  422. int *bgBuff = tmp->getBuffer();
  423. int tmpBr = sizeof( D3DCOLOR )* tmp->getBreite();
  424. for( int y = 0, pitch = 0, bry = 0; y < tmp->getHöhe(); ++y, pitch += backRect->Pitch, bry += tmp->getBreite() )
  425. memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void* )&( bgBuff[ bry ] ), tmpBr );
  426. // Beende Bild
  427. result = pBackBuffer->UnlockRect();
  428. tmp->release();
  429. result = pDevice->Present( 0, 0, 0, 0 );
  430. renderZeit->messungEnde();
  431. if( result != S_OK )
  432. {
  433. ++count;
  434. update();
  435. }
  436. else if( count )
  437. --count;
  438. }
  439. if( !pDevice )
  440. {
  441. ++count;
  442. update();
  443. }
  444. if( count > 10 )
  445. {
  446. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  447. count = 0;
  448. }
  449. rendering = 0;
  450. rend = 0;
  451. }
  452. Bildschirm *Bildschirm2D::release()
  453. {
  454. --ref;
  455. if( !ref )
  456. delete this;
  457. return 0;
  458. }
  459. // Bildschirm3D
  460. // Konstruktor
  461. Bildschirm3D::Bildschirm3D( WFenster *fenster )
  462. : Bildschirm( fenster ),
  463. d3d11Device( 0 ),
  464. d3d11Context( 0 ),
  465. d3d11SpawChain( 0 ),
  466. frameworkTextur( 0 ),
  467. vertexShader( 0 ),
  468. pixelShader( 0 ),
  469. sampleState( 0 ),
  470. rtview( 0 ),
  471. dsView( 0 ),
  472. depthStencilBuffer( 0 ),
  473. depthStencilState( 0 ),
  474. depthDisabledStencilState( 0 ),
  475. blendStateAlphaBlend( 0 ),
  476. kameras( new RCArray< Kam3D >() ),
  477. rend3D( 0 ),
  478. vp( 0 ),
  479. renderObj( new Render3D() ),
  480. texturModel( new TexturModel() )
  481. {}
  482. // Destruktor
  483. Bildschirm3D::~Bildschirm3D()
  484. {
  485. kameras->release();
  486. renderObj->release();
  487. texturModel->release();
  488. cleanUpDirectX();
  489. }
  490. // private
  491. void Bildschirm3D::cleanUpDirectX()
  492. {
  493. if( d3d11Device )
  494. {
  495. d3d11Device->Release();
  496. d3d11Device = NULL;
  497. }
  498. if( d3d11Context )
  499. {
  500. d3d11Context->Release();
  501. d3d11Context = NULL;
  502. }
  503. if( d3d11SpawChain )
  504. {
  505. d3d11SpawChain->Release();
  506. d3d11SpawChain = NULL;
  507. }
  508. if( frameworkTextur )
  509. {
  510. frameworkTextur->release();
  511. frameworkTextur = NULL;
  512. }
  513. if( pixelShader )
  514. {
  515. pixelShader->release();
  516. pixelShader = NULL;
  517. }
  518. if( vertexShader )
  519. {
  520. vertexShader->release();
  521. vertexShader = NULL;
  522. }
  523. if( sampleState )
  524. {
  525. sampleState->Release();
  526. sampleState = NULL;
  527. }
  528. if( rtview )
  529. {
  530. rtview->Release();
  531. rtview = NULL;
  532. }
  533. if( dsView )
  534. {
  535. dsView->Release();
  536. dsView = NULL;
  537. }
  538. if( depthStencilBuffer )
  539. {
  540. depthStencilBuffer->Release();
  541. depthStencilBuffer = NULL;
  542. }
  543. if( depthStencilState )
  544. {
  545. depthStencilState->Release();
  546. depthStencilState = NULL;
  547. }
  548. if( depthDisabledStencilState )
  549. {
  550. depthDisabledStencilState->Release();
  551. depthDisabledStencilState = NULL;
  552. }
  553. if( blendStateAlphaBlend )
  554. {
  555. blendStateAlphaBlend->Release();
  556. blendStateAlphaBlend = NULL;
  557. }
  558. delete vp;
  559. vp = 0;
  560. }
  561. // nicht constant
  562. void Bildschirm3D::addKamera( Kam3D *obj ) // Fügt ein Zeichnung hinzu
  563. {
  564. lock();
  565. kameras->add( obj );
  566. rend3D = 1;
  567. unlock();
  568. }
  569. void Bildschirm3D::removeKamera( Kam3D *zObj ) // Entfernt ein Zeichnung
  570. {
  571. lock();
  572. for( int i = 0; kameras->z( i ); i++ )
  573. {
  574. if( kameras->z( i ) == zObj )
  575. {
  576. kameras->lösche( i );
  577. break;
  578. }
  579. }
  580. rend3D = 1;
  581. unlock();
  582. }
  583. void Bildschirm3D::update() // aktualisiert directX
  584. {
  585. lock();
  586. HRESULT result;
  587. cleanUpDirectX();
  588. //--------------------------------------------------------------------
  589. // Create Device
  590. // create a struct to hold information about the swap chain
  591. DXGI_SWAP_CHAIN_DESC scd;
  592. // clear out the struct for use
  593. ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
  594. // fill the swap chain description struct
  595. scd.BufferCount = 1; // one back buffer
  596. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  597. scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
  598. scd.SampleDesc.Count = 1;
  599. // Set the scan line ordering and scaling to unspecified.
  600. scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  601. scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  602. scd.Windowed = !vollbild;
  603. if( !backBufferGröße.x || !backBufferGröße.y )
  604. backBufferGröße = fenster ? fenster->getKörperGröße() : Punkt( 0, 0 );
  605. scd.BufferDesc.Width = backBufferGröße.x;
  606. scd.BufferDesc.Height = backBufferGröße.y; // windowed/full-screen mode
  607. scd.BufferDesc.RefreshRate.Denominator = 1;
  608. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  609. // Discard the back buffer contents after presenting.
  610. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  611. if( renderB )
  612. renderB->release();
  613. renderB = new Bild( 1 );
  614. renderB->setAlpha3D( 1 );
  615. renderB->neuBild( backBufferGröße.x, backBufferGröße.y, füllFarbe );
  616. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  617. D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
  618. // create a device, device context and swap chain using the information in the scd struct
  619. UINT flag = 0;
  620. #ifdef _DEBUG
  621. flag |= D3D11_CREATE_DEVICE_DEBUG;
  622. #endif
  623. result = D3D11CreateDeviceAndSwapChain( NULL,
  624. D3D_DRIVER_TYPE_HARDWARE,
  625. NULL,
  626. flag,
  627. &featureLevel,
  628. 1,
  629. D3D11_SDK_VERSION,
  630. &scd,
  631. &d3d11SpawChain,
  632. &d3d11Device,
  633. &support,
  634. &d3d11Context );
  635. ID3D11Texture2D *backBufferPtr;
  636. // Get the pointer to the back buffer.
  637. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&backBufferPtr );
  638. // Create the render target view with the back buffer pointer.
  639. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
  640. // Release pointer to the back buffer as we no longer need it.
  641. backBufferPtr->Release();
  642. // Initialize the description of the depth buffer.
  643. D3D11_TEXTURE2D_DESC depthBufferDesc;
  644. ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
  645. // Set up the description of the depth buffer.
  646. depthBufferDesc.Width = backBufferGröße.x;
  647. depthBufferDesc.Height = backBufferGröße.y;
  648. depthBufferDesc.MipLevels = 1;
  649. depthBufferDesc.ArraySize = 1;
  650. depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  651. depthBufferDesc.SampleDesc.Count = 1;
  652. depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  653. depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  654. // Create the texture for the depth buffer using the filled out description.
  655. result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
  656. // Initialize the description of the stencil state.
  657. D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  658. ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
  659. // Set up the description of the stencil state.
  660. depthStencilDesc.DepthEnable = true;
  661. depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  662. depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
  663. depthStencilDesc.StencilEnable = true;
  664. depthStencilDesc.StencilReadMask = 0xFF;
  665. depthStencilDesc.StencilWriteMask = 0xFF;
  666. // Stencil operations if pixel is front-facing.
  667. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  668. depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  669. depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  670. depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  671. // Stencil operations if pixel is back-facing.
  672. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  673. depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  674. depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  675. depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  676. // Create the depth stencil state.
  677. result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
  678. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  679. // Initialize the depth stencil view.
  680. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  681. ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
  682. // Set up the depth stencil view description.
  683. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  684. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  685. // Create the depth stencil view.
  686. result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
  687. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  688. vp = new D3D11_VIEWPORT();
  689. memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
  690. vp->Width = (float)backBufferGröße.x;
  691. vp->Height = (float)backBufferGröße.y;
  692. vp->MinDepth = 0.0f;
  693. vp->MaxDepth = 1.0f;
  694. vp->TopLeftX = 0.0f;
  695. vp->TopLeftY = 0.0f;
  696. d3d11Context->RSSetViewports( 1, vp );
  697. D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
  698. // Clear the second depth stencil state before setting the parameters.
  699. ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
  700. // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
  701. // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
  702. depthDisabledStencilDesc.DepthEnable = false;
  703. depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  704. depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  705. depthDisabledStencilDesc.StencilEnable = true;
  706. depthDisabledStencilDesc.StencilReadMask = 0xFF;
  707. depthDisabledStencilDesc.StencilWriteMask = 0xFF;
  708. depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  709. depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  710. depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  711. depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  712. depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  713. depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  714. depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  715. depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  716. // Create the state using the device.
  717. result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
  718. //-------------------------------------------------
  719. // Shaders
  720. Text shader;
  721. getVertexShader( shader );
  722. vertexShader = new VertexShader();
  723. vertexShader->setShaderCode( &shader );
  724. vertexShader->compile( d3d11Device, "TextureVertexShader", "5_0" );
  725. getPixelShader( shader );
  726. pixelShader = new PixelShader();
  727. pixelShader->setShaderCode( &shader );
  728. pixelShader->compile( d3d11Device, "TexturePixelShader", "5_0" );
  729. D3D11_INPUT_ELEMENT_DESC polygonLayout[ 3 ];
  730. // Create the vertex input layout description.
  731. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
  732. polygonLayout[ 0 ].SemanticName = "POSITION";
  733. polygonLayout[ 0 ].SemanticIndex = 0;
  734. polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  735. polygonLayout[ 0 ].InputSlot = 0;
  736. polygonLayout[ 0 ].AlignedByteOffset = 0;
  737. polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  738. polygonLayout[ 0 ].InstanceDataStepRate = 0;
  739. polygonLayout[ 1 ].SemanticName = "TEXCOORD";
  740. polygonLayout[ 1 ].SemanticIndex = 0;
  741. polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
  742. polygonLayout[ 1 ].InputSlot = 0;
  743. polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  744. polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  745. polygonLayout[ 1 ].InstanceDataStepRate = 0;
  746. polygonLayout[ 2 ].SemanticName = "KNOCHEN_ID";
  747. polygonLayout[ 2 ].SemanticIndex = 0;
  748. polygonLayout[ 2 ].Format = DXGI_FORMAT_R32_UINT;
  749. polygonLayout[ 2 ].InputSlot = 0;
  750. polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  751. polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  752. polygonLayout[ 2 ].InstanceDataStepRate = 0;
  753. vertexShader->erstelleInputLayout( d3d11Device, polygonLayout, 3 );
  754. vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * 128, 0 );
  755. // Create a texture sampler state description.
  756. D3D11_SAMPLER_DESC samplerDesc;
  757. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  758. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  759. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  760. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  761. samplerDesc.MipLODBias = 0.0f;
  762. samplerDesc.MaxAnisotropy = 1;
  763. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  764. samplerDesc.BorderColor[ 0 ] = 0;
  765. samplerDesc.BorderColor[ 1 ] = 0;
  766. samplerDesc.BorderColor[ 2 ] = 0;
  767. samplerDesc.BorderColor[ 3 ] = 0;
  768. samplerDesc.MinLOD = 0;
  769. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  770. // Create the texture sampler state.
  771. result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
  772. //---------------------------------------------------------------
  773. // Framework Backbuffer Texture
  774. frameworkTextur = new Textur();
  775. frameworkTextur->setBildZ( renderB->getThis() );
  776. texturRegister->addTextur( frameworkTextur->getThis(), "f_Render_Bild" );
  777. texturModel->setGröße( backBufferGröße );
  778. texturModel->setTextur( frameworkTextur->getId() );
  779. D3D11_BLEND_DESC blendState;
  780. ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
  781. blendState.AlphaToCoverageEnable = false;
  782. blendState.IndependentBlendEnable = false;
  783. blendState.RenderTarget[ 0 ].BlendEnable = true;
  784. blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  785. blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  786. blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
  787. blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
  788. blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
  789. blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  790. blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  791. d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
  792. d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
  793. // Setup Render Objekt
  794. if( renderObj )
  795. renderObj->release();
  796. renderObj = new Render3D();
  797. d3d11Device->AddRef();
  798. renderObj->setDevice( d3d11Device );
  799. d3d11Context->AddRef();
  800. renderObj->setContext( d3d11Context );
  801. renderObj->benutzeShader( VERTEX, vertexShader->getThis() );
  802. d3d11Context->PSSetSamplers( 0, 1, &sampleState );
  803. renderObj->benutzeShader( PIXEL, pixelShader->getThis() );
  804. rend = 1;
  805. unlock();
  806. }
  807. void Bildschirm3D::tick( double tickval )
  808. {
  809. lock();
  810. __super::tick( tickval );
  811. for( auto i = kameras->getArray(); i.set; i++ )
  812. rend3D |= i.var->tick( tickval );
  813. rend3D |= texturModel->tick( tickval );
  814. unlock();
  815. }
  816. void Bildschirm3D::doMausEreignis( MausEreignis &me ) // sendet maus Ereignis
  817. {
  818. lock();
  819. __super::doMausEreignis( me );
  820. for( int i = kameras->getEintragAnzahl() - 1; i >= 0; i-- )
  821. kameras->z( i )->doMausEreignis( me );
  822. unlock();
  823. }
  824. void Bildschirm3D::doTastaturEreignis( TastaturEreignis &te ) // sendet tastatur Ereignis
  825. {
  826. lock();
  827. __super::doTastaturEreignis( te );
  828. for( int i = kameras->getEintragAnzahl() - 1; i >= 0; i-- )
  829. kameras->z( i )->doTastaturEreignis( te );
  830. unlock();
  831. }
  832. void Bildschirm3D::render() // Zeichnet das Bild
  833. {
  834. if( !rend && !rend3D && testRend )
  835. return;
  836. rendering = 1;
  837. int count = 0;
  838. if( renderB && d3d11Device )
  839. {
  840. lock();
  841. renderZeit->messungStart();
  842. float color[ 4 ];
  843. // Setup the color to clear the buffer to.
  844. color[ 0 ] = ( ( füllFarbe >> 16 ) & 0xFF ) / 255.f; // R
  845. color[ 1 ] = ( ( füllFarbe >> 8 ) & 0xFF ) / 255.f; // G
  846. color[ 2 ] = ( füllFarbe & 0xFF ) / 255.f; // B
  847. color[ 3 ] = ( ( füllFarbe >> 24 ) & 0xFF ) / 255.f; // A
  848. // Clear the back buffer.
  849. if( rend3D || !testRend || rend )
  850. {
  851. d3d11Context->ClearRenderTargetView( rtview, color );
  852. // Clear the depth buffer.
  853. d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
  854. // Bind the render target view and depth stencil buffer to the output render pipeline.
  855. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  856. // Set the depth stencil state.
  857. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  858. for( auto i = kameras->getArray(); i.set; i++ )
  859. i.var->render( renderObj );
  860. }
  861. // Set the depth stencil state.
  862. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
  863. if( rend || !testRend )
  864. {
  865. if( füll )
  866. renderB->setFarbe( füllFarbe );
  867. if( renderZeichnungen )
  868. {
  869. if( renderOnTop && deckFarbe && ( deckFarbe & ( füllFarbe | 0xFF000000 ) ) == deckFarbe )
  870. {
  871. renderB->setAlpha( 255 - (unsigned char)( deckFarbe >> 24 ) );
  872. members->render( *renderB ); // zeichnen nach zwischenbuffer
  873. renderB->releaseAlpha();
  874. }
  875. else
  876. {
  877. members->render( *renderB ); // zeichnen nach zwischenbuffer
  878. if( renderOnTop && deckFarbe )
  879. renderB->alphaRegion( 0, 0, renderB->getBreite(), renderB->getHöhe(), deckFarbe );
  880. }
  881. for( int i = 0; i < tipAnzahl; ++i )
  882. tips->z( i )->render( *renderB );
  883. }
  884. if( renderOnTop && onTop )
  885. onTop->render( *renderB );
  886. }
  887. unlock();
  888. // Beginne Bild
  889. HRESULT result;
  890. if( rend || !testRend )
  891. frameworkTextur->updateTextur( renderObj );
  892. d3d11Context->RSSetViewports( 1, vp );
  893. float screenAspect = (float)backBufferGröße.x / (float)backBufferGröße.y;
  894. Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferGröße.y * 1.2075f ) );
  895. renderObj->setKameraMatrix( view, view.projektion( DirectX::XM_PI / 4.0f, screenAspect, 0.1f, 10000.f ), Vec3< float >( 0.f, 0.f, backBufferGröße.y * 1.2075f ) );
  896. texturModel->render( renderObj );
  897. result = d3d11SpawChain->Present( 0, 0 );
  898. renderZeit->messungEnde();
  899. #ifdef _DEBUG
  900. std::cout << renderZeit->getSekunden() << "\n";
  901. #endif
  902. if( result != S_OK )
  903. {
  904. ++count;
  905. update();
  906. }
  907. else if( count )
  908. --count;
  909. }
  910. if( !d3d11Device )
  911. {
  912. ++count;
  913. update();
  914. }
  915. if( count > 10 )
  916. {
  917. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  918. count = 0;
  919. }
  920. rendering = 0;
  921. rend = 0;
  922. }
  923. // Reference Counting
  924. Bildschirm *Bildschirm3D::release()
  925. {
  926. --ref;
  927. if( !ref )
  928. delete this;
  929. return 0;
  930. }