These days , I have read the <Intruduction to the 3D Game Programming with DirectX 9.0c : A shader Approach> . And when I do the exercise in the Chapter 11 Texture , I come across a problem . I want to rotate the texture coordinate to do the texture animation in the Flare.fx file .
Here is my code :
//-------------------------------------------------------------------------------------
// declaration : Copyright (c) , 2013 , XJ . All right reserved .
// brief : This effect file make a flare effect .
// author : XJ
// file : Flare.fx
// data : 2013 / 10 / 27
//--------------------------------------------------------------------------------------
//define the global variant
uniform extern float4x4 gWVP ;
uniform extern float gRotationFlare ;
uniform extern float gRotationBlend ;
uniform extern texture gTexFlare ;
uniform extern texture gTexBlend ;
//define the OutputVS
struct OutputVS
{
float4 posH: POSITION0 ;
float2 tex: TEXCOORD0 ;
float4 rotationFlare: COLOR0 ;
float4 rotationBlend: COLOR1 ;
};
//define the sampler
sampler BlendTex = sampler_state
{
Texture = <gTexBlend> ;
MinFilter = LINEAR ;
MagFilter = LINEAR ;
MipFilter = LINEAR ;
};
sampler FlareTex = sampler_state
{
Texture = <gTexFlare> ;
MinFilter = LINEAR ;
MagFilter = LINEAR ;
MipFilter = LINEAR ;
};
//define the Vertex Shader
OutputVS FlareVS(float3 posL: POSITION0, float2 tex: TEXCOORD0)
{
//Zero out the OutputVS
OutputVS outVS = (OutputVS) 0 ;
//Transform the local position to the homogenous clip space
outVS.posH = mul(float4(posL, 1.0f), gWVP);
outVS.tex = tex ;
//Compute the 2x2 matrix
float cosf = cos(gRotationFlare) ;
float sinf = sin(gRotationFlare) ;
float4 rotation = float4(cosf,-sinf,sinf,cosf);
outVS.rotationFlare = rotation ;
cosf = cos(gRotationBlend);
sinf = sin(gRotationBlend);
rotation = float4(cosf,-sinf,sinf,cosf);
outVS.rotationBlend = rotation ;
//Done
return outVS ;
}
//define the Pixel Shader
float4 FlarePS(float2 tex: TEXCOORD0, float4 rotationFlare:COLOR0, float4 rotationBlend:COLOR) : COLOR
{
float2 flare_tex = tex ;
float2 blend_tex = tex ;
//Translate the texture coordinate
flare_tex -= 0.5f ;
blend_tex -= 0.5f ;
//Rotate the texture coordinate
flare_tex = mul(flare_tex, float2x2(rotationFlare));
blend_tex = mul(blend_tex, float2x2(rotationBlend));
//Translate the texture coordinate
flare_tex += 0.5f ;
blend_tex += 0.5f ;
//Do some logic and get the texl color
float3 flare = (float3)0 ;
if(flare_tex.x < 0 || flare_tex.x > 1 || flare.y < 0 || flare.y > 1)
{
flare = float3(0.0f, 0.0f, 0.0f);
}
else
{
flare = tex2D(FlareTex, flare_tex).rgb ;
}
float3 blend = (float3) 0 ;
if(blend_tex.x < 0 || blend_tex.x > 1 || blend_tex.y < 0 || blend_tex.y > 1)
{
blend = float3(0.0f, 0.0f, 0.0f);
}
else
{
blend = tex2D(BlendTex, blend_tex).rgb ;
}
//Done.
return float4(flare*blend, 1.0f);
}
//define the technique
technique FlareTech
{
pass P0
{
vertexShader = compile vs_2_0 FlareVS();
pixelShader = compile ps_2_0 FlarePS();
}
}
This file will do the multi-texturing and rotate the texture code in the cube object .
Here is the problem , some time , the texture will be stretched not be rotated .Just like this :
[attachment=18574:QQ??20131028144534.jpg]
but actually , the real effect I want to get is this :
[attachment=18575:QQ??20131028142939.jpg]
just rotate the multi-textured texture , not stretched one .
So , is there anybody who can save me ?