Bildschirm.cpp 32 KB

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