在延迟渲染里面,会把结果绘制到纹理里面,然后再把纹理绘制到一个四顶点的平面上。所以,这里把一个比较简单的纹理绘制贴出来,以供以后方便使用。
PWProcess:
#include "StdAfx.h"
#include "PWProcess.h"
PWProcess::PWProcess(void)
:mVertexCount(0),
mIndexCount(0),
mVertexShader(0)
,mPixelShader(0)
,mInputLayout(0)
{
}
PWProcess::~PWProcess(void)
{
}
void PWProcess::init(ID3D11Device* device)
{
VertexBuffer* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
mVertexCount = 4;
mIndexCount = 6;
// 创建顶点临时缓冲.
vertices = new VertexBuffer[mVertexCount];
// 创建索引缓冲.
indices = new unsigned long[mIndexCount];
vertices[0].position = Vector3(-1.0f, 1.0f, 0.5f); //1 顺时针
vertices[1].position = Vector3(1.0f, 1.0f, 0.5f); // 2
vertices[2].position = Vector3(-1.0f, -1.0f, 0.5f); // 3
vertices[3].position = Vector3(1.0f, -1.0f, 0.5f); // 4
vertices[0].texture = Vector2(0.0f, 0.0f); // 012 021 102 120 201 210
vertices[1].texture = Vector2(1.0f, 0.0f);
vertices[2].texture = Vector2(0.0f, 1.0f);
vertices[3].texture = Vector2(1.0f, 1.0f);
/*float2(-1, +1),
float2(+1, +1),
float2(-1, -1),
float2(+1, -1)*/
// 顺时针方向的三角形
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 2; indices[4] = 1; indices[5] = 3;
// 设置顶点缓冲描述
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexBuffer) * mVertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// 指向保存顶点数据的临时缓冲.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// 创建顶点缓冲.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &mVertexBuffer);
// 设置索引缓冲描述.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * mIndexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// 指向存临时索引缓冲.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// 创建索引缓冲.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &mIndexBuffer);
// 释放临时缓冲.
delete [] vertices;
vertices = 0;
delete [] indices;
indices = 0;
ID3D10Blob* errorMessage;
ID3D10Blob* vertexShaderBuffer;
ID3D10Blob* pixelShaderBuffer;
// 初始化指针为空.
errorMessage = 0;
vertexShaderBuffer = 0;
pixelShaderBuffer = 0;
// 编译vs代码.
D3DX11CompileFromFile(L"shader\\Texture.hlsl", NULL, NULL, "PortVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL,
&vertexShaderBuffer, &errorMessage, NULL);
// 编译ps.
D3DX11CompileFromFile(L"shader\\Texture.hlsl", NULL, NULL, "PortPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL,
&pixelShaderBuffer, &errorMessage, NULL);
// 从缓冲创建vs shader.
device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &mVertexShader);
// 从缓冲创建ps shader.
device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &mPixelShader);
// 设置数据布局,以便在shader中使用.
createInputLayout(device,vertexShaderBuffer);
//释放顶点和像素缓冲.
vertexShaderBuffer->Release();
vertexShaderBuffer = 0;
pixelShaderBuffer->Release();
pixelShaderBuffer = 0;
}
void PWProcess::createInputLayout(ID3D11Device* device,ID3D10Blob* vertexShaderBuffer)
{
unsigned int numElements;
D3D11_INPUT_ELEMENT_DESC polygonLayout[] =
{
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0},
{"NORMAL",0,DXGI_FORMAT_R32G32B32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0},
{"TANGENT",0,DXGI_FORMAT_R32G32B32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0},
{"BINORMAL",0,DXGI_FORMAT_R32G32B32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0},
{"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,D3D11_APPEND_ALIGNED_ELEMENT,D3D11_INPUT_PER_VERTEX_DATA,0}
};
// 得到layout中的元素数量
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
// 创建顶点输入布局.
device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
vertexShaderBuffer->GetBufferSize(), &mInputLayout);
}
void PWProcess::release()
{
// 释放顶点缓冲.
if(mIndexBuffer)
{
mIndexBuffer->Release();
mIndexBuffer = 0;
}
// 释放索引缓冲
if(mVertexBuffer)
{
mVertexBuffer->Release();
mVertexBuffer = 0;
}
//释放顶点布局t.
if(mInputLayout)
{
mInputLayout->Release();
mInputLayout = 0;
}
// 释放ps .
if(mPixelShader)
{
mPixelShader->Release();
mPixelShader = 0;
}
// 释放vs
if(mVertexShader)
{
mVertexShader->Release();
mVertexShader = 0;
}
}
void PWProcess::render(ID3D11DeviceContext* immediateContext)
{
unsigned int stride = sizeof(VertexBuffer);
unsigned int offset = 0;
immediateContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset); // f58c
//在input assemberl阶段绑定索引缓冲,以便能够被渲染
immediateContext->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R32_UINT, 0); // f88c
// 设置体元语义,渲染线段,画出坐标轴
immediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绑定顶点布局.
immediateContext->IASetInputLayout(mInputLayout);
// 设置渲染使用vs和ps.
immediateContext->VSSetShader(mVertexShader, NULL, 0);
immediateContext->PSSetShader(mPixelShader, NULL, 0);
// 渲染三角形
immediateContext->DrawIndexed(mIndexCount, 0, 0);
}
Texture.hlsl :
Texture2D mTexture;
SamplerState mSampleType;
cbuffer MatrixBuffer
{
matrix world;
matrix transform;
};
struct VertexInputType
{
float4 viPosition : POSITION;
float2 viTexture : TEXCOORD; //纹理坐标
};
struct PixelInputType
{
float4 piPosition : SV_POSITION;
float2 piTexture : TEXCOORD0; //纹理坐标
};
// Vertex Shader
PixelInputType PortVertexShader(VertexInputType input)
{
PixelInputType output;
// 改变顶点为四个分量其次坐标.
input.viPosition.w = 1.0f;
output.piPosition = input.viPosition;
//output.piPosition = mul(output.piPosition, transform);
output.piTexture= input.viTexture; // 纹理坐标
return output;
}
// Pixel Shader
float4 PortPixelShader(PixelInputType input) : SV_TARGET
{
float4 color = mTexture.Sample(mSampleType, input.piTexture);
return color;
}