From 94473d6b95b98f0b6b9e5f8fc5f358b1b0c12015 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 6 May 2013 21:07:10 -0700 Subject: [PATCH 1/4] made TREE_SCALE 100 --- libraries/voxels/src/VoxelConstants.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/voxels/src/VoxelConstants.h b/libraries/voxels/src/VoxelConstants.h index 91562a29d6..41007a47c7 100644 --- a/libraries/voxels/src/VoxelConstants.h +++ b/libraries/voxels/src/VoxelConstants.h @@ -14,11 +14,11 @@ #include -const int NUMBER_OF_CHILDREN = 8; +const int TREE_SCALE = 100; +const int NUMBER_OF_CHILDREN = 8; const int MAX_VOXEL_PACKET_SIZE = 1492; const int MAX_TREE_SLICE_BYTES = 26; -const int TREE_SCALE = 10; const int MAX_VOXELS_PER_SYSTEM = 250000; const int VERTICES_PER_VOXEL = 24; const int VERTEX_POINTS_PER_VOXEL = 3 * VERTICES_PER_VOXEL; From d11e490b62f92d9bbecbe0e8486a91f9113adbd2 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 6 May 2013 21:07:42 -0700 Subject: [PATCH 2/4] cleaned up code in createSphere() --- libraries/voxels/src/VoxelTree.cpp | 84 ++++++++++++++++-------------- libraries/voxels/src/VoxelTree.h | 2 +- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 8f1f072fe5..49e1f7596c 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -456,28 +456,30 @@ void VoxelTree::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, r } } -void VoxelTree::createSphere(float r,float xc, float yc, float zc, float s, bool solid, bool wantColorRandomizer) { +void VoxelTree::createSphere(float radius, float xc, float yc, float zc, float voxelSize, + bool solid, bool wantColorRandomizer, bool debug) { + // About the color of the sphere... we're going to make this sphere be a gradient // between two RGB colors. We will do the gradient along the phi spectrum - unsigned char dominantColor1 = randIntInRange(1,3); //1=r, 2=g, 3=b dominant - unsigned char dominantColor2 = randIntInRange(1,3); + unsigned char dominantColor1 = randIntInRange(1, 3); //1=r, 2=g, 3=b dominant + unsigned char dominantColor2 = randIntInRange(1, 3); - if (dominantColor1==dominantColor2) { - dominantColor2 = dominantColor1+1%3; + if (dominantColor1 == dominantColor2) { + dominantColor2 = dominantColor1 + 1 % 3; } - unsigned char r1 = (dominantColor1==1)?randIntInRange(200,255):randIntInRange(40,100); - unsigned char g1 = (dominantColor1==2)?randIntInRange(200,255):randIntInRange(40,100); - unsigned char b1 = (dominantColor1==3)?randIntInRange(200,255):randIntInRange(40,100); - unsigned char r2 = (dominantColor2==1)?randIntInRange(200,255):randIntInRange(40,100); - unsigned char g2 = (dominantColor2==2)?randIntInRange(200,255):randIntInRange(40,100); - unsigned char b2 = (dominantColor2==3)?randIntInRange(200,255):randIntInRange(40,100); + unsigned char r1 = (dominantColor1 == 1) ? randIntInRange(200, 255) : randIntInRange(40, 100); + unsigned char g1 = (dominantColor1 == 2) ? randIntInRange(200, 255) : randIntInRange(40, 100); + unsigned char b1 = (dominantColor1 == 3) ? randIntInRange(200, 255) : randIntInRange(40, 100); + unsigned char r2 = (dominantColor2 == 1) ? randIntInRange(200, 255) : randIntInRange(40, 100); + unsigned char g2 = (dominantColor2 == 2) ? randIntInRange(200, 255) : randIntInRange(40, 100); + unsigned char b2 = (dominantColor2 == 3) ? randIntInRange(200, 255) : randIntInRange(40, 100); // We initialize our rgb to be either "grey" in case of randomized surface, or // the average of the gradient, in the case of the gradient sphere. - unsigned char red = wantColorRandomizer ? 128 : (r1+r2)/2; // average of the colors - unsigned char green = wantColorRandomizer ? 128 : (g1+g2)/2; - unsigned char blue = wantColorRandomizer ? 128 : (b1+b2)/2; + unsigned char red = wantColorRandomizer ? 128 : (r1 + r2) / 2; // average of the colors + unsigned char green = wantColorRandomizer ? 128 : (g1 + g2) / 2; + unsigned char blue = wantColorRandomizer ? 128 : (b1 + b2) / 2; // Psuedocode for creating a sphere: // @@ -486,50 +488,54 @@ void VoxelTree::createSphere(float r,float xc, float yc, float zc, float s, bool // x = xc+r*cos(theta)*sin(phi) // y = yc+r*sin(theta)*sin(phi) // z = zc+r*cos(phi) - - int t=0; // total points - - // We want to make sure that as we "sweep" through our angles we use a delta angle that's small enough to not skip any - // voxels we can calculate theta from our desired arc length - // lenArc = ndeg/360deg * 2pi*R ---> lenArc = theta/2pi * 2pi*R - // lenArc = theta*R ---> theta = lenArc/R ---> theta = g/r - float angleDelta = (s/r); // assume solid for now - float ri = 0.0; + float thisRadius = 0.0; + float thisVoxelSize = radius / 4.0f; if (!solid) { - ri=r; // just the outer surface + thisRadius = radius; // just the outer surface + thisVoxelSize = voxelSize; } // If you also iterate form the interior of the sphere to the radius, makeing - // larger and larger sphere's you'd end up with a solid sphere. And lots of voxels! - for (; ri <= (r+(s/2.0)); ri+=s) { - //printLog("radius: ri=%f ri+s=%f (r+(s/2.0))=%f\n",ri,ri+s,(r+(s/2.0))); - for (float theta=0.0; theta <= 2*M_PI; theta += angleDelta) { + // larger and larger sphere'voxelSize you'd end up with a solid sphere. And lots of voxels! + while (thisRadius <= (radius + (voxelSize / 2.0))) { + if (debug) { + printLog("radius: thisRadius=%f thisVoxelSize=%f thisRadius+thisVoxelSize=%f (radius+(voxelSize/2.0))=%f\n", + thisRadius, thisVoxelSize, thisRadius+thisVoxelSize, (radius + (voxelSize / 2.0))); + } + // We want to make sure that as we "sweep" through our angles we use a delta angle that voxelSize + // small enough to not skip any voxels we can calculate theta from our desired arc length + // lenArc = ndeg/360deg * 2pi*R ---> lenArc = theta/2pi * 2pi*R + // lenArc = theta*R ---> theta = lenArc/R ---> theta = g/r + float angleDelta = (thisVoxelSize / thisRadius); + + for (float theta=0.0; theta <= 2 * M_PI; theta += angleDelta) { for (float phi=0.0; phi <= M_PI; phi += angleDelta) { - t++; // total voxels - float x = xc+ri*cos(theta)*sin(phi); - float y = yc+ri*sin(theta)*sin(phi); - float z = zc+ri*cos(phi); + float x = xc + thisRadius * cos(theta) * sin(phi); + float y = yc + thisRadius * sin(theta) * sin(phi); + float z = zc + thisRadius * cos(phi); // gradient color data - float gradient = (phi/M_PI); + float gradient = (phi / M_PI); // only use our actual desired color on the outer edge, otherwise // use our "average" color - if (ri+(s*2.0)>=r) { - //printLog("painting candy shell radius: ri=%f r=%f\n",ri,r); - red = wantColorRandomizer ? randomColorValue(165) : r1+((r2-r1)*gradient); - green = wantColorRandomizer ? randomColorValue(165) : g1+((g2-g1)*gradient); - blue = wantColorRandomizer ? randomColorValue(165) : b1+((b2-b1)*gradient); + if (thisRadius + (voxelSize * 2.0) >= radius) { + //printLog("painting candy shell radius: thisRadius=%f radius=%f\n",thisRadius,radius); + red = wantColorRandomizer ? randomColorValue(165) : r1 + ((r2 - r1) * gradient); + green = wantColorRandomizer ? randomColorValue(165) : g1 + ((g2 - g1) * gradient); + blue = wantColorRandomizer ? randomColorValue(165) : b1 + ((b2 - b1) * gradient); } - unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue); + unsigned char* voxelData = pointToVoxel(x, y, z, thisVoxelSize, red, green, blue); this->readCodeColorBufferToTree(voxelData); delete voxelData; } } + thisRadius += thisVoxelSize; + thisVoxelSize = std::max(voxelSize, thisVoxelSize / 2.0f); } this->reaverageVoxelColors(this->rootNode); } diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 1f2ec44e48..421e14ace7 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -49,7 +49,7 @@ public: VoxelNode* getVoxelAt(float x, float y, float z, float s) const; void createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue); void createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color); - void createSphere(float r,float xc, float yc, float zc, float s, bool solid, bool wantColorRandomizer); + void createSphere(float r,float xc, float yc, float zc, float s, bool solid, bool wantColorRandomizer, bool debug = false); void recurseTreeWithOperation(RecurseVoxelTreeOperation operation, void* extraData=NULL); From e05b74f25218819495c1c41cb28ada601496f88d Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 6 May 2013 21:08:06 -0700 Subject: [PATCH 3/4] cleaned up code in addScene() --- voxel-edit/src/main.cpp | 43 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/voxel-edit/src/main.cpp b/voxel-edit/src/main.cpp index 7592de00f4..f6b3ea23d2 100644 --- a/voxel-edit/src/main.cpp +++ b/voxel-edit/src/main.cpp @@ -22,7 +22,8 @@ bool countVoxelsOperation(VoxelNode* node, void* extraData) { void addScene(VoxelTree * tree) { printf("adding scene...\n"); - float voxelSize = 1.f/32; + // We want our corner voxels to be about 1/2 meter high, and our TREE_SCALE is in meters, so... + float voxelSize = 0.5f / TREE_SCALE; // Here's an example of how to create a voxel. printf("creating corner points...\n"); @@ -50,8 +51,6 @@ void addScene(VoxelTree * tree) { // Now some more examples... a little more complex printf("creating corner points...\n"); tree->createVoxel(0 , 0 , 0 , voxelSize, 255, 255 ,255); - - tree->createVoxel(1.0 - voxelSize, 0 , 0 , voxelSize, 255, 0 ,0 ); tree->createVoxel(0 , 1.0 - voxelSize, 0 , voxelSize, 0 , 255 ,0 ); tree->createVoxel(0 , 0 , 1.0 - voxelSize, voxelSize, 0 , 0 ,255); @@ -63,31 +62,32 @@ void addScene(VoxelTree * tree) { // Now some more examples... creating some lines using the line primitive printf("creating voxel lines...\n"); - float lineVoxelSize = 0.99f/256; - rgbColor red = {255,0,0}; - rgbColor green = {0,255,0}; - rgbColor blue = {0,0,255}; + // We want our corner voxels to be about 1/8 meter high, and our TREE_SCALE is in meters, so... + float lineVoxelSize = 1.f / (32 * TREE_SCALE); + rgbColor red = {255, 0, 0}; + rgbColor green = {0, 255, 0}; + rgbColor blue = {0, 0, 255}; tree->createLine(glm::vec3(0, 0, 0), glm::vec3(0, 0, 1), lineVoxelSize, blue); tree->createLine(glm::vec3(0, 0, 0), glm::vec3(1, 0, 0), lineVoxelSize, red); tree->createLine(glm::vec3(0, 0, 0), glm::vec3(0, 1, 0), lineVoxelSize, green); printf("DONE creating lines...\n"); // Now some more examples... creating some spheres using the sphere primitive - int sphereBaseSize = 512; - printf("creating spheres...\n"); - tree->createSphere(0.25, 0.5, 0.5, 0.5, (1.0 / sphereBaseSize), true, false); - printf("one sphere added...\n"); - tree->createSphere(0.030625, 0.5, 0.5, (0.25-0.06125), (1.0 / (sphereBaseSize * 2)), true, true); + // We want the smallest unit of our spheres to be about 1/16th of a meter tall + float sphereVoxelSize = 1.f / (16 * TREE_SCALE); + printf("creating spheres... sphereVoxelSize=%f\n",sphereVoxelSize); + tree->createSphere(0.25, 0.5, 0.5, 0.5, sphereVoxelSize, true, false, true); + printf("one sphere added... sphereVoxelSize=%f\n",sphereVoxelSize); - - printf("two spheres added...\n"); - tree->createSphere(0.030625, (0.75 - 0.030625), (0.75 - 0.030625), (0.75 - 0.06125), (1.0 / (sphereBaseSize * 2)), true, true); - printf("three spheres added...\n"); - tree->createSphere(0.030625, (0.75 - 0.030625), (0.75 - 0.030625), 0.06125, (1.0 / (sphereBaseSize * 2)), true, true); - printf("four spheres added...\n"); - tree->createSphere(0.030625, (0.75 - 0.030625), 0.06125, (0.75 - 0.06125), (1.0 / (sphereBaseSize * 2)), true, true); - printf("five spheres added...\n"); - tree->createSphere(0.06125, 0.125, 0.125, (0.75 - 0.125), (1.0 / (sphereBaseSize * 2)), true, true); + tree->createSphere(0.030625, 0.5, 0.5, (0.25 - 0.06125), sphereVoxelSize, true, true); + printf("two spheres added... sphereVoxelSize=%f\n",sphereVoxelSize); + tree->createSphere(0.030625, (0.75 - 0.030625), (0.75 - 0.030625), (0.75 - 0.06125), sphereVoxelSize, true, true); + printf("three spheres added... sphereVoxelSize=%f\n",sphereVoxelSize); + tree->createSphere(0.030625, (0.75 - 0.030625), (0.75 - 0.030625), 0.06125, sphereVoxelSize, true, true); + printf("four spheres added... sphereVoxelSize=%f\n",sphereVoxelSize); + tree->createSphere(0.030625, (0.75 - 0.030625), 0.06125, (0.75 - 0.06125), sphereVoxelSize, true, true); + printf("five spheres added... sphereVoxelSize=%f\n",sphereVoxelSize); + tree->createSphere(0.06125, 0.125, 0.125, (0.75 - 0.125), sphereVoxelSize, true, true); float radius = 0.0125f; printf("6 spheres added...\n"); @@ -102,7 +102,6 @@ void addScene(VoxelTree * tree) { tree->createSphere(radius, 0.025, radius * 5.0f, 0.25, (1.0 / 4096), true, true); printf("11 spheres added...\n"); printf("DONE creating spheres...\n"); - // Here's an example of how to recurse the tree and do some operation on the nodes as you recurse them. // This one is really simple, it just couts them... // Look at the function countVoxelsOperation() for an example of how you could use this function From f45bb8c455e270830af4383e2533f64c255b7111 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 6 May 2013 21:30:47 -0700 Subject: [PATCH 4/4] fix comment --- voxel-edit/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voxel-edit/src/main.cpp b/voxel-edit/src/main.cpp index f6b3ea23d2..5a474f15ee 100644 --- a/voxel-edit/src/main.cpp +++ b/voxel-edit/src/main.cpp @@ -62,7 +62,7 @@ void addScene(VoxelTree * tree) { // Now some more examples... creating some lines using the line primitive printf("creating voxel lines...\n"); - // We want our corner voxels to be about 1/8 meter high, and our TREE_SCALE is in meters, so... + // We want our line voxels to be about 1/32 meter high, and our TREE_SCALE is in meters, so... float lineVoxelSize = 1.f / (32 * TREE_SCALE); rgbColor red = {255, 0, 0}; rgbColor green = {0, 255, 0};