Blogger Templates

Thursday, December 07, 2017

0

Graphics (14 of ∞) Translucent Game Objects

Posted in ,
EAE 6320-001
write-ups
In this write up, we will talk more about making translucent objects and using a sorting order
to render them in order. The translucency will apply based on the z order the object is placed
in the camera space.


Translucent fragment shader
For the translucent fragment shader we will just follow our existing fragment shader format
and just update the alpha value of the output color. Anything close to 1 is more opaque and
anything close to 0 is more translucent. For the time being, this value is hardcoded and the
assumption is that we would use this fragment on the effect that has to be transparent.


New effects & Translucent Objects
Once I have the required translucent fragment shader, I have to map them to new effects.
Then I will use those effects on the game objects that have to be transparent.
For the purpose of this project, I added two new game objects representing Mars and
Uranus planets. Both these game objects would use the new effect that uses the translucent
fragment shader.


Enabling Transparency
To enable the transparency, the gameobject should use the render state which has its bits
set for EnableTransparency and EnableDepthBuffering. Now having done this, our game
object will be transparent in some places of the camera rendering and in some places it
won’t be. When we did depth buffering, we did not care much about the order the way the
game objects were submitted. But with translucent game objects, we have to follow painters
algorithm to paint from back to front. The reason why we do this approach is because we
need to allow the objects that are in back to be see-through from the objects that are in front.


The Ordering Math
As an architecture style, I decided to have a separate vector for my translucent game objects
rather than having one in the engine side. With this setup, in the SubmitDataToBeRendered
method of the application thread, I can simply do my sort operations on translucent
gameobject vector and submit them after the camera submission. All I had to do here was
to find all the translucent gameobject position in camera space and sort them using a
fancy c++ lambda based comparator.  I simply checked the z value of the object's position
in camera space to complete this comparison. The higher the value of z, the farther the
object is from the camera viewport. I spend some time understanding how I would capture
the values inside a lambda function, it was interesting how it did not allow me to capture the
entire class until I used ‘this’. The only reason for me to use the application thread is that,
it was handy for me to access the vector of translucent objects and do the required operation
before the render frame was started. But ideally I think the benefits are different on case
to case basis and its mostly an architectural decision.


I had an issue where my maya export had inherent position values and it was not set to
0,0,0 So I ended up rendering the position based on Maya values. This affected my
calculation for sorting of the game objects and Ameya helped in finding the issue. Once
I used models of 0,0,0 and set the position values, my sorting logic worked like a charm.


Controls
Cube Movement
Right Arrow - Move the 3D game object to the right.
Left Arrow - Move the 3D game object to the left.
Up Arrow - Move the 3D game object upwards.
Down Arrow - Move the 3D game object downwards.

Camera Movement
W - Forward Acceleration
S - Backward Acceleration
A - Rotate towards the left side
D - Rotate towards the right side
Q - Rotate Upwards
E - Rotate Downwards

Checkout the game via this release.




0 comments: