• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Migi0027

Scene Voxelization

11 posts in this topic

Hi guys! wink.png

 

Recently I have been working on my Voxel Cone Tracer, most of setup is done, but the actual voxelization isn't working right now, which is blocking further progress, or getting some results in my other code.

 

Here's my attempt:

 

Generating matrices (Some code may seem stupid, unnecessary, but again, right now my goal is to get it working, nothing else):

#define TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
D3DXVECTOR3 GetCubeMapLookat(int side)
{
	switch (side)
	{
	case 0x8515: // +X
		return CEVector3f(1, 0, 0);
		break;
	case 0x8516: // -X
		return CEVector3f(-1, 0, 0);
		break;
	case 0x8517: // +Y
		return CEVector3f(0, 1, 0);
		break;
	case 0x8518: // -Y
		return CEVector3f(0, -1, 0);
		break;
	case 0x8519: // +Z
		return CEVector3f(0, 0, 1);
		break;
	case 0x851A: // -Z
		return CEVector3f(0, 0, -1);
		break;
	}

	// What the beep are you doing here?
	return CEVector3f(0, 0, 0);
}
_until(n, 6)
	{
		until( m_vVoxelisers.size() ) // A vector containing all meshes needed to be voxelized
		{ // m_vVoxelisers->src is the mesh
			// Create constants, dependent on mSize basically
			CEVector3f const mSize(128, 128, 128);
			CEVector3f const b_max = CEVector3f(mSize.x / 2.0f,  mSize.y / 2.0f,  mSize.z / 2.0f);
			CEVector3f const b_min = CEVector3f(-mSize.x / 2.0f, -mSize.y / 2.0f, -mSize.z / 2.0f);

			CEVector3f const scale = CEVector3f(
				1.0f / (b_max.x - b_min.x),
				1.0f / (b_max.y - b_min.y),
				1.0f / (b_max.z - b_min.z)
				);

			CEVector3f const m_VVoxel_Bias = - b_min;

			// convert to a uniform scale so the voxels stay cubes
			float const m_FVoxel_Scale = std::min(scale.x, std::min(scale.y, scale.z));

			// Base Units
			D3DXMATRIX t, s, s2, t2;
			D3DXMatrixTranslation(&s, -1.0f, -1.0f, -1.0f);
			D3DXMatrixScaling    (&t, 2.0f, 2.0f, 2.0f);
			D3DXMatrixScaling    (&s2, m_FVoxel_Scale, m_FVoxel_Scale, m_FVoxel_Scale);
			D3DXMatrixTranslation(&t2, m_VVoxel_Bias.x, m_VVoxel_Bias.y, m_VVoxel_Bias.z);

			D3DXMATRIX m_MVoxel_WorldUnit =  t * s * s2 * t2;

			// Generate view matrix
			D3DXMATRIX unitCubeToNDC;
			CEVector3f CubeLookatDir = GetCubeMapLookat(TEXTURE_CUBE_MAP_POSITIVE_X + n);
			D3DXMatrixLookAtLH(&unitCubeToNDC, &CEVector3f(0, 0, 0), &CubeLookatDir, &CEVector3f(0, 1, 0));

			D3DXMatrixTranspose(&unitCubeToNDC, &unitCubeToNDC);
			D3DXMatrixTranspose(&m_MVoxel_WorldUnit, &m_MVoxel_WorldUnit);

			// m_MVoxel_WorldUnit messes this up somehow
			D3DXMATRIX MVPMatrix = m_MVoxel_WorldUnit * unitCubeToNDC;

			// Create the storage matrix (helper in 3d texture)
			D3DXMATRIX storageTransform;
			D3DXMatrixTranspose(&storageTransform, &unitCubeToNDC);

			// Send the needed data, mesh wise
			m_vVoxelisers[i]->src->SendBuffers(devcon, dev, Camera, ObjectBuffer, Bufferphase);

			// Output to the staging texture
			VoxeliserShader.Render(devcon, Bufferphase, ObjectBuffer.world, *Camera->getView(),  *Camera->getProjection(),
				MVPMatrix, storageTransform,
				&VoxelMap[n],
				false, 0);
		}
	}

