Showing the changes to interface/src/VoxelSystem.cpp (1 of 1 file). Show all
Unified · Split
| @@ -8,6 +8,12 @@ · 7 unmodified lines | |||
| 8 | 8 | ||
| 9 | #include "VoxelSystem.h" | 9 | #include "VoxelSystem.h" |
| 10 | 10 | ||
| 11 | |||
| 12 | bool onSphereShell(float radius, float scale, glm::vec3 * position) { | ||
| 13 | float vRadius = glm::length(*position); | ||
| 14 | return ((vRadius + scale/2.0 > radius) && (vRadius - scale/2.0 < radius)); | ||
| 15 | } | ||
| 16 | |||
| 11 | void VoxelSystem::init() { | 17 | void VoxelSystem::init() { |
| 12 | root = new Voxel; | 18 | root = new Voxel; |
| 13 | } | 19 | } |
| @@ -21,42 +27,48 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) { · 7 unmodified lines | |||
| 21 | int newVoxels = 0; | 27 | int newVoxels = 0; |
| 22 | if (voxel == NULL) voxel = root; | 28 | if (voxel == NULL) voxel = root; |
| 23 | averageColor[0] = averageColor[1] = averageColor[2] = 0.0; | 29 | averageColor[0] = averageColor[1] = averageColor[2] = 0.0; |
| 30 | |||
| 31 | const float RADIUS = 3.9; | ||
| 32 | |||
| 24 | // | 33 | // |
| 25 | // First, decide whether I should be a leaf node and set/return if so | 34 | // First, randomly decide whether to stop here without recursing for children |
| 26 | // | 35 | // |
| 27 | if ((randFloat() < 0.1) && (scale < 1.0)) | 36 | if (onSphereShell(RADIUS, scale, position) && (scale < 0.25) && (randFloat() < 0.01)) |
| 28 | { | 37 | { |
| 29 | voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5; | 38 | voxel->color.x = 0.1; |
| 39 | voxel->color.y = 0.5 + randFloat()*0.5; | ||
| 40 | voxel->color.z = 0.1; | ||
| 30 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL; | 41 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL; |
| 31 | return 0; | 42 | return 0; |
| 32 | } else { | 43 | } else { |
| 33 | // Decide whether to make kids, recurse into them | 44 | // Decide whether to make kids, recurse into them |
| 34 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) { | 45 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) { |
| 35 | if ((scale > 0.01) && (randFloat() > 0.6)) | 46 | if (scale > 0.01) { |
| 36 | { | ||
| 37 | // Make a new child | ||
| 38 | voxel->children[i] = new Voxel; | ||
| 39 | newVoxels++; | ||
| 40 | childrenCreated++; | ||
| 41 | glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0, | 47 | glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0, |
| 42 | scale/2.0*((i&2)>>1)-scale/4.0, | 48 | scale/2.0*((i&2)>>1)-scale/4.0, |
| 43 | scale/2.0*(i&1)-scale/4.0); | 49 | scale/2.0*(i&1)-scale/4.0); |
| 44 | *position += shift; | 50 | *position += shift; |
| 45 | newVoxels += initVoxels(voxel->children[i], scale/2.0, position); | 51 | // Test to see whether the child is also on edge of sphere |
| 52 | if (onSphereShell(RADIUS, scale/2.0, position)) { | ||
| 53 | voxel->children[i] = new Voxel; | ||
| 54 | newVoxels++; | ||
| 55 | childrenCreated++; | ||
| 56 | newVoxels += initVoxels(voxel->children[i], scale/2.0, position); | ||
| 57 | averageColor += voxel->children[i]->color; | ||
| 58 | } else voxel->children[i] = NULL; | ||
| 46 | *position -= shift; | 59 | *position -= shift; |
| 47 | averageColor += voxel->children[i]->color; | ||
| 48 | } else { | 60 | } else { |
| 49 | // No child made: Set pointer to null, nothing to see here. | 61 | // No child made: Set pointer to null, nothing to see here. |
| 50 | voxel->children[i] = NULL; | 62 | voxel->children[i] = NULL; |
| 51 | } | 63 | } |
| 52 | } | 64 | } |
| 53 | if (childrenCreated > 0) { | 65 | if (childrenCreated > 0) { |
| 54 | // If there were children created, set this voxels color to the average of it's children | 66 | // If there were children created, the color of this voxel node is average of children |
| 55 | averageColor *= 1.0/childrenCreated; | 67 | averageColor *= 1.0/childrenCreated; |
| 56 | voxel->color = averageColor; | 68 | voxel->color = averageColor; |
| 57 | return newVoxels; | 69 | return newVoxels; |
| 58 | } else { | 70 | } else { |
| 59 | // Tested and didn't make any children, so i've still got to be a leaf | 71 | // Tested and didn't make any children, so choose my color as a leaf, return |
| 60 | voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5; | 72 | voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5; |
| 61 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL; | 73 | for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL; |
| 62 | return 0; | 74 | return 0; |
| @@ -65,7 +77,12 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) { · 2 unmodified lines | |||
| 65 | } | 77 | } |
| 66 | } | 78 | } |
| 67 | 79 | ||
| 68 | const float RENDER_DISCARD = 0.01; | 80 | // |
| 81 | // The Render Discard is the ratio of the size of the voxel to the distance from the camera | ||
| 82 | // at which the voxel will no longer be shown. Smaller = show more detail. | ||
| 83 | // | ||
| 84 | |||
| 85 | const float RENDER_DISCARD = 0.04; //0.01; | ||
| 69 | 86 | ||
| 70 | // | 87 | // |
| 71 | // Returns the total number of voxels actually rendered | 88 | // Returns the total number of voxels actually rendered |
| @@ -83,7 +100,6 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 11 unmodified lines | |||
| 83 | scale/2.0*(i&1)-scale/4.0); | 100 | scale/2.0*(i&1)-scale/4.0); |
| 84 | if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) { | 101 | if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) { |
| 85 | glTranslatef(shift.x, shift.y, shift.z); | 102 | glTranslatef(shift.x, shift.y, shift.z); |
| 86 | //std::cout << "x,y,z: " << shift.x << "," << shift.y << "," << shift.z << "\n"; | ||
| 87 | *distance += shift; | 103 | *distance += shift; |
| 88 | vRendered += render(voxel->children[i], scale/2.0, distance); | 104 | vRendered += render(voxel->children[i], scale/2.0, distance); |
| 89 | *distance -= shift; | 105 | *distance -= shift; |
| @@ -94,10 +110,8 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 4 unmodified lines | |||
| 94 | // Render this voxel if the children were not rendered | 110 | // Render this voxel if the children were not rendered |
| 95 | if (!renderedChildren) | 111 | if (!renderedChildren) |
| 96 | { | 112 | { |
| 97 | //glColor4f(1,1,1,1); | 113 | // This is the place where we need to copy this data to a VBO to make this FAST |
| 98 | glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 1.0); | 114 | glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 1.0); |
| 99 | //float bright = 1.0 - glm::length(*distance)/20.0; | ||
| 100 | //glColor3f(bright,bright,bright); | ||
| 101 | glutSolidCube(scale); | 115 | glutSolidCube(scale); |
| 102 | vRendered++; | 116 | vRendered++; |
| 103 | } | 117 | } |