Reverted some changes which were just for testing the new normal encoding.

This commit is contained in:
David Williams 2014-07-24 22:21:53 +02:00
parent f9ee5a10b4
commit 2b7ef5b966
3 changed files with 50 additions and 75 deletions

View File

@ -50,34 +50,14 @@ void createSphereInVolume(SimpleVolume<uint8_t>& volData, float fRadius)
//And compute how far the current position is from the center of the volume //And compute how far the current position is from the center of the volume
float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length(); float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
// We actually want our volume to have high values in the center and low values as we move out, because our uint8_t uVoxelValue = 0;
// eath should be a solid sphere surrounded by empty space. If we invert the distance then this is a step in
// the right direction. We still have zero in the center, but lower (negative) values as we move out.
float density = -fDistToCenter;
// By adding the 'planetRadius' we now have a function which starts at 'planetRadius' and still decreases as it //If the current voxel is less than 'radius' units from the center then we make it solid.
// moves out. The function passes through zero at a distance of 'planetRadius' and then continues do decrease if(fDistToCenter <= fRadius)
// as it gets even further out. {
density += fRadius; //Our new voxel value
uVoxelValue = 255;
// Ideally we would like our final density value to be '255' for voxels inside the planet and '0' for voxels }
// outside the planet. At the surface there should be a transition but this should occur not too quickly and
// not too slowly, as both of these will result in a jagged appearance to the mesh.
//
// We probably want the transition to occur over a few voxels, whereas it currently occurs over 255 voxels
// because it was derived from the distance. By scaling the density field we effectivly compress the rate
// at which it changes at the surface. We also make the center much too high and the outside very low, but
// we will clamp these to the corect range later.
//
// Note: You can try commenting out or changing the value on this line to see the effect it has.
density *= 50;
// Until now we've been defining our density field as if the threshold was at zero, with positive densities
// being solid and negative densities being empty. But actually Cubiquity operates on the range 0 to 255, and
// uses a threashold of 127 to decide where to place the generated surface. Therefore we shift and clamp our
// density value and store it in a byte.
density += 127;
uint8_t uVoxelValue = (uint8_t)(clamp(density, 0.0f, 255.0f));
//Wrte the voxel value into the volume //Wrte the voxel value into the volume
volData.setVoxelAt(x, y, z, uVoxelValue); volData.setVoxelAt(x, y, z, uVoxelValue);
@ -94,12 +74,12 @@ int main(int argc, char *argv[])
openGLWidget.show(); openGLWidget.show();
//Create an empty volume and then place a sphere in it //Create an empty volume and then place a sphere in it
SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(31, 31, 31))); SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)));
createSphereInVolume(volData, 15); createSphereInVolume(volData, 30);
// Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see. // Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see.
//auto mesh = extractCubicMesh(&volData, volData.getEnclosingRegion()); auto mesh = extractCubicMesh(&volData, volData.getEnclosingRegion());
auto mesh = extractMarchingCubesMesh(&volData, volData.getEnclosingRegion()); //auto mesh = extractMarchingCubesMesh(&volData, volData.getEnclosingRegion());
// The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to // The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to
// decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code. // decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code.

View File

@ -2,7 +2,7 @@
// Passed in from the vertex shader // Passed in from the vertex shader
in vec4 worldPosition; in vec4 worldPosition;
in vec3 worldNormal; in vec4 worldNormal;
// the color that gets written to the display // the color that gets written to the display
out vec4 outputColor; out vec4 outputColor;
@ -15,5 +15,5 @@ void main()
// We are just using the normal as the output color, and making it lighter so it looks a bit nicer. // We are just using the normal as the output color, and making it lighter so it looks a bit nicer.
// Obviously a real shader would also do texuring, lighting, or whatever is required for the application. // Obviously a real shader would also do texuring, lighting, or whatever is required for the application.
outputColor = vec4(abs(worldNormal.xyz), 1.0); outputColor = vec4(abs(normal) * 0.5 + vec3(0.5, 0.5, 0.5), 1.0);
} }

View File

@ -1,7 +1,6 @@
#version 140 #version 140
in vec4 position; // This will be the position of the vertex in model-space in vec4 position; // This will be the position of the vertex in model-space
in vec3 normal;
// The usual matrices are provided // The usual matrices are provided
uniform mat4 cameraToClipMatrix; uniform mat4 cameraToClipMatrix;
@ -11,15 +10,11 @@ uniform mat4 modelToWorldMatrix;
// This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach // This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach
// but we use it in this example framework because not all surface extractor generate surface normals. // but we use it in this example framework because not all surface extractor generate surface normals.
out vec4 worldPosition; out vec4 worldPosition;
out vec3 worldNormal;
void main() void main()
{ {
// Standard sequence of OpenGL transformations. // Standard sequence of OpenGL transformations.
worldPosition = modelToWorldMatrix * position; worldPosition = modelToWorldMatrix * position;
vec4 cameraPosition = worldToCameraMatrix * worldPosition; vec4 cameraPosition = worldToCameraMatrix * worldPosition;
worldNormal = normal;
gl_Position = cameraToClipMatrix * cameraPosition; gl_Position = cameraToClipMatrix * cameraPosition;
} }