1 / 17

CS361

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

ami
Download Presentation

CS361

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 7 - Friday CS361

  2. Last time • What did we talk about last time? • Mipmapping • Summed area tables • Anisotropic filtering

  3. Questions?

  4. Project 2

  5. XNA Shader Examples

  6. 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

  7. 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;

  8. 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; };

  9. 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; }

  10. 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; }

  11. 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(); } }

  12. 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(); }

  13. The results! • Okay, not that impressive yet • But now we have some idea what we're doing

  14. Project 2 Work Time

  15. Upcoming

  16. Next time… • Materials • Alpha • Bump mapping

  17. Reminders • Finish Project 2 • Due tonight by midnight • Read Chapter 7 • Assignment 3 will be assigned over Spring Break

More Related