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


All times are UTC




Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: ogre <-> Polyvox (cg shader problems)
PostPosted: Fri Jun 01, 2012 7:56 pm 

Joined: Fri Jun 01, 2012 5:31 pm
Posts: 11
I read almost everything i could find about inplementing polyvox to ogre and now finally it almost works fine.
But i still have problems with scaling and texturecoordinates.
here is my cg-shader i found in another thread:
Code:
float2 tmp;

void ColouredCubicVoxelVP(
    float4 inPosition    : POSITION,
    float4 inNormal        : NORMAL,

    out float4 outClipPosition        : POSITION,
    out float4 outWorldPosition    : TEXCOORD0,
    out float4 outWorldNormal    : TEXCOORD1,

    uniform float4x4 world,
    uniform float4x4 viewProj
    )
{   
    //Compute the world space position
    outWorldPosition = mul(world, inPosition);   
   
    //Just pass though the normals without transforming them in any way. No rotation occurs.
    outWorldNormal = inNormal;

    //Compute the clip space position
    outClipPosition = mul(viewProj, outWorldPosition);
}

void ColouredCubicVoxelFP(
    float4 inPosition        : POSITION,
    float4 inWorldPosition    : TEXCOORD0,
    float4 inWorldNormal    : TEXCOORD1,

    uniform sampler2D heightMap : TEXUNIT0,
   
    out float4 result        : COLOR)
{   
    inWorldNormal = normalize(inWorldNormal);
   
    float3 col;
   
    //World position is used as texture coordinates. Choose which
    //two components of world position to use based on normal. Could
    //optionally use a different texture for each face here as well.
    if(inWorldNormal. x > 0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.y,inWorldPosition.z));
    }
   
    if(inWorldNormal. x < -0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.y,inWorldPosition.z));
    }
   
    if(inWorldNormal. y > 0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.x,inWorldPosition.z));
    }
   
    if(inWorldNormal. y < -0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.x,inWorldPosition.z));
    }
   
    if(inWorldNormal. z > 0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.x,inWorldPosition.y));
    }
   
    if(inWorldNormal. z < -0.5)
    {
        col = tex2D(heightMap, float2(inWorldPosition.x,inWorldPosition.y));
    }
   
    result = float4(col, 1.0);
}

it works, but when i scale my object lets say about ten times, the textur is ten times to small.
i think i have to devide the inWorldPosition variable by 10 and add 0.5 (textur is scrolled a bit wrong).
but when i try to do so, i just get some error messages or i get a white object without texture.

Another thing i want to know is, how can i use a textur-atlas, or in other words, how can i pass an int variable (the id of the texture in the atlas) through the shader (one specific value for each voxel)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Fri Jun 01, 2012 10:42 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
gamer8o4 wrote:
but when i try to do so, i just get some error messages or i get a white object without texture.


What do the errors say? It may be that the variable 'inWorldPosition' is a constant because it's passed in... actually I forget how it works in Cg. So you may need to assign it to a new variable and then divide that one by 10? It's hard to be sure without seeing you modified code and the errors though.

gamer8o4 wrote:
Another thing i want to know is, how can i use a textur-atlas, or in other words, how can i pass an int variable (the id of the texture in the atlas) through the shader (one specific value for each voxel)


You can get the material using the getMaterial() member of the vertex. It's returned as a float so you can send this to the graphics card and compare against it in the shader. If you search you can probably find examples of texture atlases shaders on this forum, but you may want to look elsewhere as well.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 5:30 am 

Joined: Fri Jun 01, 2012 5:31 pm
Posts: 11
when i do this:
Code:
col = tex2D(heightMap, (inWorldPosition.yz/10)+0.5);

i get this: http://mediadesign.about.lc/Unbdenannt.JPG
and when i do this:
and when i do this:
Code:
      float2 tmp = inWorldPosition.yz;
      tmp = tmp/10;
      tmp = tmp+0.5;
        col = tex2D(heightMap, tmp);

i get this: http://mediadesign.about.lc/Unbenannt.JPG


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 9:27 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
I do remember seeing this once. A quick google of your error mesage throws this up: http://forum.unity3d.com/threads/63120-ShaderModel-3.0-amp-Texture-accesss-inside-dynamic-conditional-if-blocks?p=405278&viewfull=1#post405278

It looks like you can't use regular tex2D inside an 'if' statement and you might want to try tex2Dlod instead. Not sure why the change you made triggered it though... perhaps because the temp variable is not uniform? You might find better advice on a Cg forum though as this is basically a shader problem and so my knowledge is limited.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 9:42 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
David Williams wrote:
Not sure why the change you made triggered it though... perhaps because the temp variable is not uniform?


It appears the non-uniform variable probably is the issue, by coincidence I just came across this: http://www.opengl.org/wiki/GLSL_Sampler ... ow_control

Can you declare the tmp variable as uniform, or is that only for variables which are passed into the shader? I'm not sure of the exact rules here so you'll have to experiment.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 3:04 pm 

Joined: Fri Jun 01, 2012 5:31 pm
Posts: 11
yes, thanks, but it still doesn't work.
which other argument do i need to pass in the tex2Dlod-function?
i get a white volume (so maybe shader-error)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 3:19 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
gamer8o4 wrote:
yes, thanks, but it still doesn't work.
which other argument do i need to pass in the tex2Dlod-function?


I don't know the exact details but the reference is here: http://http.developer.nvidia.com/Cg/tex2Dlod.html. Basically you need to pass a 'texelOff' as the last parameter. You can probably use zero for testing but I'm not certain... you'll have to search for more info on what this parameter means.

