Bildschirm.cpp 30 KB

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