Download Playing Games

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

General-purpose computing on graphics processing units wikipedia , lookup

Rendering (computer graphics) wikipedia , lookup

Transcript
DirectX – From Vertex to Pixel Lighting
This worksheet brings together everything we've covered so far, from basic
transformations to blending. Now we make a major visual step forward by using pixel
shaders for lighting calculation.
Practical: Getting Started



Download the PixelLighting zip file from Week 16 of the CO2409 webpage and
unzip it to your work folder
Open the project ‘PixelLighting.sln’
Run it – you will see a scene with a cube, a large floor and a light. The light is
displayed with an additively blended model and moving. The cube and camera can
be controlled in the same way as last week’s lab
Initial State: Vertex lighting + Additive Blending
Open the fx file for the project. There are two techniques in here, one for the cube and
floor that calculates lighting, and another for the light model itself that is quite basic. We
will not be changing the shaders for the light model.


Go to the techniques at the end of the fx file and read the names of the vertex and
pixel shaders used in each technique
Make sure you don't edit the light model's shaders at any point in this worksheet.
Per-Pixel Lighting
The lighting is working on the cube and floor but it is calculated per-vertex, which means
it's not especially accurate (as described in the lecture). Notice particularly how the floor
seems unaffected by the light - currently only its vertices are affected by light and they are
far away. So we're going to move the lighting calculation to the pixel shader, which will
improve the look considerably.
Note that once you begin the project will not work until you have completed a number of
steps.
The first thing you need to consider is the output of the vertex shader. At the top of the fx
file there are two vertex shader output structures used for the two different techniques:
- VS_BASIC_OUPUT is used for the light rendering - leave that alone
- VS_LIGHTING_OUPUT is the output of the vertex lighting shader - look at this one.



Now if the vertex shader is no longer going to calculate lighting, then it won't be
outputing the diffuse and specular light any longer. So delete those entries from the
output stucture.
But the vertex shader does calculating things that are required for lighting: the world
position of the vertex and the world space version of the vertex normal. These two
elements need to be added to the output structure in place of what you just deleted.
I suggest you call the new output variables WorldPos and WorldNormal. The types
and semantics (the bit after the ':') should be easy to deduce.
CO2409 - Computer Graphics, Week 16
Lab Worksheet 1-1

Now you need to update the vertex shader - the world position and normal are
already being calculated, but they are not being output. You need to add a couple of
simple lines to output them into the structure you just updated.

Next the lighting calculations themselves need to be removed from the vertex
shader and placed at the beginning of the pixel shader
You will need to make minor updates to several variable names in the calculation
code, because of the changes to the output structure. Work these out for yourself.

If done correctly, the program should now display per-pixel light. The most noticeable
change is the huge specular reflections, but if you zoom away with the camera you should
see two distinct effects: the specular highlight and the more subtle circle of diffuse around
the light on the floor.
Specular Maps
The textures provided for the floor and cube contain specular maps in their alpha channels
as described in the lecture. [Method: the textures were created in Photoshop with an alpha
channel, saved as .tga files. Then the DirectX Texture Tool in the DirectX SDK part of the
start menu was used to convert them into DDS files that DirectX can load].
The pixel shader already has the correct code to combine diffuse and specular materials
and lights. There are only minor changes to support this specular map:


The diffuse texture is reading just the RGB of the texture into a float3. We now need
to extract the alpha channel from the texture too. Make the appropriate change.
The "Specular Material" is currently being set to 1.0 - i.e. everything has maximum
shininess. This value should be taken from the alpha channel you just extracted.
If done correctly, you should now distinctly see areas of the stone that don't reflect so well.
Also the specular channel has been used to tone down the shininess of everything to a
more reasonable level.
Lab Assignment Stage 1: Demonstrate pixel-lit cube and floor using specular maps
More Lights
The main C++ code has been simplified a little for loading and rendering multiple models see the use of the new function RenderModel in the function Render. There is an update
function for the light now, UpdateLight. This makes it rather easier to see how to create
and render more models. So...
 Support a second light in the scene. In general this involves looking at what the first
light does. Notes:
o In the C++ you don't need to create new index/vertex buffers or load more
geometry because you can use the existing data.
o You do need new variables for the second light position, colour, matrix etc.
This applies to C++ and HLSL
o You also need new variables to pass information between C++ and HLSL,
and use them at the appropriate time,
o I suggest you make the second light stationary and add the code to create its
matrix to the UpdateLight function
o Call RenderModel again with the new matrix to render the model for the light
CO2409 - Computer Graphics, Week 16
Lab Worksheet 1-2

In the pixel shader, you will need to nearly duplicate the lighting calculations to get a
second value for diffuse light and specular light
o Add the light values from both lights together to get the totals to use in the
final colour combination.
o Be very careful with variables and variable names in the HLSL, it is easy to
make a mistake by using a variable for the wrong light
Lab Assignment Stage 2: Demonstrate a pixel-lit cube and floor using two lights.
Very minor errors may be overlooked
CO2409 - Computer Graphics, Week 16
Lab Worksheet 1-3