World Wind Forums

Go Back   World Wind Forums > WorldWind JAVA forums > Feature Discussion

Feature Discussion Education, discussion and proposals of WWJ features

Reply
 
Thread Tools Display Modes
Old 02-13-2009, 12:05 PM   #1
Omega
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default Sun shader

Hi all,

I've implemented a (very alpha) sun shader layer using GLSL shaders. It shows a reflection of the sun off the ocean using basic specular lighting calculations. It also shows the NASA night lights layer on the side of the earth with no sun.

It uses a slightly modified RectangularTesselator class that calculates normals according to the globe surface (assumes it's a ellipsoid, doesn't calculate terrain normals).

To use this layer you must have a video card that supports shaders. You must also set the tesselator class name using the code as follows (before the WorldWindow is set up):
Code:
Configuration.setValue(AVKey.TESSELLATOR_CLASS_NAME,
    NormalTessellator.class.getName());
I've created a special tile set to support this layer. The layer won't work without it. It is quite simple: the oceans are blue, the landmass is green, and the nightlights are red (nightlights on landmass = green+red = yellow). The shader then takes these colours and makes it look lovely.

This tile set was created from the freely available NASA blue marble and nightlight images. Obviously it is too big to upload as an attachment, but I've uploaded the first level (level 0) so people can test the layer. Simply extract it to your cache directory, or add the extracted directory to the data file cache using WorldWind.getDataFileCache().addCacheLoc ation().

I hope people extend this!

Kind regards,
-Michael
Attached Images
File Type: jpg screenshot.jpg (46.1 KB, 2614 views)
Attached Files
File Type: zip sunshader.zip (14.0 KB, 155 views)
File Type: zip cache.zip (1.47 MB, 126 views)
Omega is offline   Reply With Quote
Old 02-13-2009, 12:48 PM   #2
vash
Member
 
Join Date: May 2007
Location: Toulouse
Posts: 92
Default

Hi Michael, this is really impressive and thank you for sharing your efforts, but i don't understand why the sun spot on the earth is moving when i move the view.
This seems to me unrealistic but maybe i am wrong.
__________________
-Nicolas CASTEL- Thales Alenia Space, GMES program, Toulouse, France-
vash is offline   Reply With Quote
Old 02-13-2009, 03:30 PM   #3
patmurris
WWJ Consultant
 
patmurris's Avatar
 
Join Date: Jun 2005
Location: Saint-Paul de Vence, Alpes Maritimes, France
Posts: 3,412
Default

Nice work Michael! I wish i had more time to play with the fun stuff.

I'm a big fan of shading and i'm glad to see you are interested too. You are just inches from terrain shading.

There is some commented out normal computation code in the tessellator that is a 'work in progress'. It is only intended to compute one tile normals, but there is another step needed to deal with the edges - where normals should account for the neighboring tiles too. The strategy is to use the skirts vertices to compute one extra row and column of terrain surface around the tile, compute the normals, and then 'fold down' the skirts around the sides. Care to try it?

Nice stuff with the shaders too. I would suggest you factor out the shader code from the layer and create a Shader base class that would handle reading source code, compiling and applying. That would make it easier to use shaders in other parts of the rendering pass.

And thanks for the sun calculator too
__________________
My World Wind Java Blog & WW.net Plugins page
patmurris is offline   Reply With Quote
Old 02-16-2009, 02:07 AM   #4
Omega
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default

Quote:
Originally Posted by vash View Post
Hi Michael, this is really impressive and thank you for sharing your efforts, but i don't understand why the sun spot on the earth is moving when i move the view.
This seems to me unrealistic but maybe i am wrong.
It does seem weird at first, but remember that the whole of space is moving when you move the view. This includes the sun. The sun position is calculated according to the current time of day, and the lighted part of the globe is constant even when you move the globe around. The sun spot is using specular lighting calculations: a vector is calculated that is halfway between the sunPosition and the cameraVector for each vertex, and the dot product between that vector and the normal for the vertex is used to find out how much specular lighting is required at that vertex. This means that as the camera moves, so does the apparent 'reflection' of the sun on the globe.

Quote:
Originally Posted by patmurris View Post
There is some commented out normal computation code in the tessellator that is a 'work in progress'. It is only intended to compute one tile normals, but there is another step needed to deal with the edges - where normals should account for the neighboring tiles too. The strategy is to use the skirts vertices to compute one extra row and column of terrain surface around the tile, compute the normals, and then 'fold down' the skirts around the sides. Care to try it?
I was almost there! Thanks for your suggestion, it worked well. I've rewritten the buildVerts() function to add an extra row and column around the tile, calculated the normals, and then fold the skirts down. I've also written my own getNormals() function which should be more optimal than the one commented out at the bottom of RectangularTesselator. See the attachment.

Can you tell me the reason for the refCenter Vec4 in the original buildVerts()? I modified it to a zero vector and it worked fine; it just seems to complicate things?
Attached Files
File Type: txt NormalTessellator.java.txt (40.0 KB, 128 views)
Omega is offline   Reply With Quote
Old 02-16-2009, 08:11 AM   #5
patmurris
WWJ Consultant
 
patmurris's Avatar
 
