Directx11学习笔记【十一】 画一个简单的三角形--effect框架的使用

这里不再介绍effect框架的具体使用,有关effect框架使用可参考http://www.cnblogs.com/zhangbaochong/p/5475961.html

实现的功能依然是画一个简单的三角形,只不过使用了effect框架。

为了体现使用effect框架方便变量绑定的优点,我们对着色器代码做了修改,增加了一个常量float4x4 gWorldViewProj

cbuffer cbPerObject
{
float4x4 gWorldViewProj;
}; float4 VS_Main( float4 pos : POSITION ) : SV_POSITION {
pos = mul(pos,gWorldViewProj);
return pos; } float4 PS_Main( float4 pos : SV_POSITION ) : SV_TARGET { return float4( 1.0f, 0.0f, 0.0f, 1.0f ); } technique11 ColorTech
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS_Main() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_5_0, PS_Main() ) );
}
}

由于代码很简单,这里不再给出解释,直接看代码吧!

下面是修改后的TriangleDemo.h

 #pragma once

 #include "Dx11DemoBase.h"
#include "d3dx11effect.h" class TriangleDemo : public Dx11DemoBase
{
public:
TriangleDemo();
~TriangleDemo(); bool LoadContent() override;
void UnLoadContent() override; void Update(float dt) override;
void Render() override; private:
ID3D11Buffer *m_pVertexBuffer;
ID3D11InputLayout *m_pInputLayout; ID3DX11Effect *m_pFx;
ID3DX11EffectTechnique *m_pTechnique;
ID3DX11EffectMatrixVariable *m_pFxWorldViewProj;
XMFLOAT4X4 m_world;
XMFLOAT4X4 m_view;
XMFLOAT4X4 m_proj; };

TriangleDemo.cpp

 #include "TriangleDemo.h"

 struct VertexPos
{
XMFLOAT3 pos;
}; TriangleDemo::TriangleDemo()
: m_pInputLayout(), m_pVertexBuffer()
{
XMMATRIX I = XMMatrixIdentity();
XMStoreFloat4x4(&m_world, I);
XMStoreFloat4x4(&m_view, I);
XMStoreFloat4x4(&m_proj, I);
} TriangleDemo::~TriangleDemo()
{
} void TriangleDemo::UnLoadContent()
{
if (m_pVertexBuffer)
m_pVertexBuffer->Release();
if (m_pInputLayout)
m_pInputLayout->Release();
if (m_pFx)
m_pFx->Release();
m_pVertexBuffer = ;
m_pInputLayout = ;
m_pFx = ;
} bool TriangleDemo::LoadContent()
{
HRESULT result;
VertexPos vertices[] =
{
XMFLOAT3(0.5f, 0.5f, 0.5f),
XMFLOAT3(0.5f, -0.5f, 0.5f),
XMFLOAT3(-0.5f, -0.5f, 0.5f)
}; D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(VertexPos)* ; D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
resourceData.pSysMem = vertices; result = m_pd3dDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer);
if (FAILED(result))
{
return false;
} DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined _DEBUG || defined DEBUG
shaderFlags = D3DCOMPILE_DEBUG;
#endif ID3D10Blob *compiledShader = ;
ID3D10Blob *compilationMsgs = ;
result = D3DX11CompileFromFile("SolidColor.fx", , , , "fx_5_0", shaderFlags,
, , &compiledShader, &compilationMsgs, );
if (compilationMsgs != )
{
MessageBox(, (char*)compilationMsgs->GetBufferPointer(), , );
compilationMsgs->Release();
compilationMsgs = ;
}
if (FAILED(result))
{
MessageBox(, "载入着色器错误", , );
return false;
} result = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),
, m_pd3dDevice, &m_pFx);
compiledShader->Release();
if (FAILED(result))
{
MessageBox(, "载入着色器失败", , );
return false;
}
m_pTechnique = m_pFx->GetTechniqueByName("ColorTech");
m_pFxWorldViewProj = m_pFx->GetVariableByName("gWorldViewProj")->AsMatrix(); D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", , DXGI_FORMAT_R32G32B32_FLOAT, , , D3D11_INPUT_PER_VERTEX_DATA, }
};
UINT numLayoutElements = ARRAYSIZE(solidColorLayout);
D3DX11_PASS_DESC passDesc;
m_pTechnique->GetPassByIndex()->GetDesc(&passDesc); result = m_pd3dDevice->CreateInputLayout(solidColorLayout, numLayoutElements, passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &m_pInputLayout); return true;
} void TriangleDemo::Update(float dt)
{ } void TriangleDemo::Render()
{
if (m_pImmediateContext == )
return;
//清除渲染目标视图
float clearColor[] = { 0.5f, 0.5f, 0.5f, 1.0f };//背景颜色
m_pImmediateContext->ClearRenderTargetView(m_pRenderTargetView, clearColor); UINT stride = sizeof(VertexPos);
UINT offset = ;
//设置数据信息格式控制信息
m_pImmediateContext->IASetInputLayout(m_pInputLayout);
//设置要绘制的几何体信息
m_pImmediateContext->IASetVertexBuffers(,,&m_pVertexBuffer,&stride,&offset);
//指明如何绘制三角形
m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //设置常量
XMMATRIX world = XMLoadFloat4x4(&m_world);
XMMATRIX view = XMLoadFloat4x4(&m_view);
XMMATRIX proj = XMLoadFloat4x4(&m_proj);
XMMATRIX worldViewProj = world*view*proj;
m_pFxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj)); D3DX11_TECHNIQUE_DESC techDesc;
m_pTechnique->GetDesc(&techDesc);
for (UINT i = ; i < techDesc.Passes; ++i)
{
m_pTechnique->GetPassByIndex(i)->Apply(, m_pImmediateContext);
m_pImmediateContext->Draw(, );
}
//马上输出
m_pSwapChain->Present(, );
}

运行结果同之前的一样,不再贴图了

上一篇:python urllib


下一篇:利用nginx实现负载均衡