Bildschirm.cpp 32 KB

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