Join Date: Jun 2005
Location: Saint-Paul de Vence, Alpes Maritimes, France
Posts: 3,412
Default

Excellent! Can't wait to see some screenshots (i have little time to play with it unfortunately).

The reference center we use in many parts of the SDK is actually an important step to ensure maximum accuracy on the globe surface. The reason is that actual GPUs are not using doubles but floats which precision decrease sharply as you go away from zero. As all units in WW are in meter, any point on the globe will have cartesian coordinates in the range of the Earth radius. Without a local reference center for each geometry tile, vertices will end up being drawn slightly off their expected position, which can introduce significant errors when you try to place objects a few meters apart. This also produces what is often described as 'view jitter' where the eye seems to move in steps rather then smoothly.

Looking at your getNormals() code, i see you compute two normals for each vertice, based on the four surrounding vertices. I don't think this is good enough since some vertices are involved in up to six adjacent triangles with their own orientation and normals. That is why the commented out code works in two pass.
__________________
My World Wind Java Blog & WW.net Plugins page
patmurris is offline   Reply With Quote
Old 02-18-2009, 04:39 AM   #6
Omega
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default

Quote:
Originally Posted by patmurris View Post
Looking at your getNormals() code, i see you compute two normals for each vertice, based on the four surrounding vertices. I don't think this is good enough since some vertices are involved in up to six adjacent triangles with their own orientation and normals. That is why the commented out code works in two pass.
Ahh, now I realise why the code was like that. The reason I wrote my own was because the commented out code didn't calculate correct normals: it wasn't taking into account the difference between clockwise and anti-clockwise triangles, which caused vertical strips of alternating dark/light vertices. I've refactored it so it works. The normals are now calculated taking into account each face the vertex is involved in. See attached if interested
Attached Files
File Type: txt NormalTessellator.java.txt (41.7 KB, 82 views)
Omega is offline   Reply With Quote
Old 02-18-2009, 10:48 AM   #7
patmurris
WWJ Consultant
 
patmurris's Avatar
 
Join Date: Jun 2005
Location: Saint-Paul de Vence, Alpes Maritimes, France
Posts: 3,412
Default

Thanks for fixing and cleaning up that piece of code.

The next step would be to add a light vector to the tessellator, or more likely to the surface tile renderer. If null, lighting would be disabled (default) otherwise it would be used to setup light before rendering a tile.

Good work Michael, i'll try to integrate this tessellator as an alternate option in 0.6 and see if it fits in - there has been some refactoring since 0.5.

How does it look like? - any screenshot?
__________________
My World Wind Java Blog & WW.net Plugins page
patmurris is offline   Reply With Quote
Old 02-20-2009, 03:20 PM   #8
patmurris
WWJ Consultant
 
patmurris's Avatar
 
Join Date: Jun 2005
Location: Saint-Paul de Vence, Alpes Maritimes, France
Posts: 3,412
Default

I managed to 'merge' your updated tessellator with the current 0.6 one and it works well, although i've seen some artefacts in some places (there may be something wrong with normals at the edge of tiles or i may have introduced a bug while converting your code...). Here is the new RectangularNormalTessellator and a SunShading example app that sets the light direction relative to the current eye position.

In the tessellator code, you will be interested in the beginLighting() and endLighting() methods.

Attached Files
File Type: txt RectangularNormalTessellator.java.txt (76.4 KB, 121 views)
File Type: txt SunShading.java.txt (7.3 KB, 102 views)
__________________
My World Wind Java Blog & WW.net Plugins page

Last edited by patmurris; 02-20-2009 at 03:37 PM.
patmurris is offline   Reply With Quote
Old 02-20-2009, 09:42 PM   #9
bull
Cosmic Overlord
 
bull's Avatar
 
Join Date: Oct 2004
Location: United Kingdom
Posts: 2,362
Default

Nice work Pat and Omega, you just couldn't resist having some fun could you
__________________
Bull_[UK]

bull is offline   Reply With Quote
Old 02-23-2009, 05:54 AM   #10
Omega
Junior Member
 
Join Date: Oct 2008
Posts: 26
Default

Well done Pat, thanks for merging it with 0.6, and thanks for the screenshot! It looks great.

Quote:
Originally Posted by patmurris View Post
...although i've seen some artefacts in some places (there may be something wrong with normals at the edge of tiles or i may have introduced a bug while converting your code...).
I noticed this as well; could the artifacts be caused from drawing tiles at different levels? When two tiles of different levels are drawn next to each other, the normals will be slightly different at the edges, therefore the edges are visible. It's especially noticable when the GLSL shader lighting is used.
Omega is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GLSL Shader support that renders tiles correctly tve Development Help 7 09-09-2009 08:53 AM
Sun Shading? MicheleMarcon Development Help 14 07-30-2008 10:03 PM
Adding sun control to planets' information? Guest_Jim_* Suggestion Box 46 02-19-2008 12:29 AM
WWJ featured on Sun Dev. Network patmurris Development Help 1 07-05-2007 07:44 PM
Sun shading and Atmospheric scattering bull Builds 21 12-15-2006 08:07 AM


All times are GMT +1. The time now is 04:43 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.