Tutorial 19 HLSL Basic object

September 15th, 2010

No Comments

HLSL Basic object

Now for the more advanced TV3D tutorials. The next 6 tutorials will be about shader and how to work with them. I’m not going to explain all about shaders, there are a lot of good resources for that already. Take a look at this wiki page: wiki. In this first tutorial we’re going to create a simple shader that will just output the cube with a texture. This can be the template for any object shader you’re going to write.


What do we need?

- We need a TVShader object, that will tell the engine to render with our shader.

- We need the shader that we’re going to write. We will load that shader from a text file.

How do we set things up?

- First of all we’ll set up the TVShader object. It’s pretty much the same as loading a mesh:

Shader = Scene.CreateShader("object_simple");
Shader.CreateFromEffectFile("..\\..\\..\\..\\common\\shaders\\inoutobject.shade");



- Now we’ll tell the mesh to render with this shader:

Cube.SetShader(Shader);

This is once again not hard to understand.

- Now for the more tricky part we’ll take a quick peek at the actual shader. I’m not going in depth on all of the instructions, but i’ll try to explain what it roughly does.

void VertexProgram(in TV3DtoVERTEX IN, out VERTEXtoFRAGMENT OUT)
{       
    OUT.PosOfVertex = mul(IN.PosOfVertex, ViewProj);
    OUT.UV = IN.UV;
}

This is the vertex program. It will tell your graphics card what to do with every vertex that is passed to it. It is quite simple still, what is happening, is that the vertexposition (IN.PosOfVertex) is multiplied with the ViewProjection matrix. What this basicly means, is that the vertices are correctly placed on your screen. Then the UV coordinates are passed on to the pixel program. That is being done in that last line.

void FragmentProgram(in VERTEXtoFRAGMENT IN, out FRAGMENTtoSCREEN OUT)
{
float4 Col = tex2D(DiffuseSample, IN.UV);
OUT.Colour = Col;
}

This is the pixel program. The instructions described here will be executed for every pixel the triangles fill. In this case, you can see that the Col variable (which is an array of four floats) is filled by a texture lookup from the DiffuseSample texture sampler, from position IN.UV. That IN.UV contains the UV coordinates we need to sample from. Then the graphics card is told that the output color should be the Col variable. That means that we will output the texture color for every pixel. What we should see, is the cube with a plain texture of the tv3d logo.

How do we render this?

- Rendering is not different from rendering without a shader, so we don’t have to change anything here.

Things you can add/change yourself

- Try to play with the vertex and pixel program. For example try to multiply the vertex position with a sinus of some value, or try to output a single color in the pixel program. If you’re looking for more shader tutorials, you should have a look at this page, as well as (if you’re more serious about developing shader) “Shaders for game programmers and artists” by Sebastien st Laurent.

Reply