Monday, 19 November 2007

First Shader

We had a lab on shaders this morning, although only vertex shaders. They are nice to work with if you use version 2 or higher, the assembler ones are horrible. They do give nice effects but to program in assembler is just not nice especially if you compare them with GLSL or HLSL. It's like comparing asm to C++, I like the higher ones more. Here is an example of a vertex shader and it's output.
//c0 = matrix
//c4 = colour
//c5 = lightpos
//c6 = another color
//c7 = lightpos
//c9 = scalar constants

vs.1.1 // version instruction

dcl_position v0 // declare register data
dcl_normal v4 // v0 is position, v4 is normal
dcl_texcoord0 v8 // v8 is texture coordinates

mov oD0, c4
//mov oPos, v0


m3x3 r0, v4, c0 //transform normal

mul r6, v4, c7.x
add r6, v0, r6
m4x4 oPos, r6, c0

//m3x3 r1, c5, c0 //transform light pos
dp3 r1, r0, -c5 //dot product
mul r2, r1, c4 //multiply dot product with lightcolor

dp3 r3, r0, -c7
mul r4, r3, c4
mul r5, r2, r4
mov oD1, r5 //move to output

m4x4 r7, v0, c0
mul r7, r7, c9
mov oT0, r7

As you can see they are not nice to look at although the outcome does look nice. This shader transforms and lights and does a texture map for the skull mesh.

No comments: