It is currently Sat Aug 22, 2020 1:53 pm


All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Ambient Occlusion help
PostPosted: Sat Mar 03, 2012 9:09 pm 

Joined: Wed Feb 29, 2012 12:35 am
Posts: 25
First of all I would like to say thanks for the great library, I have recently started using it and its absolutely awesome.

A few days ago I have read that Polyvox supports Ambient Occlusion and has a calculator for it.
I struggelled a couple days trying to figure out how to implement it on my Ogre3d project but I failed.

I have the Voxelmaterial files and everything.

Can anyone kindly show me step by step how would I achieve implementing it into my project?

I use Ogre3d . I already managed to create a volume and texture it using triplanar texturing.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Sun Mar 04, 2012 1:02 am 

Joined: Wed Apr 27, 2011 7:10 am
Posts: 43
There are many ways of going about this: basically you need to have the ambient occlusion level for each vertex available in the shader. An easy way to do this would be to add it as a texture coordinate, or vertex color. That is, for each vertex, check which voxel it is in, look up it's occlusion value in the computed occlusion array and set a self-specified value for that vertex (for example the vertex color). Then in the vertex/fragment shader, have that contribute to the lighting/color (perhaps negatively). I am quite new to shading so I may be wrong, or there may be better ways.

_________________
irc://irc.freenode.net/#polyvox


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Sun Mar 04, 2012 1:40 am 

Joined: Wed Feb 29, 2012 12:35 am
Posts: 25
realazthat wrote:
There are many ways of going about this: basically you need to have the ambient occlusion level for each vertex available in the shader. An easy way to do this would be to add it as a texture coordinate, or vertex color. That is, for each vertex, check which voxel it is in, look up it's occlusion value in the computed occlusion array and set a self-specified value for that vertex (for example the vertex color). Then in the vertex/fragment shader, have that contribute to the lighting/color (perhaps negatively). I am quite new to shading so I may be wrong, or there may be better ways.


Thanks for the reply, this sounds a little bit complicated for me since I'm totally new to shaders as well, what I have done so far is store all the values in a 3d array using the ambient occlusion calculator just like in the example source provided with polyvox, then I have used the ColouredCubicVoxel.cg, ColouredCubicVoxel.material and ColouredCubicVoxel.program posted before in this forum, it has a default shader integration for ambient occlusion. That's all I'm able to do now :(


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Sun Mar 04, 2012 8:16 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
realazthat wrote:
...basically you need to have the ambient occlusion level for each vertex available in the shader.


Actually the ambient occlusion calculator in PolyVox does not work on a per-vertex basis (though something like this would also be interesting). Instead it generates a volume texture which can be uploaded to the GPU and sampled when rendering the mesh. The advantage of this is that you can use it to light other ingame object besides the world, and you have nice smooth lighting changes as you fly into a cave for example. It all looks nicely unified.

The downside is that it is slow to compute, uses more memory, makes paging complicated, and is tricky to locally update. I actually took it out of Voxeliens because it was too slow to update when destroying the terrain, but I will be revisiting it for our next project.

As for using it, well, it is a little complex. I suggest to start with you skip PolyVox and just learn how to create a volume texture in Ogre and sample it from the GPU. Fill your volume texture with dummy data (a checkerboard or something so you can recognise it) add it to your material as a texture unit, and sample it from your shader code using uvw coordinates computed from your xyz coordinates.

Once that works you can come back to PolyVox and try converting a dummy PolyVox volume into a volume texture. Then you can try generating a real PolyVox volume from the AmbientOcclusionCalculator.

Really, the hard part is the Ogre/shader stuff and having a feel for how volume texture work.

Anyway, be sure to check out these threads if you haven't already:

Using Ambient Occlusion
Calculating Ambient Occlusion in PolyVox


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Sun Mar 04, 2012 8:46 am 

Joined: Wed Apr 27, 2011 7:10 am
Posts: 43
David Williams wrote:
realazthat wrote:
...basically you need to have the ambient occlusion level for each vertex available in the shader.


Actually the ambient occlusion calculator in PolyVox does not work on a per-vertex basis (though something like this would also be interesting). Instead it generates a volume texture which can be uploaded to the GPU and sampled when rendering the mesh. The advantage of this is that you can use it to light other ingame object besides the world, and you have nice smooth lighting changes as you fly into a cave for example. It all looks nicely unified.

I was thinking that one could simply calculate which cell/voxel the vertex came from and get the data from the array and store it per-vertex. Ofc this means all vertices in a cell/voxel will still have the same occlusion value.

_________________
irc://irc.freenode.net/#polyvox


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Mon Mar 05, 2012 3:29 pm 

Joined: Wed Feb 29, 2012 12:35 am
Posts: 25
David Williams wrote:
realazthat wrote:
...basically you need to have the ambient occlusion level for each vertex available in the shader.


Actually the ambient occlusion calculator in PolyVox does not work on a per-vertex basis (though something like this would also be interesting). Instead it generates a volume texture which can be uploaded to the GPU and sampled when rendering the mesh. The advantage of this is that you can use it to light other ingame object besides the world, and you have nice smooth lighting changes as you fly into a cave for example. It all looks nicely unified.

The downside is that it is slow to compute, uses more memory, makes paging complicated, and is tricky to locally update. I actually took it out of Voxeliens because it was too slow to update when destroying the terrain, but I will be revisiting it for our next project.

As for using it, well, it is a little complex. I suggest to start with you skip PolyVox and just learn how to create a volume texture in Ogre and sample it from the GPU. Fill your volume texture with dummy data (a checkerboard or something so you can recognise it) add it to your material as a texture unit, and sample it from your shader code using uvw coordinates computed from your xyz coordinates.

Once that works you can come back to PolyVox and try converting a dummy PolyVox volume into a volume texture. Then you can try generating a real PolyVox volume from the AmbientOcclusionCalculator.

Really, the hard part is the Ogre/shader stuff and having a feel for how volume texture work.

Anyway, be sure to check out these threads if you haven't already:

Using Ambient Occlusion
Calculating Ambient Occlusion in PolyVox


I do understand how texturing works, I did several small project previously using Ogre and its shaders system, my current texturing method is triplanar. Other than using polyvox, is there any other easier way to achieve ambient occlusion? or any other similar method to achieve a "fake" ambient occlusion?


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Thu Mar 08, 2012 9:34 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
xelons wrote:
I do understand how texturing works...


Ok, but I'm particulary referring to volume textures. Perhaps I misinterpretted your experience but if you have already worked with them then great. Either way, getting a dummy volume texture working is a good approach before trying to use the PolyVox data.

xelons wrote:
Other than using polyvox, is there any other easier way to achieve ambient occlusion? or any other similar method to achieve a "fake" ambient occlusion?


You can use popular screen space methods such as SSAO with all the usual benefits and drawbacks.

Also, I've prototyped a new approach based on blurring the volume data. The idea is that you threshold your volume data so that solid voxels are black and empty voxels are white, and you store this in a volume texture. Then you blur this volume texture. The result is that voxels which are distant from a surface remain white, while those that are near a surface pick up some of the black through the blurring. The result is not physically based but looks a bit like ambient occlusion. This technique has not made it into voxeliens but I hope to further develop it for our next game.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Fri Mar 09, 2012 4:42 am 

Joined: Wed Feb 29, 2012 12:35 am
Posts: 25
Thanks for the reply, I implemented the SSAO compositor to see how things will ends up, unfortunately it is not giving me the expected result. In other word I have a problem with it.

The first problem is a hug drop in FPS, I think this is related to the bad shaders configuration that I implemented.
The second problem is that the SSAO is not working as expected, when I slightly move the camera I see a distortion on the surface while there is no ambient occlusion working.

To give you an idea of what I did I just implemented the SSAO provided in this thread http://www.ogre3d.org/tikiwiki/Simple+SSAO , then I derived my material from diffuse_template.

I guess my main problem here is that I'm using triplanar texturing, so what I basically did was this:

