Direct3D 中的纹理映射问题

发布于 2024-10-20 22:25:15 字数 2444 浏览 1 评论 0原文

嘿伙计们 我在通过着色器渲染纹理时遇到问题。我真的不知道为什么,着色器文件只是实现了一个简单的phong照明并在一个简单的四边形上插入纹理,但我得到的只是照明和材质颜色,就好像没有纹理一样(纹理是石头纹理但我得到的只是白色)

这是着色器文件,

//------------------------------------
uniform extern float4x4 matWVP;
uniform extern float4x4 matITW;
uniform extern float4x4 matW;
uniform extern float4 DiffMatr;
uniform extern float4 DiffLight;
uniform extern float4 AmbLight;
uniform extern float4 SpecLight;
uniform extern float4 AmbMatr;
uniform extern float3 LightPosW;
uniform extern float4 SpecMatr;
uniform extern float SpecPower;
uniform extern float3 EyePos;
uniform extern texture Tex;


//------------------------------------

sampler sTex = sampler_state
{
    Texture = <Tex>;
    Minfilter = LINEAR;
    Magfilter = LINEAR;
    Mipfilter = POINT;
    AddressU = WRAP;
    AddressV = WRAP;
};

struct vOut {
    float4 posH : POSITION0;
    float3 posW : TEXCOORD0;
    float3 normW: TEXCOORD1;
    float2 cTex : TEXCOORD2;
};

//------------------------------------
vOut VS_Def(float3 posL : POSITION0
 , float3 normL : NORMAL0
 , float2 cTex : TEXCOORD0)
{
    vOut V = (vOut)0;

    V.posH = mul(float4(posL, 1.0f), matWVP);
    V.posW = mul(float4(posL, 1.0f), matW).xyz;

    V.normW = mul(float4(normL, 1.0f), matITW).xyz;
    V.normW = normalize(V.normW);

    V.cTex = V.cTex;
    return V;

}

float4 PS_Def(float3 posW : TEXCOORD0
    ,float4 normW : TEXCOORD1
    ,float2 cTex : TEXCOORD2 ): COLOR
{
    float3 LightVec = LightPosW - posW;
    LightVec = normalize(LightVec);

    float3 RefLightVec = reflect(-LightVec, normW);

    float3 EyeVec = EyePos - posW;
    EyeVec = normalize(EyePos - posW);

    float d = max(0.0f, dot(normW, LightVec));
    float s = pow(max(dot(RefLightVec, EyeVec), 0.0f), SpecPower);

    float3 Diff = d * (DiffMatr * DiffLight).rgb;
    float3 Amb  =     (AmbMatr * AmbLight).rgb;
    float3 Spec = s * (SpecMatr * SpecLight).rgb;

    float3 TexColor = tex2D(sTex, cTex.xy).rgb;
    float3 color = Diff + Amb;

    return float4(color * TexColor + Spec, DiffMatr.a);;
}


//-----------------------------------
technique DefTech
{
    pass p0 
    {        
        VertexShader = compile vs_2_0 VS_Def();
        PixelShader = compile ps_2_0 PS_Def();

    }
}

照明和材质颜色如下: (PS:白色= D3DXCOLOR(1.0f,1.0f,1.0f,1.0f))

漫反射材质=白色; 环境材质 = 白色; 镜面材质 = 白色 * 0.5f;

漫射照明 = 白色 * 0.8f; 环境照明 = 白色 * 0.8f; 镜面照明 = 白色 * 0.5f; 镜面反射强度 = 48.0f;

感谢大家的帮助

hey guys
i am having trouble rendering textures through shaders. I really don't know why, the shader file just implements a simple phong lighting and interpolate the texture on a simple quad but all i am getting is the lighting and material colors, as if there is no texture(the texture is a Stone texture but all i am getting is white color)

here is the shader file

//------------------------------------
uniform extern float4x4 matWVP;
uniform extern float4x4 matITW;
uniform extern float4x4 matW;
uniform extern float4 DiffMatr;
uniform extern float4 DiffLight;
uniform extern float4 AmbLight;
uniform extern float4 SpecLight;
uniform extern float4 AmbMatr;
uniform extern float3 LightPosW;
uniform extern float4 SpecMatr;
uniform extern float SpecPower;
uniform extern float3 EyePos;
uniform extern texture Tex;


//------------------------------------

sampler sTex = sampler_state
{
    Texture = <Tex>;
    Minfilter = LINEAR;
    Magfilter = LINEAR;
    Mipfilter = POINT;
    AddressU = WRAP;
    AddressV = WRAP;
};

struct vOut {
    float4 posH : POSITION0;
    float3 posW : TEXCOORD0;
    float3 normW: TEXCOORD1;
    float2 cTex : TEXCOORD2;
};

//------------------------------------
vOut VS_Def(float3 posL : POSITION0
 , float3 normL : NORMAL0
 , float2 cTex : TEXCOORD0)
{
    vOut V = (vOut)0;

    V.posH = mul(float4(posL, 1.0f), matWVP);
    V.posW = mul(float4(posL, 1.0f), matW).xyz;

    V.normW = mul(float4(normL, 1.0f), matITW).xyz;
    V.normW = normalize(V.normW);

    V.cTex = V.cTex;
    return V;

}

float4 PS_Def(float3 posW : TEXCOORD0
    ,float4 normW : TEXCOORD1
    ,float2 cTex : TEXCOORD2 ): COLOR
{
    float3 LightVec = LightPosW - posW;
    LightVec = normalize(LightVec);

    float3 RefLightVec = reflect(-LightVec, normW);

    float3 EyeVec = EyePos - posW;
    EyeVec = normalize(EyePos - posW);

    float d = max(0.0f, dot(normW, LightVec));
    float s = pow(max(dot(RefLightVec, EyeVec), 0.0f), SpecPower);

    float3 Diff = d * (DiffMatr * DiffLight).rgb;
    float3 Amb  =     (AmbMatr * AmbLight).rgb;
    float3 Spec = s * (SpecMatr * SpecLight).rgb;

    float3 TexColor = tex2D(sTex, cTex.xy).rgb;
    float3 color = Diff + Amb;

    return float4(color * TexColor + Spec, DiffMatr.a);;
}


//-----------------------------------
technique DefTech
{
    pass p0 
    {        
        VertexShader = compile vs_2_0 VS_Def();
        PixelShader = compile ps_2_0 PS_Def();

    }
}

the lighting and material colors are as follows:
(P.S : WHITE = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f))

Diffuse Material = WHITE;
Ambient Material = WHITE;
Specular Material = WHITE * 0.5f;

Diffuse Lighting = WHITE * 0.8f;
Ambient Lighting = WHITE * 0.8f;
Specular Lighting = WHITE * 0.5f;
Specular Power = 48.0f;

Appreciate the help, guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-10-27 22:25:15

在你的顶点着色器中你有

V.cTex = V.cTex; 

这不应该是

V.cTex = cTex;

In your vertex shader you have

V.cTex = V.cTex; 

Shouldn't this be

V.cTex = cTex;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文