Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Use vertex shader.doc
Скачиваний:
30
Добавлен:
02.05.2014
Размер:
252.93 Кб
Скачать

Shader Definitions

Since this sample has four shaders, it must contain four shader definitions that parallel the vertex definitions and shader declarations. Let's cover these briefly, as I will cover them in more detail later. SimpleVertexShader0 simply performs a 4x4 matrix transform using them4x4instruction and copies the result to theoPos position output register.SimpleVertexShader0 then copies a constant color to theoD0diffuse color output register.SimpleVertexShader1 performs the same operation for the transform phase, but does it for each component using the 4x1 dot product using thedp4instruction and writes the result to theoPos position output register. Instead of simply copying the constant color, the diffuse color specified with the vertices is used and copied to theoD0diffuse color output register. SimpleVertexShader2 is slightly more complex. It too uses thedp4instruction for transform, and writes the resulting position to theoPos position output register. This shader now adds texturing and texture coordinates to the mix and uses the texture coordinates to update theoTOoutput texture coordinate register. Finally, theSimpleVertexShader3 performs the same transform phase operation using thedp4instruction for transform, and writes the resulting position to theoPos position output register. Instead of simply copying a color value or a texture value, this shader determines if the "quad" is facing a light, and if not, does not render any bits. It does this using thedp3instruction using the normal component and a light direction stored in a constant register.

The source for these shaders is included below:

// Simple vertex shader0

// Constants

// reg c0 = (0,0.5,1.0,2.0)

// reg c4-7 = WorldViewProj matrix

// reg c8 = constant color

// Stream 0

// reg v0 = position ( 4x1 vector )

// reg v5 = diffuse color

const char SimpleVertexShader0[] =

"vs.1.0 // Shader version 1.0 \n"\

"m4x4 oPos , v0 , c4 // emit projected position \n"\

"mov oD0 , c8 // Diffuse color = c8 \n";

// Simple vertex shader1

// Constants

// reg c0 = (0,0.5,1.0,2.0)

// reg c4-7 = WorldViewProj matrix

// reg c8 = constant color

// Stream 0

// reg v0 = position ( 4x1 vector )

// reg v5 = diffuse color

const char SimpleVertexShader1[] =

"vs.1.0 // Shader version 1.0 \n"\

"dp4 oPos.x , v0 , c4 // emit projected x position \n"\

"dp4 oPos.y , v0 , c5 // emit projected y position \n"\

"dp4 oPos.z , v0 , c6 // emit projected z position \n"\

"dp4 oPos.w , v0 , c7 // emit projected w position \n"\

"mov oD0 , v5 // Diffuse color = vertex color \n";

// Simple vertex shader2

// Constants

// reg c0 = (0,0.5,1.0,2.0)

// reg c4-7 = WorldViewProj matrix

// reg c8 = constant color

// Stream 0

// reg v0 = position ( 4x1 vector )

// reg v5 = diffuse color

// reg v7 = texcoords ( 2x1 vector )

const char SimpleVertexShader2[] =

"vs.1.0 // Shader version 1.0 \n"\

"dp4 oPos.x , v0 , c4 // emit projected x position \n"\

"dp4 oPos.y , v0 , c5 // emit projected y position \n"\

"dp4 oPos.z , v0 , c6 // emit projected z position \n"\

"dp4 oPos.w , v0 , c7 // emit projected w position \n"\

"mov oT0.xy , v7 // copy texcoords \n";

// Simple vertex shader3

// Constants

// reg c0 = (0,0.5,1.0,2.0)

// reg c4-7 = WorldViewProj matrix

// reg c8 = constant color

// reg c12 = light dir

// Stream 0

// reg v0 = position ( 4x1 vector )

// reg v3 = normal ( 4x1 vector )

// reg v5 = diffuse color

// reg v7 = texcoords ( 2x1 vector )

const char SimpleVertexShader3[] =

"vs.1.0 //Shader version 1.0 \n"\

"dp4 oPos.x , v0 , c4 //emit projected x position \n"\

"dp4 oPos.y , v0 , c5 //emit projected y position \n"\

"dp4 oPos.z , v0 , c6 //emit projected z position \n"\

"dp4 oPos.w , v0 , c7 //emit projected w position \n"\

"dp3 r0.x , v3 , c12 //N dot L in world space \n"\

"mul oD0 , r0.x , v5 //Calculate color intensity \n"\

"mov oT0.xy , v7 //copy texcoords \n";

Each of these shader definitions is specified as a text string, making it easy to use with D3DXAssembleShader. In addition, I clearly document the constant registers and stream register definitions with each shader to make sure the bindings are clear. I highly suggest you adopt some similar self-documenting scheme for your own shader development.

Соседние файлы в предмете Разработка игр