the Shader.Render function:

bff_PrObject.worldMatrix = world;

	bff_PrObject.gi_storageTransform = gi_storage;
	bff_PrObject.gi_VPMatrix = gi_VP; // in the other piece of code it's MVP, but it's the same matrix

	devcon->UpdateSubresource(pCBuffer[0], 0, 0, &bff_PrObject, NULL, NULL);

	devcon->VSSetConstantBuffers(0, 2, pCBuffer);
	devcon->PSSetConstantBuffers(0, 2, pCBuffer);

	// Set the output texture
	devcon->OMSetRenderTargetsAndUnorderedAccessViews(0, NULL, NULL, 1, 1, &t3D->pTexture, 0);

	if (!instanced)
		devcon->DrawIndexed(bf.IndexCount, 0, 0);
	else
		devcon->DrawIndexedInstanced(bf.IndexCount, instanceCount, 0, 0, 0);

(The shader it self is laughable, basing the color on y pos, but right now that's because I just want a simple and quick way to generate a result.)

cbuffer ConstantObjectBuffer : register (b0)
{
	matrix worldMatrix;
	
	matrix gi_vp;
	matrix gi_storage;
};

cbuffer ConstantFrameBuffer : register(b1)
{
	matrix viewMatrix;
	matrix projectionMatrix;

	matrix lightViewMatrix;
	matrix lightProjectionMatrix;
	float3 sLightPos;
	float pad2;

	float3 eyepos;
	float cppad;

	float4 lightvec;
	float4 lightcol;

	float FogStart;
	float FogEnd;
	float2 __space;

	float3 FogColor;
	float shadows;

	float SpecularIntensity;
	float2 pad3;
	float4 SpecularColor;

	float3 viewvec;
	float _dpmSetting;

	matrix worldToVoxel;
	float minVoxelDiameter;
	float minVoxelDiameterInv;
	float2 pad__;
};

struct VOut
{
    float4 position : SV_POSITION;
	float2 texcoord : TEXCOORD;
	float3 lNormal : LNORM;

	float yPos : UOS;
};

// First RT has been taken
RWTexture3D<float> voxelTex : register(u1);

SamplerState ss;

VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD, float3 instancePosition : INSTANCEPOS)
{
    VOut output;

	/*if (_instance == 1) Not yet implemented
	{
		position.x += instancePosition.x;
		position.y += instancePosition.y;
		position.z += instancePosition.z;
	}*/

    position.w = 1.0f;
	output.texcoord = texcoord;

	output.yPos = position.y;
	
	output.position = mul(position, gi_vp);
    //output.position = mul(output.position, projectionMatrix);

	float diffusebrightness = saturate(dot(mul(worldMatrix, normal), lightvec));
	output.lNormal = lightcol * diffusebrightness;

    return output;
}

float4 PShader(VOut input) : SV_TARGET
{
	float4 col = float4(1, 1, 1, 1);
	col.rgb = input.lNormal;

	if (input.yPos > 0.1f)
	{
		col.rgb *= float3(0.3, 1.0f, 0.3f);
	}

	float4 storagePos = mul(input.position, gi_storage);
	storagePos.xyz *= 1.0 / storagePos.w;
	
	float3 voxelPos = float3(float3(128, 128, 128) * (storagePos.xyz * .5 + .5));

	col.xyz *= .5;
	voxelTex[voxelPos] = col;

	return col;
}

I know, this is a bunch of code that I just threw upon you, sorry about that, but it may be useful.

 

I do also have a pix session right here: (Around 200 MB, the voxelization starts at EID/Call 245, frame 112)

https://docs.google.com/file/d/0B7ElGDKoFX3ZOF9NS296Q3pvXzA/edit?usp=sharing

 

Anyway, thanks for taking your time to get to the bottom! biggrin.png

 

Thanks, as usual.

-MIGI0027

0

Share this post


Link to post
Share on other sites

I've never used pix before so I don't know how I would be able to help you with that.

Are you just getting a black screen?

 

Have you tried testing your shader to see if you can output just anything? - like "col.xyz = float3(1,1,1);"

Edited by gboxentertainment
0

Share this post


Link to post
Share on other sites

Hi gbox.

 

I've set up a simple experiment using UAVs, and it proved that I could successfully write to the uav used by the voxelizer at the current axis (Cube map).

 

So I basically set pixel 68,68, 3(depth) to white, and when debugging with pix, that specific pixel in depth slice 3 was white in all 3D Textures.

0

Share this post


Link to post
Share on other sites

Just one quick question, I've successfully been able to render the Debug Grid, generating the cubes on the GPU, with an ok fps (with vct, ~20fps 128*128*128).

 

But do I actually need to render 128x128x128 cubes, can't I render half of that, as I don't need perfect debug info, what have other people chosen?

0

Share this post


Link to post
Share on other sites

Glad you got it working smile.png

I use 64x64x64. For everything other than reflections, it seems to be the optimum - although it also depends on the size of your scene.

My scene is probably a double-scaled cornell box scene.

Edited by gboxentertainment
0

Share this post


Link to post
Share on other sites

If you want to debug the voxel data, you can try using an append buffer. Go over all the voxels in your scene and append a vertex to the buffer at that voxel position, with all the necessary info (also skipping all voxels that have nothing in them). Then run a shader using it as a vertex buffer and expand cubes using the geometry shader.

0

Share this post


Link to post
Share on other sites

If you want to debug the voxel data, you can try using an append buffer. Go over all the voxels in your scene and append a vertex to the buffer at that voxel position, with all the necessary info (also skipping all voxels that have nothing in them). Then run a shader using it as a vertex buffer and expand cubes using the geometry shader.

 

That's exactly what I'm doing happy.png

 

But thanks anyways!

0

Share this post


Link to post
Share on other sites

Hmm, could it be because of how OpenGL handles matrices and how Direct3D does?

 

I'll try reversing the multiplication order. (matrix * vector instead of vector * matrix).

0

Share this post


Link to post
Share on other sites

So are you able to display anything now? What do you see - are all the voxels in your debug grid black/white?

In terms of matrices, OpenGL uses column-major - so maybe you could try transposing your matrices.

0

Share this post


Link to post
Share on other sites

When debugging over all of the textures, only some few depth slices actually have been written to, but it's usually just one pixel, which doesn't give any sense.

 

Gbox, did you implement your own implementation or did you follow this? : http://www.geeks3d.com/20121214/voxel-cone-tracing-global-illumination-in-opengl-4-3/

 

If the 2nd, can you share what matrices do you transpose?

0

Share this post


Link to post
Share on other sites
vec4 storagePos = transpose(gsout.oProj)*gsout.ndcPos;
storagePos.xyz *= 1.0/storagePos.w;

Btw, how are you actually doing your voxelization? I'm using the geometry shader with the dominant-axis method. i.e:

#version 430

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

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

out GSOutput
{
	vec4 ndcPos;
	vec4 fNorm;
	vec2 fTexCoord;
	vec4 worldPos;
	vec4 shadowCoord[6];
	mat4 oProj;
	float density;
} gsout;

uniform mat4 voxSpace;
uniform mat4 invVoxSpace;

void main()
{
	gsout.oProj = mat4(0);
	vec4 n = normalize(vsout[0].fNorm);

	for(int i = 0; i<gl_in.length(); i++)
	{
		vec4 voxPos = invVoxSpace*vsout[i].ndcPos;	
		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 : 1;
		y = abs(n.y) < maxC ? 0 : 1;
		z = abs(n.z) < maxC ? 0 : 1;

		vec4 axis = vec4(x,y,z,1);
		
		if(axis == vec4(1,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);
			gsout.density = (voxPos.x - floor(voxPos.x));
		}	
		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);
		gsout.density = (voxPos.y - floor(voxPos.y));
		}
		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.density = (voxPos.z - floor(voxPos.z));
		}

		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;
		gsout.shadowCoord[0] = vsout[i].shadowCoord[0];

		EmitVertex();
	}
	
}
2

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  
Followers 0