Originally Posted By: txesmi
Thanks!

but should the rotation matrix be calculated in the CPU only once?


I believe that I have implemented your suggestion (in version 2 below) by doing the calculation in a material_init event. Thanks for the suggestion as it has helped me learn a lot more about the engine and coding!

Version 2 - Rotating a Texture in a Shader

Quote:

//=============================================================================
// Texture Coord Rotation
//=============================================================================
//
// Eric (Steempipe) Hendrickson-Lambert
// needs: 3DGS C/P DirectX 9 vs/ps_2_0
//
// 1/15/16 version 1
// 1/17/16 version 2
// changed code to calculate/initialize rotation matrix outside of
// shader in material event. recomendation by "txesmi"
//=============================================================================


BMAP* tex = "stone.tga";


function mtlEffect_init()

{

// example of using 4x4 matrix to rotate a texture in a shader
//

// tweakables to control how our texture is translated
var tex_rotation_in_degrees = 45; // 0-360 degrees in CW rotation.

// initialize our rotation matrix
float rotation_matrix[16] =
{ 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };



// apply necessary maths to our matrix
rotation_matrix[0] = cosv(tex_rotation_in_degrees);
rotation_matrix[1] = -sinv(tex_rotation_in_degrees);
rotation_matrix[4] = sinv(tex_rotation_in_degrees);
rotation_matrix[5] = cosv(tex_rotation_in_degrees);

mat_effect1 = rotation_matrix; // assign our matrix to a pointer for the shader

}

MATERIAL* mtlEffect =


{

event = mtlEffect_init; // set up the texture rotation matrix for the shader

skin1 = tex;

effect = "

//-------------------------------------
// non-tweakables
//-------------------------------------
float4x4 matWorldViewProj;
float4x4 matEffect1;


//-------------------------------------
// tweakables
//-------------------------------------
// rotation degree variable located in mtlEffect_init function

//-------------------------------------
// textures
//-------------------------------------

texture mtlSkin1;
sampler sTex = sampler_state
{
Texture = <mtlSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap;
AddressV = wrap;
};


//-------------------------------------
// structs
//-------------------------------------
struct VS_INPUT
{
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 PosWorld : POSITION;
float2 TexCoord0 : TEXCOORD0;

};

// ----------------------------------------------------------------
// vertexshader
// ----------------------------------------------------------------
VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;

float4 Position = mul(In.Position, matWorldViewProj);
Out.PosWorld = Position;
Out.TexCoord0 = In.TexCoord0.xy;

return Out;
}



// ----------------------------------------------------------------
// pixelshader
// ----------------------------------------------------------------
float4 ps_main(VS_OUTPUT In) : COLOR
{


// translate texture origin to the center
float2 rotated_tex = In.TexCoord0 -=0.5;

// rotate
rotated_tex = mul(In.TexCoord0.xy ,matEffect1);

// translate texture back to 0..1 range
rotated_tex += 0.5;

float4 oTexture = tex2D(sTex, rotated_tex * 2);

return oTexture;

}


// ----------------------------------------------------------------
// Technique
// ----------------------------------------------------------------
Technique tech1
{
Pass One
{
vertexshader = compile vs_2_0 vs_main();
pixelshader = compile ps_2_0 ps_main();
}


}


";

}

action mtl_texrot () {

my.material = mtlEffect;

}