基本上,绘制的矩形是黑色的,这意味着输入到RenderColorBox
着色器方法的UV坐标始终为零。绝对是UV坐标,因为我可以很好地更改着色器的输出颜色。使用return float4(1,0,0, 1);
会导致出现一个红色矩形。
着色器代码:
struct VSOUT
{
float4 vertPos : POSITION;
float2 UVCoord : TEXCOORD0;
};
float4 RenderColorBox(VSOUT IN) : COLOR0
{
float2 uvCoord = IN.UVCoord;
// Go from black to red as x UV increases rightward.
return float4(uvCoord.x,0,0, 1);
}
相关的Shader创建C ++
ID3DXBuffer* errors = 0;
LPD3DXBUFFER dxShaderCode = 0x0;;
LPD3DXCONSTANTTABLE constantTable;
if (FAILED(D3DXCompileShaderFromFileA(GetShaderPath().c_str(), NULL, NULL, "RenderColorBox", "ps_3_0", 0, &dxShaderCode, &errors, &constantTable))) {
if (errors) {
MessageBoxA(NULL, (char*)errors->GetBufferPointer(), "Shader Compilation Error!", MB_ICONERROR);
errors->Release();
}
oaassert(false);
}
if (errors)
errors->Release();
oaassert_hresult(pDevice->CreatePixelShader((DWORD*)dxShaderCode->GetBufferPointer(), &pPixelShader));
和绘图:
// FVF : (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
struct IMDRAWLISTVERTEX
{
float pos[3];
D3DCOLOR col;
float uv[2];
};
IMDRAWLISTVERTEX rectVertices[4];
OAVec2 startPos = drawData->posSize.ConvertToVec2()[0];
OAVec2 rectSize = drawData->posSize.ConvertToVec2()[1];
IMDRAWLISTVERTEX quad[4] = {
CreateImVertex(startPos, 0, 0),
CreateImVertex(startPos + rectSize.GetHorizVector(), 1, 0),
CreateImVertex(startPos + rectSize.GetVertVector(), 0, 1),
CreateImVertex(startPos + rectSize, 1, 1)
};
auto pixelShader = drawData->shader->pPixelShader;
pDevice->SetPixelShader(pixelShader);
pDevice->DrawPrimitiveUP(D3DPRIMITIVETYPE::D3DPT_TRIANGLESTRIP, 2, &quad, sizeof(quad[0]));
pDevice->SetPixelShader(NULL);
以及在绘制之前设置的设备渲染状态(我认为这可能是问题所在,但似乎找不到任何有效的方法)
pDevice->SetPixelShader(NULL);
pDevice->SetVertexShader(NULL);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pDevice->SetRenderState(D3DRS_LIGHTING, false);
pDevice->SetRenderState(D3DRS_ZENABLE, false);
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, false);
pDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
pDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, true);
pDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
修改 我能够通过使用另一个项目中的顶点声明代码来解决此问题。我仍然不知道为什么上面的代码无法正常运行,并希望有人能为此提供可靠的答案。
const D3DVERTEXELEMENT9 vertexElements[3] = {
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
仅使用上述方法并调用设备函数(例如CreateVertexDeclaration,SetVertexDeclaration),然后在传递到着色器的正确uv坐标中绘制基本结果。