SOL9 Sample: Direct3D10FilledCircleEffect
|
1 Screenshot
2 Source code
/*
* Direct3D10FilledCircleEffect.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2015/10/26
#define COMMONCONTROLS_V6
#include <sol/direct3d10/DirectX3D10MainView.h>
#include <sol/direct3d10/DirectX3D10View.h>
#include <sol/direct3d10/Direct3D10RenderTargetView.h>
#include <sol/direct3d10/Direct3D10DepthStencilView.h>
#include <sol/direct3d10/Direct3D10Texture2D.h>
#include <sol/direct3d10/Direct3DX10Font.h>
#include <sol/direct3d10/D3D10Texture2DDesc.h>
#include <sol/direct3d10/D3D10DepthStencilViewDesc.h>
#include <sol/direct3d10/Direct3D10Effect.h>
#include <sol/direct3d10/Direct3D10EffectTechnique.h>
#include <sol/direct3d10/Direct3D10EffectPass.h>
#include <sol/direct3d10/Direct3D10InputLayout.h>
#include <sol/direct3d10/Direct3D10Buffer.h>
#include <sol/direct3d10/Direct3D10RasterizerState.h>
#include "resource.h"
namespace SOL {
static char const tutorial02Fx[] =
"// VS (Vertex Shader) \n"
"float4 VS( float4 Pos : POSITION ) : SV_POSITION\n"
"{\n"
" return Pos;\n"
"}\n"
"\n"
"\n"
"// PS (Pixel Shader)\n"
"float4 PS( float4 Pos : SV_POSITION ) : SV_Target\n"
"{\n"
" return float4( 1.0f, 0.0f, 0.0f, 1.0f ); // Red \n"
"}\n"
"\n"
"\n"
"technique10 Render\n"
"{\n"
" pass P0\n"
" {\n"
" SetVertexShader( CompileShader( vs_4_0, VS() ) );\n"
" SetGeometryShader( NULL );\n"
" SetPixelShader( CompileShader( ps_4_0, PS() ) );\n"
" }\n"
"}\n"
;
class MainView :public DirectX3D10MainView {
private:
////////////////////////////////////////////
// Inner class starts.
class SimpleView : public DirectX3D10View {
private:
Direct3D10RenderTargetView* renderTargetView;
Direct3D10DepthStencilView* depthStencilView;
Direct3D10Effect* effect;
Direct3D10EffectTechnique* effectTechnique;
Direct3D10InputLayout* vertexLayout;
Direct3D10RasterizerState* rasterizerState;
Direct3D10Buffer* vertexBuffer;
UINT vertexCount;
public:
void deleteViews()
{
Direct3D10Device* d3d10Device = getD3D10Device();
d3d10Device ->setOMRenderTargets(0, NULL, NULL);
if (renderTargetView) {
delete renderTargetView;
renderTargetView = NULL;
}
if (depthStencilView) {
delete depthStencilView;
depthStencilView = NULL;
}
}
virtual void createViews()
{
int width = 0;
int height = 0;
validateClientSize(width, height);
try {
Direct3D10Device* d3d10Device = getD3D10Device();
DirectXGISwapChain* swapChain = getSwapChain();
d3d10Device ->setOMRenderTargets(0, NULL, NULL);
//1 Create an instance of Direct3D10RenderTargetView
Direct3D10Texture2D renderTargetViewTexture(*swapChain); ;
renderTargetView = new Direct3D10RenderTargetView(*d3d10Device, renderTargetViewTexture, NULL);
//2 Create a temporary depthDesc(D3D10Texture2DDesc).
D3D10Texture2DDesc 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(D3D10_USAGE_DEFAULT);
depthDesc.bindFlags(D3D10_BIND_DEPTH_STENCIL);
//3 Create a temporary depthStencilTexture(Direct3D10Texture2D) from texture2DDesc.
Direct3D10Texture2D depthStencilTexute(*d3d10Device, depthDesc);
//4 Create a temporary depthStencilViewDesc(D3D10DepthStencilViewDesc)
D3D10DepthStencilViewDesc depthStencilViewDesc(DXGI_FORMAT_D32_FLOAT, D3D10_DSV_DIMENSION_TEXTURE2D);
//5 Create an instance of Direct3DDepthStencilView from depthStencilTexture and depthStencilViewDesc
depthStencilView = new Direct3D10DepthStencilView(*d3d10Device, depthStencilTexute, depthStencilViewDesc);
ID3D10RenderTargetView* targetView = *renderTargetView;
//6 Set renderTargetView and depthStencilView to id3d10Device
d3d10Device ->setOMRenderTargets(1, &targetView, *depthStencilView);
} catch (Exception& ex) {
ex.display();
}
}
void createRasterizerState()
{
try {
Direct3D10Device* d3d10Device = getD3D10Device();
D3D10_RASTERIZER_DESC desc;
memset(&desc, 0, sizeof(desc));
desc.CullMode = D3D10_CULL_NONE;
desc.FillMode = D3D10_FILL_SOLID;
desc.FrontCounterClockwise = true;
desc.DepthBias = false;
desc.DepthClipEnable = true;
desc.ScissorEnable = false;
desc.MultisampleEnable = false;
desc.AntialiasedLineEnable = true;
rasterizerState = new Direct3D10RasterizerState (*d3d10Device, &desc);
d3d10Device->setRSState(*rasterizerState);
} catch (Exception& ex) {
ex.display();
}
}
void createEffect()
{
Direct3D10Device* d3d10Device = getD3D10Device();
// Create an effect from a string stream.
effect = new Direct3D10Effect(
*d3d10Device,
(void*)tutorial02Fx,
sizeof(tutorial02Fx),
"tutorial02.fx",
D3D10_SHADER_ENABLE_STRICTNESS);
// Create an effectTechnique from "Render" name contained in the above effect.
effectTechnique = new Direct3D10EffectTechnique(effect->getTechniqueByName("Render") );
}
void createInputLayout()
{
Direct3D10Device* d3d10Device = getD3D10Device();
D3D10_INPUT_ELEMENT_DESC inputElementDesc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
Direct3D10EffectPass effectPass = effectTechnique->getPassByIndex(0);
D3D10_PASS_DESC passDesc;
effectPass.getDesc(passDesc);
vertexLayout = new Direct3D10InputLayout(
*d3d10Device,
inputElementDesc, SizeOf(inputElementDesc),
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize);
d3d10Device->setIAInputLayout(*vertexLayout);
}
void createCircleBuffer(float posx, float posy, float radius)
{
static D3DXVECTOR3 lines[360+6] ={};
vertexCount = SizeOf(lines);
for (int i = 0; i< vertexCount-2; i +=3) {
lines[i ] = D3DXVECTOR3( posx, posy, 0.5f); //center
lines[i+1] = D3DXVECTOR3( posx + radius * COS(i), posy + radius * SIN(i) , 0.5f);
lines[i+2] = D3DXVECTOR3( posx + radius * COS(i+1), posy + radius * SIN(i+1), 0.5f);
}
Direct3D10Device* d3d10Device = getD3D10Device();
D3D10_BUFFER_DESC bufferDesc;
memset(&bufferDesc, 0, sizeof(bufferDesc));
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(lines) ;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
D3D10_SUBRESOURCE_DATA subresourceData;
memset(&subresourceData, 0, sizeof(subresourceData));
subresourceData.pSysMem = lines;
vertexBuffer = new Direct3D10Buffer(*d3d10Device, &bufferDesc, &subresourceData);
// Set vertex buffer
UINT stride = sizeof(D3DXVECTOR3);
UINT offset = 0;
ID3D10Buffer* vbuf = *vertexBuffer;
d3d10Device -> setIAVertexBuffers( 0, 1, &vbuf, &stride, &offset );
// Set primitive topology
d3d10Device->setIAPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP );
}
virtual void initialize()
{
try {
createEffect();
createRasterizerState();
createInputLayout();
createCircleBuffer(0.0f, 0.0f, 0.5f);
// Create rendarTargetView and depthStencilView.
createViews();
} catch (Exception& ex) {
ex.display();
}
}
virtual void display()
{
try {
Direct3D10Device* d3d10Device = getD3D10Device();
DirectXGISwapChain* swapChain = getSwapChain();
if (renderTargetView && depthStencilView) {
//Clear renderTargetView and depthStencilView.
renderTargetView -> clear( D3DXCOLOR(0.8f, 0.8f, 0.8f, 0.0));
depthStencilView -> clear();
D3D10_TECHNIQUE_DESC techDesc;
effectTechnique -> getDesc(techDesc);
for( UINT i = 0; i < techDesc.Passes; i++ ) {
Direct3D10EffectPass pass = effectTechnique -> getPassByIndex(i);
pass.apply(0);
d3d10Device -> draw(vertexCount, 0);
}
}
swapChain -> present();
} catch (Exception& ex) {
ex.display();
}
}
public:
// Constructor
SimpleView(DirectX3D10MainView* parent, const TCHAR* name, Args& args)
:DirectX3D10View(parent, name, args),
renderTargetView(NULL),
depthStencilView(NULL),
effect(NULL),
effectTechnique(NULL),
vertexLayout(NULL),
vertexBuffer(NULL),
rasterizerState(NULL),
vertexCount(0)
{
postResizeRequest();
}
public:
~SimpleView()
{
delete effect;
delete effectTechnique;
delete rasterizerState;
delete vertexLayout;
delete vertexBuffer;
delete renderTargetView;
delete depthStencilView;
}
private:
void resize(int width, int height)
{
Direct3D10Device* d3d10Device = getD3D10Device();
DirectXGISwapChain* swapChain = getSwapChain();
if (d3d10Device == 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) {
ex.display();
}
}
};
//////////////////////////////////////////////////////
// Inner class ends.
private:
SimpleView* view;
public:
/**
* Constructor
*/
MainView(Application& applet, const TCHAR* name, Args& args)
:DirectX3D10MainView(applet, name,
args.set(XmNstyle, (ulong)WS_CLIPCHILDREN|WS_CLIPSIBLINGS) ),
view(NULL)
{
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()
{
delete view;
}
private:
void resize(int width, int height)
{
if (view) {
view -> reshape(0, 0, width, height);
view -> postResizeRequest(width, height);
}
}
};
}
//////////////////////////////////////////////
//
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = appName(argv[0]);
try {
Application applet(appClass, argc, argv);
Args args;
args.set(XmNwidth, 640);
args.set(XmNheight, 480);
MainView mainView(applet, appClass, args);
mainView.realize();
applet.run();
} catch (Exception& ex) {
ex.display();
}
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.