Friday, January 25, 2013

Graphics Programming 2


This week is kinda tough, the goal of the assignment itself is not hard, but re-factory the code (render simply gets too long if we don't do that) actually took me most of the time.

As always, I'll start on the theory for future reference:
This assignment is all about showing a 3D mesh on a screen, which is, in fact, project everything in the 3D space on a 2D plane. To be able to do that, you need three matrices:

ModelToWorld Matrix: Transform the mesh information from model space (which starts from the origin) to world space where the mesh actually is. To be able to do that, you need to get the transform information from your code (including position, rotation, even scaling value). And you can get the final matrix from multiply the rotation matrix, position matrix, and so on. (Ideally, you can get any transform you want by continually multiplying matrices, it's all linear algebra. ) 

WorldToView Matrix: Transform everything from world space based on camera's location (position and rotation, to be exact), so that everything will be placed at our point of view. To do that you can just multiply the ModelToWorld matrix by the INVERSE camera transform matrix (since everything will move at the opposite position the camera moves, for example, when the camera move left, everything will be moving right in the viewport.)

Projection Matrix: Right now what we see is a 3D place, but what we see on the screen is actually all 2D, so we need a matrix to project everything from 3D to a 2D plane. A projection matrix will do that, and DirectX does provide some function for you to do that. (D3DXMatrixPerspectiveLH() or D3DXMatrixPerspectiveFovLH())

Now back to the code:
I actually did this assignment twice, the first time I just went along and started re-factory right at the beginning. After I finished everything, the cube just doesn't show on the screen. I debugged and rechecked the code for 6 hours and did everything I could  and still couldn't figure out what could possibly the reason, even now. That really pisses me off. 


Here is the PIX debug screen for that one:



Everything works perfect, the vertex buffer and index buffer did passed into the vertex shader, the preVS and postVS are all right on track and all the matrices are loaded into the vertex shader. It's just that nothing is shown on the viewport. I even tried to disable the backface culling, and it still doesn't work. My guess is that somehow the data doesn't passed into the fragment shader, but I can't figure out why.

So I restarted, again from the last assignment. This time every time I made a change to part of the code, I debugged it and made sure everything showed properly on the screen. It worked and didn't take me much time since I simply reused most of the code I did at the first time. 

Anyway, the box:


PreVS:


PostVS:


   
Input instructions:

  Move Camera: Arrow Keys;
  Move Box: WASD

Link to the code:
Graphic02_WorkingCode

Here is also the link to the first one that doesn't work if anyone wants to check it out:
Graphics02_BrokenCode

No comments:

Post a Comment