如何将纹理传递到DirectX 9像素着色器? [英] How to pass textures to DirectX 9 pixel shader?

查看:86
本文介绍了如何将纹理传递到DirectX 9像素着色器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像素着色器

// fxc.exe tiles.fs /T ps_3_0 /Fotiles.fsc /Fctiles.fsl

struct PSInput
{   
    float4 Pos : TEXCOORD0;
    float3 Normal : TEXCOORD1;
    float2 TexcoordUV : TEXCOORD2;
    float2 TexcoordST : TEXCOORD3;
};

sampler2D sampler0; //uniform 
sampler2D sampler1; //uniform 
sampler2D sampler2; //uniform 
sampler2D sampler3; //uniform 
sampler2D alphamap1;//uniform 
sampler2D alphamap2;//uniform 
sampler2D alphamap3;//uniform 
uniform int tex_count = 0;

uniform float4 color_ambient = float4(0.75, 0.75, 0.75, 1.0);
uniform float4 color_diffuse = float4(0.25, 0.25, 0.25, 1.0);
uniform float4 color_specular = float4(1.0, 1.0, 1.0, 1.0);
uniform float shininess = 77.0f;
uniform float3 light_position = float3(12.0f, 32.0f, 560.0f);

float4 main(PSInput In) : COLOR
{
    float3 light_direction = normalize(light_position - (float3)In.Pos);
    float3 normal = normalize(In.Normal);
    float3 half_vector = normalize(light_direction + normalize((float3)In.Pos));
    float diffuse = max(0.0, dot(normal, light_direction));
    float specular = pow(max(0.0, dot(In.Normal, half_vector)), shininess);
    float4 color = tex2D(sampler0, In.TexcoordUV);

    if (tex_count > 0){
        float4 temp = tex2D(sampler1, In.TexcoordUV);
        float4 amap = tex2D(alphamap1, In.TexcoordST);
        color = lerp(color, temp, amap.a);
    }
    if (tex_count > 1){
        float4 temp = tex2D(sampler2, In.TexcoordUV);
        float4 amap = tex2D(alphamap2, In.TexcoordST);
        color = lerp(color, temp, amap.a);
    }
    if (tex_count > 2){
        float4 temp = tex2D(sampler3, In.TexcoordUV);
        float4 amap = tex2D(alphamap3, In.TexcoordST);
        color = lerp(color, temp, amap.a);
    }
    color = color * color_ambient + diffuse * color_diffuse + specular * color_specular;

    return color;
}

顶点着色器

// fxc.exe tiles.vs /T vs_3_0 /Fotiles.vsc /Fctiles.vsl

struct VSInput
{
    float3 Pos : POSITION;
    float3 Normal : NORMAL;
    float2 TexcoordUV : TEXCOORD0;
    float2 TexcoordST : TEXCOORD1;
};

struct PSInput
{   
    float4 Pos : POSITION;
    float3 Normal : TEXCOORD0;
    float2 TexcoordUV : TEXCOORD1;
    float2 TexcoordST : TEXCOORD2;
};

uniform matrix modelMatrix;
uniform matrix projectionMatrix;
uniform matrix lookAtMatrix;

PSInput main(VSInput In)
{
    PSInput Out = (PSInput) 0;

    //projectionMatrix * lookAtMatrix * modelMatrix;
    matrix MVP = mul(modelMatrix, lookAtMatrix); 
    MVP = mul(MVP, projectionMatrix); 

    Out.Normal = mul(In.Normal, (float3x3)modelMatrix);
    Out.Pos = mul(float4(In.Pos, 1.0), MVP);    
    Out.TexcoordUV = In.TexcoordUV;
    Out.TexcoordST = In.TexcoordST;

    return Out;
}

在OpenGL + GLSL下工作相同,但用lerp代替了mix(我希望它是正确的). 例如,来自 http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html 我通过以下方式传递纹理:

same works under OpenGL + GLSL except mix replaced by lerp (I hope its correct). By example from http://www.two-kings.de/tutorials/dxgraphics/dxgraphics18.html I passing textures with:

ps_pConstantTable.SetInt(m_pD3DDevice, texCountHandle, 0);
for i := 0 to texCount - 1 do begin
  tBlp := texture_buf[cx, cy][i];
  if tBlp = nil then
      break;

  m_pD3DDevice.SetTexture(i, tBlp.itex);
  ps_pConstantTable.SetInt(m_pD3DDevice, texCountHandle, i);

  if i > 0 then begin
    // this time, use blending:
    m_pD3DDevice.SetTexture(i + 3, AlphaMaps[cx, cy][i]);
  end;
end;

因此,顺序纹理的索引为0-3和alpha 4-6(最大texCount 4). 问题是:我可以看到网格(地形),但是它是纯黑色的.我是否需要其他设置(在没有着色器的情况下,在分配材质和灯光之前,它也是黑色的)?我可以传递这样的纹理吗?我可以将sampler2D设置为uniform(如何)吗?

so ordinal textures have indices 0-3 and alpha 4-6 (max texCount 4). The problem is: I can see mesh (terrain) but it is solid black. Am I need something else to set (without shaders it also was black until I assigned materials and light)? Can I pass textures like that? Can I do this with sampler2D as uniform (how)?

示例,其中包含源,着色器,一些使用的纹理和Alpha贴图,在Filebeam http://fbe处具有法线的顶点数据. am/nm4 添加了.越小越好.还包含DXErr9ab.dll来记录错误.

example with sources, shaders, several used textures and alphamaps, vertex data with normals at filebeam http://fbe.am/nm4 added. As small as possible. Also contains DXErr9ab.dll to log errors.

推荐答案

要在像素着色器中使用纹理,您可以按照以下步骤操作

To use texture in pixel shader, you may following below steps

通过D3DXCreateTextureFromFile或其他函数在C/C ++文件中创建纹理.

Create texture in your C/C++ file by D3DXCreateTextureFromFile or other functions.

if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "FaceTexture.jpg",
    &g_pTexture ) ) )
return E_FAIL;

声明一个D3DXHANDLE并将其与着色器文件中的纹理关联.(您应该在此步骤之前编译效果文件,effects_这里是指向ID3DXEffect的指针)

Declare a D3DXHANDLE and associate it with the texture in your shader file.(you should compile your effect file before this step, effects_ here is a pointer to ID3DXEffect)

texture_handle = effects->GetParameterByName(0, "FaceTexture");

在渲染功能中设置纹理

effects_->SetTexture(texture_handle, g_pTexture);

在着色器文件中声明纹理

Declare a texture in your shader file

纹理FaceTexture;

texture FaceTexture;

在着色器文件中声明一个采样器

Declare a sampler in your shader file

// Face texture sampler
sampler FaceTextureSampler = sampler_state
{
    Texture   = <FaceTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

在像素着色器功能中进行采样

Do sampling in your pixel shader function

float4 BasicPS(OutputVS outputVS) : COLOR
{
    float4 Output;
    Output = FaceTexture.Sample(FaceTextureSampler, outputVS.texUV);
    return Output;
}

如果已安装DirectX SDK,建议您看一下示例"BasicHLSL",其中对Vertex着色器和Pixel着色器(包括纹理)进行了非常基本的介绍.

If you have DirectX SDK installed, I recommend you to take a look at the sample "BasicHLSL" which has a very basic introduction of Vertex shader and Pixel shader(including texture).

这篇关于如何将纹理传递到DirectX 9像素着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