Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

Improved CubicSurfaceExtractor
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=2&t=81
Page 1 of 2

Author:  David Williams [ Thu Oct 21, 2010 10:11 pm ]
Post subject:  Improved CubicSurfaceExtractor

Hi all,

I know that at least a couple of people are using the CubicSurfaceExtractor for MineCraft style levels so I wanted to let you know that I've made some improvements to it. These will break your existing code so read carefully.

The biggest difference is that this surface extractor no longer generates normals for the vertices. This mean less data per vertex, and also it means vertices can be shared between different faces of a cube. So generally less data and faster uploads to the GPU.

The catch is that you will still want normals for your lighting, and to solve this I propose generating your normals in the pixel shader. I use the following shader code to do this in Thermite:

Code:
float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
worldNormal = normalize(worldNormal);


I can't use this same trick on the surfaces generated by the Marching Cubes algorithm, and so that one still generates normals. As a result there is now more than one type of vertex in PolyVox. The old 'SurfaceVertex' is gone and in its place are two classes called 'PositionMaterial' and 'PositionMaterialNormal'. The SurfaceMesh class has also been templatized so it can work with either of these types.

If for some reason you still want normals from the CubicSurfaceExtractor then the old version of the code is still available as a class called CubicSurfaceExtractorWithNormals.

It seems to all work OK though there are still some loose ends to tidy up. PolyVox does not have a stable API but hopefully these changes aren't too problematic.

I won't be doing any development for the next week or so but should be able to answer any questions.

Author:  beyzend [ Sat Oct 23, 2010 3:14 am ]
Post subject:  Re: Improved CubicSurfaceExtractor

Upvote!

Yeah I mean cool. I will check it out ASAP. Thanks.

Author:  AndiNo [ Tue Nov 02, 2010 5:03 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

Just tried out the new CubicSurfaceExtractor. I noticed two things:
1) The outmost voxels are correctly drawn now :)
2) I had to include <Array.h> in order to compile my project using CubicSurfaceExtractor. I think the CSE class file should include this file itself.

And again a handful of warnings, I list them here in case you want to fix them (it took me only a couple of minutes):

Code:
__PolyVox_CubicSurfaceExtractor_H__
..\Tools\include\PolyVox\CubicSurfaceExtractor.h||In constructor 'PolyVox::CubicSurfaceExtractor<VoxelType>::CubicSurfaceExtractor(PolyVox::Volume<VoxelType>*, PolyVox::Region, PolyVox::SurfaceMesh<PolyVox::PositionMaterial>*) [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]':|
D:\Programmieren\Mine_ModDLL\main.cpp:244|152|instantiated from here|
..\Tools\include\PolyVox\CubicSurfaceExtractor.h|59|warning: 'PolyVox::CubicSurfaceExtractor<PolyVox::MaterialDensityPair<unsigned char, 7u, 1u> >::m_regSizeInVoxels' will be initialized after|
..\Tools\include\PolyVox\CubicSurfaceExtractor.h|56|warning:   'PolyVox::SurfaceMesh<PolyVox::PositionMaterial>* PolyVox::CubicSurfaceExtractor<PolyVox::MaterialDensityPair<unsigned char, 7u, 1u> >::m_meshCurrent'|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl|36|warning:   when initialized here|

__PolyVox_Array_H__
..\Tools\include\PolyVox\Array.h||In constructor 'PolyVox::Array<dimensions, ElementType>::Array() [with unsigned int noOfDims = 3u, ElementType = PolyVox::IndexAndMaterial]':|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:40|24|instantiated from 'PolyVox::CubicSurfaceExtractor<VoxelType>::CubicSurfaceExtractor(PolyVox::Volume<VoxelType>*, PolyVox::Region, PolyVox::SurfaceMesh<PolyVox::PositionMaterial>*) [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]'|
D:\Programmieren\Mine_ModDLL\main.cpp:244|152|instantiated from here|
..\Tools\include\PolyVox\Array.h|111|warning: 'PolyVox::Array<3u, PolyVox::IndexAndMaterial>::m_pElements' will be initialized after|
..\Tools\include\PolyVox\Array.h|108|warning:   'uint32_t* PolyVox::Array<3u, PolyVox::IndexAndMaterial>::m_pDimensions'|
..\Tools\include\PolyVox\Array.inl|31|warning:   when initialized here|

Array.inl
..\Tools\include\PolyVox\Array.inl||In member function 'void PolyVox::Array<dimensions, ElementType>::resize(const uint32_t (&)[noOfDims]) [with unsigned int noOfDims = 3u, ElementType = PolyVox::IndexAndMaterial, uint32_t = unsigned int]':|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:53|3|instantiated from 'void PolyVox::CubicSurfaceExtractor<VoxelType>::execute() [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]'|
D:\Programmieren\Mine_ModDLL\main.cpp:245|27|instantiated from here|
..\Tools\include\PolyVox\Array.inl|147|warning: comparison between signed and unsigned integer expressions|

CubicSurfaceExtractor.inl
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl||In member function 'int32_t PolyVox::CubicSurfaceExtractor<VoxelType>::addVertex(float, float, float, uint8_t, PolyVox::Array<3u, PolyVox::IndexAndMaterial>&) [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>, int32_t = int, uint8_t = unsigned char]':|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:87|103|instantiated from 'void PolyVox::CubicSurfaceExtractor<VoxelType>::execute() [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]'|
D:\Programmieren\Mine_ModDLL\main.cpp:245|27|instantiated from here|

