Created class Chunk with a corresponding rendering method

This commit is contained in:
Erik Broberg
2016-05-10 20:00:15 -04:00
parent e6d6c15874
commit 5cade91a37
9 changed files with 262 additions and 146 deletions

View File

@@ -42,12 +42,9 @@ namespace openspace {
int ChunkNode::instanceCount = 0;
int ChunkNode::renderedPatches = 0;
ChunkNode::ChunkNode(ChunkLodGlobe& owner, const ChunkIndex& index, ChunkNode* parent)
: _owner(owner)
, _index(index)
, _patch(index)
ChunkNode::ChunkNode(const Chunk& chunk, ChunkNode* parent)
: _chunk(chunk)
, _parent(parent)
, _isVisible(true)
{
_children[0] = nullptr;
_children[1] = nullptr;
@@ -79,26 +76,19 @@ void ChunkNode::render(const RenderData& data) {
// Returns true or false wether this node can be merge or not
bool ChunkNode::internalUpdateChunkTree(const RenderData& data) {
using namespace glm;
Geodetic2 center = _patch.center();
//Geodetic2 center = _chunk.surfacePatch.center();
//LDEBUG("x: " << patch.x << " y: " << patch.y << " level: " << patch.level << " lat: " << center.lat << " lon: " << center.lon);
if (isLeaf()) {
int desiredLevel = calculateDesiredLevelAndUpdateIsVisible(data, _index);
desiredLevel = glm::clamp(desiredLevel, _owner.minSplitDepth, _owner.maxSplitDepth);
if (desiredLevel > _index.level) {
Chunk::Status status = _chunk.update(data);
if (status == Chunk::WANT_SPLIT) {
split();
}
else if(desiredLevel < _index.level){
return true; // request a merge from parent
}
return false;
return status == Chunk::WANT_MERGE;
}
else {
int requestedMergeMask = 0;
char requestedMergeMask = 0;
for (int i = 0; i < 4; ++i) {
if (_children[i]->internalUpdateChunkTree(data)) {
requestedMergeMask |= (1 << i);
@@ -119,10 +109,11 @@ bool ChunkNode::internalUpdateChunkTree(const RenderData& data) {
void ChunkNode::internalRender(const RenderData& data) {
if (isLeaf()) {
if (_isVisible) {
LatLonPatchRenderer& patchRenderer = _owner.getPatchRenderer();
patchRenderer.renderPatch(_patch, data, _owner.ellipsoid(), _index);
if (_chunk.isVisible()) {
ChunkRenderer& patchRenderer = _chunk.owner()->getPatchRenderer();
patchRenderer.renderChunk(_chunk, _chunk.owner()->ellipsoid(), data);
//patchRenderer.renderPatch(_chunk.surfacePatch, data, _chunk.owner->ellipsoid(), _chunk.index);
ChunkNode::renderedPatches++;
}
}
@@ -133,90 +124,16 @@ void ChunkNode::internalRender(const RenderData& data) {
}
}
int ChunkNode::calculateDesiredLevelAndUpdateIsVisible(
const RenderData& data,
const ChunkIndex& traverseData) {
_isVisible = true;
Vec3 globePosition = data.position.dvec3();
Vec3 patchPosition =
globePosition +
_owner.ellipsoid().geodetic2ToCartesian(_patch.center());
Vec3 cameraPosition = data.camera.position().dvec3();
//Vec3 cameraDirection = Vec3(data.camera.viewDirection());
Vec3 cameraToChunk = patchPosition - cameraPosition;
Scalar minimumGlobeRadius = _owner.ellipsoid().minimumRadius();
/*
// if camera points at same direction as latlon patch normal,
// we see the back side and dont have to split it
//Scalar cosNormalCameraDirection = glm::dot(patchNormal, cameraDirection);
Vec3 globeToCamera = cameraPosition - globePosition;
Geodetic2 cameraPositionOnGlobe =
_owner.ellipsoid().cartesianToGeodetic2(globeToCamera);
Geodetic2 closestPatchPoint = _patch.closestPoint(cameraPositionOnGlobe);
Vec3 normalOfClosestPatchPoint =
_owner.ellipsoid().geodeticSurfaceNormal(closestPatchPoint);
Scalar cosPatchNormalNormalizedGlobeToCamera =
glm::dot(normalOfClosestPatchPoint, glm::normalize(globeToCamera));
//LDEBUG(cosPatchNormalCameraDirection);
// Get the minimum radius from the ellipsoid. The closer the ellipsoid is to a
// sphere, the better this will make the splitting. Using the minimum radius to
// be safe. This means that if the ellipsoid has high difference between radii,
// splitting might accur even though it is not needed.
Scalar minimumGlobeRadius = _owner.ellipsoid().minimumRadius();
double cosAngleToHorizon = minimumGlobeRadius / glm::length(globeToCamera);
if (cosPatchNormalNormalizedGlobeToCamera < cosAngleToHorizon) {
_isVisible = false;
return traverseData.level - 1;
}
*/
if (!HorizonCuller::isVisible(
data,
_patch,
_owner.ellipsoid(),
8700))
{
_isVisible = false;
return traverseData.level - 1;
}
// Do frustrum culling
//FrustumCuller& culler = _owner.getFrustumCuller();
if (!FrustumCuller::isVisible(data, _patch, _owner.ellipsoid())) {
_isVisible = false;
return traverseData.level - 1;
}
// Calculate desired level based on distance
Scalar distance = glm::length(cameraToChunk);
_owner.minDistToCamera = fmin(_owner.minDistToCamera, distance);
Scalar scaleFactor = 10 * minimumGlobeRadius;
Scalar projectedScaleFactor = scaleFactor / distance;
int desiredLevel = floor( log2(projectedScaleFactor) );
return desiredLevel;
}
void ChunkNode::split(int depth) {
if (depth > 0 && isLeaf()) {
auto childIndices = _index.childIndices();
auto childIndices = _chunk.index().childIndices();
for (size_t i = 0; i < childIndices.size(); i++) {
_children[i] = std::unique_ptr<ChunkNode>(
new ChunkNode(_owner, childIndices[i], this));
new ChunkNode(Chunk(_chunk.owner(), childIndices[i]), this));
}
}