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