Thursday, October 27, 2011

Diffuse, Ambient, and Specular Lighting

I have another applet I wrote a while back that might be useful to you. It demonstrates the various kinds of lighting and can really help you understand what's happening behind the equations.  

In class you learned about diffuse, ambient, and specular lighting.  Diffuse lighting comes from a direct light source like the sun, while ambient lighting is just what's hanging around in the environment.  Highlights on a surface from the direct light source appear as specular highlights and depend on where your eye is positioned relative to the surface.

In my applet, you can adjust the intensities of the red, green, and blue light coming from the ambient and direct light sources.  You can also adjust the surface reflectance, which determines how much of a certain colour of light will be reflected back from the surface (and eventually reach your eye, making the material look like a certain colour).  If the surface does not reflect any blue, then changing the blue intensity on the lights won't affect anything (the only exception being for the specular highlight, since those are made before the light would be absorbed/reflected in the surface anyway).

You can move the sun (direct light source) and the eye (viewpoint/camera centre), and you can tilt the surface to see how the angle between its normal and the other directions changes the outcome.

I recommend trying to isolate each type of light first to see how they look.  Turn the specular highlight off at first as well.  Then try to compare what you see with the equations involved in defining the light sources.

As always, if you have any questions, comment here or email me!

Go to the applet now...

Wednesday, October 26, 2011

Midterm Comments

Here are some general comments about the more problematic questions. I sometimes referred to these comments on your midterm paper.

These notes assume that you have access to the prof’s answer key. If you answered more questions than those that would give you 100 possible points, I stopped after 100 possible points as per the instructions on the midterm booklet. (In the future, if there are certain questions you don’t want marked, write “do not mark” on them and I will skip them.)
  1. In Number 8, some said that flickering could be caused by objects that occupy the same space, and that you could fix this by always choosing just one of the objects to render. If both objects were being rendered because they are the same distance away, then you wouldn’t have the flickering. If they were alternating being rendered then choosing just one object wouldn’t fix the problem unless you had a way to always choose the same one (but how would you ensure that?). Suggesting that you change the z-value of one of the objects is a bit more reasonable, but I’m not convinced it’s a great solution since you might have to go in and change your model and because inaccuracies could still be an issue. The answer that was being looked for was that the flickering is caused by inaccuracies of the z-buffer, so to fix it you want to increase the buffer’s resolution.
     
  2. In Number 1, if you draw a diagram that shows the overall idea but not the defining parameters (or if you label angles and such but don’t say what they are for), you will get 1 mark. If the defining parameters are more related to what I showed in my lecture, that’s fine, but I want to know more detail. Also, a few people were getting concepts mixed up between the geometry of the viewing plane (pinhole camera model for instance) versus the actual viewing frustum. We want to know about the frustum here, but I tried to give some marks for the perspective projection diagrams as well so long as you didn’t mix things up too much.
     
  3.  In Number 10, a good description of what the center and position points are comes from the Toymaker website (that place just keeps popping up in my Google searches!). It’s not clear to me whether the description in the answer key is also correct (can’t tell in the API, don’t now from experience), but it’s certainly possible and it does appear in the notes. (It seems to not make sense to have to update the position any time you want to change the center of rotation, leading me to believe that these are unrelated to each other, but I’ll go by what’s in the notes.)

    Center: “pointer to a 3D vector specifying the position in the sprite around which it can be rotated or NULL for top left”

    Position (aka translation): “pointer to a 3D vector defining the screen position of the sprite. Note: in Direct3D 0,0 is the top left of the screen”
     
  4. In Number 1, if you talk about implications related to the perspective transform itself rather than the frustum, I accepted that, so long as that made sense in the context of your previous answers.
     
  5. In Number 7 if you talked about giving the system incorrect drawing instructions (e.g. triangle set vs. triangle fan), I accepted that.
     
  6. There seemed to be a fair number of issues with Number 4. First of all, when given actual points in the question, you should make sure to use them in the answer. In other words, be mathematically specific. Also, there seems to be some misunderstandings in how to actually compare the normal to the view point to see if they can “see” each other. The trick is to use the fact that the sign of the dot product of the two entities can tell you something about the angle between them, and thus whether they are pointing away from each other or not.
     
  7. In Number 3, remember that if you are putting your point first (to the left of the transformation matrices) that it would be a row vector, not a column vector, in order to get the correct matrix multiplication happening.
     
  8. The biggest problems in Number 4 were either not giving enough detail (how do you actually compute certain things, etc) or just plain old having the method wrong. Be sure to review the answer for this one carefully, and keep in mind for the future that a question worth 25 marks is going to warrant some specific detail (in this case, including the mathematics behind it).

Midterm Marking Scheme

I've marked the midterms and posted the grades on WebCT. Hopefully you will receive your papers back in class tomorrow (Oct 27).

Here is how I broke down the marks for most questions (some of the 5 mark questions aren’t listed because they are more straightforward).

If you think your answer deserves more marks or that I missed something in determining the correctness of your answer, send an email that demonstrates this.  If indeed I think you deserve more marks, I’ll ask you to bring in your midterm paper.

