This week, we finally cover the alpha transparency that I was wondering about in the first assignment. The alpha blend and addition was easy to implement. The alpha binary, however, is a bit tricky. I didn't know what to do with the clip() function that we supposed to use. The information in DirectX documentation doesn't give me understanding at all. Thankfully, Derek came to our lab and helped me understanding this clip function. He had also confirmed my doubt about using the texture's alpha as the clipping criteria and ... here my new scene.
After enabling alpha blend
As comparison, this is the scene before enabling the alpha blend
Before enabling the alpha blend
Again, the most challenging task in this assignment not the graphics itself but sorting the data. I found bug in my previous multi-level quick sort. It took me a while to fix it and came up with a better multi-level quick sorting. I still remembered the data flow is crucial in game graphics so I wasn't even a bit complaining about it. Instead, I was persistent to make a nice and efficient sorting algorithm for this - which made some people said "I am over achiever". :)
For my transparent assets, I used insertion sort. The transparent assets needed to be sorted every frame thus when camera position stay still for a while, quick sort would give a worst case: O(n^2). Instead, when the data were already sorted, insertion case would give the best case: O(n).
I had also remembered that switching between state is the expensive in graphics pipeline. Therefore, it made sense to group together entities by the render state first then by fragment shader, vertex shader, material, and finally mesh.
PIX screenshot with a clean events
Inside the Opaque event
Inside the Translucent event
I almost forgot to mention about my new effect file format. Now the format is:
<vertexShader> --> start of vertex shader data
blablabla.vp --> name of file where the vertex shader can be found
</vertexShader> --> end of vertex shader data
<fragmentShader> --> start of fragment shader data
blablabla.fp --> name of file where the fragment shader can be found
</fragmentShader> --> end of fragment shader data
<renderState> --> start of render state data
alpha_blend/alpha_none/alpha_additive/alpha_binary
</renderState> --> end of render state data
<textureMode>
-> start of texture mode data
diffuse_map/environment_map/normal_map
</textureMode>
-> end of texture mode data
While helping Chris debugging his homework last week, I understood why we need to draw the opaque object first before the transparent ones. He drew his transparent object before the opaque. The consequence, the transparent was getting darker as the transparency is increased. In other words, instead of showing the object behind it, his transparent object show the black background of his scene! So to avoid this, we need to draw the opaque object first before the transparency.
ReplyDelete