1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- #include "Bildschirm.h"
- #include "Bild.h"
- #include "Fenster.h"
- #include "Text.h"
- #include "Zeichnung.h"
- #include "Globals.h"
- #include "Zeit.h"
- #include "ToolTip.h"
- #include "MausEreignis.h"
- #include <iostream>
- #include "Datei.h"
- #include "Zeichnung3D.h"
- #include "Mat3.h"
- #include "Model3D.h"
- #include "Textur.h"
- #include "TexturModel.h"
- #include "TexturList.h"
- #ifdef WIN32
- #include "Kam3D.h"
- #include "DefaultShader.h"
- #include <DirectXMath.h>
- #include <D3Dcompiler.h>
- #include <d3d11.h>
- #include <d3d9.h>
- #include "comdef.h"
- #include "DXBuffer.h"
- #include "Shader.h"
- #include "Render3D.h"
- #endif
- using namespace Framework;
- // Inhalt der Bildschirmklass aus Bildschirm.h
- // Konstruktor
- Bildschirm::Bildschirm( WFenster *f )
- : fenster( f ),
- renderB( new Bild( 1 ) ),
- ref( 1 ),
- members( new RCArray<Zeichnung>() ),
- fillColor( 0xFF000000 ),
- deckFarbe( 0 ),
- onTop( 0 ),
- renderOnTop( 0 ),
- renderZeichnungen( 1 ),
- vollbild( 0 ),
- rendering( 0 ),
- renderZeit( new ZeitMesser() ),
- backBufferSize( 0, 0 ),
- tips( new RCArray< ToolTip >() ),
- tipAnzahl( 0 ),
- testRend( 1 ),
- fill( 1 ),
- rend( 0 )
- {
- }
- // Destruktor
- Bildschirm::~Bildschirm()
- {
- lock();
- if( renderB )
- renderB->release();
- #ifdef WIN32
- if( fenster )
- fenster->release();
- #endif
- members->release();
- tipAnzahl = 0;
- tips->release();
- renderZeit->release();
- if( onTop )
- onTop->release();
- unlock();
- }
- // nicht konstant
- void Bildschirm::lock()
- {
- cs.lock();
- }
- void Bildschirm::unlock()
- {
- cs.unlock();
- }
- void Bildschirm::setFill( bool f )
- {
- fill = f;
- }
- void Bildschirm::setTestRend( bool tr ) // legt fest, ob vo rendern auf updates geprüft werden soll
- {
- testRend = tr;
- }
- void Bildschirm::setRenderZeichnungen( bool rO ) // legt fest, ob die Zeichnunge gerendert werden
- {
- lock();
- renderZeichnungen = rO;
- rend = 1;
- unlock();
- }
- void Bildschirm::setOnTop( bool onTop ) // legt fest, ob das onTop Zeichnung gerendert wid
- {
- renderOnTop = onTop;
- rend = 1;
- }
- void Bildschirm::setOnTopZeichnung( Zeichnung *obj ) // setzt das OnTop Zeichnung
- {
- lock();
- if( onTop )
- onTop->release();
- onTop = obj;
- rend = 1;
- unlock();
- }
- void Bildschirm::setdeckFarbe( int f ) // setzt die deckFarbe
- {
- deckFarbe = f;
- rend = 1;
- }
- void Bildschirm::addMember( Zeichnung *obj ) // Fügt ein Zeichnung hinzu
- {
- lock();
- members->add( obj );
- rend = 1;
- unlock();
- }
- void Bildschirm::removeMember( Zeichnung *zObj ) // Entfernt ein Zeichnung
- {
- lock();
- for( int i = 0; i < members->getEintragAnzahl(); i++ )
- {
- if( members->z( i ) == zObj )
- members->remove( i );
- }
- rend = 1;
- unlock();
- }
- void Bildschirm::setFillFarbe( int f ) // setzt die Fill Farbe
- {
- fillColor = f;
- rend = 1;
- }
- void Bildschirm::setVollbild( bool vollbild ) // setzt vollbild
- {
- lock();
- this->vollbild = vollbild;
- rend = 1;
- unlock();
- }
- void Bildschirm::tick( double tickval )
- {
- lock();
- if( !renderOnTop )
- {
- for( int i = 0; i < tipAnzahl; ++i )
- rend |= tips->z( i )->tick( tickval );
- for( auto i = members->getIterator(); i; i++ )
- rend |= i->tick( tickval );
- }
- else if( onTop )
- {
- rend |= onTop->tick( tickval );
- for( int i = 0; i < tipAnzahl; ++i )
- rend |= tips->z( i )->tick( tickval );
- }
- unlock();
- }
- void Bildschirm::setBackBufferSize( int breite, int height ) // setzt die Größe des Backbuffers
- {
- lock();
- backBufferSize.x = breite;
- backBufferSize.y = height;
- rend = 1;
- unlock();
- }
- void Bildschirm::setBackBufferSize( Punkt &size )
- {
- lock();
- backBufferSize = size;
- rend = 1;
- unlock();
- }
- void Bildschirm::doMausEreignis( MausEreignis &me ) // sendet maus Ereignis
- {
- int fBr = backBufferSize.x;
- int fHi = backBufferSize.y;
- #ifdef WIN32
- if( fenster )
- {
- fBr = fenster->getKörperBreite();
- fHi = fenster->getKörperHöhe();
- }
- #endif
- me.mx = (int)( me.mx * backBufferSize.x / (double)fBr + 0.5 );
- me.my = (int)( me.my * backBufferSize.y / (double)fHi + 0.5 );
- lock();
- if( !renderOnTop )
- {
- for( int i = 0; i < tipAnzahl; ++i )
- tips->z( i )->doMausEreignis( me );
- for( int i = members->getEintragAnzahl() - 1; i >= 0; i-- )
- members->z( i )->doMausEreignis( me );
- }
- else if( onTop )
- {
- onTop->doMausEreignis( me );
- for( int i = 0; i < tipAnzahl; ++i )
- tips->z( i )->doMausEreignis( me );
- }
- unlock();
- }
- void Bildschirm::doTastaturEreignis( TastaturEreignis &te ) // sendet tastatur Ereignis
- {
- lock();
- if( !renderOnTop )
- {
- for( int i = members->getEintragAnzahl() - 1; i >= 0; i-- )
- members->z( i )->doTastaturEreignis( te );
- }
- else if( onTop )
- onTop->doTastaturEreignis( te );
- unlock();
- }
- void Bildschirm::addToolTip( ToolTip *tip ) // fügt ToolTip hinzu
- {
- lock();
- tips->add( tip, tipAnzahl );
- ++tipAnzahl;
- rend = 1;
- unlock();
- }
- bool Bildschirm::removeToolTip( ToolTip *zTip ) // entfernt ToolTip
- {
- lock();
- bool gefunden = 0;
- for( int i = 0; i < tipAnzahl; ++i )
- {
- ToolTip *tmp = tips->z( i );
- if( tmp == zTip )
- {
- tips->remove( i );
- --tipAnzahl;
- gefunden = 1;
- rend = 1;
- break;
- }
- }
- unlock();
- return gefunden;
- }
- // constant
- Bild *Bildschirm::getRenderBild() const
- {
- return renderB->getThis();
- }
- Bild *Bildschirm::zRenderBild() const
- {
- return renderB;
- }
- Iterator<Zeichnung*> Bildschirm::getMembers() const // gibt die Zeichnunge zurück
- {
- return members->getIterator();
- }
- int Bildschirm::getFillFarbe() const // gibt die Füll Farbe zurück
- {
- return fillColor;
- }
- bool Bildschirm::istVolbild() const // gibt zurück, ob vollbild an ist
- {
- return vollbild;
- }
- const Punkt &Bildschirm::getBackBufferSize() const // gibt die Größe des Backbuffers zurück
- {
- return backBufferSize;
- }
- void Bildschirm::warteAufRendern() const // wartet auf die render Funktion
- {
- while( rendering )
- {
- if( !rendering )
- return;
- }
- }
- double Bildschirm::getRenderZeit() const // gibt zurück wie viele Sekunden das Rendern dauert
- {
- return renderZeit->getSekunden();
- }
- // Reference Counting
- Bildschirm *Bildschirm::getThis()
- {
- ++ref;
- return this;
- }
- Bildschirm *Bildschirm::release()
- {
- --ref;
- if( !ref )
- delete this;
- return 0;
- }
- #ifdef WIN32
- int MonitorEnum( HMONITOR m, HDC dc, LPRECT r, LPARAM p )
- {
- Monitor *mon = new Monitor();
- mon->existiert = 1;
- mon->x = r->left;
- mon->y = r->top;
- mon->breite = r->right - r->left;
- mon->height = r->bottom - r->top;
- ( ( Array< Monitor* >* )p )->add( mon );
- return 1;
- }
- Monitor Framework::getMonitor( int id )
- {
- if( id < 0 )
- {
- Monitor m;
- m.existiert = 0;
- return m;
- }
- Array< Monitor* > *monitore = new Array< Monitor* >();
- EnumDisplayMonitors( 0, 0, (MONITORENUMPROC)MonitorEnum, (LPARAM)monitore );
- int anz = monitore->getEintragAnzahl();
- if( id >= monitore->getEintragAnzahl() )
- {
- for( int i = 0; i < anz; ++i )
- delete monitore->get( i );
- delete monitore;
- Monitor m;
- m.existiert = 0;
- return m;
- }
- Monitor m = *monitore->get( id );
- for( int i = 0; i < anz; ++i )
- delete monitore->get( i );
- delete monitore;
- return m;
- }
- // Bildschirm2D
- // Konstruktor
- Bildschirm2D::Bildschirm2D( WFenster *fenster )
- : Bildschirm( fenster ),
- pDirect3D( 0 ),
- pDevice( 0 ),
- pBackBuffer( 0 ),
- backRect( new D3DLOCKED_RECT() )
- {}
- // Destruktor
- Bildschirm2D::~Bildschirm2D()
- {
- cleanUpDirectX();
- delete backRect;
- }
- // private
- void Bildschirm2D::cleanUpDirectX()
- {
- backRect->pBits = NULL;
- if( pDevice )
- {
- pDevice->Release();
- pDevice = NULL;
- }
- if( pDirect3D )
- {
- pDirect3D->Release();
- pDirect3D = NULL;
- }
- if( pBackBuffer )
- {
- pBackBuffer->Release();
- pBackBuffer = NULL;
- }
- }
- // nicht constant
- void Bildschirm2D::update() // aktualisiert directX
- {
- lock();
- HRESULT result;
- cleanUpDirectX();
- pDirect3D = Direct3DCreate9( D3D_SDK_VERSION );
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof( d3dpp ) );
- d3dpp.Windowed = !vollbild;
- d3dpp.hDeviceWindow = fenster->getFensterHandle();
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
- d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
- d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
- if( !backBufferSize.x || !backBufferSize.y )
- backBufferSize = fenster->getKörperGröße();
- d3dpp.BackBufferHeight = backBufferSize.y;
- d3dpp.BackBufferWidth = backBufferSize.x;
- if( renderB )
- renderB->release();
- renderB = new Bild( 1 );
- renderB->neuBild( backBufferSize.x, backBufferSize.y, fillColor );
- result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
- D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
- if( pDevice )
- result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
- rend = 1;
- unlock();
- }
- void Bildschirm2D::render() // Zeichnet das Bild
- {
- if( !rend && testRend )
- return;
- rendering = 1;
- int count = 0;
- if( renderB && pDevice )
- {
- lock();
- renderZeit->messungStart();
- if( fill )
- renderB->setFarbe( fillColor );
- if( renderZeichnungen )
- {
- if( renderOnTop && deckFarbe && ( deckFarbe & ( fillColor | 0xFF000000 ) ) == deckFarbe )
- {
- renderB->setAlpha( 255 - (unsigned char)( deckFarbe >> 24 ) );
- for( auto i = members->getIterator(); i; i++ )
- i->render( *renderB ); // zeichnen nach zwischenbuffer
- renderB->releaseAlpha();
- }
- else
- {
- for( auto i = members->getIterator(); i; i++ )
- i->render( *renderB ); // zeichnen nach zwischenbuffer
- if( renderOnTop && deckFarbe )
- renderB->alphaRegion( 0, 0, renderB->getBreite(), renderB->getHeight(), deckFarbe );
- }
- for( int i = 0; i < tipAnzahl; ++i )
- tips->z( i )->render( *renderB );
- }
- if( renderOnTop && onTop )
- onTop->render( *renderB );
- Bild *tmp = renderB->getThis();
- unlock();
- // Beginne Bild
- HRESULT result;
- if( !fillColor )
- result = pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 0.0f, 0 );
- result = pBackBuffer->LockRect( backRect, 0, 0 );
- // kopieren zum Bildschrirm
- int *bgBuff = tmp->getBuffer();
- int tmpBr = sizeof( D3DCOLOR )* tmp->getBreite();
- for( int y = 0, pitch = 0, bry = 0; y < tmp->getHeight(); ++y, pitch += backRect->Pitch, bry += tmp->getBreite() )
- memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void* )&( bgBuff[ bry ] ), tmpBr );
- // Beende Bild
- result = pBackBuffer->UnlockRect();
- tmp->release();
- result = pDevice->Present( 0, 0, 0, 0 );
- renderZeit->messungEnde();
- if( result != S_OK )
- {
- ++count;
- update();
- }
- else if( count )
- --count;
- }
- if( !pDevice )
- {
- ++count;
- update();
- }
- if( count > 10 )
- {
- WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
- count = 0;
- }
- rendering = 0;
- rend = 0;
- }
- // Bildschirm3D
- // Konstruktor
- Bildschirm3D::Bildschirm3D( WFenster *fenster )
- : Bildschirm( fenster ),
- d3d11Device( 0 ),
- d3d11Context( 0 ),
- d3d11SpawChain( 0 ),
- frameworkTextur( 0 ),
- vertexShader( 0 ),
- pixelShader( 0 ),
- sampleState( 0 ),
- rtview( 0 ),
- dsView( 0 ),
- depthStencilBuffer( 0 ),
- depthStencilState( 0 ),
- depthDisabledStencilState( 0 ),
- blendStateAlphaBlend( 0 ),
- kameras( new RCArray< Kam3D >() ),
- rend3D( 0 ),
- vp( 0 ),
- renderObj( new Render3D() ),
- texturModel( new TexturModel() )
- {}
- // Destruktor
- Bildschirm3D::~Bildschirm3D()
- {
- kameras->release();
- texturModel->release();
- cleanUpDirectX();
- }
- // private
- void Bildschirm3D::cleanUpDirectX()
- {
- if( renderObj )
- {
- renderObj->release();
- renderObj = 0;
- }
- if( blendStateAlphaBlend )
- {
- blendStateAlphaBlend->Release();
- blendStateAlphaBlend = NULL;
- }
- if( frameworkTextur )
- {
- frameworkTextur->release();
- frameworkTextur = NULL;
- }
- if( sampleState )
- {
- sampleState->Release();
- sampleState = NULL;
- }
- if( pixelShader )
- {
- pixelShader->release();
- pixelShader = NULL;
- }
- if( vertexShader )
- {
- vertexShader->release();
- vertexShader = NULL;
- }
- if( depthDisabledStencilState )
- {
- depthDisabledStencilState->Release();
- depthDisabledStencilState = NULL;
- }
- delete vp;
- vp = 0;
- if( dsView )
- {
- dsView->Release();
- dsView = NULL;
- }
- if( depthStencilState )
- {
- depthStencilState->Release();
- depthStencilState = NULL;
- }
- if( depthStencilBuffer )
- {
- depthStencilBuffer->Release();
- depthStencilBuffer = NULL;
- }
- if( rtview )
- {
- rtview->Release();
- rtview = NULL;
- }
- if( d3d11SpawChain )
- {
- d3d11SpawChain->Release();
- d3d11SpawChain = NULL;
- }
- if( d3d11Device )
- {
- d3d11Device->Release();
- d3d11Device = NULL;
- }
- if( d3d11Context )
- {
- d3d11Context->Release();
- d3d11Context = NULL;
- }
- }
- // nicht constant
- void Bildschirm3D::addKamera( Kam3D *obj ) // Fügt ein Zeichnung hinzu
- {
- lock();
- kameras->add( obj );
- rend3D = 1;
- unlock();
- }
- void Bildschirm3D::removeKamera( Kam3D *zObj ) // Entfernt ein Zeichnung
- {
- lock();
- for( int i = 0; kameras->z( i ); i++ )
- {
- if( kameras->z( i ) == zObj )
- {
- kameras->remove( i );
- break;
- }
- }
- rend3D = 1;
- unlock();
- }
- void Bildschirm3D::update() // aktualisiert directX
- {
- lock();
- HRESULT result;
- cleanUpDirectX();
- //--------------------------------------------------------------------
- // Create Device
- // create a struct to hold information about the swap chain
- DXGI_SWAP_CHAIN_DESC scd;
- // clear out the struct for use
- ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
- // fill the swap chain description struct
- scd.BufferCount = 1; // one back buffer
- scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
- scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
- scd.SampleDesc.Count = 1;
- // Set the scan line ordering and scaling to unspecified.
- scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
- scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
- scd.Windowed = !vollbild;
- if( !backBufferSize.x || !backBufferSize.y )
- backBufferSize = fenster ? fenster->getKörperGröße() : Punkt( 0, 0 );
- scd.BufferDesc.Width = backBufferSize.x;
- scd.BufferDesc.Height = backBufferSize.y; // windowed/full-screen mode
- scd.BufferDesc.RefreshRate.Denominator = 1;
- scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
- // Discard the back buffer contents after presenting.
- scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- if( renderB )
- renderB->release();
- renderB = new Bild( 1 );
- renderB->setAlpha3D( 1 );
- renderB->neuBild( backBufferSize.x, backBufferSize.y, fillColor );
- D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
- D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
- // create a device, device context and swap chain using the information in the scd struct
- UINT flag = 0;
- #ifdef _DEBUG
- flag |= D3D11_CREATE_DEVICE_DEBUG;
- #endif
- result = D3D11CreateDeviceAndSwapChain( NULL,
- D3D_DRIVER_TYPE_HARDWARE,
- NULL,
- flag,
- &featureLevel,
- 1,
- D3D11_SDK_VERSION,
- &scd,
- &d3d11SpawChain,
- &d3d11Device,
- &support,
- &d3d11Context );
- ID3D11Texture2D *backBufferPtr;
- // Get the pointer to the back buffer.
- result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&backBufferPtr );
- // Create the render target view with the back buffer pointer.
- result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
- // Release pointer to the back buffer as we no longer need it.
- backBufferPtr->Release();
- // Initialize the description of the depth buffer.
- D3D11_TEXTURE2D_DESC depthBufferDesc;
- ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
- // Set up the description of the depth buffer.
- depthBufferDesc.Width = backBufferSize.x;
- depthBufferDesc.Height = backBufferSize.y;
- depthBufferDesc.MipLevels = 1;
- depthBufferDesc.ArraySize = 1;
- depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- depthBufferDesc.SampleDesc.Count = 1;
- depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- // Create the texture for the depth buffer using the filled out description.
- result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
- // Initialize the description of the stencil state.
- D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
- ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
- // Set up the description of the stencil state.
- depthStencilDesc.DepthEnable = true;
- depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
- depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
- depthStencilDesc.StencilEnable = true;
- depthStencilDesc.StencilReadMask = 0xFF;
- depthStencilDesc.StencilWriteMask = 0xFF;
- // Stencil operations if pixel is front-facing.
- depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
- depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
- depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
- depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
- // Stencil operations if pixel is back-facing.
- depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
- depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
- depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
- depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
- // Create the depth stencil state.
- result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
- d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
- // Initialize the depth stencil view.
- D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
- ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
- // Set up the depth stencil view description.
- depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
- // Create the depth stencil view.
- result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
- d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
- vp = new D3D11_VIEWPORT();
- memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
- vp->Width = (float)backBufferSize.x;
- vp->Height = (float)backBufferSize.y;
- vp->MinDepth = 0.0f;
- vp->MaxDepth = 1.0f;
- vp->TopLeftX = 0.0f;
- vp->TopLeftY = 0.0f;
- d3d11Context->RSSetViewports( 1, vp );
- D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
- // Clear the second depth stencil state before setting the parameters.
- ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
- // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
- // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
- depthDisabledStencilDesc.DepthEnable = false;
- depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
- depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
- depthDisabledStencilDesc.StencilEnable = true;
- depthDisabledStencilDesc.StencilReadMask = 0xFF;
- depthDisabledStencilDesc.StencilWriteMask = 0xFF;
- depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
- depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
- depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
- depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
- depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
- depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
- depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
- depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
- // Create the state using the device.
- result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
- //-------------------------------------------------
- // Shaders
- Text shader;
- getVertexShader( shader );
- vertexShader = new VertexShader();
- vertexShader->setShaderCode( &shader );
- vertexShader->compile( d3d11Device, "TextureVertexShader", "5_0" );
- getPixelShader( shader );
- pixelShader = new PixelShader();
- pixelShader->setShaderCode( &shader );
- pixelShader->compile( d3d11Device, "TexturePixelShader", "5_0" );
- D3D11_INPUT_ELEMENT_DESC polygonLayout[ 3 ];
- // Create the vertex input layout description.
- // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
- polygonLayout[ 0 ].SemanticName = "POSITION";
- polygonLayout[ 0 ].SemanticIndex = 0;
- polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
- polygonLayout[ 0 ].InputSlot = 0;
- polygonLayout[ 0 ].AlignedByteOffset = 0;
- polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[ 0 ].InstanceDataStepRate = 0;
- polygonLayout[ 1 ].SemanticName = "TEXCOORD";
- polygonLayout[ 1 ].SemanticIndex = 0;
- polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
- polygonLayout[ 1 ].InputSlot = 0;
- polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
- polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[ 1 ].InstanceDataStepRate = 0;
- polygonLayout[ 2 ].SemanticName = "KNOCHEN_ID";
- polygonLayout[ 2 ].SemanticIndex = 0;
- polygonLayout[ 2 ].Format = DXGI_FORMAT_R32_UINT;
- polygonLayout[ 2 ].InputSlot = 0;
- polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
- polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
- polygonLayout[ 2 ].InstanceDataStepRate = 0;
- vertexShader->erstelleInputLayout( d3d11Device, polygonLayout, 3 );
- vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 );
- // Create a texture sampler state description.
- D3D11_SAMPLER_DESC samplerDesc;
- samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.MipLODBias = 0.0f;
- samplerDesc.MaxAnisotropy = 1;
- samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
- samplerDesc.BorderColor[ 0 ] = 0;
- samplerDesc.BorderColor[ 1 ] = 0;
- samplerDesc.BorderColor[ 2 ] = 0;
- samplerDesc.BorderColor[ 3 ] = 0;
- samplerDesc.MinLOD = 0;
- samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
- // Create the texture sampler state.
- result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
- //---------------------------------------------------------------
- // Framework Backbuffer Texture
- frameworkTextur = new Textur();
- frameworkTextur->setBildZ( renderB->getThis() );
- texturRegister->addTextur( frameworkTextur->getThis(), "f_Render_Bild" );
- texturModel->setSize( backBufferSize );
- texturModel->setTextur( frameworkTextur->getId() );
- D3D11_BLEND_DESC blendState;
- ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
- blendState.AlphaToCoverageEnable = false;
- blendState.IndependentBlendEnable = false;
- blendState.RenderTarget[ 0 ].BlendEnable = true;
- blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
- blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
- blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
- blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
- blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
- blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
- blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
- d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
- d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
- // Setup Render Objekt
- if( renderObj )
- renderObj->release();
- renderObj = new Render3D();
- d3d11Device->AddRef();
- renderObj->setDevice( d3d11Device );
- d3d11Context->AddRef();
- renderObj->setContext( d3d11Context );
- renderObj->benutzeShader( VERTEX, vertexShader->getThis() );
- d3d11Context->PSSetSamplers( 0, 1, &sampleState );
- renderObj->benutzeShader( PIXEL, pixelShader->getThis() );
- rend = 1;
- unlock();
- }
- void Bildschirm3D::tick( double tickval )
- {
- lock();
- __super::tick( tickval );
- for( auto i = kameras->getIterator(); i; i++ )
- rend3D |= i->tick( tickval );
- rend3D |= texturModel->tick( tickval );
- unlock();
- }
- void Bildschirm3D::doMausEreignis( MausEreignis &me ) // sendet maus Ereignis
- {
- lock();
- __super::doMausEreignis( me );
- for( int i = kameras->getEintragAnzahl() - 1; i >= 0; i-- )
- kameras->z( i )->doMausEreignis( me );
- unlock();
- }
- void Bildschirm3D::doTastaturEreignis( TastaturEreignis &te ) // sendet tastatur Ereignis
- {
- lock();
- __super::doTastaturEreignis( te );
- for( int i = kameras->getEintragAnzahl() - 1; i >= 0; i-- )
- kameras->z( i )->doTastaturEreignis( te );
- unlock();
- }
- void Bildschirm3D::render() // Zeichnet das Bild
- {
- if( !rend && !rend3D && testRend )
- return;
- rendering = 1;
- int count = 0;
- if( renderB && d3d11Device )
- {
- lock();
- renderZeit->messungStart();
- float color[ 4 ];
- // Setup the color to clear the buffer to.
- color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
- color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
- color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
- color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
- // Clear the back buffer.
- if( rend3D || !testRend || rend )
- {
- if( fill )
- {
- d3d11Context->ClearRenderTargetView( rtview, color );
- // Clear the depth buffer.
- d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
- }
- // Bind the render target view and depth stencil buffer to the output render pipeline.
- d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
- // Set the depth stencil state.
- d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
- for( auto i = kameras->getIterator(); i; i++ )
- i->render( renderObj );
- rend3D = 0;
- }
- // Set the depth stencil state.
- d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
- if( rend || !testRend )
- {
- if( fill )
- renderB->setFarbe( fillColor );
- if( renderZeichnungen )
- {
- if( renderOnTop && deckFarbe && ( deckFarbe & ( fillColor | 0xFF000000 ) ) == deckFarbe )
- {
- renderB->setAlpha( 255 - (unsigned char)( deckFarbe >> 24 ) );
- for( auto i = members->getIterator(); i; i++ )
- i->render( *renderB ); // zeichnen nach zwischenbuffer
- renderB->releaseAlpha();
- }
- else
- {
- for( auto i = members->getIterator(); i; i++ )
- i->render( *renderB ); // zeichnen nach zwischenbuffer
- if( renderOnTop && deckFarbe )
- renderB->alphaRegion( 0, 0, renderB->getBreite(), renderB->getHeight(), deckFarbe );
- }
- for( int i = 0; i < tipAnzahl; ++i )
- tips->z( i )->render( *renderB );
- }
- if( renderOnTop && onTop )
- onTop->render( *renderB );
- }
- // Beginne Bild
- HRESULT result;
- if( rend || !testRend )
- frameworkTextur->updateTextur( renderObj );
- d3d11Context->RSSetViewports( 1, vp );
- float screenAspect = (float)backBufferSize.x / (float)backBufferSize.y;
- Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
- 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 ) );
- if( fenster && !IsIconic( fenster->getFensterHandle() ) )
- texturModel->render( renderObj );
- unlock();
- result = d3d11SpawChain->Present( 0, 0 );
- renderZeit->messungEnde();
- #ifdef _DEBUG
- //std::cout << renderZeit->getSekunden() << "\n";
- #endif
- if( !SUCCEEDED( result ) )
- {
- ++count;
- update();
- }
- else if( count )
- --count;
- }
- if( !d3d11Device )
- {
- ++count;
- update();
- }
- if( count > 10 )
- {
- WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
- count = 0;
- }
- rendering = 0;
- rend = 0;
- }
- #endif
|