123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038 |
- #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 ZeichnungArray() ),
- 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
- delete members;
- tipAnzahl = 0;
- tips->release();
- renderZeit->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();
- 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->addZeichnung( obj );
- members->updateIndex( 0 );
- rend = 1;
- unlock();
- }
- void Bildschirm::removeMember( Zeichnung *obj ) // Entfernt ein Zeichnung
- {
- lock();
- members->removeZeichnung( obj );
- members->updateIndex( 0 );
- 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 );
- rend |= members->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 );
- members->sendMausAll( 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 )
- members->sendTastaturAll( 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;
- }
- ZeichnungArray *Bildschirm::getMembers() const // gibt die Zeichnunge zurück
- {
- return members;
- }
- 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 ) );
- members->render( *renderB ); // zeichnen nach zwischenbuffer
- renderB->releaseAlpha();
- }
- else
- {
- members->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;
- }
- Bildschirm *Bildschirm2D::release()
- {
- --ref;
- if( !ref )
- delete this;
- return 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 ) );
- members->render( *renderB ); // zeichnen nach zwischenbuffer
- renderB->releaseAlpha();
- }
- else
- {
- members->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;
- }
- // Reference Counting
- Bildschirm *Bildschirm3D::release()
- {
- --ref;
- if( !ref )
- delete this;
- return 0;
- }
- #endif
|