170 likes | 385 Views
Week 7 - Friday. CS361. Last time. What did we talk about last time? Mipmapping Summed area tables Anisotropic filtering. Questions?. Project 2. XNA Shader Examples. Effect files. We have been using BasicEffect to achieve most of our shading
E N D
Week 7 - Friday CS361
Last time • What did we talk about last time? • Mipmapping • Summed area tables • Anisotropic filtering
Effect files • We have been using BasicEffect to achieve most of our shading • BasicEffect gets so much done that it's tempting not to move any further • But more complex effects can be achieved by writing shader code ourselves • To start, right click in your Content project and choose Add > New Item • Choose Effect File from the wizard and name it whatever you want
Shader declarations • We need to declare the variables we are going to use at the top of the file • These usually include at least the following (which are already given in the template) • We're also going to add an ambient color and an ambient intensity for this shader float4x4 World; float4x4 View; float4x4 Projection; float4 AmbientColor = float4(1, 1, 1, 1); float AmbientIntensity = 0.1;
Shader data structures • We also have to define structures to take the input and the output • These vary because different vertex formats include different data (position, normals, colors, texture coordinates) • The simplest possible input and output would have position only • The POSITION0 is called a semantic • Semantics are used to tell the shader what the purpose of a variable is structVertexShaderInput { float4 Position : POSITION0; }; structVertexShaderOutput { float4 Position : POSITION0; };
Vertex shader • The job of the vertex shader is, at the very least, to transform a vertex from model space to world space to view space to clip space • It can also do normal and color calculations, but this first shader will only do the transforms VertexShaderOutputVertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4 viewPosition = mul(worldPosition, View); output.Position = mul(viewPosition, Projection); return output; }
Pixel shader • The pixel shader must find the final color of the pixel fragment • This first pixel shader uses an ambient shading model • The ambient shading model is the simplest possible • Everything gets colored the same, no matter where it is float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { return AmbientColor * AmbientIntensity; }
Techniques • You're allowed to name your vertex and pixel shaders anything you want • You specify which you're going to use in a technique • At this level, techniques only have one pass, but it is possible to use multiple techniques to achieve interesting effects technique Ambient { pass Pass1 { VertexShader = compile vs_2_0 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }
Using the shader in the game • You have to load the shader like other content • Then, we run a loop similar to the earlier one, setting the parameters for the effect object effect = Content.Load<Effect>("Shaders/ambient"); foreach( ModelMeshmesh inmodel.Meshes ) { foreach( ModelMeshPartpart inmesh.MeshParts ) { part.Effect= effect; effect.Parameters["World"].SetValue(world * mesh.ParentBone.Transform); effect.Parameters["View"].SetValue(view); effect.Parameters["Projection"].SetValue(projection); } mesh.Draw(); }
The results! • Okay, not that impressive yet • But now we have some idea what we're doing
Next time… • Materials • Alpha • Bump mapping
Reminders • Finish Project 2 • Due tonight by midnight • Read Chapter 7 • Assignment 3 will be assigned over Spring Break