Bildschirm.cpp 30 KB

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