SOL9 Sample: Direct3DX11MultipleTexturedShapesBuffer
|
1 Screenshot
2 Source code
/*
* Direct3DX11MultipleTexturedShapesBuffer.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2016/02/01
// 2017/01/19 Updated display to call setShaderMatrices.
#pragma warning(disable : 4005)
#pragma warning(disable : 4838)
#define COMMONCONTROLS_V6
#include <sol/direct3d11/DirectX3D11MainView.h>
#include <sol/direct3d11/DirectX3D11View.h>
#include <sol/direct3d11/Direct3D11RasterizerState.h>
#include <sol/direct3d11/Direct3D11DepthStencilView.h>
#include <sol/direct3d11/Direct3D11RenderTargetView.h>
#include <sol/direct3d11/Direct3D11VertexShader.h>
#include <sol/direct3d11/Direct3D11PixelShader.h>
#include <sol/direct3d11/Direct3DX11TexturedCube.h>
#include <sol/direct3d11/Direct3DX11TexturedTriangle.h>
#include <sol/direct3d11/Direct3DX11TexturedRectangle.h>
#include <sol/direct3d11/Direct3D11Texture2D.h>
#include <sol/direct3d11/D3D11Texture2DDesc.h>
#include <sol/direct3d11/Direct3D11Buffer.h>
#include <sol/direct3d11/Direct3D11Blob.h>
#include <sol/direct3d11/D3D11PosColorVertex.h>
#include <sol/direct3d11/D3D11ConstantBuffer.h>
#include <sol/direct3d11/D3D11DepthStencilViewDesc.h>
#include <sol/direct3d11/D3D11InputElements.h>
#include "resource.h"
namespace SOL {
class MainView :public DirectX3D11MainView {
private:
//////////////////////////////////////////////
//Inner class starts
class SimpleView : public DirectX3D11View {
private:
SmartPtr<Direct3D11RenderTargetView> renderTargetView;
SmartPtr<Direct3D11DepthStencilView> depthStencilView;
SmartPtr<Direct3D11RasterizerState> rasterizerState;
static const int SHAPES =3;
SmartPtr<Direct3DX11Shape> shapes[SHAPES];
SmartPtr<Direct3D11Blob> vertexShaderBlob;
SmartPtr<Direct3D11Blob> pixelShaderBlob;
SmartPtr<Direct3D11Blob> pixelShaderSolidBlob;
SmartPtr<Direct3D11VertexShader> vertexShader;
SmartPtr<Direct3D11PixelShader> pixelShader;
SmartPtr<Direct3D11PixelShader> pixelShaderSolid;
SmartPtr<Direct3D11Buffer> constantBuffer;
XMMatrix world;
XMMatrix view;
XMMatrix projection;
StringT<TCHAR> directory;
float angle;
public:
void deleteViews()
{
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
d3d11ImmediateContext ->setOMRenderTargets(0, NULL, NULL);
renderTargetView = NULL;
depthStencilView = NULL;
}
virtual void createViews()
{
int width = 0;
int height = 0;
validateClientSize(width, height);
try {
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
DirectXGISwapChain* swapChain = getSwapChain();
d3d11ImmediateContext ->setOMRenderTargets(0, NULL, NULL);
//1 Create an instance of Direct3D11RenderTargetView
Direct3D11Texture2D renderTargetViewTexture(*swapChain); ;
renderTargetView = new Direct3D11RenderTargetView(*d3d11Device, renderTargetViewTexture, NULL);
//2 Create a temporary depthDesc(D3D11Texture2DDesc).
D3D11Texture2DDesc depthDesc;
depthDesc.width(width);
depthDesc.height(height);
depthDesc.mipLevels(1);
depthDesc.arraySize(1);
depthDesc.format(DXGI_FORMAT_D32_FLOAT);
depthDesc.sampleDescCount(1);
depthDesc.sampleDescQuality(0);
depthDesc.usage(D3D11_USAGE_DEFAULT);
depthDesc.bindFlags(D3D11_BIND_DEPTH_STENCIL);
//3 Create a temporary depthStencilTexture(Direct3D11Texture2D) from texture2DDesc.
Direct3D11Texture2D depthStencilTexute(*d3d11Device, depthDesc);
//4 Create a temporary depthStencilViewDesc(D3D11DepthStencilViewDesc)
D3D11DepthStencilViewDesc depthStencilViewDesc(DXGI_FORMAT_D32_FLOAT, D3D11_DSV_DIMENSION_TEXTURE2D);
//5 Create an instance of Direct3DDepthStencilView from depthStencilTexture and depthStencilViewDesc
depthStencilView = new Direct3D11DepthStencilView(*d3d11Device, depthStencilTexute, depthStencilViewDesc);
ID3D11RenderTargetView* targets[1];
targets[0]= *renderTargetView;
//6 Set renderTargetView and depthStencilView to id3d11ImmediateContext
d3d11ImmediateContext ->setOMRenderTargets(1, targets, *depthStencilView);
} catch (Exception& ex) {
caught(ex);
}
}
void createVertexShader()
{
Direct3D11Device* d3d11Device = getD3D11Device();
TCHAR fullpath[MAX_PATH];
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\fx\\%s"),
(const TCHAR*)directory,
_T("shader.fx"));
try {
vertexShaderBlob = new Direct3D11Blob(fullpath, "VS", "vs_4_0");
if (vertexShaderBlob) {
vertexShader = new Direct3D11VertexShader(*d3d11Device, *vertexShaderBlob);
}
} catch (Exception& ex) {
caught(ex);
}
}
void createPixelShader()
{
Direct3D11Device* d3d11Device = getD3D11Device();
TCHAR fullpath[MAX_PATH];
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\fx\\%s"),
(const TCHAR*)directory,
_T("shader.fx"));
try {
pixelShaderBlob = new Direct3D11Blob(fullpath, "PS", "ps_4_0");
if (pixelShaderBlob) {
pixelShader = new Direct3D11PixelShader(*d3d11Device, *pixelShaderBlob);
}
} catch (Exception& ex) {
caught(ex);
}
}
void createRasterizerState()
{
try {
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
D3D11_RASTERIZER_DESC desc;
memset(&desc, 0, sizeof(desc));
desc.CullMode = D3D11_CULL_NONE;
desc.FillMode = D3D11_FILL_SOLID;
desc.FrontCounterClockwise = true;
desc.DepthBias = false;
desc.DepthClipEnable = true;
desc.ScissorEnable = false;
desc.MultisampleEnable = false;
desc.AntialiasedLineEnable = true;
rasterizerState = new Direct3D11RasterizerState (*d3d11Device, &desc);
d3d11ImmediateContext->setRSState(*rasterizerState);
} catch (Exception& ex) {
caught(ex);
}
}
void createShapesBuffer()
{
try {
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
TCHAR* imageFiles[SHAPES] = {
_T("flower1.png"),
_T("flower2.png"),
_T("flower3.png"),
} ;
TCHAR fullpath[MAX_PATH];
int i = 0;
//1 Create a colored cube.
{
shapes[i] = new Direct3DX11TexturedCube(d3d11Device, d3d11ImmediateContext, vertexShaderBlob,
0.5f);
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\images\\%s"),
(const TCHAR*)directory,
imageFiles[i]);
shapes[i] -> createShaderResourceView(fullpath);
shapes[i] -> createSamplerState();
i++;
}
//2 Create a colored triangle.
{
shapes[i] = new Direct3DX11TexturedTriangle(d3d11Device, d3d11ImmediateContext, vertexShaderBlob,
0.5f, 0.5f);
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\images\\%s"),
(const TCHAR*)directory,
imageFiles[i]);
shapes[i] -> createShaderResourceView(fullpath);
shapes[i] -> createSamplerState();
i++;
}
//3 Create a colored rectangle.
{
shapes[i] = new Direct3DX11TexturedRectangle(d3d11Device, d3d11ImmediateContext, vertexShaderBlob,
2.0f, 0.8f);
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\images\\%s"),
(const TCHAR*)directory,
imageFiles[i]);
shapes[i] -> createShaderResourceView(fullpath);
shapes[i] -> createSamplerState();
i++;
}
} catch (Exception& ex) {
caught(ex);
}
}
virtual void createConstantBuffer()
{
try {
Direct3D11Device* d3d11Device = getD3D11Device();
D3D11_BUFFER_DESC constantBufferDesc;
memset(&constantBufferDesc, 0, sizeof(constantBufferDesc));
constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
constantBufferDesc.ByteWidth = sizeof(D3D11ConstantBuffer);
constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
// Create a constantBuffer from above parameters.
constantBuffer = new Direct3D11Buffer(*d3d11Device, &constantBufferDesc, NULL);
} catch (Exception& ex) {
caught(ex);
}
}
void setShaderMatrices()
{
int width = 0;
int height = 0;
getClientSize(width, height);
try {
XMVECTOR eye = XMVectorSet( 0.0f, 3.0f, -8.0f, 0.0f );
XMVECTOR at = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
XMVECTOR up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
view = XMMatrix::lookAtLH( (XMVECTOR)eye, (XMVECTOR)at, (FXMVECTOR)up );
projection = XMMatrix::perspectiveFovLH( XM_PIDIV2*0.3f, width / (FLOAT)height, 0.01f, 100.0f );
} catch (Exception& ex) {
caught(ex);
}
}
virtual void initialize()
{
try {
createVertexShader();
createPixelShader();
createShapesBuffer();
createRasterizerState();
createConstantBuffer();
setShaderMatrices();
createViews();
postResizeRequest();
} catch (Exception& ex) {
caught(ex);
}
}
virtual void display()
{
try {
Direct3D11Device* d3d11Device = getD3D11Device();
DirectXGISwapChain* swapChain = getSwapChain();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
if (renderTargetView && depthStencilView ) {
//1 Clear renderTargetView and depthStencilView
d3d11ImmediateContext -> clear(*renderTargetView, XMColor(0.0f, 0.0f, 0.0f, 0.0f));
d3d11ImmediateContext -> clear(*depthStencilView);
//2017/01/19
setShaderMatrices();
XMFLOAT3 pos[SHAPES] = {
XMFLOAT3(-1.0f, 1.0f, 1.0f),
XMFLOAT3( 3.0f, 1.0f, 1.0f),
XMFLOAT3(-1.0f, -3.0f, 0.0f),
};
//2 Rotate world around axis y by angle
world = XMMatrixRotationY( angle );
for (int i = 0; i<SHAPES; i++) {
XMMatrix temp = XMMatrix::translation(0.0f, 0.0f, 0.0f);
world = XMMatrix::scaling( 0.8f, 0.8f, 0.8f);
XMMatrix m = XMMatrix::translation(pos[i].x, pos[i].y, pos[i].z);
world += m;
world *= temp;
//3 Update the constantBuffer
D3D11ConstantBuffer cb;
memset(&cb, 0, sizeof(cb));
cb.world = XMMatrix::transpose(world );
cb.view = XMMatrix::transpose(view );
cb.projection = XMMatrix::transpose(projection );
d3d11ImmediateContext -> updateSubresource(*constantBuffer, 0, NULL, &cb, 0, 0 );
//4 Draw each shape.
ID3D11Buffer* cbuffer = *constantBuffer;
d3d11ImmediateContext -> setVSShader( *vertexShader, NULL, 0 );
d3d11ImmediateContext -> setVSConstantBuffers( 0, 1, &cbuffer );
d3d11ImmediateContext -> setPSShader( *pixelShader, NULL, 0 );
d3d11ImmediateContext -> setPSConstantBuffers( 0, 1, &cbuffer );
shapes[i] -> setSamplers();
shapes[i] -> setShaderResources();
shapes[i] -> setInputLayout();
shapes[i] -> setBuffers();
shapes[i] -> drawIndexed();
}
}
swapChain -> present();
} catch (Exception& ex) {
caught(ex);
}
}
public:
// Constructor
SimpleView(DirectX3D11MainView* parent, const TCHAR* name, Args& args)
:DirectX3D11View(parent, name, args),
directory(_T("")),
angle(10.0f)
{
directory = (const TCHAR*)args.get(XmNapplicationDirectory);
}
~SimpleView()
{
}
private:
void resize(int width, int height)
{
Direct3D11Device* device = getD3D11Device();
DirectXGISwapChain* swapChain = getSwapChain();
if (device == NULL &&
swapChain == NULL &&
renderTargetView == NULL) {
return ;
}
try {
// 1 Delete existing rendarTargetView and depthStencilView.
deleteViews();
// 2 ResizeBuffers swapChain(IDXGISwapChain)
swapChain -> resizeBuffers(width, height);
// 3 Recreate rendarTargetView and depthStencilView.
createViews();
// 4 SetViewPort
setViewPort(width, height);
//angle += 0.1f;
} catch (Exception& ex) {
caught(ex);
}
}
};
//////////////////////////////////////////////
// Inner class ends.
private:
SmartPtr<SimpleView> view;
public:
// Constructor
MainView(Application& applet, const TCHAR* name, Args& args)
:DirectX3D11MainView(applet, name,
args.set(XmNstyle, (ulong)WS_CLIPCHILDREN|WS_CLIPSIBLINGS) )
{
DWORD interval = (DWORD)args.get(XmNrenderingInterval);
const TCHAR* directory = (const TCHAR*)args.get(XmNapplicationDirectory);
// 1 Create a view of SimpleView.
Args ar;
int width = 0;
int height = 0;
getClientSize(width, height);
ar.set(XmNwidth, width);
ar.set(XmNheight,height);
ar.set(XmNapplicationDirectory, directory);
ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
view = new SimpleView(this, _T(""), ar);
// 2 Post a resize request to this MainView.
//postResizeRequest();
}
public:
~MainView()
{
}
private:
void resize(int width, int height)
{
if (view != nullptr) {
view -> reshape(0, 0, width, height);
view -> postResizeRequest(width, height);
}
}
};
}
//////////////////////////////////////////////
//
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = appName(argv[0]);
TCHAR directory[MAX_PATH];
appDirectory(argv[0], directory, CountOf(directory));
try {
Application applet(appClass, argc, argv);
Args args;
args.set(XmNapplicationDirectory, directory);
args.set(XmNwidth, 640);
args.set(XmNheight, 480);
MainView mainv(applet, appClass, args);
mainv.realize();
applet.run();
} catch (Exception& ex) {
caught(ex);
}
}
Last modified: 1 Feb 2017
Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.