beim nachfolgenden Code ist lediglich pVSPerObject
in der Zeile:
Code: Alles auswählen
// Matrizen
D3DXMatrixTranspose( &pVSPerObject->m_mWorldViewProjection, &mWorldViewProjection);
D3DXMatrixTranspose( &pVSPerObject->m_mWorld, &mWorld);
Code: Alles auswählen
//--------------------------------------------------------------------------------------
// File: EmptyProject11.cpp
//
// Empty starting point for new Direct3D Direct3D 11 applications
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Edit by Timo Becker
//--------------------------------------------------------------------------------------
#include "DXUT.h"
#include "SDKmisc.h"
#include "DXUTgui.h"
#include "DXUTsettingsdlg.h"
#include "DXUTcamera.h"
#include "SDKmesh.h"
// Globale Variablen
ID3D11VertexShader* g_pVertexShader11 = NULL;
ID3D11PixelShader* g_pPixelShader11 = NULL;
ID3D11Buffer* g_pcbVSPerObject11 = NULL;
ID3D11Buffer* g_pcbVSPerFrame11 = NULL;
ID3D11InputLayout* g_pLayout11 = NULL;
CModelViewerCamera g_Camera;
//--------------------------------------------------------------------------------------
// Constant-Buffers
//--------------------------------------------------------------------------------------
// Per-Object-Buffer fasst alle Informationen zusammen, die für jedes Objekt in der Szene neu berechnet werden müssen
#pragma pack(push, 1)
struct CB_VS_PER_OBJECT
{
D3DXMATRIX m_mWorldViewProjection;
D3DXMATRIX m_mWorld;
D3DXVECTOR4 m_MaterialAmbientColor;
D3DXVECTOR4 MaterialDiffuseColor;
};
// Per-Frame-Buffer muss nur einmal in jedem Frame übergeben werden
struct CB_VS_PER_FRAME
{
D3DXVECTOR3 m_vLightDir;
float m_fTime;
D3DXVECTOR4 m_LightDiffuse;
};
#pragma pack(pop)
//--------------------------------------------------------------------------------------
// Reject any D3D11 devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output, const CD3D11EnumDeviceInfo *DeviceInfo,
DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
return true;
}
//--------------------------------------------------------------------------------------
// Called right before creating a D3D9 or D3D11 device, allowing the app to modify the device settings as needed
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
{
return true;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that aren't dependant on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
HRESULT hr;
// Kompilieren der Shader --------------------
// --
WCHAR str[MAX_PATH];
DXUTFindDXSDKMediaFileCch(str, MAX_PATH, L"SimpleSample.hlsl");
DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined(DEBUG) || defined(_DEBUG)
dwShaderFlags |= D3D10_SHADER_DEBUG;
#endif
ID3DBlob* pVertexShaderBuffer = NULL;
D3DX11CompileFromFile(str, NULL, NULL, "RenderSceneVS", "vs_4_0_level_9_1", dwShaderFlags, 0, NULL, &pVertexShaderBuffer, NULL, NULL);
ID3DBlob* pPixelShaderBuffer = NULL;
D3DX11CompileFromFile(str, NULL, NULL, "RenderScenePS", "ps_4_0_level_9_1", dwShaderFlags, 0, NULL, &pPixelShaderBuffer, NULL, NULL);
pd3dDevice->CreateVertexShader(pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader11);
pd3dDevice->CreatePixelShader(pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader11);
// --
// -------------------------------------------
// Anlegen der Konstantenbuffer --------------
// --
D3D11_BUFFER_DESC cbDesc;
ZeroMemory(&cbDesc, sizeof(cbDesc));
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.ByteWidth = sizeof(CB_VS_PER_OBJECT);
pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pcbVSPerObject11);
cbDesc.ByteWidth = sizeof(CB_VS_PER_FRAME);
pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pcbVSPerFrame11);
// --
// -------------------------------------------
// View-Matrix der Kamera initialisieren -----
// --
D3DXVECTOR3 vecEye(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 vecAt (0.0f, 0.0f, -0.0f);
g_Camera.SetViewParams(&vecEye, &vecAt);
// --
// -------------------------------------------
// Anlegen eines Vertexlayouts ---------------
// --
const D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL" , 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
V_RETURN(pd3dDevice->CreateInputLayout(layout, ARRAYSIZE(layout),
pVertexShaderBuffer->GetBufferPointer(),
pVertexShaderBuffer->GetBufferSize(),
&g_pLayout11));
// --
// -------------------------------------------
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
// Einstellen der Projektionsparameter für die Kamera -----
// --
float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
g_Camera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f);
g_Camera.SetWindow(pBackBufferSurfaceDesc->Width,pBackBufferSurfaceDesc->Height);
g_Camera.SetButtonMasks(MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON);
// --
// --------------------------------------------------------
return S_OK;
}
//--------------------------------------------------------------------------------------
// Handle updates to the scene. This is called regardless of which D3D API is used
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
// Aktualisierung der Kamera -----
// --
g_Camera.FrameMove(fElapsedTime);
// --
// -------------------------------
}
//--------------------------------------------------------------------------------------
// Render the scene using the D3D11 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext,
double fTime, float fElapsedTime, void* pUserContext )
{
// Clear render target and the depth stencil
float ClearColor[4] = { 0.3f, 0.2f, 0.8f, 0.0f };
ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor );
pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );
pd3dImmediateContext->IASetInputLayout( g_pLayout11 );
// Get the projection & view matrix from the camera class
D3DXMATRIX mWorld = *g_Camera.GetWorldMatrix();
D3DXMATRIX mView = *g_Camera.GetViewMatrix();
D3DXMATRIX mProj = *g_Camera.GetProjMatrix();
D3DXMATRIX mWorldViewProjection = mWorld * mView * mProj;
HRESULT hr;
D3D11_MAPPED_SUBRESOURCE MappedResource;
V(pd3dImmediateContext->Map(g_pcbVSPerObject11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
CB_VS_PER_OBJECT* pVSPerObject11 = (CB_VS_PER_OBJECT*)MappedResource.pData;
// Matrizen
D3DXMatrixTranspose( &pVSPerObject->m_mWorldViewProjection, &mWorldViewProjection);
D3DXMatrixTranspose( &pVSPerObject->m_mWorld, &mWorld);
// Material
pVSPerObject->m_MaterialAmbientColor = D3DXVECTOR4(0.3f, 0.3f, 0.3f, 1.0f);
pVSPerObject->m_MaterialDiffuseColor = D3DXVECTOR4(0.7f, 0.7f, 0.7f, 1.0f);
pd3dImmediateContext->Unmap(g_pcbVSPerObject11, 0);
// Beleuchtung
pd3dImmediateContext->Map(g_pcbVSPerFrame11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
CB_VS_PER_FRAME* pVSPerFrame = (CB_VS_PER_FRAME*)MappedResource.pData;
pVSPerFrame->m_vLightDir = D3DXVECTOR3(0, 0.707f, -0.707f);
pVSPerFrame->m_LightDiffuse = D3DXVECTOR4(1.f, 1.f, 1.f, 1.f);
pd3dImmediateContext->Unmap(g_pcbVSPerFrame11, 0);
pd3dImmediateContext->VSSetConstantBuffers(0, 1, &g_pcbVSPerObject11);
pd3dImmediateContext->VSSetConstantBuffers(1, 1, &g_pcbVSPerFrame11);
// Übergeben der Shader an den Device Context
pd3dImmediateContext->VSSetShader(g_pVertexShader11, NULL, 0);
pd3dImmediateContext->PSSetShader(g_pPixelShader11, NULL, 0);
}
//--------------------------------------------------------------------------------------
// Release D3D11 resources created in OnD3D11ResizedSwapChain
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Release D3D11 resources created in OnD3D11CreateDevice
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11DestroyDevice( void* pUserContext )
{
SAFE_RELEASE(g_pVertexShader11);
SAFE_RELEASE(g_pPixelShader11);
//...
SAFE_RELEASE(g_pLayout11);
SAFE_RELEASE(g_pcbVSPerObject11);
SAFE_RELEASE(g_pcbVSPerFrame11);
}
//--------------------------------------------------------------------------------------
// Handle messages to the application
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
bool* pbNoFurtherProcessing, void* pUserContext )
{
// Kamera Gelegenheit geben, auf Tastatur und Mausnachrichten zu reagieren ---------
// --
g_Camera.HandleMessages(hWnd, uMsg, wParam, lParam);
// --
// ---------------------------------------------------------------------------------
return 0;
}
//--------------------------------------------------------------------------------------
// Handle key presses
//--------------------------------------------------------------------------------------
void CALLBACK OnKeyboard( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Handle mouse button presses
//--------------------------------------------------------------------------------------
void CALLBACK OnMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown,
bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta,
int xPos, int yPos, void* pUserContext )
{
}
//--------------------------------------------------------------------------------------
// Call if device was removed. Return true to find a new device, false to quit
//--------------------------------------------------------------------------------------
bool CALLBACK OnDeviceRemoved( void* pUserContext )
{
return true;
}
//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
// DXUT will create and use the best device (either D3D9 or D3D11)
// that is available on the system depending on which D3D callbacks are set below
// Set general DXUT callbacks
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCallbackKeyboard( OnKeyboard );
DXUTSetCallbackMouse( OnMouse );
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
DXUTSetCallbackDeviceRemoved( OnDeviceRemoved );
// Set the D3D11 DXUT callbacks. Remove these sets if the app doesn't need to support D3D11
DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
// Perform any application-level initialization here
DXUTInit( true, true, NULL ); // Parse the command line, show msgboxes on error, no extra command line params
DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
DXUTCreateWindow( L"Timos little World" );
// Only require 10-level hardware
DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 800, 600 );
DXUTMainLoop(); // Enter into the DXUT ren der loop
// Perform any application-level cleanup here
return DXUTGetExitCode();
}