Bildschirm.cpp 30 KB

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