Bildschirm.cpp 31 KB

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