Be sure to check my addition!

Number 1
·         2 for the projection role
·         2.5 for the defining parameters (0.5 each)
·         2.5 for the diagram
·         1 for what defining parameters form
·         2 for implication on visualizing game objects

Number 2
·         4 for explaining what homogeneous coordinates are
o   3 if correct but not quite enough detail
·         4 for explaining the motivation for them
·         2 for giving example of where they are used
o   1 if you give a correct example but offer an incorrect reason

Number 3
·         For general matrices, 1.5 each for the two translation matrices and 2 for the rotation
·         5 marks for the second part: 1 mark for each of four matrices (including the point) and 1 one mark for the correct order

Number 4
·         15 for describing the steps properly
o   4 for defining two vectors using the triangle points
o   3 for finding the normal via the cross product of these two vectors
o   4 for defining a vector from the triangle to the viewpoint
o   4 for computing the cosine of the angle between normal and vector to the viewpoint (only the sign matters)
·         5 for drawing an accurate picture
o   2.5 if the overall idea is there but it does not generalize well and/or does not show U, V, W, P, or other parts of the computation in the first part
o   2 if the diagram is good but based on wrong method
·         5 for correctly computing whether T is visible from P

Number 5
·         5 for saying why the transformation computes Z-values
o   1 if you mention removing shapes outside view frustum (this will happen before the projection transformation)
·         10 for correct computation
o   7 if correct but not enough math shown to show full understanding
o   9 if there’s a small error
o   7 if you don’t properly multiply the homogeneous coordinate of the point with the value in position (4,3) in the matrix to end up getting zero in the result
Number 6
·         5 for computing orientation
·         7 for computing avatar position
o   5 if you don’t show enough math
o   4 if you don’t normalize the orientation vector and multiply that by 50 units
·         8 for  avatar’s position and orientation
o   7 if you did not do the math and/or there are small errors
o   4 if the method is wrong
Number 8
·         2.5/5 for saying the objects occupy the same space and to pick just one (without saying how)
·         3.5/5 for saying the objects occupy the same space and to change their z-value

Number 9
·         3/5 for saying the object is outside the “render distance”

Number 10
·         3 for explaining the purpose
·         2 for the example
o   1.5 if example is good but based on incorrect understanding of part 1

Friday, October 21, 2011

Assignment 2 Marked

Tried really hard to get it marked by yesterday so you'd have more time to take into account any useful feedback for Assignment 3, but I just couldn't make it happen.  It's done now, and just like last time, you will find a PDF attached with the marking scheme and my comments to you.

I will be marking the midterm next, so I won't get to the third assignment for a while.

Wednesday, October 19, 2011

DirectX Matrices Related to Viewing

I promised a while back to write a post that related the theory in the lecture that I did a while back on viewing/cameras/projection in DirectX, but alas I haven't had the chance until now. Though it's perhaps a bit late, hopefully this can serve as another point of view for understanding things, or act as a reference.

The Toymaker website I mentioned in an earlier post has a good page on the various matrices you need in Direct3D to go from your model to the screen.  Let's have a look at the descriptions there and see how it relates to the terminology and concepts from my lecture.

Note: DirectX assumes a horizontal/row vector that is multiplied on the left hand side of the transformation matrices.  This means that the transformations are applied from left to right.  In linear algebra class and in my notes, a vertical/column vector is multiplied on the right hand side so that transformations are actually applied right to left.  I'm not going to switch things around from my lecture here, so keep this discrepancy in mind when looking at the Toymaker site in more detail.

Toymaker lists the following three matrices used to transform your game content from 3D model space to 2D screen space:
  1. World Matrix - Transforms 3D data from Model Space into World Space. You need to set this before rendering every entity in your world.
  2. View Matrix - Transforms from World Space into View Space. You need to set this each time your camera changes position
  3. Projection Matrix - Transforms from View Space into Screen Space. You normally set this just once during initialization.
The projection matrix mentioned here is like MoMp from my slides (i.e. the combination of the orthographic projection, which included the windowing transform, and the projection transform that squished the view frustum into a rectangular prism).

The view matrix here is the same as the view matrix Mv in my slides, which essentially did a coordinate transform so the model points in world coordinates would instead become relative to the camera's coordinate system.

The world matrix isn't something I talked about explicitly in the slides.  If you look at the orthographic viewing volume or the view frustum, you can imagine that the model points inside are going to be in the coordinate system of the world or camera (depending which stage you are looking at).  Of course, if our models are defined according to their own coordinate systems, then their points would first have to be transformed to be relative to the world coordinate system.

Toymaker goes on to show the nice helper functions that DirectX has to help you set up these three matrices.  You don't even have to know how these matrices look.

