Jump to content
  • Advertisement
Sign in to follow this  
daerom

Implimenting Shaders, am I right?

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I've read several tutorials and looked over several examples of implimenting HLSL shaders, but I want to make sure I've got it down correctly.
//-------------------------------------------------------------------
// GLOBAL VARIABLES

float4x4 WorldViewProj : WorldViewProj;
float3   LightVector;                       //in object space; must be normalized
float3   EyePosition;                       //in object space
float    Ambient; 

texture  DiffuseMap;
texture  NormalMap;

//-------------------------------------------------------------------
// TEXTURE SAMPLERS

sampler DiffuseMapSampler = sampler_state 
{
    Texture   = <DiffuseMap>;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU  = WRAP;
    AddressV  = WRAP;
};

sampler NormalMapSampler = sampler_state 
{
    Texture   = <NormalMap>;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    MipFilter = LINEAR;
    AddressU  = WRAP;
    AddressV  = WRAP;
};

struct VS_INPUT {
    float4 Position : POSITION; //in object space
    float3 Normal   : NORMAL;   //in object space
    float2 TexCoord : TEXCOORD0;
    float3 T        : TEXCOORD1; //in object space
    float3 B        : TEXCOORD2; //in object space
    float3 N        : TEXCOORD3; //in object space
};

struct VS_OUTPUT_DIFFUSE {
    float4 Position    : POSITION; //in projection space
    float2 TexCoord0   : TEXCOORD0;
    float2 TexCoord1   : TEXCOORD1;
    float3 Normal      : TEXCOORD2;   //in tangent space
    float3 LightVector : TEXCOORD3;   //in tangent space
};


VS_OUTPUT_DIFFUSE Diffuse_VS(VS_INPUT IN)
{
    VS_OUTPUT_DIFFUSE OUT;

    // pass texture coordinates for fetching the diffuse map
    OUT.TexCoord0 = IN.TexCoord.xy;

    // pass texture coordinates for fetching the normal map
    OUT.TexCoord1 = IN.TexCoord.xy;

    // compute the 3x3 tranform from tangent space to object space; we will 
    //   use it "backwards" (vector = mul(matrix, vector) to go from object 
    //   space to tangent space, though.
    float3x3 objToTangentSpace;
    objToTangentSpace[0] = IN.T;
    objToTangentSpace[1] = IN.B;
    objToTangentSpace[2] = IN.N;

    // transform normal from object space to tangent space, and pack into [0..1] range.
    // SHORTCUT: the tangent-space normal is, by definition, <0,0,1>, so we
    //     don't really have to transform it.  (Packed, <0,0,1> is <0.5,0.5,1>).
    OUT.Normal = float3(0.5,0.5,1);

    // transform light vector from object space to tangent space, and pack into [0..1] range
    OUT.LightVector = normalize(mul(objToTangentSpace, LightVector.xyz)) * 0.5 + 0.5.xxx;

    // transform position to projection space
    OUT.Position = mul(IN.Position, WorldViewProj);

    return OUT;
}

float4 BumpDiffuse_PS(VS_OUTPUT_DIFFUSE IN) : COLOR
{
    //fetch base color
    float4 color = tex2D(DiffuseMapSampler, IN.TexCoord0.xy);

    //fetch bump normal and unpack it to [-1..1] range
    float3 bumpNormal = 2 * tex2D(NormalMapSampler, IN.TexCoord1.xy) - 1;

    //compute diffuse lighting coefficient
    float diffuse = saturate(dot(bumpNormal, 2 * IN.LightVector - 1));
    
    //compute self-shadowing term
    float shadow = saturate(4 * dot(2 * IN.Normal.xyz - 1, 2 * IN.LightVector.xyz - 1));
    
    //compute final color
    return color * shadow * saturate(diffuse + Ambient);
}



Technique Bump
{
    Pass P0
    {
        VertexShader = compile vs_1_1 Diffuse_VS();
        PixelShader  = compile ps_1_1 BumpDiffuse_PS();
        
        AlphaBlendEnable = False;
    }
}


Lets say I want to impliment the above NVIDIA sample shader. I am not at my programming station, so I don't quite remember each function, but if you could let me know if this is the general jist of how to do it, I'd appreciate it. m_Effect->SetTechnique("Bump"); m_Effect->Begin(); for each mesh using Bump Calculate variables per mesh m_Effect->SetMatrix("WorldViewProj",&matWorldViewProj); m_Effect->SetValue("LightVector",&fLight,sizeof(float)*3); m_Effect->SetValue("EyePosition",&fEyePos,sizeof(float)*3); m_Effect->SetFloat("Ambient",&fAmbient); m_Effect->SetTexture("DiffuseMap",&m_DiffuseMap); m_Effect->SetTexture("NormalMap",&m_NormalMap); m_Effect->SetValue("Position",&vPosition,sizeof(float)*4); m_Effect->SetValue("Normal",&vNormal,sizeof(float)*3); m_Effect->SetValue("TexCoord",&vTexCoords,sizeof(float)*2); m_Effect->SetValue("T",&fTangent,sizeof(float)*3); m_Effect->SetValue("B",&fB,sizeof(float)*3); m_Effect->SetValue("N",&fN,sizeof(float)*3); Mesh->Draw(); next m_Effect->End(); Assuming I properly initialized m_Effect and calculated the right values, would that work? or do I have it all wrong? Also, I am not totally sure what T,B, and N are, but I think they are Tangent, Binormal, and something, but what? Thanks for the help.

Share this post


Link to post
Share on other sites
Advertisement
Please use [source] tags when posting large amounts of code! [lol]

...you could always test it out to see if it works...

Good luck with that! (I use VB6 = no shaders 4 me... [depressed])

Share this post


Link to post
Share on other sites
Heh... thanks, I accidently did [sorce]... Well, I would try it out by I am currently 20 miles away from my developing computer because I am at work. I just wanted to see if I had it right before I did try and impliment the shader.

Share this post


Link to post
Share on other sites
Quote:
Original post by daerom
Heh... thanks, I accidently did [sorce]... Well, I would try it out by I am currently 20 miles away from my developing computer because I am at work. I just wanted to see if I had it right before I did try and impliment the shader.


You need to call Pass and EndPass

Share this post


Link to post
Share on other sites
That's right... I forgot about that... don't have my source in front of me. So, other than that, that should work?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!