Jump to content

  • Log In with Google      Sign In   
  • Create Account

Voxelization using Dominant Axis (Voxel Cone Tracing)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 gboxentertainment   Members   -  Reputation: 770

Like
0Likes
Like

Posted 12 February 2013 - 05:37 AM

I am trying to voxelize my scene by rasterization using the dominant axis theory - which involves using the vertex normals to find the dominant axis of each triangle which would then determine the direction to apply an orthographic projection of the scene. This is done in the geometry shader and then passed to the fragment shader.

 

I manage to voxelize the scene, except for one major problem as shown below, adjacent to the non-voxelized scene:

givox0.jpg givox1.jpg

 

Notice how there are artifacts above the Buddha and the Sphere?

 

Does anyone know what could be the cause of this and how this can be fixed?

 

Here is my geometry shader code:

#version 430

layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;

in VSOutput
{
	vec4 ndcPos;
	vec4 fNorm;
	vec2 fTexCoord;
	vec4 worldPos;
} vsout[];

out GSOutput
{
	vec4 ndcPos;
	vec4 fNorm;
	vec2 fTexCoord;
	vec4 worldPos;
	vec4 axisCol;
	mat4 oProj;
} gsout;

uniform mat4 voxSpace;

void main()
{
	gsout.oProj = mat4(0.0);

	for(int i = 0; i<gl_in.length(); i++)
	{	
		
		vec4 n = vsout[i].fNorm;

		gsout.fNorm = n;
		gsout.fTexCoord = vsout[i].fTexCoord;
		gsout.worldPos = vsout[i].worldPos;
		
		float maxC = max(abs(n.x), max(abs(n.y), abs(n.z)));
		float x,y,z;
		x = abs(n.x) < maxC ? 0.0 : 1.0;
		y = abs(n.y) < maxC ? 0.0 : 1.0;
		z = abs(n.z) < maxC ? 0.0 : 1.0;

		vec4 axis = vec4(x,y,z,1);
		
		if(axis == vec4(1.0,0.0,0.0,1))
		{
			gsout.oProj[0] = vec4(0,0,-1,0);
			gsout.oProj[1] = vec4(0,-1,0,0);
			gsout.oProj[2] = vec4(-1,0,0,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}	
		else if(axis == vec4(0,1,0,1))
		{
			gsout.oProj[0] = vec4(1,0,0,0);
			gsout.oProj[1] = vec4(0,0,1,0);
			gsout.oProj[2] = vec4(0,-1,0,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}
		else if(axis == vec4(0,0,1,1))
		{
			gsout.oProj[0] = vec4(1,0,0,0);
			gsout.oProj[1] = vec4(0,-1,0,0);
			gsout.oProj[2] = vec4(0,0,-1,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}
		
		gsout.axisCol = axis;

		gl_Position = gsout.oProj*voxSpace*gl_in[i].gl_Position;
		gsout.ndcPos = gsout.oProj*voxSpace*vsout[i].ndcPos;

		EmitVertex();
	}
	
}

Edited by gboxentertainment, 13 February 2013 - 07:41 AM.


Sponsor:

#2 Krypt0n   Crossbones+   -  Reputation: 2664

Like
1Likes
Like

Posted 12 February 2013 - 05:56 AM

 which involves using the vertex normals to find the dominant axis of each triangle 

is that a typo or you're really doing that? (it's not fully clear to me from your source, but it seems like).

 

you need to calculate the face normal, in the GS, not using the Vertex normals, all vertices per face need to be transformed in the same way. could explain those random voxelizations you got for the sphere and buddah, and the broken edges of your cube.



#3 gboxentertainment   Members   -  Reputation: 770

Like
0Likes
Like

Posted 12 February 2013 - 07:00 AM

Cheers Krypt0n,

 

The moment you mentioned face normal, I realised immediately what was wrong. Here's the results:

givox2.jpg

 

and here's the corrected code:

#version 430

layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;
//layout(binding = 0) uniform atomic_uint ac;

in VSOutput
{
	vec4 ndcPos;
	vec4 fNorm;
	vec2 fTexCoord;
	vec4 worldPos;
} vsout[];

out GSOutput
{
	vec4 ndcPos;
	vec4 fNorm;
	vec2 fTexCoord;
	vec4 worldPos;
	vec4 axisCol;
	mat4 oProj;
} gsout;

uniform mat4 voxSpace;

void main()
{
	gsout.oProj = mat4(0.0);

	vec4 n = normalize(vsout[0].fNorm);

	for(int i = 0; i<gl_in.length(); i++)
	{	
		gsout.fNorm = n;
		
		float maxC = max(abs(n.x), max(abs(n.y), abs(n.z)));
		float x,y,z;
		x = abs(n.x) < maxC ? 0.0 : 1.0;
		y = abs(n.y) < maxC ? 0.0 : 1.0;
		z = abs(n.z) < maxC ? 0.0 : 1.0;

		vec4 axis = vec4(x,y,z,1);
		
		if(axis == vec4(1.0,0.0,0.0,1))
		{
			gsout.oProj[0] = vec4(0,0,-1,0);
			gsout.oProj[1] = vec4(0,-1,0,0);
			gsout.oProj[2] = vec4(-1,0,0,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}	
		else if(axis == vec4(0,1,0,1))
		{
			gsout.oProj[0] = vec4(1,0,0,0);
			gsout.oProj[1] = vec4(0,0,1,0);
			gsout.oProj[2] = vec4(0,-1,0,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}
		else if(axis == vec4(0,0,1,1))
		{
			gsout.oProj[0] = vec4(1,0,0,0);
			gsout.oProj[1] = vec4(0,-1,0,0);
			gsout.oProj[2] = vec4(0,0,-1,0);
			gsout.oProj[3] = vec4(0,0,0,1);
		}
		
		gsout.axisCol = axis;

		gl_Position = gsout.oProj*voxSpace*gl_in[i].gl_Position;
		gsout.ndcPos = gsout.oProj*voxSpace*vsout[i].ndcPos;
		gsout.fTexCoord = vsout[i].fTexCoord;
		gsout.worldPos = vsout[i].worldPos;

		EmitVertex();
	}
	
}

 

Now the new challenge with switching from 6-directional 3d textures for storing my voxels to a single 3d texture is sampling anisotropically for voxel cone tracing.

Originally this was my code:

vec4 sampleVox(vec3 pos, vec3 dir, float lod)
{
	vec4 sampleX = dir.x < 0.0 ? textureLod(voxTex[0], pos, lod) : textureLod(voxTex[1], pos, lod);
	vec4 sampleY = dir.y < 0.0 ? textureLod(voxTex[2], pos, lod) : textureLod(voxTex[3], pos, lod);
	vec4 sampleZ = dir.z < 0.0 ? textureLod(voxTex[4], pos, lod) : textureLod(voxTex[5], pos, lod);
	
	vec3 wts = abs(dir);

	float invSampleMag = 1.0/(wts.x + wts.y + wts.z + 0.0001);
	wts *= invSampleMag;

	vec4 filtered = (sampleX*wts.x + sampleY*wts.y + sampleZ*wts.z)/10;

	return filtered;
}

 

where 'dir' is the direction of the cone that I'm tracing. I guess I would just offset 'pos' by 1 voxel in each direction?

 

[EDIT]

 

So I've tried offsetting 'pos' by 1 voxel for each direction to get anisotropic sampling - seems to work:

 

givox3.jpg givox4.jpg

 

Here's the code:

vec4 sampleVox(vec3 pos, vec3 dir)
{
	vec4 sampleX = dir.x < 0.0 ? textureLod(sampler0, pos-vec3(1,0,0)/voxDim, 0) : textureLod(sampler0, pos+vec3(1,0,0)/64.0, 0);
	vec4 sampleY = dir.y < 0.0 ? textureLod(sampler0, pos-vec3(0,1,0)/voxDim, 0) : textureLod(sampler0, pos+vec3(0,1,0)/64.0, 0);
	vec4 sampleZ = dir.z < 0.0 ? textureLod(sampler0, pos-vec3(0,0,1)/voxDim, 0) : textureLod(sampler0, pos+vec3(0,0,1)/64.0, 0);
	vec3 wts = abs(dir);
	float invSampleMag = 1.0/(wts.x + wts.y + wts.z + 0.0001);
	wts *= invSampleMag;
	vec4 filtered = (sampleX*wts.x+sampleY*wts.y+sampleZ*wts.z);

	return filtered;
}

 

I will share results once I have implemented this into my voxel cone tracer and have proven that it is anisotropically sampled.


Edited by gboxentertainment, 12 February 2013 - 07:24 AM.


#4 gboxentertainment   Members   -  Reputation: 770

Like
1Likes
Like

Posted 12 February 2013 - 08:32 AM

Okay, so I implemented the dominant axis voxelization into my voxel cone tracer. Here are the results:

 

vct3.jpg givox5.jpg

 

Aside from the very soft and very voxelized shadows, the main artifact is the diffuse reflection from the red box caused by the illumination sampled from the base mip - this is due to the voxelized box being extruded out too much. I'm not using conservative voxelization by the way.

I don't think I've got the soft shadowing calculation right either, all I'm doing is just taking the vct function and tracing a single cone in the direction of the light.

 

Here is my vct code:

 

#version 430

in vec4 fNorm;
in vec2 fTexCoord;
in vec4 worldPos;
out vec4 fColor;

uniform sampler2D sampler0;

layout(binding = 1) uniform sampler3D voxTex;

uniform mat4 worldSpace;
uniform mat4 voxTexSpace;

uniform float voxDim;

uniform vec4 baseCol;
uniform float specConeRatio;
uniform vec4 lightPos;
uniform vec4 lightDir;
uniform vec3 camPos;

float minVoxDiam = 1/voxDim;

vec4 sampleVox(vec3 pos, vec3 dir, float lod)
{
	vec4 sampleX = dir.x < 0.0 ? textureLod(voxTex, pos-vec3(1,0,0)/voxDim, lod) : textureLod(voxTex, pos+vec3(1,0,0)/voxDim, lod);
	vec4 sampleY = dir.y < 0.0 ? textureLod(voxTex, pos-vec3(0,1,0)/voxDim, lod) : textureLod(voxTex, pos+vec3(0,1,0)/voxDim, lod);
	vec4 sampleZ = dir.z < 0.0 ? textureLod(voxTex, pos-vec3(0,0,1)/voxDim, lod) : textureLod(voxTex, pos+vec3(0,0,1)/voxDim, lod);
	
	vec3 wts = abs(dir);

	float invSampleMag = 1.0/(wts.x + wts.y + wts.z + 0.0001);
	wts *= invSampleMag;

	vec4 filtered = (sampleX*wts.x + sampleY*wts.y + sampleZ*wts.z)/10;

	return filtered;
}

vec4 coneTrace(vec3 o, vec3 d, float coneRatio, float maxDist)
{
	vec3 samplePos = o;
	vec4 accum = vec4(0.0);
	float minDiam = 1.0/voxDim;
	float startDist = minDiam;

	float dist = startDist;
	while(dist <= maxDist && accum.w < 1.0)
	{
		float sampleDiam = max(minDiam, coneRatio*dist);
		float sampleLOD = log2(sampleDiam*voxDim);
		vec3 samplePos = o + d*dist;
		vec4 sampleVal = sampleVox(samplePos, -d, sampleLOD);

		float sampleWt = (1.0 - accum.w);
		accum += sampleVal * sampleWt;

		dist += sampleDiam;
	}

	accum.xyz *= 8.0;

	return accum;
}

vec4 shadowConeTrace(vec3 o, vec3 d, float coneRatio, float maxDist)
{
	vec3 samplePos = o;
	vec4 accum = vec4(0.0);
	float minDiam = 1/voxDim;
	float startDist = minDiam;

	float dist = startDist;
	while(dist <= maxDist && accum.w < 1.0)
	{
		float sampleDiam = max(minDiam, coneRatio*dist);
		float sampleLOD = log2(sampleDiam*voxDim);
		vec3 samplePos = o + d*dist;
		vec4 sampleVal = sampleVox(samplePos, -d, sampleLOD);

		float sampleWt = (1.0 - accum.w);
		accum += sampleVal * sampleWt;

		dist += sampleDiam;
	}

	accum.xyz *= 2.0;

	return accum;
}

void main()
{
	vec4 color = vec4(0,0,0,0);
	vec4 texMap = texture(sampler0, fTexCoord);
	vec4 voxPos = voxTexSpace*worldPos;
	voxPos.xyz *= 1.0/voxPos.w;

	//Direct Lighting
	vec4 L = normalize(lightPos - worldPos);
	vec4 D = normalize(lightDir);
	float spotAngle = dot(-L, D);
	float outerCone = 0.7;
	float innerCone = 0.2;
	float spot = clamp((spotAngle - outerCone)/innerCone, 0.0, 1.0);
	float dist = length(L);
	vec4 N = normalize(fNorm);
	float I = max(dot(N, L), 0.0);
	
	if(I > 0.0)
		color += I*spot;

	//Specular Reflection
	vec3 spec;
	vec3 viewToFrag = normalize(worldPos.xyz - camPos);
	vec3 reflDir = reflect(viewToFrag, N.xyz);
	spec = specConeRatio <= 1.0 ? coneTrace(voxPos.xyz, reflDir, specConeRatio, 0.3).xyz : vec3(0);

	//Indirect Lighting
	vec3 iDiffuse = vec3(0);
	float iConeRatio = 0.9;
	float iMaxDist = 0.3;
	iDiffuse += coneTrace(voxPos.xyz, N.xyz, iConeRatio, iMaxDist).xyz;
	iDiffuse += 0.707*coneTrace(voxPos.xyz, N.xyz+vec3(1,0,0), iConeRatio, iMaxDist).xyz;
	iDiffuse += 0.707*coneTrace(voxPos.xyz, N.xyz+vec3(0,1,0), iConeRatio, iMaxDist).xyz;
	iDiffuse += 0.707*coneTrace(voxPos.xyz, N.xyz+vec3(0,0,1), iConeRatio, iMaxDist).xyz;
	iDiffuse += 0.707*coneTrace(voxPos.xyz, N.xyz+vec3(0,-1,0), iConeRatio, iMaxDist).xyz;
	vec4 shadowComp = vec4(0);
	shadowComp += shadowConeTrace(voxPos.xyz, L.xyz, 0.3, 0.4);
	float shadow = (shadowComp.x+shadowComp.y+shadowComp.z)*4;

	fColor.xyz = ((color.xyz/2+iDiffuse)*baseCol.xyz*texMap.xyz+spec)*(1-shadow);
	fColor.w = color.w*1;
}

 

 

Hopefully someone will come up with some tips to help me improve this.

 

[EDIT]

 

On second thoughts, it might actually be the shadowing calc that's causing the artifact with the red box.


Edited by gboxentertainment, 12 February 2013 - 08:40 AM.


#5 gboxentertainment   Members   -  Reputation: 770

Like
0Likes
Like

Posted 12 February 2013 - 04:53 PM

Here's some results on performance with various tests on my gtx485m at 1024 x 768 resolution and 64x64x64 voxel dimensions:

 

6-directional texture voxelization:

With anisotropic sampling enabled: ~25 to 30fps

I can't really disable anisotropic sampling for this test because that would mean most voxels would not be captured.

 

Dominant axis voxelization:

With anisotropic sampling enabled: ~25 to 35fps (that's if I get real close to the high-res Buddha).

With anisotropic sampling disabled: ~60fps (double the fps!)

 

There does not seem to be much noticeable difference in quality - although this is yet to be tested properly.

I guess disabling anisotropic sampling significantly reduces the amount of calculations done for every mip level during the cone tracing.



#6 Frenetic Pony   Members   -  Reputation: 1396

Like
0Likes
Like

Posted 12 February 2013 - 06:48 PM

I'd suggest trying to pre-voxelize everything and just inject it into your grid. I really don't understand why there was the insistence on re-voxelization every frame, except maybe because the voxel cone tracing paper was more academic than practical initially. Certainly dynamic re-voxelization might be useful for dynamic destruction/other physics/deformation stuff. But really, most of your stuff is just going to a static model, and even animated models can be voxelized and transformed.

 

Why waste all those resources rasterizing everything multiple times per frame? That being said, if you really do have something that needs to be re-rasterized, you can use Epic's trick of marking things as static and checking each frame to see if they actually need to be re-voxelized. Otherwise they just re-use the previous voxelization. Anyway, it's looking good.


Edited by Frenetic Pony, 12 February 2013 - 06:49 PM.


#7 gboxentertainment   Members   -  Reputation: 770

Like
0Likes
Like

Posted 13 February 2013 - 07:55 AM

Why waste all those resources rasterizing everything multiple times per frame? That being said, if you really do have something that needs to be re-rasterized, you can use Epic's trick of marking things as static and checking each frame to see if they actually need to be re-voxelized.

 

For this scene:

Dominant axis voxelization (anisotropic sampling enabled):

Static pre-voxelization once during initialization: ~25 to 35 fps

Dynamic voxelization per frame: ~25 to 35fps

 

Majority of the cost is actually in the cone tracing, which is why when I turn off anisotropic sampling there is such a massive increase in framerate - however, I have found out that anisotropic filtering is required for more physically accurate lighting, particularly with specular where the cone ratio is much smaller.

Of course, if I implemented an octree system, then the cost of building the octree might be too much for a per frame basis. Likewise if I had a more complex scene. Ultimately, the plan for this particular test scene was to keep the walls and floor static and the objects dynamic so that I can move them around to demonstrate the lighting effect. However, this is low on my priority list - something that I would implement at the end after I've corrected all noticeable artifacts.

 

At the moment, my aim is to get the lighting as accurate as possible. Then I would worry about performance. This is a hobby research project - I am not trying to create a game.

 

Anyhow, check out my latest semi-success in soft shadowing using vct:

giboxshadows.jpg giboxshadows1.jpg giboxshadows2.jpg

 

float sampleVoxShadows(vec3 pos, vec3 dir, float lod)
{
	float shadowSamp = textureLod(voxTex, pos, lod).w;

	return shadowSamp;
}

float shadowConeTrace(vec3 o, vec3 d, float coneRatio, float maxDist)
{
	vec3 samplePos = o;
	float sampleVal = 0.0;
	float accum = 0.0;
	float minDiam = 1/voxDim;
	float startDist = minDiam;

	float dist = startDist;
	while(dist <= maxDist && accum < 1.0)
	{
		float sampleDiam = max(minDiam, coneRatio*dist);
		float sampleLOD = log2(sampleDiam*voxDim);
		vec3 samplePos = o + d*dist;
		sampleVal = sampleVoxShadows(samplePos, -d, sampleLOD);

		float sampleWt = (1.0 - accum);
		accum += sampleVal * sampleWt;

		dist += sampleDiam;
	}

	return accum;
}

	float shadow = 0;
	shadow += shadowConeTrace(voxPos.xyz, L.xyz, 0.1, 1.0);

	fColor.xyz = ((color.xyz*(1-shadow)+iDiffuse.xyz)*baseCol.xyz*texMap.xyz+spec); 

 

I just need to figure out a way to remove the voxelized self-shadowing artifacts that are apparent on the blue sphere.

I also need to work out how to remove or reduce the add-on effect that the radiance has on direct lighting.


Edited by gboxentertainment, 14 February 2013 - 04:15 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS