Game Development Community

The most basic Shader

by Anthony Rosenbaum · in Torque Game Engine Advanced · 06/16/2004 (7:27 am) · 4 replies

I decided to finally test my knowledge of shaders and actually write one, nothing special but something quick and able too see a change, not to mention to get acustomed to the attribute variables used in TSE that can be passed into the Shaders.

Now for anyone who has already read many articles on Shading this will not be so important. However in several months when we have people who don't know the basics of shaders, well they might find this post interesting.

This is a green shader, inspired by The Cg Tutorial first example. BTW I never have read any HLSL books but Cg and HLSL are similar (designed in tandem with each other) so code from that book can be ported easily.

First off there is always 2 shaders Vertex and Pixel ( aka Fragment)

This Vertex shader does nothing but change the vertex coord ( recieved from the game engine, we call these attribute variables) and multiply it my the modelview matrix ( also an attribute variable)
#define IN_HLSL
#include "shdrConsts.h"

//---------------------------------------------------------------
// Constants
//---------------------------------------------------------------
struct Appdata
{
   float4 position   : POSITION;
};
struct Conn
{
   float4 HPOS : POSITION;
};
//---------------------------------------------------------------
// Main
//---------------------------------------------------------------
Conn main( Appdata In, uniform float4x4 modelview : register(VC_WORLD_PROJ) )
{
   Conn Out;

//take vert and get coord by transforming by modelviewmatrix
   Out.HPOS = mul(modelview, In.position);
   return Out;
}
save that as greenShaderV.HLSL
then the Pixel Shader which changes the color to green
//---------------------------------------------------------------
//Structures                                                    
//---------------------------------------------------------------
struct Fragout
{
   float4 col : COLOR0;
};
//---------------------------------------------------------------
//Main                                                           
//---------------------------------------------------------------
Fragout main( )
{
   Fragout OUT;
   OUT.col = float4(0.0, 1.0, 0.0, 1.0);
   return OUT;
}
save that as greenP.HLSL

open up shader.cs add this at the bottom
datablock ShaderData( greenShader )
{
   DXVertexShaderFile 	= "shaders/greenV.hlsl";
   DXPixelShaderFile 	= "shaders/greenP.hlsl";
   pixVersion = 1.1;
};

then in material add this at the bottom
datablock CustomMaterial( aGreenShader )
{

   shader = greenShader;
   version = 1.1;

};
Finally add change the binding of the SpaceOrc's skin in materialMap.cs from
addMaterialMapping("orc_ID1_skin", "material: OrcSkin");
to
addMaterialMapping("orc_ID1_skin", "material: aGreenShader");

Then load up the game the orc should look like this
this
www.afterschoolcartoons.com/pres/greenOrc.JPG

#1
06/16/2004 (9:31 am)
Nicely done. Everyone thinks, "Ah this is basic stuff," however this is exactly the kind of thing we need in documentation and resources etc.
#2
06/16/2004 (11:38 am)
Ya, thanks Anthony. You could turn this into a whole tutorial on how to use CustomMaterials. That would be a great resource.
#3
06/16/2004 (12:01 pm)
Will do!!!
#4
06/19/2004 (2:07 pm)
Very useful. Thanks! I'm still trying to wrap my head around the whole shaders thing.