• Advertisement
Sign in to follow this  

Simple shader

This topic is 4140 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

Hello everyone I new to shaders and I have a problem with the one I'm writing. In theory I should only transform the vertex and render the texture. I'm trying to change an engine I got in a book so it can support shaders. I'm using the effecs framework from directx. Now, I'm having trouble manipulating the matrices and when I try to move the camera in real time nothing happens. this is the HSLS code
//------------------------------------
float4x4 matWorldViewProj : WorldViewProjection;
float4x4 matWorldView  : ViewInverse;
float4x4 MatITWorldView : WorldInverseTranspose;
float4x4 matWorld  : World;

// Material Texture.
texture diffuseTexture : Diffuse;

//------------------------------------
struct vertexInput {
    float3 Position				: POSITION;
    float3 Normal				: NORMAL;
    float4 Texture				: TEXCOORD0;
};

struct vertexOutput {
    float4 Position				: POSITION;
    float4 Texture				: TEXCOORD0;
    float3 Normal				: TEXCOORD1;
    float3 View					: TEXCOORD3;
};
//------------------------------------
vertexOutput VS_TransformDiffuse(vertexInput IN) 
{
	vertexOutput OUT;
    	OUT.Position 		= mul( float4(IN.Position.xyz , 1.0) , matWorldViewProj);
 	float3 worldEyePos = matWorldView[3].xyz;
    	float3 worldVertPos = mul(IN.Position, matWorld).xyz;
	OUT.Normal = normalize(mul(IN.Normal, matWorld)); //normal vector
	OUT.View = normalize(worldEyePos - worldVertPos); //eye vector
    	OUT.Texture 		= IN.Texture;
    	return OUT;
}

//------------------------------------
sampler TextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{
  float4 diffuseTexture = tex2D( TextureSampler, IN.Texture );
  return diffuseTexture;
}

//-----------------------------------
technique nothing
{
    pass p0 
    {		
		VertexShader = compile vs_1_1 VS_TransformDiffuse();
		PixelShader  = compile ps_1_1 PS_Textured();
    }
}


Share this post


Link to post
Share on other sites
Advertisement
Got it, here is the shader for doing it and I added a technique to make it monochrome.


// MARVIT ENGINE FX FILE.
string description = "MARVIT ENGINE FX'S";

//------------------------------------
// Our Matrices.
//------------------------------------
float4x4 matWorldViewProj : WorldViewProjection;
float4x4 matView : ViewInverse;
float4x4 matWorld : World;
float4x4 matProj : Projection;

//------------------------------------
// Our Texture.
//------------------------------------
texture diffuseTexture : Diffuse;

//------------------------------------
// Our Texture.
//------------------------------------
float3 LightDir[3]; // Light's direction in world space
float4 LightDiffuse[3]; // Light's diffuse color
float4 LightAmbient; // Light's ambient color

//------------------------------------
// Vertex Input Structure.
//------------------------------------
struct vertexInput {
float3 Position : POSITION;
float3 Normal : NORMAL;
float4 Texture : TEXCOORD0;
};

//------------------------------------
// Vertex Output Structure.
//------------------------------------
struct vertexOutput {
float4 Position : POSITION;
float4 Texture : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 View : TEXCOORD3;
};

//------------------------------------
// Normal Vertex Shader Routine.
//------------------------------------
vertexOutput VS_TransformDiffuse(vertexInput IN)
{
vertexOutput OUT;
matWorldViewProj = mul(matWorld, mul(matView, matProj));
OUT.Position = mul( float4(IN.Position.xyz , 1.0) , matWorldViewProj);
float3 worldEyePos = matWorld[3].xyz;
float3 worldVertPos = mul(IN.Position, matWorld).xyz;
OUT.Normal = normalize(mul(IN.Normal, matWorld)); //normal vector
OUT.View = normalize(worldEyePos - worldVertPos); //eye vector
OUT.Texture = IN.Texture;
return OUT;
}

