History

remove deadhead (of self) at origin. First commit of voxels with LOD, recursive test pattern.

Philip Rosedale committed 13 years ago

Commit 15fd9631 parent fd151fb4 files · +107 −38Browse files

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

Unified · Split

interface/src/VoxelSystem.cpp+73−29
@@ -15,47 +15,91 @@ void VoxelSystem::init() { · 14 unmodified lines
15//15//
16// Recursively initialize the voxel tree16// Recursively initialize the voxel tree
17//17//
18int VoxelSystem::initVoxels(Voxel * voxel, float scale) {18int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) {
19 float childColor[3], averageColor[3];19 glm::vec3 averageColor(0,0,0);
20 int averageCount = 0;20 int childrenCreated = 0;
21 int newVoxels = 0;21 int newVoxels = 0;
22 if (voxel == NULL) voxel = root;22 if (voxel == NULL) voxel = root;
23 averageColor[0] = averageColor[1] = averageColor[2] = 0.0;23 averageColor[0] = averageColor[1] = averageColor[2] = 0.0;
24 for (unsigned char i = 0; i < NUM_CHILDREN; i++) {24 //
25 if ((scale > 0.01) && (randFloat() < 0.5)) {25 // First, decide whether I should be a leaf node and set/return if so
26 voxel->children[i] = new Voxel;26 //
27 newVoxels += initVoxels(voxel->children[i], scale/2.0);27 if ((randFloat() < 0.1) && (scale < 1.0))
28 for (int j = 0; j < 3; j++) averageColor[j] += childColor[j];28 {
29 averageCount++;29 voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
30 for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
31 return 0;
32 } else {
33 // Decide whether to make kids, recurse into them
34 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++;
41 glm::vec3 shift(scale/4.0*((i&4)>>2), scale/4.0*((i&2)>>1), scale/4.0*(i&1));
42 *position += shift;
43 newVoxels += initVoxels(voxel->children[i], scale/2.0, position);
44 *position -= shift;
45 averageColor += voxel->children[i]->color;
46 } else {
47 // No child made: Set pointer to null, nothing to see here.
48 voxel->children[i] = NULL;
49 }
30 }50 }
31 else {51 if (childrenCreated > 0) {
32 voxel->children[i] = NULL;52 // If there were children created, set this voxels color to the average of it's children
53 averageColor *= 1.0/childrenCreated;
54 voxel->color = averageColor;
55 return newVoxels;
56 } else {
57 // Tested and didn't make any children, so i've still got to be a leaf
58 voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
59 for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
60 return 0;
61
33 }62 }
34 }63 }
35 if (averageCount == 0) {
36 // This is a leaf, so just pick a random color
37 voxel->color.x = voxel->color.y = voxel->color.z = randFloat();
38 } else {
39 voxel->color.x = averageColor[0]/averageCount;
40 voxel->color.y = averageColor[1]/averageCount;
41 voxel->color.z = averageColor[2]/averageCount;
42 }
43 newVoxels++;
44 return newVoxels;
45}64}
46 65
47void VoxelSystem::render(Voxel * voxel, float scale) {66const float RENDER_DISCARD = 0.01;
48 if (voxel == NULL) voxel = root;67
68//
69// Returns the total number of voxels actually rendered
70//
71int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
72 // If null passed in, start at root
73 if (voxel == NULL) voxel = root;
49 unsigned char i;74 unsigned char i;
75 bool renderedChildren = false;
76 int vRendered = 0;
77 // Recursively render children
50 for (i = 0; i < NUM_CHILDREN; i++) {78 for (i = 0; i < NUM_CHILDREN; i++) {
51 if (voxel->children[i] != NULL) {79 glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0,
52 glTranslatef(scale/2.0*((i&4)>>2), scale/2.0*((i&2)>>1), scale/2.0*(i&1));80 scale/2.0*((i&2)>>1)-scale/4.0,
53 render(voxel->children[i], scale/2.0);81 scale/2.0*(i&1)-scale/4.0);
54 glTranslatef(-scale/2.0*((i&4)>>2), -scale/2.0*((i&2)>>1), -scale/2.0*(i&1));82 if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) {
83 glTranslatef(shift.x, shift.y, shift.z);
84 //std::cout << "x,y,z: " << shift.x << "," << shift.y << "," << shift.z << "\n";
85 *distance += shift;
86 vRendered += render(voxel->children[i], scale/2.0, distance);
87 *distance -= shift;
88 glTranslatef(-shift.x, -shift.y, -shift.z);
89 renderedChildren = true;
55 }90 }
56 }91 }
57 glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 0.5);92 // Render this voxel if the children were not rendered
58 glutSolidCube(scale);93 if (!renderedChildren)
94 {
95 //glColor4f(1,1,1,1);
96 glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 1.0);
97 //float bright = 1.0 - glm::length(*distance)/20.0;
98 //glColor3f(bright,bright,bright);
99 glutSolidCube(scale);
100 vRendered++;
101 }
102 return vRendered;
59}103}
60 104
61void VoxelSystem::simulate(float deltaTime) {105void VoxelSystem::simulate(float deltaTime) {