SOL9 Sample: Direct3D11CircleBufferWithFileReader
|
1 Screenshot
2 Source code
/*
* Direct3D11CircleBufferWithFileReader.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2016/01/10
// 2017/01/28 Updated to use ModuleFileName class and caught macro.
#pragma warning(disable : 4005)
#pragma warning(disable : 4838)
#define COMMONCONTROLS_V6
#include <sol/StringT.h>
#include <sol/CharFileReader.h>
#include <sol/direct3d11/DirectX3D11MainView.h>
#include <sol/direct3d11/DirectX3D11View.h>
#include <sol/direct3d11/Direct3D11RenderTargetView.h>
#include <sol/direct3d11/Direct3D11DepthStencilView.h>
#include <sol/direct3d11/D3D11DepthStencilViewDesc.h>
#include <sol/direct3d11/Direct3D11VertexShader.h>
#include <sol/direct3d11/Direct3D11PixelShader.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/Direct3D11InputLayout.h>
#include "resource.h"
namespace SOL {
#define PI 3.14159265f
class MainView :public DirectX3D11MainView {
private:
////////////////////////////////////////////
// Inner class starts.
class SimpleView : public DirectX3D11View {
private:
SmartPtr<Direct3D11RenderTargetView> renderTargetView;
SmartPtr<Direct3D11DepthStencilView> depthStencilView;
SmartPtr<CharFileReader> fileReader;
SmartPtr<Direct3D11Buffer> vertexBuffer;
SmartPtr<Direct3D11Buffer> indexBuffer;
SmartPtr<Direct3D11Blob> vertexShaderBlob;
SmartPtr<Direct3D11Blob> pixelShaderBlob;
SmartPtr<Direct3D11VertexShader> vertexShader;
SmartPtr<Direct3D11PixelShader> pixelShader;
SmartPtr<Direct3D11Buffer> constantBuffer;
int vertexCounter;
SmartPtr<Direct3D11InputLayout> inputLayout;
StringT<TCHAR> directory;
public:
void deleteViews()
{
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
d3d11ImmediateContext -> setOMRenderTargets(0, NULL, NULL);
renderTargetView = NULL;
depthStencilView = NULL;
}
void createFileReader()
{
try {
TCHAR fullpath[MAX_PATH];
_stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\fx\\%s"),
(const TCHAR*)directory,
_T("shader.fx"));
fileReader = new CharFileReader(fullpath);
} catch (Exception& ex) {
caught(ex);
}
}
virtual void createViews()
{
int width = 0;
int height = 0;
validateClientSize(width, height);
try {
Direct3D11Device* d3d11Device = getD3D11Device();
DirectXGISwapChain* swapChain = getSwapChain();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
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* targetView = *renderTargetView;
//6 Set renderTargetView and depthStencilView to id3d11Device
d3d11ImmediateContext -> setOMRenderTargets(1, &targetView, *depthStencilView);
} catch (Exception& ex) {
caught(ex);
}
}
void createInputLayout()
{
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
D3D11_INPUT_ELEMENT_DESC inputElementDesc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
inputLayout = new Direct3D11InputLayout(
*d3d11Device,
inputElementDesc, CountOf(inputElementDesc),
*vertexShaderBlob);
d3d11ImmediateContext -> setIAInputLayout(*inputLayout);
}
float SIN(int degree)
{
float radian = (float)degree * PI / 180.0f;
return (float)sin(radian);
}
float COS(int degree)
{
float radian = (float)degree * PI / 180.0f;
return (float)cos(radian);
}
void createVertexShader()
{
Direct3D11Device* d3d11Device = getD3D11Device();
try {
vertexShaderBlob = new Direct3D11Blob(fileReader->getData(),
fileReader->getDataSize(), NULL, "VS", "vs_4_0");
if (vertexShaderBlob) {
vertexShader = new Direct3D11VertexShader(*d3d11Device, *vertexShaderBlob);
}
} catch (Exception& ex) {
caught(ex);
}
}
void createPixelShader()
{
Direct3D11Device* d3d11Device = getD3D11Device();
try {
pixelShaderBlob = new Direct3D11Blob(fileReader->getData(),
fileReader->getDataSize(), NULL, "PS", "ps_4_0");
if (pixelShaderBlob) {
pixelShader = new Direct3D11PixelShader(*d3d11Device, *pixelShaderBlob);
}
} catch (Exception& ex) {
caught(ex);
}
}
void createCircleBuffer(float px, float py, float radius)
{
static XMFLOAT3 lines[360] ={};
vertexCounter = CountOf(lines);
for (int i = 0; i<vertexCounter - 1 ; i +=2) {
lines[i ] = XMFLOAT3( px + radius * COS(i ), py + radius * SIN(i ) , 0.5f);
lines[i+1] = XMFLOAT3( px + radius * COS(i+1), py + radius * SIN(i+1) , 0.5f);
}
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
D3D11_BUFFER_DESC bufferDesc;
memset(&bufferDesc, 0, sizeof(bufferDesc));
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(lines) ;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
D3D11_SUBRESOURCE_DATA subresourceData;
memset(&subresourceData, 0, sizeof(subresourceData));
subresourceData.pSysMem = lines;
vertexBuffer = new Direct3D11Buffer(*d3d11Device, &bufferDesc, &subresourceData);
// Set vertex buffer
UINT stride = sizeof(XMFLOAT3);
UINT offset = 0;
ID3D11Buffer* vbuf = *vertexBuffer;
d3d11ImmediateContext -> setIAVertexBuffers( 0, 1, &vbuf, &stride, &offset );
// Set primitive topology
d3d11ImmediateContext->setIAPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
}
virtual void initialize()
{
try {
createFileReader();
createPixelShader();
createVertexShader();
createInputLayout();
createCircleBuffer(0.5f, 0.0f, 0.5f);
// Create rendarTargetView and depthStencilView.
createViews();
} catch (Exception& ex) {
caught(ex);
}
}
virtual void display()
{
try {
Direct3D11Device* d3d11Device = getD3D11Device();
Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
DirectXGISwapChain* swapChain = getSwapChain();
if (renderTargetView && depthStencilView) {
//Clear renderTargetView and depthStencilView.
renderTargetView -> clear(XMColor(0.8f, 0.8f, 0.8f, 0.0));
depthStencilView -> clear();
d3d11ImmediateContext -> setVSShader(*vertexShader, NULL, 0);
d3d11ImmediateContext -> setPSShader( *pixelShader, NULL, 0 );
d3d11ImmediateContext -> draw( vertexCounter, 0);
}
swapChain -> present();
} catch (Exception& ex) {
caught(ex);
}
}
public:
// Constructor
SimpleView(DirectX3D11MainView* parent, const TCHAR* name, Args& args)
:DirectX3D11View(parent, name, args),
vertexCounter(0)
{
directory = (const TCHAR*)args.get(XmNapplicationDirectory);
postResizeRequest();
}
public:
~SimpleView()
{
}
private:
void resize(int width, int height)
{
Direct3D11Device* d3d11Device = getD3D11Device();
DirectXGISwapChain* swapChain = getSwapChain();
if (d3d11Device == NULL ||
swapChain == NULL ||
renderTargetView == NULL ||
depthStencilView == 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);
} 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) )
{
const TCHAR* directory = (const TCHAR*)args.get(XmNapplicationDirectory);
restorePlacement();
Args ar;
int width = 0;
int height = 0;
getClientSize(width, height);
ar.set(XmNwidth, width);
ar.set(XmNheight, height);
ar.set(XmNapplicationDirectory, directory);
view = new SimpleView(this, _T(""), ar);
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)
{
ModuleFileName module(argv[0]);
const TCHAR* directory = module.getDirectory();
const TCHAR* appClass = module.getAppName();
try {
Application applet(appClass, argc, argv);
Args args;
args.set(XmNapplicationDirectory, directory);
args.set(XmNwidth, 640);
args.set(XmNheight, 480);
MainView mainView(applet, appClass, args);
mainView.realize();
applet.run();
} catch (Exception& ex) {
caught(ex);
}
}
Last modified: 1 Feb 2017
Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.