//------------------------------------
// Object Texture Sampler.
//------------------------------------
sampler ObjectSampler = sampler_state
{
texture = <diffuseTexture>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

//------------------------------------
// Scene Texture Sampler.
//------------------------------------
sampler2D SceneSampler =
sampler_state
{
Texture = <diffuseTexture>;
AddressU = Wrap;
AddressV = Wrap;
MinFilter = Point;
MagFilter = Linear;
MipFilter = Linear;
};

//-----------------------------------
// Normal Pixel Shader Routine.
//-----------------------------------
float4 PS_Textured( vertexOutput IN ): COLOR
{
float4 diffuseTexture = tex2D( SceneSampler, IN.Texture );
return diffuseTexture;
}

//------------------------------------
// Monochrome Pixel Shader Routine.
//------------------------------------
float4 PS_Monochrome ( vertexOutput IN ) : COLOR0
{
float4 LuminanceConv = { 0.2125f, 0.7154f, 0.0721f, 1.0f };
float grayScale = dot( (float3)tex2D( SceneSampler, IN.Texture ), LuminanceConv );
return float4(grayScale,grayScale,grayScale,1.0);
}

//-----------------------------------
// Normal Rendering Technique.
//-----------------------------------
technique normal
{
pass p0
{
VertexShader = compile vs_1_1 VS_TransformDiffuse();
PixelShader = compile ps_1_1 PS_Textured();
}
}

//-----------------------------------
// Monochrome Rendering Technique.
//-----------------------------------
technique mono
{
pass p0
{
VertexShader = compile vs_1_1 VS_TransformDiffuse();
PixelShader = compile ps_1_1 PS_Monochrome();
}
}



I'm working on a lighting model to support multiple lights, I'll post it when I finish.

cheers

Share this post


Link to post
Share on other sites
I have Finished the shader for a single light, it does self shadowing my only problem is that the monochrome technique, I cannot do lights, maybe I need to do another pass, but I have not been able to get it to work. So I anyone has any ideas I put another post to discuss that, or just post it here.


// MARVIT ENGINE FX FILE.
string description = "MARVIT ENGINE FX'S";

//------------------------------------
// Our Matrices.
//------------------------------------
float4x4 matWorldViewProj : WorldViewProjection;
float4x4 matView : ViewInverse;
float4x4 matWorld : World;
float4x4 matProj : Projection;

//------------------------------------
// Our Texture.
//------------------------------------
texture diffuseTexture : Diffuse;
texture Shadow;

//------------------------------------
// Our Lighting Variables.
//------------------------------------
bool LightOn; // Lights is on
float3 LightPos; // Light's Position in world space
float4 LightDiffuse; // Light's diffuse color
float Range; // Light's range.
float4 Ambient; // Scene's ambient color
int NumLights; // Number of Lights in the Scene.


//------------------------------------
// Our Material Variables.
//------------------------------------
float4 MaterialDiffuse; // Materials's Diffuse color
float4 MaterialAmbient; // Materials's Ambient color
float4 MaterialSpecular; // Materials's Specular color
float4 MaterialEmissive; // Materials's Emissive color
float Power; // Material Power.

//------------------------------------
// Vertex Input Structure.
//------------------------------------
struct vertexInput {
float3 Position : POSITION0;
float3 Normal : NORMAL0;
float4 Texture : TEXCOORD0;
};

//------------------------------------
// Vertex Output Structure.
//------------------------------------
struct vertexOutput {
float4 Position : POSITION0;
float4 Texture : TEXCOORD0;
float3 Normal : TEXCOORD1;
float3 View : TEXCOORD2;
float3 WorldPos : TEXCOORD3;
};

//------------------------------------
// Normal Vertex Shader Routine.
//------------------------------------
vertexOutput VS_Transform(vertexInput IN)
{
vertexOutput OUT;
matWorldViewProj = mul(matWorld, mul(matView, matProj));
OUT.Position = mul( float4(IN.Position.xyz , 1.0) , matWorldViewProj);
float3 worldEyePos = matWorld[3].xyz;
float3 worldVertPos = mul(IN.Position, matWorld).xyz;
OUT.Normal = normalize(mul(IN.Normal, matWorld)); //normal vector
OUT.View = normalize(worldEyePos - worldVertPos); //eye vector
OUT.View = normalize(worldEyePos - worldVertPos); //eye vector
OUT.WorldPos = mul(IN.Position, matWorld);
OUT.Texture = IN.Texture;
return OUT;
}

//------------------------------------
// Pixel Input Structure.
//------------------------------------
struct pixelInput
{
float4 Texture : TEXCOORD0;
float4 Normal : TEXCOORD1;
float3 View : TEXCOORD2;
float3 WorldPos : TEXCOORD3;
};

//------------------------------------
// Pixel Input Structure.
//------------------------------------
struct pixelOutput
{
float4 Color : COLOR0;
};

//------------------------------------
// Scene Texture Sampler.
//------------------------------------
sampler2D SceneSampler =
sampler_state
{
Texture = <diffuseTexture>;
AddressU = Wrap;
AddressV = Wrap;
MinFilter = Point;
MagFilter = Linear;
MipFilter = Linear;
};

//------------------------------------
// Shadow Texture Sampler.
//------------------------------------
sampler2D g_samShadow =
sampler_state
{
Texture = <Shadow>;
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};

//-----------------------------------
// Normal Pixel Shader Routine.
//-----------------------------------
pixelOutput PS_Textured( pixelInput IN ) : COLOR0
{
pixelOutput OUT;
OUT.Color = tex2D( SceneSampler, IN.Texture );
return OUT;
}

//-----------------------------------
// Per Pixel Lighting Shader Routine Normal.
//-----------------------------------
pixelOutput PS_Lighting( pixelInput IN ) : COLOR0
{
pixelOutput OUT;

float3 normal = normalize (IN.Normal);
float4 temp_Texture = tex2D( SceneSampler, IN.Texture );

// Do Lighting.

float dist = length ( IN.WorldPos - LightPos);
float3 direction = -1*normalize ( IN.WorldPos - LightPos);

// Lighting variables.
// Diffuse.
float4 diff = saturate(dot(IN.Normal, direction)) * (MaterialDiffuse * LightDiffuse);

// compute self-shadowing term
float shadow = saturate(4* diff);

// Specular.
float3 Reflect = normalize(2 * diff * IN.Normal - direction);
float4 specular = pow(saturate(dot(Reflect, IN.View)), Power) * MaterialSpecular;

// Attenuation.
float att = 1- (saturate (dist / Range));

// Resulting color.
temp_Texture *= (((Ambient*MaterialAmbient) + shadow * ( diff + specular)) * att) + MaterialEmissive;

OUT.Color = temp_Texture;

return OUT;

//return diffuseTexture;
}

//------------------------------------
// Monochrome Pixel Shader Routine.
//------------------------------------
pixelOutput PS_Monochrome ( pixelInput IN ) : COLOR0
{
pixelOutput OUT;
float4 LuminanceConv = { 0.2125f, 0.7154f, 0.0721f, 1.0f };
float grayScale = dot( (float3)tex2D(SceneSampler, IN.Texture ), LuminanceConv);
OUT.Color = float4(grayScale,grayScale,grayScale,1.0);
return OUT;
}

//-----------------------------------
// Normal Rendering Technique using Light.
//-----------------------------------
technique normal
{
pass p0
{
VertexShader = compile vs_1_1 VS_Transform();
PixelShader = compile ps_2_0 PS_Lighting();
}
}

//-----------------------------------
// Normal Rendering Technique no lights.
//-----------------------------------
technique nolight
{
pass p0
{
VertexShader = compile vs_1_1 VS_Transform();
PixelShader = compile ps_2_0 PS_Textured();
}
}



It's all point lights, I think you can do pretty much everything with that if you can get a shadow casting technique working. Currently working on multiple lights.

Share this post


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

  • Advertisement