Friday, February 22, 2013

Graphics Programming 5(Second Half)&6

For assignment5, I didn't do the sorting, instead I added a effectmanager and material manager to manage those things. I linked all the materials to the effect that it cares about. And did the same with the entities and materials. It adds a few overhead on load, but reduces the performance hit in every loop. Anyway, I thought it was the right thing to do.

Here is the screenshot of the PIX:

As for assignment 6, I changed the Maya exporter to export the file format exactly the same as I did before. So that I can use the parser that I wrote before. Here is the screen shot of the mesh.

File parsing in the mesh builder did took me sometime (not the parsing part, since it's exactly the same as before), I spent a few hours trying to figure out how to debug in the MeshBuilder. Turns out I was doing it the right way all the time, except there is a space in the file directory that i passed to the command, which the console doesn't recognize of course. Once i was able to debug, it took me a few minutes to solve the issue (again, some stupid mistakes).

The binary files is the easy part, in order to get every mesh info in three load, I create a new struct for the extra information I need (vertex number and index number), something like this:


//load binary file
//load vertex and index number
myFile.read((char*)&m_info, sizeof(VertexFormat::info));
m_vertexData = new VertexFormat::data[m_info.vertexNumber];
m_indexData = new unsigned int[m_info.indexNumber];
myFile.read((char*)m_vertexData, sizeof(VertexFormat::data) * m_info.vertexNumber);
myFile.read((char*)m_indexData, sizeof(unsigned int) * m_info.indexNumber);

Anyway, i'm able to load any shape from Maya into my renderer with my own file format, which is kind of exciting.

Here is the screen shot of scene:



Control:
To control the point light:
use "I" - up, "K" - down, "J" - left, "L" - right, "U" - backward, "O" - forward;
To control the camera: Arrow Keys
To control the box: WASD.

Link to the code:
Graphic 06








Friday, February 15, 2013

Graphics Programming 4&5(First Half)


So this one took a little bit of time to finish. Since we are adding scene files, entity files, materials and effects, we are kind of forced to re-factory the code again and write a bunch of parser. Which is pretty good of course since we have a chance to make our own file formats and have a pretty good sense of what data we need to pass to the renderer. "Data-driven" is what this is and it's important because this is commonly used in applications.

On the other hand, the shader part is not that hard actually. I added an ambient light, an attenuation and color to the point light that I have, and also specular lighting to a few materials. The way I'm doing this is to assign the texture sampler to the diffuse color and multiply it by the fragment color from the vertex shader.

Then I get the light direction L and the normal of that fragment N, normalize L, then get the dot product and get the strength of the point light and clamp it:

strength = clamp(dot(L, N), 0 ,1);

After that, I calculate the light attenuation. First I check the distance d from the point light and fragment position. Then get the light radius r from the scene file. Then I do something like this: 1/(1 + (d/r))^2 to get the attenuation.(More information about the equation: here)

So the diffuse lighting = light color * light attenuation * strength.

After that is specular lighting, first I get the reflection R, then I get the camera view V from the fragment position to the camera position. Then I get the dot product of these two:  saturate(dot(V, R)); And I can get the specular lighting by power it by an exponent. (We also do strength and attenuation because you shouldn't see any light on the back side of the mesh.)

So the specular lighting =  power(dotproduct, exp) * strength * attenuation.

So the final output is diffuseColor * (diffuse lighting + ambient light) + specular lighting.

Here is the screenshot of the actual effect:



Here is the screenshot of the PIX:


Here is the source code:
Graphics04_Source

Control:
To control the point light:
use "I" - up, "K" - down, "J" - left, "L" - right, "U" - backward, "O" - forward;
To control the camera: Arrow Keys
To control the box: WASD.