Triplanar texturing material:
Code:
import diffuse_template from "diffuse.material"
material TriplanarTexturing : diffuse_template
{
   technique
   {
      pass
      {
         vertex_program_ref TriplanarTexturingVertexProgram
         {
            param_named_auto world world_matrix
            param_named_auto viewProj viewproj_matrix
            param_named_auto camPos camera_position
            
            // 0.083333333 = 1/12.0
            param_named textureScale float 0.083333333
            param_named lightColor float3 1.3 1.3 1.3
            param_named darkColor float3 0.2 0.2 0.2
            //param_named dirToLight float3 0 0.957826285 -0.287347886
            param_named_auto fogParams fog_params
            
            param_named_auto light0.position light_position 0
            param_named_auto light0.diffuseColour light_diffuse_colour 0
            param_named_auto light0.attenuation light_attenuation 0
            param_named_auto light1.position light_position 1
            param_named_auto light1.diffuseColour light_diffuse_colour 1
            param_named_auto light1.attenuation light_attenuation 1
            param_named_auto light2.position light_position 2
            param_named_auto light2.diffuseColour light_diffuse_colour 2
            param_named_auto light2.attenuation light_attenuation 2
         }
         fragment_program_ref TriplanarTexturingFragmentProgram
         {
            param_named_auto fogColor fog_colour
            param_named textureSize float 1024.0
            param_named noOfTexturesPerDimension float 4.0
         }
         ambient 0 0 0 1
         diffuse 0.65098 0.647059 0.596078 1
      }
   }
}

and then my main material that is applied to Polyvox volume is this:
ARNOLD.material
Code:
import * from "triplanarTexturing.material"

vertex_program Simple_Perpixel_Vert cg
 {
       source ARNOLD.cg
 
       default_params
       {
                  param_named_auto lightPosition light_position_object_space 0
         param_named_auto eyePosition camera_position_object_space
 
                  param_named_auto worldviewproj worldviewproj_matrix
       }
 
       entry_point Simple_Perpixel_Vert
       profiles vs_1_1 arbvp1
 }
 
 fragment_program Simple_PerPixel_Frag cg
 {
       source ARNOLD.cg
 
       default_params
       {
                  param_named_auto lightDiffuse light_diffuse_colour 0
                  param_named_auto lightSpecular light_specular_colour 0
 
         param_named exponent float 127
             //VERY high value, to produce large highlights
 
         param_named ambient float4 0.0 0.0 0.0 1.0
             //faint environment lighting
       }
 
       entry_point Simple_PerPixel_Frag
       profiles ps_2_0 arbfp1
 }

 material Simple_Perpixel : TriplanarTexturing
 {
       technique
       {
   
         pass
         {
   
 
                    vertex_program_ref TriplanarTexturingVertexProgram
                    {
                     }
 
                     fragment_program_ref TriplanarTexturingFragmentProgram
                     {
                     }
               
                        texture_unit
                        {

               texture texatlas.tga 2d 5
                      
                        }
              }


          
       }
      }

 
 void Ambient_vp(
         float4 position : POSITION,
 
           out float4 oPosition : POSITION,
           out float4 colour    : COLOR,
 
           uniform float4x4 worldViewProj,
           uniform float4 ambient)
 {
     oPosition = mul(worldViewProj, position);
     colour = ambient;
 }
 
 vertex_program Ambient cg
 {
     source ARNOLD.cg
 
     default_params
     {
         param_named_auto worldViewProj worldviewproj_matrix
         param_named ambient float4 0.2 0.2 0.2 1.0
     }
 
     entry_point Ambient_vp
     profiles vs_1_1 arbvp1
 }


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Fri Mar 09, 2012 10:25 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
xelons wrote:
The first problem is a hug drop in FPS, I think this is related to the bad shaders configuration that I implemented.


How much of a drop? In general performance of individual effects should be measured in milliseconds rather than FPS (see here). You should probably aim for your SSAO to take something like 1ms... is this in line with what you are seeing?

xelons wrote:
I guess my main problem here is that I'm using triplanar texturing...


It seems unlikely, as SSAO is basically a postprocessing technique and should not depend on the texturing method. But while you get it working you should probably replace your material with a white one anyway, so you can properly see the effect of the SSAO. I can't help much more than that as I've never used SSAO myself.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: Ambient Occlusion help
PostPosted: Fri Mar 09, 2012 3:31 pm 

Joined: Wed Feb 29, 2012 12:35 am
Posts: 25
Usually when I draw a large volume without initializing SSAO the FPS is around 80-90, when I do initialize it using
Code:
PFXSSAO* mSSAO = new PFXSSAO(window, camera);
the FPS drops to about 20-30.

I initialize the PFXSSAO in my createscene function.
The other weird problem is that if I don't create a light in my createscene function the fps seems to be ok, once I create a simple light in my scene the FPS drops considerably.

I'm still learning about it tho, thanks for all the help.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net