Created
October 23, 2022 09:14
-
-
Save kinjalkishor/2f51bfba355333ac7c83a87d50b11ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <d3d11.h> | |
#include <d3d11_4.h> | |
//#include <dxgi.h> | |
//#include <dxgi1_6.h> | |
#pragma comment(lib, "d3d11.lib") | |
#pragma comment(lib, "dxgi.lib") | |
#pragma comment(lib, "dxguid.lib") | |
// For using Windows Imaging Component (WIC) | |
#pragma comment(lib, "uuid.lib") | |
#pragma comment(lib, "d3dcompiler.lib") | |
// for compiling shader files, D3DCompileFromFile | |
#include <D3Dcompiler.h> | |
//#include <D3Dcommon.h> | |
#include <wrl.h> | |
using Microsoft::WRL::ComPtr; | |
// For RoInitializeWrapper required for CreateWICTextureFromFile | |
#pragma comment(lib, "runtimeobject.lib") | |
//===================================== | |
//#include "E:/nex/NexEngine/NexWars/source/dep/tut/d3dxlib/DirectXMath/Inc/DirectXMath.h" | |
//#include "E:/nex/NexEngine/NexWars/source/dep/tut/d3dxlib/DirectXMath/Inc/DirectXPackedVector.h" | |
//#include "E:/nex/NexEngine/NexWars/source/dep/tut/d3dxlib/DirectXMath/Inc/DirectXColors.h" | |
//#include "E:/nex/NexEngine/NexWars/source/dep/tut/d3dxlib/DirectXMath/Inc/DirectXCollision.h" | |
// For textures | |
#include "E:/nex/NexEngine/NexWars/source/code/lib_src/d3dxlib/DDSTextureLoader/DDSTextureLoader11.h" | |
#include "E:/nex/NexEngine/NexWars/source/code/lib_src/d3dxlib/WICTextureLoader/WICTextureLoader11.h" | |
// Add DDSTextureLoader11.cpp to project also | |
//------------------------------ | |
#include "base_def.h" | |
#include "base_io.h" | |
#include "base_gmath.h" | |
#include "win_sys.h" | |
#include "t2_rs_renderer.h" | |
#if 0 | |
HRESULT CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut ) | |
{ | |
HRESULT hr = S_OK; | |
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; | |
#if defined( DEBUG ) || defined( _DEBUG ) | |
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. | |
// Setting this flag improves the shader debugging experience, but still allows | |
// the shaders to be optimized and to run exactly the way they will run in | |
// the release configuration of this program. | |
dwShaderFlags |= D3DCOMPILE_DEBUG; | |
#endif | |
ID3DBlob* pErrorBlob; | |
hr = D3DX11CompileFromFile( szFileName, NULL, NULL, szEntryPoint, szShaderModel, | |
dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL ); | |
if( FAILED(hr) ) | |
{ | |
if( pErrorBlob != NULL ) | |
OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() ); | |
if( pErrorBlob ) pErrorBlob->Release(); | |
return hr; | |
} | |
if( pErrorBlob ) pErrorBlob->Release(); | |
return S_OK; | |
} | |
struct SimpleVertex | |
{ | |
XMFLOAT3 Pos; | |
}; | |
struct SimpleVertex | |
{ | |
XMFLOAT3 Pos; | |
XMFLOAT4 Color; | |
}; | |
struct SimpleVertex | |
{ | |
XMFLOAT3 Pos; | |
XMFLOAT2 Tex; | |
}; | |
struct SimpleVertex | |
{ | |
XMFLOAT3 Pos; | |
XMFLOAT3 Normal; | |
}; | |
struct ConstantBuffer | |
{ | |
XMMATRIX mWorld; | |
XMMATRIX mView; | |
XMMATRIX mProjection; | |
}; | |
// LIGHTS | |
struct ConstantBuffer | |
{ | |
XMMATRIX mWorld; | |
XMMATRIX mView; | |
XMMATRIX mProjection; | |
XMFLOAT4 vLightDir[2]; | |
XMFLOAT4 vLightColor[2]; | |
XMFLOAT4 vOutputColor; | |
}; | |
#endif | |
//--- | |
//struct CBNeverChanges | |
//{ | |
// XMMATRIX mView; | |
//}; | |
//struct CBChangeOnResize | |
//{ | |
// XMMATRIX mProjection; | |
//}; | |
//struct CBChangesEveryFrame | |
//{ | |
// XMMATRIX mWorld; | |
// XMFLOAT4 vMeshColor; | |
//}; | |
//-------------- | |
class renderer_d3d11_basic : public irenderer | |
{ | |
public: | |
ID3D11Device* g_pd3dDevice = NULL; | |
ID3D11DeviceContext* g_pImmediateContext = NULL; | |
IDXGISwapChain* g_pSwapChain = NULL; | |
ID3D11RenderTargetView* g_pRenderTargetView = NULL; | |
ID3D11VertexShader* g_pVertexShader = NULL; | |
ID3D11PixelShader* g_pPixelShader = NULL; | |
ID3D11InputLayout* g_pVertexLayout = NULL; | |
ID3D11Buffer* g_pVertexBuffer = NULL; | |
ID3D11Buffer* g_pIndexBuffer = NULL; | |
ID3D11Buffer* g_pConstantBuffer = NULL; | |
//XMMATRIX g_World; | |
//XMMATRIX g_View; | |
//XMMATRIX g_Projection; | |
ID3D11Texture2D* g_pDepthStencil = NULL; | |
ID3D11DepthStencilView* g_pDepthStencilView = NULL; | |
// 2 model animation | |
//XMMATRIX g_World1; | |
//XMMATRIX g_World2; | |
//ID3D11Buffer* g_pCBNeverChanges = NULL; | |
//ID3D11Buffer* g_pCBChangeOnResize = NULL; | |
//ID3D11Buffer* g_pCBChangesEveryFrame = NULL; | |
//XMFLOAT4 g_vMeshColor( 0.7f, 0.7f, 0.7f, 1.0f ); | |
ID3D11ShaderResourceView* g_pTextureRV = NULL; | |
ID3D11SamplerState* g_pSamplerLinear = NULL; | |
// LIGHTS: light cube | |
ID3D11PixelShader* g_pPixelShaderSolid = NULL; | |
void deinit() | |
{ | |
if( g_pImmediateContext ) g_pImmediateContext->ClearState(); | |
com_release(g_pDepthStencil); | |
com_release(g_pDepthStencilView); | |
com_release(g_pRenderTargetView); | |
com_release(g_pSwapChain); | |
com_release(g_pImmediateContext); | |
com_release(g_pd3dDevice); | |
} | |
bool init(HWND hwnd, int width, int height, bool fullscreen) | |
{ | |
set_viewport(0.0f, 0.0f, width, height); | |
if (!hwnd) { eprintln("Window Handle passed is null."); } | |
m_render_window = hwnd; | |
sys_win_title_append(m_render_window, " : Direct3D11 Basic"); | |
HRESULT hr = S_OK; | |
//RECT rc; | |
//GetClientRect( m_render_window, &rc ); | |
//UINT width = rc.right - rc.left; | |
//UINT height = rc.bottom - rc.top; | |
UINT createDeviceFlags = 0; | |
#ifdef _DEBUG | |
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; | |
#endif | |
D3D_DRIVER_TYPE driverTypes[] = | |
{ | |
D3D_DRIVER_TYPE_HARDWARE, | |
D3D_DRIVER_TYPE_WARP, | |
D3D_DRIVER_TYPE_REFERENCE, | |
}; | |
UINT numDriverTypes = ARRAYSIZE( driverTypes ); | |
D3D_DRIVER_TYPE driver_type = D3D_DRIVER_TYPE_NULL; | |
D3D_FEATURE_LEVEL featureLevels[] = | |
{ | |
D3D_FEATURE_LEVEL_11_0, | |
D3D_FEATURE_LEVEL_10_1, | |
D3D_FEATURE_LEVEL_10_0, | |
//D3D_FEATURE_LEVEL_9_3, | |
//D3D_FEATURE_LEVEL_9_2, | |
//D3D_FEATURE_LEVEL_9_1, | |
}; | |
UINT numFeatureLevels = ARRAYSIZE( featureLevels ); | |
D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; | |
DXGI_SWAP_CHAIN_DESC sd; | |
ZeroMemory( &sd, sizeof( sd ) ); | |
sd.BufferCount = 1; | |
sd.BufferDesc.Width = width; | |
sd.BufferDesc.Height = height; | |
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | |
sd.BufferDesc.RefreshRate.Numerator = 60; | |
sd.BufferDesc.RefreshRate.Denominator = 1; | |
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; | |
sd.OutputWindow = m_render_window; | |
sd.SampleDesc.Count = 1; | |
sd.SampleDesc.Quality = 0; | |
sd.Windowed = TRUE; | |
for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ) | |
{ | |
driver_type = driverTypes[driverTypeIndex]; | |
hr = D3D11CreateDeviceAndSwapChain( NULL, driver_type, NULL, createDeviceFlags, featureLevels, numFeatureLevels, | |
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &feature_level, &g_pImmediateContext ); | |
if( SUCCEEDED( hr ) ) | |
break; | |
} | |
if( FAILED( hr ) ) | |
return false; | |
// Create a render target view | |
ID3D11Texture2D* pBackBuffer = NULL; | |
hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer ); | |
if( FAILED( hr ) ) | |
return false; | |
hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView ); | |
pBackBuffer->Release(); | |
if( FAILED( hr ) ) | |
return false; | |
// Without depth stencil texture | |
//g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL ); | |
//--- | |
// Create depth stencil texture | |
D3D11_TEXTURE2D_DESC descDepth; | |
ZeroMemory( &descDepth, sizeof(descDepth) ); | |
descDepth.Width = width; | |
descDepth.Height = height; | |
descDepth.MipLevels = 1; | |
descDepth.ArraySize = 1; | |
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; | |
descDepth.SampleDesc.Count = 1; | |
descDepth.SampleDesc.Quality = 0; | |
descDepth.Usage = D3D11_USAGE_DEFAULT; | |
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; | |
descDepth.CPUAccessFlags = 0; | |
descDepth.MiscFlags = 0; | |
hr = g_pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Create the depth stencil view | |
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV; | |
ZeroMemory( &descDSV, sizeof(descDSV) ); | |
descDSV.Format = descDepth.Format; | |
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; | |
descDSV.Texture2D.MipSlice = 0; | |
hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView ); | |
if( FAILED( hr ) ) | |
return hr; | |
g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView ); | |
// Setup the viewport | |
D3D11_VIEWPORT vp = {}; | |
vp.Width = (FLOAT)width; | |
vp.Height = (FLOAT)height; | |
vp.MinDepth = 0.0f; | |
vp.MaxDepth = 1.0f; | |
vp.TopLeftX = 0; | |
vp.TopLeftY = 0; | |
g_pImmediateContext->RSSetViewports( 1, &vp ); | |
return true; | |
} | |
void swap_buffers() { | |
// Present our back buffer to our front buffer | |
//g_pSwapChain->Present( 0, 0 ); | |
g_pSwapChain->Present(1, 0); | |
} | |
void cleanup() | |
{ | |
if( g_pConstantBuffer ) g_pConstantBuffer->Release(); | |
if( g_pVertexBuffer ) g_pVertexBuffer->Release(); | |
if( g_pIndexBuffer ) g_pIndexBuffer->Release(); | |
if( g_pVertexLayout ) g_pVertexLayout->Release(); | |
if( g_pVertexShader ) g_pVertexShader->Release(); | |
if( g_pPixelShader ) g_pPixelShader->Release(); | |
//if( g_pCBNeverChanges ) g_pCBNeverChanges->Release(); | |
//if( g_pCBChangeOnResize ) g_pCBChangeOnResize->Release(); | |
//if( g_pCBChangesEveryFrame ) g_pCBChangesEveryFrame->Release(); | |
if( g_pSamplerLinear ) g_pSamplerLinear->Release(); | |
if( g_pTextureRV ) g_pTextureRV->Release(); | |
//if( g_pPixelShaderSolid ) g_pPixelShaderSolid->Release(); | |
} | |
void setup() | |
{ | |
#if 0 | |
// Compile the vertex shader | |
ID3DBlob* pVSBlob = NULL; | |
HRESULT hr = S_OK; | |
hr = CompileShaderFromFile( L"Tutorial03.fx", "VS", "vs_4_0", &pVSBlob ); | |
if( FAILED( hr ) ) | |
{ | |
MessageBox( NULL, | |
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK ); | |
return hr; | |
} | |
// Create the vertex shader | |
hr = g_pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVertexShader ); | |
if( FAILED( hr ) ) | |
{ | |
pVSBlob->Release(); | |
return hr; | |
} | |
// Define the input layout | |
D3D11_INPUT_ELEMENT_DESC layout[] = | |
{ | |
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
}; | |
UINT numElements = ARRAYSIZE( layout ); | |
D3D11_INPUT_ELEMENT_DESC layout[] = | |
{ | |
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
}; | |
UINT numElements = ARRAYSIZE( layout ); | |
D3D11_INPUT_ELEMENT_DESC layout[] = | |
{ | |
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
}; | |
UINT numElements = ARRAYSIZE( layout ); | |
D3D11_INPUT_ELEMENT_DESC layout[] = | |
{ | |
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, | |
}; | |
UINT numElements = ARRAYSIZE( layout ); | |
// Create the input layout | |
hr = g_pd3dDevice->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(), | |
pVSBlob->GetBufferSize(), &g_pVertexLayout ); | |
pVSBlob->Release(); | |
if( FAILED( hr ) ) | |
return hr; | |
// Set the input layout | |
g_pImmediateContext->IASetInputLayout( g_pVertexLayout ); | |
// Compile the pixel shader | |
ID3DBlob* pPSBlob = NULL; | |
hr = CompileShaderFromFile( L"Tutorial02.fx", "PS", "ps_4_0", &pPSBlob ); | |
if( FAILED( hr ) ) | |
{ | |
MessageBox( NULL, | |
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK ); | |
return hr; | |
} | |
// Create the pixel shader | |
hr = g_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader ); | |
pPSBlob->Release(); | |
if( FAILED( hr ) ) | |
return hr; | |
//// LIGHTS: PSSolid pixel shader | |
//// Compile the PSSolid pixel shader | |
//pPSBlob = NULL; | |
//hr = CompileShaderFromFile( L"Tutorial06.fx", "PSSolid", "ps_4_0", &pPSBlob ); | |
//if( FAILED( hr ) ) | |
//{ | |
// MessageBox( NULL, | |
// L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK ); | |
// return hr; | |
//} | |
//// Create the pixel shader | |
//hr = g_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShaderSolid ); | |
//pPSBlob->Release(); | |
//if( FAILED( hr ) ) | |
// return hr; | |
// Create vertex buffer | |
// SimpleVertex_pos, no indices | |
SimpleVertex vertices[] = | |
{ | |
XMFLOAT3( 0.0f, 0.5f, 0.5f ), | |
XMFLOAT3( 0.5f, -0.5f, 0.5f ), | |
XMFLOAT3( -0.5f, -0.5f, 0.5f ), | |
}; | |
// SimpleVertex_pos_color | |
SimpleVertex vertices[] = | |
{ | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT4( 0.0f, 0.0f, 1.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT4( 0.0f, 1.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT4( 0.0f, 1.0f, 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT4( 1.0f, 0.0f, 1.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT4( 1.0f, 1.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ) }, | |
}; | |
//bd.ByteWidth = sizeof( SimpleVertex ) * 8; | |
// SimpleVertex_pos_tex | |
SimpleVertex vertices[] = | |
{ | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) }, | |
}; | |
//bd.ByteWidth = sizeof( SimpleVertex ) * 24; | |
// SimpleVertex_pos_normal | |
SimpleVertex vertices[] = | |
{ | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT3( 0.0f, -1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT3( 0.0f, -1.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT3( 0.0f, -1.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT3( 0.0f, -1.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT3( -1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT3( -1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT3( -1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT3( -1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT3( 0.0f, 0.0f, -1.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT3( 0.0f, 0.0f, -1.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT3( 0.0f, 0.0f, -1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT3( 0.0f, 0.0f, -1.0f ) }, | |
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) }, | |
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) }, | |
}; | |
//bd.ByteWidth = sizeof( SimpleVertex ) * 24; | |
D3D11_BUFFER_DESC bd; | |
ZeroMemory( &bd, sizeof(bd) ); | |
bd.Usage = D3D11_USAGE_DEFAULT; | |
bd.ByteWidth = sizeof( SimpleVertex ) * 3; | |
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; | |
bd.CPUAccessFlags = 0; | |
D3D11_SUBRESOURCE_DATA InitData; | |
ZeroMemory( &InitData, sizeof(InitData) ); | |
InitData.pSysMem = vertices; | |
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Set vertex buffer | |
UINT stride = sizeof( SimpleVertex ); | |
UINT offset = 0; | |
g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset ); | |
// Set primitive topology | |
//g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); | |
// Create index buffer | |
// SimpleVertex_pos_color | |
WORD indices[] = | |
{ | |
3,1,0, | |
2,1,3, | |
0,5,4, | |
1,5,0, | |
3,4,7, | |
0,4,3, | |
1,6,5, | |
2,6,1, | |
2,7,6, | |
3,7,2, | |
6,4,5, | |
7,4,6, | |
}; | |
// SimpleVertex_pos_tex | |
// SimpleVertex_pos_normal | |
WORD indices[] = | |
{ | |
3,1,0, | |
2,1,3, | |
6,4,5, | |
7,4,6, | |
11,9,8, | |
10,9,11, | |
14,12,13, | |
15,12,14, | |
19,17,16, | |
18,17,19, | |
22,20,21, | |
23,20,22 | |
}; | |
bd.Usage = D3D11_USAGE_DEFAULT; | |
bd.ByteWidth = sizeof( WORD ) * 36; // 36 vertices needed for 12 triangles in a triangle list | |
bd.BindFlags = D3D11_BIND_INDEX_BUFFER; | |
bd.CPUAccessFlags = 0; | |
InitData.pSysMem = indices; | |
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Set index buffer | |
g_pImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 ); | |
// Set primitive topology | |
g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); | |
// Create the constant buffer | |
bd.Usage = D3D11_USAGE_DEFAULT; | |
bd.ByteWidth = sizeof(ConstantBuffer); | |
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; | |
bd.CPUAccessFlags = 0; | |
hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pConstantBuffer ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Create the constant buffers | |
//bd.Usage = D3D11_USAGE_DEFAULT; | |
//bd.ByteWidth = sizeof(CBNeverChanges); | |
//bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; | |
//bd.CPUAccessFlags = 0; | |
//hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBNeverChanges ); | |
//if( FAILED( hr ) ) | |
// return hr; | |
//bd.ByteWidth = sizeof(CBChangeOnResize); | |
//hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBChangeOnResize ); | |
//if( FAILED( hr ) ) | |
// return hr; | |
//bd.ByteWidth = sizeof(CBChangesEveryFrame); | |
//hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBChangesEveryFrame ); | |
//if( FAILED( hr ) ) | |
// return hr; | |
// Load the Texture | |
hr = D3DX11CreateShaderResourceViewFromFile( g_pd3dDevice, L"seafloor.dds", NULL, NULL, &g_pTextureRV, NULL ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Create the sample state | |
D3D11_SAMPLER_DESC sampDesc; | |
ZeroMemory( &sampDesc, sizeof(sampDesc) ); | |
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; | |
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; | |
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; | |
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; | |
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; | |
sampDesc.MinLOD = 0; | |
sampDesc.MaxLOD = D3D11_FLOAT32_MAX; | |
hr = g_pd3dDevice->CreateSamplerState( &sampDesc, &g_pSamplerLinear ); | |
if( FAILED( hr ) ) | |
return hr; | |
// Initialize the world matrix | |
g_World = XMMatrixIdentity(); | |
//// 2 model animation | |
////g_World1 = XMMatrixIdentity(); | |
////g_World2 = XMMatrixIdentity(); | |
// Initialize the view matrix | |
XMVECTOR Eye = XMVectorSet( 0.0f, 1.0f, -5.0f, 0.0f ); | |
XMVECTOR At = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); | |
XMVECTOR Up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); | |
g_View = XMMatrixLookAtLH( Eye, At, Up ); | |
// Initialize the projection matrix | |
g_Projection = XMMatrixPerspectiveFovLH( XM_PIDIV2, width / (FLOAT)height, 0.01f, 100.0f ); | |
//CBNeverChanges cbNeverChanges; | |
//cbNeverChanges.mView = XMMatrixTranspose( g_View ); | |
//g_pImmediateContext->UpdateSubresource( g_pCBNeverChanges, 0, NULL, &cbNeverChanges, 0, 0 ); | |
//// Initialize the projection matrix | |
//g_Projection = XMMatrixPerspectiveFovLH( XM_PIDIV4, width / (FLOAT)height, 0.01f, 100.0f ); | |
//CBChangeOnResize cbChangesOnResize; | |
//cbChangesOnResize.mProjection = XMMatrixTranspose( g_Projection ); | |
//g_pImmediateContext->UpdateSubresource( g_pCBChangeOnResize, 0, NULL, &cbChangesOnResize, 0, 0 ); | |
return S_OK; | |
#endif | |
} | |
void render(float dt) | |
{ | |
//// Update our time | |
//static float t = 0.0f; | |
//if( g_driverType == D3D_DRIVER_TYPE_REFERENCE ) | |
//{ | |
// t += ( float )XM_PI * 0.0125f; | |
//} | |
//else | |
//{ | |
// static DWORD dwTimeStart = 0; | |
// DWORD dwTimeCur = GetTickCount(); | |
// if( dwTimeStart == 0 ) | |
// dwTimeStart = dwTimeCur; | |
// t = ( dwTimeCur - dwTimeStart ) / 1000.0f; | |
//} | |
//// Animate the cube | |
//g_World = XMMatrixRotationY( t ); | |
// 2 model animation | |
//// 1st Cube: Rotate around the origin | |
//g_World1 = XMMatrixRotationY( t ); | |
//// 2nd Cube: Rotate around origin | |
//XMMATRIX mSpin = XMMatrixRotationZ( -t ); | |
//XMMATRIX mOrbit = XMMatrixRotationY( -t * 2.0f ); | |
//XMMATRIX mTranslate = XMMatrixTranslation( -4.0f, 0.0f, 0.0f ); | |
//XMMATRIX mScale = XMMatrixScaling( 0.3f, 0.3f, 0.3f ); | |
//g_World2 = mScale * mSpin * mTranslate * mOrbit; | |
// Tutorial 7 | |
//// Rotate cube around the origin | |
//g_World = XMMatrixRotationY( t ); | |
//// Modify the color | |
//g_vMeshColor.x = ( sinf( t * 1.0f ) + 1.0f ) * 0.5f; | |
//g_vMeshColor.y = ( cosf( t * 3.0f ) + 1.0f ) * 0.5f; | |
//g_vMeshColor.z = ( sinf( t * 5.0f ) + 1.0f ) * 0.5f; | |
// LIGHTS | |
// Rotate cube around the origin | |
//g_World = XMMatrixRotationY( t ); | |
// Setup our lighting parameters | |
//XMFLOAT4 vLightDirs[2] = | |
//{ | |
// XMFLOAT4( -0.577f, 0.577f, -0.577f, 1.0f ), | |
// XMFLOAT4( 0.0f, 0.0f, -1.0f, 1.0f ), | |
//}; | |
//XMFLOAT4 vLightColors[2] = | |
//{ | |
// XMFLOAT4( 0.5f, 0.5f, 0.5f, 1.0f ), | |
// XMFLOAT4( 0.5f, 0.0f, 0.0f, 1.0f ) | |
//}; | |
//// Rotate the second light around the origin | |
//XMMATRIX mRotate = XMMatrixRotationY( -2.0f * t ); | |
//XMVECTOR vLightDir = XMLoadFloat4( &vLightDirs[1] ); | |
//vLightDir = XMVector3Transform( vLightDir, mRotate ); | |
//XMStoreFloat4( &vLightDirs[1], vLightDir ); | |
// Clear the back buffer | |
//float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red, green, blue, alpha | |
float ClearColor[4] = { 1.0f, 0.125f, 0.3f, 1.0f }; | |
g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor ); | |
// Clear the depth buffer to 1.0 (max depth) | |
g_pImmediateContext->ClearDepthStencilView( g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 ); | |
#if 0 | |
// Update variables | |
ConstantBuffer cb; | |
cb.mWorld = XMMatrixTranspose( g_World ); | |
cb.mView = XMMatrixTranspose( g_View ); | |
cb.mProjection = XMMatrixTranspose( g_Projection ); | |
g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb, 0, 0 ); | |
// Render a triangle | |
g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 ); | |
g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer ); | |
g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 ); | |
//g_pImmediateContext->Draw( 3, 0 ); | |
g_pImmediateContext->DrawIndexed( 36, 0, 0 ); // 36 vertices needed for 12 triangles in a triangle list | |
#endif | |
//------------------------------------------------ | |
// 2 model animation | |
//// Update variables for the first cube | |
//ConstantBuffer cb1; | |
//cb1.mWorld = XMMatrixTranspose( g_World1 ); | |
//cb1.mView = XMMatrixTranspose( g_View ); | |
//cb1.mProjection = XMMatrixTranspose( g_Projection ); | |
//g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb1, 0, 0 ); | |
//// Render the first cube | |
//g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 ); | |
//g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer ); | |
//g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 ); | |
//g_pImmediateContext->DrawIndexed( 36, 0, 0 ); | |
//// Update variables for the second cube | |
//ConstantBuffer cb2; | |
//cb2.mWorld = XMMatrixTranspose( g_World2 ); | |
//cb2.mView = XMMatrixTranspose( g_View ); | |
//cb2.mProjection = XMMatrixTranspose( g_Projection ); | |
//g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb2, 0, 0 ); | |
//// Render the second cube | |
//g_pImmediateContext->DrawIndexed( 36, 0, 0 ); | |
// Tutorial 7 Textures | |
//// Update variables that change once per frame | |
//CBChangesEveryFrame cb; | |
//cb.mWorld = XMMatrixTranspose( g_World ); | |
//cb.vMeshColor = g_vMeshColor; | |
//g_pImmediateContext->UpdateSubresource( g_pCBChangesEveryFrame, 0, NULL, &cb, 0, 0 ); | |
//// Render the cube | |
//g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 ); | |
//g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pCBNeverChanges ); | |
//g_pImmediateContext->VSSetConstantBuffers( 1, 1, &g_pCBChangeOnResize ); | |
//g_pImmediateContext->VSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame ); | |
//g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 ); | |
//g_pImmediateContext->PSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame ); | |
//g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV ); | |
//g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear ); | |
//g_pImmediateContext->DrawIndexed( 36, 0, 0 ); | |
//-------------- | |
// LIGHTS | |
//// Update matrix variables | |
//ConstantBuffer cb1; | |
//cb1.mWorld = XMMatrixTranspose( g_World ); | |
//cb1.mView = XMMatrixTranspose( g_View ); | |
//cb1.mProjection = XMMatrixTranspose( g_Projection ); | |
//// Update lighting variables | |
//cb1.vLightDir[0] = vLightDirs[0]; | |
//cb1.vLightDir[1] = vLightDirs[1]; | |
//cb1.vLightColor[0] = vLightColors[0]; | |
//cb1.vLightColor[1] = vLightColors[1]; | |
//cb1.vOutputColor = XMFLOAT4(0, 0, 0, 0); | |
//g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb1, 0, 0 ); | |
//// Render the cube | |
//g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 ); | |
//g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer ); | |
//g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 ); | |
//g_pImmediateContext->PSSetConstantBuffers( 0, 1, &g_pConstantBuffer ); | |
//g_pImmediateContext->DrawIndexed( 36, 0, 0 ); | |
//// Render each light | |
//for( int m = 0; m < 2; m++ ) | |
//{ | |
// XMMATRIX mLight = XMMatrixTranslationFromVector( 5.0f * XMLoadFloat4( &vLightDirs[m] ) ); | |
// XMMATRIX mLightScale = XMMatrixScaling( 0.2f, 0.2f, 0.2f ); | |
//mLight = mLightScale * mLight; | |
// | |
//// Update the world variable to reflect the current light | |
// cb1.mWorld = XMMatrixTranspose( mLight ); | |
// cb1.vOutputColor = vLightColors[m]; | |
// g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb1, 0, 0 ); | |
// | |
// g_pImmediateContext->PSSetShader( g_pPixelShaderSolid, NULL, 0 ); | |
// g_pImmediateContext->DrawIndexed( 36, 0, 0 ); | |
//} | |
} | |
}; | |
#if 0 | |
// File: Tutorial03.fx | |
float4 VS( float4 Pos : POSITION ) : SV_POSITION | |
{ | |
return Pos; | |
} | |
float4 PS( float4 Pos : SV_POSITION ) : SV_Target | |
{ | |
return float4( 1.0f, 1.0f, 0.0f, 1.0f ); // Yellow, with Alpha = 1 | |
} | |
//============================================== | |
// File: Tutorial04.fx | |
cbuffer ConstantBuffer : register( b0 ) | |
{ | |
matrix World; | |
matrix View; | |
matrix Projection; | |
} | |
struct VS_OUTPUT | |
{ | |
float4 Pos : SV_POSITION; | |
float4 Color : COLOR0; | |
}; | |
VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR ) | |
{ | |
VS_OUTPUT output = (VS_OUTPUT)0; | |
output.Pos = mul( Pos, World ); | |
output.Pos = mul( output.Pos, View ); | |
output.Pos = mul( output.Pos, Projection ); | |
output.Color = Color; | |
return output; | |
} | |
float4 PS( VS_OUTPUT input ) : SV_Target | |
{ | |
return input.Color; | |
} | |
//============================================== | |
// File: Tutorial05.fx | |
cbuffer ConstantBuffer : register( b0 ) | |
{ | |
matrix World; | |
matrix View; | |
matrix Projection; | |
} | |
struct VS_INPUT | |
{ | |
float4 Pos : POSITION; | |
float4 Color : COLOR; | |
}; | |
struct PS_INPUT | |
{ | |
float4 Pos : SV_POSITION; | |
float4 Color : COLOR; | |
}; | |
PS_INPUT VS( VS_INPUT input ) | |
{ | |
PS_INPUT output = (PS_INPUT)0; | |
output.Pos = mul( input.Pos, World ); | |
output.Pos = mul( output.Pos, View ); | |
output.Pos = mul( output.Pos, Projection ); | |
output.Color = input.Color; | |
return output; | |
} | |
float4 PS( PS_INPUT input) : SV_Target | |
{ | |
return input.Color; | |
} | |
//============================================== | |
// File: Tutorial07.fx | |
Texture2D txDiffuse : register( t0 ); | |
SamplerState samLinear : register( s0 ); | |
cbuffer cbNeverChanges : register( b0 ) | |
{ | |
matrix View; | |
}; | |
cbuffer cbChangeOnResize : register( b1 ) | |
{ | |
matrix Projection; | |
}; | |
cbuffer cbChangesEveryFrame : register( b2 ) | |
{ | |
matrix World; | |
float4 vMeshColor; | |
}; | |
struct VS_INPUT | |
{ | |
float4 Pos : POSITION; | |
float2 Tex : TEXCOORD0; | |
}; | |
struct PS_INPUT | |
{ | |
float4 Pos : SV_POSITION; | |
float2 Tex : TEXCOORD0; | |
}; | |
PS_INPUT VS( VS_INPUT input ) | |
{ | |
PS_INPUT output = (PS_INPUT)0; | |
output.Pos = mul( input.Pos, World ); | |
output.Pos = mul( output.Pos, View ); | |
output.Pos = mul( output.Pos, Projection ); | |
output.Tex = input.Tex; | |
return output; | |
} | |
float4 PS( PS_INPUT input) : SV_Target | |
{ | |
return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor; | |
} | |
//============================================== | |
// File: Tutorial06.fx | |
cbuffer ConstantBuffer : register( b0 ) | |
{ | |
matrix World; | |
matrix View; | |
matrix Projection; | |
float4 vLightDir[2]; | |
float4 vLightColor[2]; | |
float4 vOutputColor; | |
} | |
struct VS_INPUT | |
{ | |
float4 Pos : POSITION; | |
float3 Norm : NORMAL; | |
}; | |
struct PS_INPUT | |
{ | |
float4 Pos : SV_POSITION; | |
float3 Norm : TEXCOORD0; | |
}; | |
PS_INPUT VS( VS_INPUT input ) | |
{ | |
PS_INPUT output = (PS_INPUT)0; | |
output.Pos = mul( input.Pos, World ); | |
output.Pos = mul( output.Pos, View ); | |
output.Pos = mul( output.Pos, Projection ); | |
output.Norm = mul( input.Norm, World ); | |
return output; | |
} | |
float4 PS( PS_INPUT input) : SV_Target | |
{ | |
float4 finalColor = 0; | |
//do NdotL lighting for 2 lights | |
for(int i=0; i<2; i++) | |
{ | |
finalColor += saturate( dot( (float3)vLightDir[i],input.Norm) * vLightColor[i] ); | |
} | |
finalColor.a = 1; | |
return finalColor; | |
} | |
float4 PSSolid( PS_INPUT input) : SV_Target | |
{ | |
return vOutputColor; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment