Sign in to follow this  

how to made real glass sphere with shader

Recommended Posts

hi,I try to real glass sphere or water sphere with shader. as picture now I try to use Nvidia SDK glass example, with Refraction & spec diffuse , but it's not good . Photobucket any idea with it,Thanks

Share this post

Link to post
Share on other sites
Realistic water and glass are usually rendered using a ray tracer that takes into account reflection and refraction. However, as D3D is conceptually a scanline rasterizer at heart, ray tracing - while possible to do in modern pixel shaders - is quite expensive.

You can emulate the above-mentioned effects in D3D by approximating the light transfer function; that is, calculate the statistical effects of the optical phenomena in advance and store the resulting values in a texture, or approximate the actual function real-time with a simpler polynomial function.

In order to implement the approximation function from scratch, you need solid understanding of all the optics involved. Reflection and refraction are not very difficult concepts, nor are they difficult to code and combine together.

To emulate your reference image, you could render finely tessellated, stretched spheres.

In the pixel shader, you would calculate the refraction given the normals, calculate the direction in which the refracted ray goes from behind the drops, sample the background color (which is constant green-yellow in this case) from that direction, and calculate how "dense" the refraction is at the given point in order to brighten or darken said point.

Then, you would calculate reflection in somewhat similar (but simpler) way. In the reference image, the drop reflects a disc-shaped light source that is situated above the camera. The light reflects on both the front surface of the sphere (a small disc-shaped specular highlight) and the inner back surface (the wide bright area at the bottom part of the sphere). In reality, the inner light would be refracted by the drop itself.

The next step, you would combine the reflection and refraction with additive blending, using Fresnel's equation as a weight for the refraction coefficient.

Since the refraction's local density (lightness) is a complex function, it is a good candidate for approximation given a normal and the assumption of a spherical shape. The local variation of the light density is commonly called "caustics".

The final step to would be to apply motion blur to the drops so that they seem to be falling fast. In the reference image, the bottom part of the drops is blurred more. This is because when the drops leave the leaf (which is close), they are somewhat spherical. The bottom part will have greater velocity than the top part when the drop hasn't acquired its terminal velocity (maximum drop speed). Hence, you have to adjust the motion blur vectors per-pixel to take this difference into account.

I know this isn't something that a beginner would like to hear, but realistic water rendering really isn't the easiest topic on graphics programming.

I hope this helps,

Share this post

Link to post
Share on other sites
Thanks for your kind infor.
I will try to use
pass 1. ray trace to simulate shadow into a texture.
pass 2. simuate high light specular on the front
pass 3. invere normal and simuate high light specular on the bottom
pass 4. refract base a background texture
pass 5. reflect base a foreground texture
pass 6. combine all .

these is my step.
Thanks again.

Share this post

Link to post
Share on other sites
Let me know how it goes [smile]

I'll try to make an implementation of this myself, if I find some free time to do so. If/when I do, I'll post my code in these forums.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this