For the view matrix, you can use the same entities we defined in the slides to define where the camera is.  Then DirectX will construct the viewing matrix for you.  As described on Toymaker:
D3DXMatrixLookAtLH(D3DXMATRIX *out, CONST D3DXVECTOR3 *eye, CONST D3DXVECTOR3 *at, CONST D3DXVECTOR3 *up );
  • out - this is where we pass in the address of the matrix we want filling
  • eye - the world position of the eye point
  • at - the position in the world we are looking at
  • up - defines which way is up. This is needed because if you are positioned somewhere in the world looking in a certain direction you could be upright or standing on your head and in each case the position and direction would still be the same. So to determine which way up we are we need to provide it here
So the 'eye' here is the eye position/camera center e in the slides, 'at' here is the gaze direction g in the slides, and 'up' here is the view-up vector t in the slides.

Notice that the projection matrix is defined on Toymaker a bit differently than we define it (in addition to the fact that the DirectX projection matrix takes you from the world to the screen in one fell swoop).  While we talked about the view frustum as a set of planes, they use the field of view, aspect, and near and far planes instead.  Either way is fine because there is enough information to move from one representation to the other - for instance, the field of view can be determined at the angle between the lines from the origin to the edges of the frustum's side planes.

That should basically cover it.  You can take a look at the Toymaker page on cameras as well to see some methods they use to define the view matrix.  I encourage you to try to relate what you see there to what's in the slides to get a good feel for how well you understand the concepts.

Tuesday, October 11, 2011

Comments on Assignment 1

Overall, the first assignment was quite well done.  There were a few reoccurring issues that I figured I'd point out here so that everyone can avoid these (mostly minor) problems in the future:
  • Be sure to remove all warnings before submitting your assignment.  I did not take marks off for this, but it's very good practice to fix your warnings.  Even though most may be harmless, they can make you not notice more serious warnings.  I have seen even professional developers get into trouble when they let their warnings slide in large projects.
     
  • Try to organize your code in a nice, object-oriented way.  Don't just add code to the main game class that was provided without thinking about whether that makes sense.  Instead, decide what concepts should be their own objects and how these objects should interact.  This will be especially important for your project since you'll inevitably have more code than on the shorter assignments.
     
  • A lot of you used int to define things like the colour of your block.  Though this does work, I want to encourage you all to use enumerations in cases like these.  There is some good discussion as to why to favour this approach on this Stack Overflow discussion.  Again, from my own experience, I can say that using enum has proven to be rather useful.
      
  • Avoid using magic numbers.  A fair number of you used literal numbers to both define the size of your game grid and to loop through its elements.  In one case, these numbers didn't even agree.  You can use a #DEFINE for your numbers, or even better, use const variables.  If you group all your defines together in your header file, you also have a one-stop shop for adjusting values in your game.
      
  • Be careful with your user interface.  Why leave the window the size used for the demo when you are only using a fraction of it for your game? Even worse is if you don't show the bounds of the actual playing area.  How is the player to know where their blocks can go when there's an invisible wall? In each assignment think about how to help your user better know what's going on.  This and feedback are important elements of game design.
I think that's it for now.  If there was something specific I commented about on your assignment that you'd like to know more about, just send me an email.

VS2010 Project for Assignment 2

In the announcements section of WebCT I uploaded a Visual Studio 2010 project for those who need it.  There are a couple of changes I made to the Project Properties that should be useful to you in the future (if you make these changes directly, you should be able to do the conversion yourself from now on):
  • Go to C/C++ > General and paste the following into Additional Include Directories:
    $(DXSDK_DIR)Include;%(AdditionalIncludeDirectories)
  • Go to Linker > General and paste the following into Additional Library Directories:
    $(DXSDK_DIR)Lib\x86;$(DXSDK_DIR)Lib\x64;%(AdditionalLibraryDirectories)
  • Go to Linker > Input and under Additional Dependencies, remove the d3dxerr9.lib file (we don't appear to actually need it)
Before submitting your assignment, be sure to remove all warnings (I didn't do that with the sample project).

Friday, October 7, 2011

Assignment 1 Graded

You'll find a PDF attached to your assignment submission in WebCT.  I tried to put as many useful general comments as I could find time to write.  (I probably won't be able to spend as much time doing that on future assignments, unfortunately!)

Overall the assignment was well done.  I'll try to post some general comments here early next week, along with the promised relationship between my viewing lecture and relevant DirectX functions.

Have a great Thanksgiving weekend!

Wednesday, October 5, 2011

Assignment 1 and Midterm 1

I'm currently working through the first assignment submissions and hope to have it all graded on WebCT in the next couple of days.  I'm writing out a bunch of notes/suggestions that are not necessarily related to the actual marks for the assignment, so if you see a bunch of text, don't panic!

I also just found out your first midterm is coming up Tuesday next week.  I had been hoping to do an informal tutorial before the midterm to help you prepare, but the test is a little too soon.  So instead what I might do is organize one after the midterms are marked so we can at least go over the topics that caused the most trouble.  We can also do one before the second midterm.

A quick tip for the midterm: whenever you see inconsistencies in things like how transformation matrices are written (is the vector on the left hand side or the right?) or what kind of coordinate system is being used (left handed or right handed?), don't worry about which one you use on the test - just make sure it's clear to me. You can write a little note at the start of a question if it helps.