#include "GraphicsApi.h" #include "Fenster.h" #include "Bild.h" #include "TexturModel.h" #include "Textur.h" #include "Shader.h" #include "Globals.h" #include "UIVertexShader.h" #include "UIPixelShader.h" #include "Render3D.h" #include "TexturList.h" #include "Kam3D.h" #include "DLLRegister.h" #include #include using namespace Framework; GraphicsApi::GraphicsApi( GraphicApiType typ ) { this->typ = typ; fenster = 0; backBufferSize = Vec2( 0, 0 ); fullScreen = 0; ref = 1; } GraphicsApi::~GraphicsApi() { if( fenster ) fenster->release(); } void GraphicsApi::initialize( WFenster * fenster, Vec2 backBufferSize, bool fullScreen ) { if( this->fenster ) this->fenster->release(); this->fenster = fenster; this->backBufferSize = backBufferSize; this->fullScreen = fullScreen; } void GraphicsApi::setBackBufferSize( Vec2< int > size ) { backBufferSize = size; update(); } void GraphicsApi::setFullScreen( bool fullScreen ) { this->fullScreen = fullScreen; update(); } void GraphicsApi::beginFrame( bool fill2D, bool fill3D, int fillColor ) {} void GraphicsApi::renderKamera( Kam3D *zKamera ) {} GraphicApiType GraphicsApi::getTyp() const { return typ; } Vec2< int > GraphicsApi::getBackBufferSize() const { return backBufferSize; } bool GraphicsApi::isFullScreen() const { return fullScreen; } GraphicsApi *GraphicsApi::getThis() { ref++; return this; } GraphicsApi *GraphicsApi::release() { if( !--ref ) delete this; return 0; } DirectX9::DirectX9() : GraphicsApi( DIRECTX9 ), pDirect3D( 0 ), pDevice( 0 ), pBackBuffer( 0 ), backRect( new D3DLOCKED_RECT() ) { uiBild = new Bild( 1 ); } DirectX9::~DirectX9() { backRect->pBits = NULL; if( pBackBuffer ) { pBackBuffer->Release(); pBackBuffer = NULL; } if( pDevice ) { pDevice->Release(); pDevice = NULL; } if( pDirect3D ) { pDirect3D->Release(); pDirect3D = NULL; } uiBild->release(); } typedef IDirect3D9 *( *D3D9CreateFunction )( UINT ); void DirectX9::initialize( WFenster * fenster, Vec2 backBufferSize, bool fullScreen ) { if( pDirect3D ) return GraphicsApi::initialize( fenster, backBufferSize, fullScreen ); GraphicsApi::initialize( fenster, backBufferSize, fullScreen ); HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d9.dll", "d3d9.dll" ); D3D9CreateFunction direct3DCreate9 = (D3D9CreateFunction)GetProcAddress( dll, "Direct3DCreate9" ); pDirect3D = direct3DCreate9( D3D_SDK_VERSION ); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.Windowed = !fullScreen; 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; uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 ); HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(), D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice ); if( result != S_OK ) WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR ); if( pDevice ) result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer ); if( result != S_OK ) WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR ); } void DirectX9::update() { if( !pDirect3D ) return; backRect->pBits = NULL; if( pBackBuffer ) { pBackBuffer->Release(); pBackBuffer = NULL; } if( pDevice ) { pDevice->Release(); pDevice = NULL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.Windowed = !fullScreen; 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; uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 ); HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(), D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice ); if( result != S_OK ) WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR ); if( pDevice ) result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer ); if( result != S_OK ) WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR ); } void DirectX9::beginFrame( bool fill2D, bool fill3D, int fillColor ) { if( fill2D ) uiBild->setFarbe( fillColor ); } void DirectX9::presentFrame() { if( !uiBild->getBuffer() ) return; HRESULT result; result = pBackBuffer->LockRect( backRect, 0, 0 ); if( result != S_OK ) { WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR ); update(); } // kopieren zum Bildschrirm int *bgBuff = uiBild->getBuffer(); int tmpBr = sizeof( D3DCOLOR ) * uiBild->getBreite(); for( int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight(); ++y, pitch += backRect->Pitch, bry += uiBild->getBreite() ) memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr ); // Beende Bild result = pBackBuffer->UnlockRect(); if( result != S_OK ) { WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR ); update(); } if( result != S_OK ) { WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR ); update(); } result = pDevice->Present( 0, 0, 0, 0 ); if( result != S_OK ) { WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR ); update(); } } Bild *DirectX9::zUIRenderBild() const { return uiBild; } DirectX11::DirectX11() : GraphicsApi( DIRECTX11 ), d3d11Device( 0 ), d3d11Context( 0 ), d3d11SpawChain( 0 ), uiTextur( 0 ), vertexShader( 0 ), pixelShader( 0 ), sampleState( 0 ), rtview( 0 ), dsView( 0 ), depthStencilBuffer( 0 ), depthStencilState( 0 ), depthDisabledStencilState( 0 ), blendStateAlphaBlend( 0 ), vp( 0 ), texturModel( new TexturModel() ), renderObj( new Render3D() ), texturRegister( new TexturList() ) {} DirectX11::~DirectX11() { if( renderObj ) { renderObj->release(); renderObj = 0; } texturModel->release(); texturRegister->release(); if( blendStateAlphaBlend ) { blendStateAlphaBlend->Release(); blendStateAlphaBlend = NULL; } if( uiTextur ) { uiTextur->release(); uiTextur = 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; } } void DirectX11::initialize( WFenster *fenster, Vec2 backBufferSize, bool fullScreen ) { if( d3d11Device ) return GraphicsApi::initialize( fenster, backBufferSize, fullScreen ); GraphicsApi::initialize( fenster, backBufferSize, fullScreen ); //-------------------------------------------------------------------- // 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 = !fullScreen; 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; 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 HRESULT result = D3D11CreateDeviceAndSwapChain( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, flag, &featureLevel, 1, D3D11_SDK_VERSION, &scd, &d3d11SpawChain, &d3d11Device, &support, &d3d11Context ); if( result != S_OK ) { std::cout << "ERROR: D3D11CreateDeviceAndSwapChain returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } ID3D11Texture2D *backBufferPtr; // Get the pointer to the back buffer. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID *)& backBufferPtr ); if( result != S_OK ) { std::cout << "ERROR: d3d11SpawChain->GetBuffer returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } // Create the render target view with the back buffer pointer. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateRenderTargetView returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } // 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 ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateTexture2D returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } // 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 ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } 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 ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateDepthStencilView returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } 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 ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } //------------------------------------------------- // Shaders vertexShader = new VertexShader(); vertexShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIVertexShader, sizeof( UIVertexShader ) ); pixelShader = new PixelShader(); pixelShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIPixelShader, sizeof( UIPixelShader ) ); D3D11_INPUT_ELEMENT_DESC polygonLayout[ 4 ]; // 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 = "NORMAL"; polygonLayout[ 2 ].SemanticIndex = 0; polygonLayout[ 2 ].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[ 2 ].InputSlot = 0; polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[ 2 ].InstanceDataStepRate = 0; polygonLayout[ 3 ].SemanticName = "KNOCHEN_ID"; polygonLayout[ 3 ].SemanticIndex = 0; polygonLayout[ 3 ].Format = DXGI_FORMAT_R32_UINT; polygonLayout[ 3 ].InputSlot = 0; polygonLayout[ 3 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[ 3 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[ 3 ].InstanceDataStepRate = 0; vertexShader->erstelleInputLayout( d3d11Device, polygonLayout, 4 ); vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 ); // matrizen für skelett annimationen vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * 2, 1 ); // View and Projection Matrix pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 0 ); // Kamera Position pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model pixelShader->erstelleConstBuffer( d3d11Device, sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model // TODO: Remove Following Test Code int lc[] = { 1, 6 }; pixelShader->füllConstBuffer( d3d11Context, (char *)lc, 2, sizeof( int ) * 2 ); // 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 ); if( result != S_OK ) { std::cout << "ERROR: d3d11Device->CreateSamplerState returned " << result << "\n"; WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR ); return; } //--------------------------------------------------------------- // Framework Backbuffer Texture Bild *renderB = new Bild( 1 ); renderB->setAlpha3D( 1 ); renderB->neuBild( backBufferSize.x, backBufferSize.y, 0 ); uiTextur = new Textur(); uiTextur->setBildZ( renderB ); texturRegister->addTextur( uiTextur->getThis(), "f_Render_Bild" ); texturModel->setSize( backBufferSize ); texturModel->setTextur( uiTextur->getThis() ); 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() ); } void DirectX11::update() { if( renderObj ) { renderObj->release(); renderObj = 0; } texturModel->release(); texturRegister->leeren(); if( blendStateAlphaBlend ) { blendStateAlphaBlend->Release(); blendStateAlphaBlend = NULL; } if( uiTextur ) { uiTextur->release(); uiTextur = 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; } initialize( fenster->getThis(), backBufferSize, fullScreen ); } void DirectX11::beginFrame( bool fill2D, bool fill3D, int fillColor ) { if( fill2D ) uiTextur->zBild()->setFarbe( fillColor ); if( fill3D ) { float color[ 4 ]; // Setup the color to clear the buffer. 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 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 ); } void DirectX11::renderKamera( Kam3D *zKamera ) { zKamera->render( renderObj ); } void DirectX11::presentFrame() { // Set the depth stencil state. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 ); uiTextur->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( (float)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 ); HRESULT result = d3d11SpawChain->Present( 0, 0 ); if( !SUCCEEDED( result ) ) { update(); WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR ); } } Bild *DirectX11::zUIRenderBild() const { return uiTextur->zBild(); }