mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 21:43:03 +02:00
First commit of 'Death Star' voxel planet
This commit is contained in:
parent
5f60bf6376
commit
67115ccad4
1 changed files with 32 additions and 18 deletions
|
@ -8,6 +8,12 @@
|
||||||
|
|
||||||
#include "VoxelSystem.h"
|
#include "VoxelSystem.h"
|
||||||
|
|
||||||
|
|
||||||
|
bool onSphereShell(float radius, float scale, glm::vec3 * position) {
|
||||||
|
float vRadius = glm::length(*position);
|
||||||
|
return ((vRadius + scale/2.0 > radius) && (vRadius - scale/2.0 < radius));
|
||||||
|
}
|
||||||
|
|
||||||
void VoxelSystem::init() {
|
void VoxelSystem::init() {
|
||||||
root = new Voxel;
|
root = new Voxel;
|
||||||
}
|
}
|
||||||
|
@ -21,42 +27,48 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) {
|
||||||
int newVoxels = 0;
|
int newVoxels = 0;
|
||||||
if (voxel == NULL) voxel = root;
|
if (voxel == NULL) voxel = root;
|
||||||
averageColor[0] = averageColor[1] = averageColor[2] = 0.0;
|
averageColor[0] = averageColor[1] = averageColor[2] = 0.0;
|
||||||
|
|
||||||
|
const float RADIUS = 3.9;
|
||||||
|
|
||||||
//
|
//
|
||||||
// First, decide whether I should be a leaf node and set/return if so
|
// First, randomly decide whether to stop here without recursing for children
|
||||||
//
|
//
|
||||||
if ((randFloat() < 0.1) && (scale < 1.0))
|
if (onSphereShell(RADIUS, scale, position) && (scale < 0.25) && (randFloat() < 0.01))
|
||||||
{
|
{
|
||||||
voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
|
voxel->color.x = 0.1;
|
||||||
|
voxel->color.y = 0.5 + randFloat()*0.5;
|
||||||
|
voxel->color.z = 0.1;
|
||||||
for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
|
for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
// Decide whether to make kids, recurse into them
|
// Decide whether to make kids, recurse into them
|
||||||
for (unsigned char i = 0; i < NUM_CHILDREN; i++) {
|
for (unsigned char i = 0; i < NUM_CHILDREN; i++) {
|
||||||
if ((scale > 0.01) && (randFloat() > 0.6))
|
if (scale > 0.01) {
|
||||||
{
|
|
||||||
// Make a new child
|
|
||||||
voxel->children[i] = new Voxel;
|
|
||||||
newVoxels++;
|
|
||||||
childrenCreated++;
|
|
||||||
glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0,
|
glm::vec3 shift(scale/2.0*((i&4)>>2)-scale/4.0,
|
||||||
scale/2.0*((i&2)>>1)-scale/4.0,
|
scale/2.0*((i&2)>>1)-scale/4.0,
|
||||||
scale/2.0*(i&1)-scale/4.0);
|
scale/2.0*(i&1)-scale/4.0);
|
||||||
*position += shift;
|
*position += shift;
|
||||||
newVoxels += initVoxels(voxel->children[i], scale/2.0, position);
|
// Test to see whether the child is also on edge of sphere
|
||||||
|
if (onSphereShell(RADIUS, scale/2.0, position)) {
|
||||||
|
voxel->children[i] = new Voxel;
|
||||||
|
newVoxels++;
|
||||||
|
childrenCreated++;
|
||||||
|
newVoxels += initVoxels(voxel->children[i], scale/2.0, position);
|
||||||
|
averageColor += voxel->children[i]->color;
|
||||||
|
} else voxel->children[i] = NULL;
|
||||||
*position -= shift;
|
*position -= shift;
|
||||||
averageColor += voxel->children[i]->color;
|
|
||||||
} else {
|
} else {
|
||||||
// No child made: Set pointer to null, nothing to see here.
|
// No child made: Set pointer to null, nothing to see here.
|
||||||
voxel->children[i] = NULL;
|
voxel->children[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (childrenCreated > 0) {
|
if (childrenCreated > 0) {
|
||||||
// If there were children created, set this voxels color to the average of it's children
|
// If there were children created, the color of this voxel node is average of children
|
||||||
averageColor *= 1.0/childrenCreated;
|
averageColor *= 1.0/childrenCreated;
|
||||||
voxel->color = averageColor;
|
voxel->color = averageColor;
|
||||||
return newVoxels;
|
return newVoxels;
|
||||||
} else {
|
} else {
|
||||||
// Tested and didn't make any children, so i've still got to be a leaf
|
// Tested and didn't make any children, so choose my color as a leaf, return
|
||||||
voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
|
voxel->color.x = voxel->color.y = voxel->color.z = 0.5 + randFloat()*0.5;
|
||||||
for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
|
for (unsigned char i = 0; i < NUM_CHILDREN; i++) voxel->children[i] = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -65,7 +77,12 @@ int VoxelSystem::initVoxels(Voxel * voxel, float scale, glm::vec3 * position) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const float RENDER_DISCARD = 0.01;
|
//
|
||||||
|
// The Render Discard is the ratio of the size of the voxel to the distance from the camera
|
||||||
|
// at which the voxel will no longer be shown. Smaller = show more detail.
|
||||||
|
//
|
||||||
|
|
||||||
|
const float RENDER_DISCARD = 0.04; //0.01;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Returns the total number of voxels actually rendered
|
// Returns the total number of voxels actually rendered
|
||||||
|
@ -83,7 +100,6 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
|
||||||
scale/2.0*(i&1)-scale/4.0);
|
scale/2.0*(i&1)-scale/4.0);
|
||||||
if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) {
|
if ((voxel->children[i] != NULL) && (scale / glm::length(*distance) > RENDER_DISCARD)) {
|
||||||
glTranslatef(shift.x, shift.y, shift.z);
|
glTranslatef(shift.x, shift.y, shift.z);
|
||||||
//std::cout << "x,y,z: " << shift.x << "," << shift.y << "," << shift.z << "\n";
|
|
||||||
*distance += shift;
|
*distance += shift;
|
||||||
vRendered += render(voxel->children[i], scale/2.0, distance);
|
vRendered += render(voxel->children[i], scale/2.0, distance);
|
||||||
*distance -= shift;
|
*distance -= shift;
|
||||||
|
@ -94,10 +110,8 @@ int VoxelSystem::render(Voxel * voxel, float scale, glm::vec3 * distance) {
|
||||||
// Render this voxel if the children were not rendered
|
// Render this voxel if the children were not rendered
|
||||||
if (!renderedChildren)
|
if (!renderedChildren)
|
||||||
{
|
{
|
||||||
//glColor4f(1,1,1,1);
|
// This is the place where we need to copy this data to a VBO to make this FAST
|
||||||
glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 1.0);
|
glColor4f(voxel->color.x, voxel->color.y, voxel->color.z, 1.0);
|
||||||
//float bright = 1.0 - glm::length(*distance)/20.0;
|
|
||||||
//glColor3f(bright,bright,bright);
|
|
||||||
glutSolidCube(scale);
|
glutSolidCube(scale);
|
||||||
vRendered++;
|
vRendered++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue