0

我创建了一个标准的 Win32 DirectX9 窗口,并使用自定义效果对其进行渲染,但是我遇到了一个问题,即结果中没有插入顶点的颜色。

void CRender::Begin()
{
    perf.begin();

    // Capture device state so it can be restored later.
    // We use ID3DXLine::Begin() to fix some bugs that I don't know how to fix.
    mpLine->Begin();

    // Setup shader
    shader.Begin( static_cast<float>(FloatTime()) );
}
void CRender::End()
{
    // Reverse order of Begin()
    shader.End();
    mpLine->End();
}

这里的问题在于 mpLine->Begin(),不调用它我得到一个非常好的插值三角形,整个三角形与第一个顶点具有相同的颜色。图片澄清:http: //i.imgur.com/vKN4SnE.png

我使用 ID3DXLine::Begin() 只是为我设置设备状态。我使用它的原因是因为我通过挂钩 EndScene() 在另一个程序(游戏)的上下文中进行渲染。游戏可能会使设备处于无法使用的状态,从而导致我的叠加层出现渲染故障,使用 ID3DXLine::Begin() 时,所有这些问题都会消失,除了不再插入顶点颜色。

顶点声明:

// Create the vertex declaration for use with the shaders.
static const D3DVERTEXELEMENT9 vformat[] =
{
    { 0, 0,  D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
    { 0, 8,  D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,    0 },
    { 0, 12, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
    D3DDECL_END()
};
HRESULT hr = dev->CreateVertexDeclaration( vformat, &decl );

效果来源:

// Vertex shader input
struct VSIN
{
    float2 coord  : POSITION;
    float4 color  : COLOR0;
    float2 tex    : TEXCOORD0;
};
// Vertex shader output / Pixel shader input
struct VSOUT
{
    float4 coord  : POSITION;
    float4 color  : COLOR0;
    float2 tex    : TEXCOORD0;
    float2 pos    : TEXCOORD1;
};

uniform float2 screen;
uniform float2x4 project;

float4 vstransform( float2 coord, const float2 shift )
{
    float2 final = ( mul( project, float4(coord.x,coord.y,1,1) ) + shift ) * 2 / screen;
    return float4( final.x-1, 1-final.y, 0, 1 );
}

VSOUT vsfix( VSIN data )
{
    VSOUT vert;
    const float2 shift = { -0.5f, -0.5f };
    vert.coord = vstransform( data.coord, shift );
    vert.color = data.color;
    vert.tex = data.tex;
    vert.pos = vert.coord.xy;
    return vert;
}

float4 diffuse( VSOUT vert ) : COLOR
{
    float4 px = vert.color;
    return px;
}

technique Diffuse
{
    pass p0
    {
        PixelShader = compile ps_2_0 diffuse();
        VertexShader = compile vs_2_0 vsfix();
    }
}
4

0 回答 0