Design Raytracing Renderer

manu3d

Active Member
Contributor
Architecture
As requested by @Cervator I repost in a separate thread one of the two ideas mentioned in this thread.

Terasology's current renderer is a rasterizer. To do things such as shadows, transparencies and reflections it renders the scene multiple times using some tricks and then combines the resulting layers. The flow is somewhat tricky to understand and it keeps in memory the various resulting images. It also doesn't scale well. I.e. if I'm not mistaken the current renderer can only handle one shadow-casting light (the sun) by rendering the scene into a shadowmap. It would need to render the scene additional times if additional shadow-casting lights were desired. Same thing if it needed to render additional reflecting surfaces beside the generally horizontal surface of liquids. Any additional reflective surface that is not oriented or is not at sea-level would require an additional render.

By comparison Ray-tracing does things such as shadows, reflections and refractions in a mathematically simple way. Adding shadow-casting lights only adds rays from the fragments being rendered to the light, a process that is also used for standard transparencies, reflections and refractions, a process that is relatively easy to understand and implement. What tends to make things much trickier for ray-tracing is that each ray needs to know if and where it hits something, requiring complex spatial partitioning (Octrees, BSPs) to avoid testing each triangle in the scene for a collision with the ray.

But that's where it occurred to me that a voxel-based game such as Terasology should be able to take advantage of its chunk/block-structured data and, at least for the rendering of the landscape, should be able to perform ray-block collision tests very quickly without particularly complex algorithms. In fact, the 3d rasterizers currently in use to generate the landscape from noise functions might be able (hopefully with little modification) to rasterize a ray into a distance-ordered list of chunks to be checked for collision and then do the same for individual blocks within the chunks.

Raytracing still has issues. For example a naive, least-rays-possible implementation will generate sharp shadows. Soft shadows would make things more complex, requiring additional rays per fragment. Also, while the landscape is made of blocks, things roaming it (players, critters, vehicles) are not and would require special handling, i.e. some sort of space partitioning to speed up collision testing. But my gut feeling is that in the long run a ray-tracing renderer would make things simpler code-wise and it would position Terasology well to handle increases in visual complexity as GPUs becomes more powerful.
 

Sagacity159

New Member
As far as softening of the shadows go, wouldn't it make more sense to do this post-process instead of taking exceedingly-large samples? This is how it is done in most games.
 

manu3d

Active Member
Contributor
Architecture
Short answer: yes.

Longer answer: I didn't look into the details but something along those lines is currently done to soften the shadowmap created for the sun, when the PCF option is selected. In this context I do not know how much of the PCF algorithm can be applied to raytraced shadows. Maybe all of it, maybe nothing, I just don't know. Do you know of an algorithm that would do the trick? I'm not in a position to implement raytracing any time soon, but it'd be good to add it to this discussion, for posterity.
 

Sagacity159

New Member
As far I'm aware if the ray traced shadows render to a shadow map we can apply most filters. I sadly don't think that PCF can be applied without the being able to give the distance of the objects from the cast point. At least traditionally.
 

manu3d

Active Member
Contributor
Architecture
To be clear: for the purpose of this discussion a shadow map is actually a depth map from the point of view of a light which is used for shading calculation later on. A shadow pass is the result of the scene rendered from the point of view of the camera, all materials set to a white diffuse, no specular, no ambient, no texture contributions, one or more shadowcasting lights active, each light potentially color-coded for individual post-processing-based manipulation. What are you referring to? A shadow map, a shadow pass, something else?
 

manu3d

Active Member
Contributor
Architecture
So, are you thinking about applying a simple 2d blur to the shadow pass to get soft raytraced shadows?
 

Sagacity159

New Member
Well it doesn't have to be simple, but yes I was planning on having it applied in 2D to give the illusion of soft shadows. This would of course be for those who cannot spare the horse-power for multiple ray samples.
 

Cervator

Org Co-Founder & Project Lead
Contributor
Design
Logistics
SpecOps
Anything that helps further our quest for better rendering! Any code assistance you can provide, or solid design work, would be hugely appreciated :)

(Welcome btw!)
 

manu3d

Active Member
Contributor
Architecture
Well it doesn't have to be simple, but yes I was planning on having it applied in 2D to give the illusion of soft shadows.
The problem with soft shadows is that they are not just "blurred shadows". A tower of blocks would produce sharp shadows close to the ground and blurry shadows from the blocks higher up. Meanwhile, an identical faraway tower, in screen space, would produce fairly sharp shadows, the penumbra areas too small to be resolved. One could potentially blur more or less depending on distance from the camera and distance from the shadow-casting surface to the fragment being shaded, but I wouldn't call this "most filters". Furthermore, I suspect a 2d blur will generate artifacts where an overlapping object occludes a shadow and the blur kernel gets to sample from the occluding object even though this isn't relevant for the shadow.

So, whatever you have in mind I'll be very curious to see how it works!
 

Sagacity159

New Member
I was actually just suggesting this for implementation in case of lower-end machines so that they aren't murdered mercilessly by extra raycasts. However, if we could store a depth map for shadow-casting objects as well we could potentially have a blur intensity curve based on that. I'll draw up a quick imagination of this using a RGB image with one channel representing the shadows position, and the other representing their distance from their object.

Edit: Actually I think it'd be better explained so I'm going to double post and the next post will be an extremely thorough explanation of what I think would be a good alternative.
 
Last edited:

Sagacity159

New Member
So, let's assume we can formulate a way to store the ray collision positions in a vector 3 format. If we can do this, then we can theoretically create a shadow map with semi-accurate blurring using 2D effects with some added 3D accountability by using blur intensities based on a distance based blur curve. So say a single dot in space is 3 feet from it's casting point. We will then apply the shadow intensity according to the intensity curve followed by the blur intensity. The blur will be applied gradually from the center position of the shadow. I believe object cross-talk as you feared would not pose an issue if we used this method, and we could potentially allow user-configuration of the curves. Also, I never planned on implementing this, thought I'd just chip in with an idea I cannot bring to fruition myself.
 

manu3d

Active Member
Contributor
Architecture
Thank you for your contribution @Sagacity159, it's good that we store the ideas here. Hopefully at some point somebody will go through them and implement them as needed.

I certainly concur that any renderer needs a number of customization parameters so that lower end machines do not grind to a halt, including entirely different algorithms for things such as shadows. So, if you or somebody else picks up your description and makes a working implementation that sacrifice accuracy for speed, I'm sure we'd love to have it as an option.

Have you looked into the GPU PRO book series? By the sound of it your might be interested in it. I am currently reading something else but I suspect I'll eventually head in that direction. Also, the GPU GEMS series is available online through the nvidia website: something else you might find interesting if you are not familiar with it already.
 

manu3d

Active Member
Contributor
Architecture
Thank you @dadix ! Unfortunately it is not inspiration that is lacking, it's the human resources! =)

That been said, I am getting back into the swing of things, and at least it will be a little easier for other people to chip in all things rendering. =)
 
Top