History

First commit of 'Death Star' voxel planet

Philip Rosedale committed 13 years ago

Commit 67115cc1 parent 5f60bf61 file · +32 −18Browse files

Showing the changes to interface/src/VoxelSystem.cpp (1 of 1 file). Show all

Unified · Split

interface/src/VoxelSystem.cpp+32−18
@@ -8,6 +8,12 @@ · 7 unmodified lines
88
99 #include "VoxelSystem.h"
1010
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+
1117 void VoxelSystem::init() {
1218 root = new Voxel;
1319 }
@@ -21,42 +27,48 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) { · 7 unmodified lines
2127 int newVoxels = 0;
2228 if (voxel == NULL) voxel = root;
2329 averageColor[0] = averageColor[1] = averageColor[2] = 0.0;
30+
31+ const float RADIUS = 3.9;
32+
2433 //
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
2635 //
27− if ((randFloat() < 0.1) && (scale < 1.0))
36+ if (onSphereShell(RADIUS, scale, position) && (scale < 0.25) && (randFloat() < 0.01))
2837 {
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;
3041 for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
3142 return 0;
3243 } else {
3344 // Decide whether to make kids, recurse into them
3445 for (unsigned char i = 0; i < NUM_CHILDREN; i++) {
35− if ((scale > 0.01) && (randFloat() > 0.6))
36− {
37− // Make a new child
38− voxel->children[i] = new Voxel;
39− newVoxels++;
40− childrenCreated++;
46+ if (scale > 0.01) {
4147 glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0,
4248 scale/2.0*((i&2)>>1)-scale/4.0,
4349 scale/2.0*(i&1)-scale/4.0);
4450 *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;
4659 *position -= shift;
47− averageColor += voxel->children[i]->color;
4860 } else {
4961 // No child made: Set pointer to null, nothing to see here.
5062 voxel->children[i] = NULL;
5163 }
5264 }
5365 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
5567 averageColor *= 1.0/childrenCreated;
5668 voxel->color = averageColor;
5769 return newVoxels;
5870 } 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
6072 voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
6173 for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
6274 return 0;
@@ -65,7 +77,12 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) { · 2 unmodified lines
6577 }
6678 }
6779
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;
6986
7087 //
7188 // 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
83100 scale/2.0*(i&1)-scale/4.0);
84101 if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) {
85102 glTranslatef(shift.x, shift.y, shift.z);
86− //std::cout << "x,y,z: " << shift.x << "," << shift.y << "," << shift.z << "\n";
87103 *distance += shift;
88104 vRendered += render(voxel->children[i], scale/2.0, distance);
89105 *distance -= shift;
@@ -94,10 +110,8 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) { · 4 unmodified lines
94110 // Render this voxel if the children were not rendered
95111 if (!renderedChildren)
96112 {
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
98114 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);
101115 glutSolidCube(scale);
102116 vRendered++;
103117 }