Bildschirm.cpp 31 KB

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