gamer8o4 wrote:
i get a white volume (so maybe shader-error)

Yes, this is a material error (usually a shader error). If you look in the Ogre.log it will tell you what what the shader compiler says.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sat Jun 02, 2012 3:41 pm 

Joined: Fri Jun 01, 2012 5:31 pm
Posts: 11
i uploaded my test project with the shader code.
maybe someone can modify the code, because i'm very new to cg.
further i think i can handle the implemantation of a textureatlas myself.
mediadesign.about.lc/Debug.zip

@David Williams:
you programmed a shader for ogre, too, didn't you?
would be nice i you would post yours.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sun Jun 03, 2012 6:30 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
I don't have time to debug your code, unfortunatly, but I can give some further pointers. But first, can I just confrm that you are using one of the cubic surface extractors rather than the marching cubes one? If so, have a look at this thread: http://www.volumesoffun.com/phpBB3/viewtopic.php?f=2&t=152

And specifically this shader code:

Code:
   float2 uv;
   if(worldNormal.x > 0.5)
      uv = inWorldPosition.yz;

   if(worldNormal.x < -0.5)
      uv = inWorldPosition.yz;

   if(worldNormal.y > 0.5)//Down
      uv = inWorldPosition.xz;

   if(worldNormal.y < -0.5)//Up
      uv = inWorldPosition.xz;

   if(worldNormal.z > 0.5)
      uv = inWorldPosition.xy;

   if(worldNormal.z < -0.5)
      uv = inWorldPosition.xy;

   uv = frac(uv / 2 - 0.5);
   uv = uv / 4;

   result = tex2D(texture, uv);


It's similar in principle to the code you posted yourself, but notice that it does not sample the texture within the if statement. Instead it uses the if statements to set up the texcoords and then samples the texture after all the if statements. This presumably avoids the issue you are encountering, because it appears to scale the texture at the end (the divide by 4).

The only shaderer code which I have for subic surfaces is what's in the Voxeliens demo. I wouldn't really recommend you work from it (it's complex and dependant on setup done on the CPU), but if you want to see it then look at 'materials\VoxelMaterial\ColouredCubicVoxel.hlsl' The important function is buildCoordinateSystem. Note that this function doesn't actually sample the texture but only sets up the texcoords for later use:

Code:
void buildCoordinateSystem(float3 worldPosition, out float2 texCoords, out float3 tangent, out float3 binormal, out float3 normal)
{
   //Compute normals from derivitive instrutions
   normal = cross(ddy(worldPosition), ddx(worldPosition));
   normal = normalize(normal);
   
   //Need to initialise to something, otherwise 45
   //degree angles (fireballs) never get texcoords.
   texCoords = float2(0.5, 0.5);
   
   //For some reason, in OpenGL the computed normals face the other way.
   #if OPENGL
      normal *= -1.0f;
   #endif
   
   //Small offset, because our cubes are centered on integer position rather than
   float3 worldPositionOffset = worldPosition + float3(0.5, 0.5, 0.5);
   
   //Maybe we can improve on this with a x1x1 cubemap lookup texture, to convert
   //normal to face index? Then a look up table from face index to other details?
   if(normal.x > 0.9)
   {
      texCoords = float2(-worldPositionOffset.z, worldPositionOffset.y);
      tangent = float3(0.0, 0.0,-1.0);
      binormal = float3(0.0, 1.0,0.0);
   }
   else if(normal.y > 0.9)
   {
      texCoords = float2(worldPositionOffset.x, -worldPositionOffset.z);
      tangent = float3(1.0, 0.0, 0.0);
      binormal = float3(0.0, 0.0, -1.0);
   }
   else if(normal.z > 0.9)
   {
      texCoords = float2(worldPositionOffset.x, worldPositionOffset.y);
      tangent = float3(1.0, 0.0, 0.0);
      binormal = float3(0.0, 1.0, 0.0);
   }   
   else if(normal.x < -0.9)
   {
      texCoords = float2(worldPositionOffset.z, worldPositionOffset.y);
      tangent = float3(0.0, 0.0,1.0);
      binormal = float3(0.0, 1.0,0.0);
   }   
   else if(normal.y < -0.9)
   {
      texCoords = float2(worldPositionOffset.x, worldPositionOffset.z);
      tangent = float3(1.0, 0.0, 0.0);
      binormal = float3(0.0, 0.0, 1.0);
   }
   else if(normal.z < -0.9)
   {
      texCoords = float2(-worldPositionOffset.x, worldPositionOffset.y);
      tangent = float3(-1.0, 0.0, 0.0);
      binormal = float3(0.0, 1.0, 0.0);
   }
}


gamer8o4 wrote:
i think i can handle the implemantation of a textureatlas myself.

Be aware that texture atlases get pretty complex if you want to make use of linear filtering. You should find some useful code on these forums but I'm not sure anyone got it working 100%. If you just want point filtering (like Minecraft) then it's much easier, but you still might be better off using a different system suc as different texture units or texture arrays (if you have hardware support).


Top
Offline Profile  
Reply with quote  
 Post subject: Re: ogre <-> Polyvox (cg shader problems)
PostPosted: Sun Jun 03, 2012 8:17 am 

Joined: Fri Jun 01, 2012 5:31 pm
Posts: 11
Quote:
are using one of the cubic surface extractors rather than the marching cubes one

yes, i use the cubicsurfaceextractorwithnormals and material8.

i will look through you code later and post my results...


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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