Question

I have pixel shader

// 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;
}

vertex shader

// 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;
}

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;

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)?

Edit: 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.

Était-ce utile?

La solution

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

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

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

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");

Set the texture in render function

effects_->SetTexture(texture_handle, g_pTexture);

Declare a texture in your shader file

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;
}

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).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top