CubicSurfaceExtractor.inl
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl|174|warning: comparison between signed and unsigned integer expressions|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:87|103|instantiated from 'void PolyVox::CubicSurfaceExtractor<VoxelType>::execute() [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]'|
D:\Programmieren\Mine_ModDLL\main.cpp:245|27|instantiated from here|

CubicSurfaceExtractor.inl
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl|172|warning: unused variable 'uZ'|

__PolyVox_SubArray_H__
..\Tools\include\PolyVox\PolyVoxImpl\SubArray.h||In constructor 'PolyVox::SubArray<noOfDims, ElementType>::SubArray(ElementType*, uint32_t*, uint32_t*) [with unsigned int noOfDims = 2u, ElementType = PolyVox::IndexAndMaterial, uint32_t = unsigned int]':|
..\Tools\include\PolyVox\Array.inl:79|33|instantiated from 'PolyVox::SubArray<(noOfDims - 1), ElementType> PolyVox::Array<dimensions, ElementType>::operator[](uint32_t) [with unsigned int noOfDims = 3u, ElementType = PolyVox::IndexAndMaterial, uint32_t = unsigned int]'|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:176|58|instantiated from 'int32_t PolyVox::CubicSurfaceExtractor<VoxelType>::addVertex(float, float, float, uint8_t, PolyVox::Array<3u, PolyVox::IndexAndMaterial>&) [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>, int32_t = int, uint8_t = unsigned char]'|
..\Tools\include\PolyVox\CubicSurfaceExtractor.inl:87|103|instantiated from 'void PolyVox::CubicSurfaceExtractor<VoxelType>::execute() [with VoxelType = PolyVox::MaterialDensityPair<unsigned char, 7u, 1u>]'|
D:\Programmieren\Mine_ModDLL\main.cpp:245|27|instantiated from here|
..\Tools\include\PolyVox\PolyVoxImpl\SubArray.h|56|warning: 'PolyVox::SubArray<2u, PolyVox::IndexAndMaterial>::m_pElements' will be initialized after|
..\Tools\include\PolyVox\PolyVoxImpl\SubArray.h|53|warning:   'uint32_t* PolyVox::SubArray<2u, PolyVox::IndexAndMaterial>::m_pDimensions'|
..\Tools\include\PolyVox\PolyVoxImpl\SubArray.inl|47|warning:   when initialized here|


Author:  David Williams [ Wed Nov 03, 2010 10:15 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

Great, I'm glad it works for you. I was worried the lack of normals was going to cause problems for people but apparently it is ok.

Regarding the warning... I know have a lot to fix :-) If you can create a patch (if you are using TortoiseSVN then you just right click on the folder and choose 'create patch') then I can apply this to my code, otherwise I will just try to fix them when I get a chance.

Author:  AndiNo [ Thu Nov 04, 2010 6:43 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

Actually there were only about 7 warnings, I just copied the whole error-log here. The patch file is attached. Just see if it works, I have never used this before :)

Attachments:
warnings.zip [2.88 KiB]
Downloaded 292 times

Author:  David Williams [ Thu Nov 04, 2010 11:27 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

Don't worry, you did great. I've applied the patch with a couple of changes (e.g. uint32_t rather than unsigned int). If you update you might get conflicts (as we've edited the same parts of the code) so just revert your local changes if there is a problem.

Also, I've added new classes 'Density8' and 'Maaterial8'. You should be able to use the Material8 class to get the full 8 bits of material that you wanted. It's not well tested yet though :-)

Author:  AndiNo [ Mon Nov 08, 2010 9:32 am ]
Post subject:  Re: Improved CubicSurfaceExtractor

That sounds good! Thanks for the Material8 class, too. Funnily enough I found out that I probably won't be using more than 20 or so materials in my game, as I intend to handle some things differently than Minecraft does. Thanks anyway :)

Author:  David Williams [ Mon Nov 08, 2010 11:26 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

AndiNo wrote:
...I found out that I probably won't be using more than 20 or so materials in my game, as I intend to handle some things differently than Minecraft does. Thanks anyway :)


Ok, then keep in mind that in the future you could implement your own voxel class (as long as it matches the interface of Material8/Density8/etc) which only uses a smaller number of bits for the material. You could then use the spare bits for any other propertes you might want ('voxel health/strength', for example).

Author:  AndiNo [ Sun Jan 23, 2011 1:16 am ]
Post subject:  Re: Improved CubicSurfaceExtractor

Time for bumping ;)
David Williams wrote:
You could then use the spare bits for any other propertes you might want

First: I actually do this now :)

The main reason for bumping is a bit offtopic but relates to the newly calculated normals. I still use the ColouredCubicVoxel shader for my voxels. Everything is working fine, but now I'm trying to implement lighting, too (I guess everyone here already knows that ;) ). I just wanted to ask if anybody knows if I can use the calculated normals for per-pixel lighting. I followed this tutorial: http://www.ogre3d.org/tikiwiki/White+Phong+part+1+-+JaJDoo+Shader+Guide+-+Basics&structure=Cookbook The tutorial uses the normals passed in to the vertex shader, my calculation is obviously done in the fragment shader.
It seems to work with directional light, but testing with a point light was a bit strange. Will investigate further tomorrow.

Author:  David Williams [ Sun Jan 23, 2011 5:49 pm ]
Post subject:  Re: Improved CubicSurfaceExtractor

You should be able to use the normals just fine. I did play with point lights in Thermite3D at one time, but I don't have the support in there at the moment so there's no code I can give. If you want to verify/debug your normals, a good trick is to make sure it is normalised and write the x/y/z values into the r/g/b components of the output colour. If you want to see negative components you'll have to do some adjustment.

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/