Some cleanup of RenderableFov

This commit is contained in:
Alexander Bock
2016-07-31 21:43:55 +02:00
parent 484ef636c7
commit 3fb6d3aa84
2 changed files with 71 additions and 72 deletions
+51 -53
View File
@@ -36,6 +36,8 @@
#include <ghoul/io/texture/texturereader.h>
#include <ghoul/opengl/textureunit.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
#include <openspace/query/query.h>
#include <glm/gtx/projection.hpp>
@@ -49,7 +51,7 @@
namespace {
const std::string _loggerCat = "RenderableFov";
//constants
const std::string keyBody = "Body";
const std::string keyFrame = "Frame";
const std::string keyPathModule = "ModulePath";
@@ -61,8 +63,6 @@ namespace {
const int InterpolationSteps = 10;
const int Stride = 8;
}
namespace openspace {
@@ -133,10 +133,6 @@ void RenderableFov::allocateData() {
}
}
RenderableFov::~RenderableFov() {
deinitialize();
}
bool RenderableFov::initialize() {
bool completeSuccess = true;
if (_programObject == nullptr) {
@@ -209,11 +205,12 @@ void RenderableFov::updateGPU() {
PerfMeasure("updateGPU");
glBindBuffer(GL_ARRAY_BUFFER, _fovBoundsVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, _vBoundsSize * sizeof(GLfloat), _fovBounds.data());
if (!_rebuild){
if (!_rebuild) {
// no new points
glBindBuffer(GL_ARRAY_BUFFER, _fovPlaneVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, _vPlaneSize * sizeof(GLfloat), _fovPlane.data());
}else{
}
else {
// new points - memory change
glBindVertexArray(_fovPlaneVAO);
glBindBuffer(GL_ARRAY_BUFFER, _fovPlaneVBO);
@@ -313,16 +310,16 @@ glm::dvec3 RenderableFov::bisection(glm::dvec3 p1, glm::dvec3 p2) {
ivec = result.surfaceVector;
bool intercepted = result.interceptFound;
if (glm::distance(_previousHalf, half) < Tolerance){
if (glm::distance(_previousHalf, half) < Tolerance) {
_previousHalf = glm::dvec3(0);
return half;
}
_previousHalf = half;
//recursive search
if (!intercepted){
if (!intercepted) {
return bisection(p1, half);
}
else{
else {
return bisection(half, p2);
}
}
@@ -330,35 +327,35 @@ glm::dvec3 RenderableFov::bisection(glm::dvec3 p1, glm::dvec3 p2) {
void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds) {
_nrInserted = 0;
_fovPlane.clear();// empty the array
_fovPlane.clear(); // empty the array
glm::dvec3 mid;
glm::dvec3 interpolated;
glm::dvec3 current;
glm::dvec3 next;
glm::vec4 tmp(1);
if (bounds.size() > 1){
for (int i = 0; i < bounds.size(); i++){
if (bounds.size() > 1) {
for (int i = 0; i < bounds.size(); ++i) {
int k = (i + 1 > bounds.size() - 1) ? 0 : i + 1;
current = bounds[i];
next = bounds[k];
if (H[i] == false){ // If point is non-interceptive, project it.
if (H[i] == false) { // If point is non-interceptive, project it.
insertPoint(_fovPlane, orthogonalProjection(current).vec4(), tmp);
if (H[i + 1] == false && _withinFOV){
if (H[i + 1] == false && _withinFOV) {
// IFF incident point is also non-interceptive BUT something is within FOV
// we need then to check if this segment makes contact with surface
glm::dvec3 half = interpolate(current, next, 0.5f);
std::string bodyfixed = "IAU_";
bool convert = (_frame.find(bodyfixed) == std::string::npos);
if (convert)
if (convert) {
bodyfixed = SpiceManager::ref().frameFromBody(_fovTarget);
else
}
else {
bodyfixed = _frame;
}
SpiceManager::SurfaceInterceptResult res =
SpiceManager::ref().surfaceIntercept(_fovTarget, _spacecraft,
@@ -372,14 +369,13 @@ void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds
ivec = res.surfaceVector;
bool intercepted = res.interceptFound;
if (intercepted){
if (intercepted) {
// find the two outer most points of intersection
glm::dvec3 root1 = bisection(half, current);
glm::dvec3 root2 = bisection(half, next);
insertPoint(_fovPlane, orthogonalProjection(root1).vec4(), col_sq);
for (int j = 1; j < InterpolationSteps; j++){
for (int j = 1; j < InterpolationSteps; ++j) {
float t = (static_cast<float>(j) / InterpolationSteps);
interpolated = interpolate(root1, root2, t);
_interceptVector = checkForIntercept(interpolated);
@@ -389,10 +385,10 @@ void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds
}
}
}
if (H[i] == true && H[i + 1] == false){ // current point is interceptive, next is not
if (H[i] == true && H[i + 1] == false) { // current point is interceptive, next is not
// find outer most point for interpolation
mid = bisection(current, next);
for (int j = 1; j <= InterpolationSteps; j++){
for (int j = 1; j <= InterpolationSteps; ++j) {
float t = (static_cast<float>(j) / InterpolationSteps);
interpolated = interpolate(current, mid, t);
_interceptVector = (j < InterpolationSteps) ? checkForIntercept(interpolated) : orthogonalProjection(interpolated);
@@ -401,7 +397,7 @@ void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds
}
if (H[i] == false && H[i + 1] == true){ // current point is non-interceptive, next is
mid = bisection(next, current);
for (int j = 1; j <= InterpolationSteps; j++){
for (int j = 1; j <= InterpolationSteps; ++j) {
float t = (static_cast<float>(j) / InterpolationSteps);
interpolated = interpolate(mid, next, t);
_interceptVector = (j > 1) ? checkForIntercept(interpolated) : orthogonalProjection(interpolated);
@@ -409,7 +405,7 @@ void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds
}
}
if (H[i] == true && H[i + 1] == true){ // both points intercept
for (int j = 0; j <= InterpolationSteps; j++){
for (int j = 0; j <= InterpolationSteps; ++j) {
float t = (static_cast<float>(j) / InterpolationSteps);
interpolated = interpolate(current, next, t);
_interceptVector = checkForIntercept(interpolated);
@@ -418,7 +414,7 @@ void RenderableFov::fovSurfaceIntercept(bool H[], std::vector<glm::dvec3> bounds
}
}
}
if (_nrInserted == 0){
if (_nrInserted == 0) {
_rebuild = false;
}
else {
@@ -438,7 +434,8 @@ void RenderableFov::computeColors() {
if (diff <= interpolationStart)
t = static_cast<float>(1.0 - (diff / interpolationStart));
if (diff < 0.0) t = 0.f;
if (diff < 0.0)
t = 0.f;
// This is a bit hardcoded - either we go for color tables
// or make these properties.
@@ -464,7 +461,7 @@ void RenderableFov::computeColors() {
void RenderableFov::determineTarget() {
PerfMeasure("determineTarget");
_fovTarget = _potentialTargets[0]; //default;
for (int i = 0; i < _potentialTargets.size(); i++) {
for (int i = 0; i < _potentialTargets.size(); ++i) {
_withinFOV = openspace::SpiceManager::ref().isTargetInFieldOfView(
_potentialTargets[i],
_spacecraft,
@@ -484,14 +481,16 @@ void RenderableFov::computeIntercepts(const RenderData& data) {
PerfMeasure("computeIntercepts");
// for each FOV vector
_fovBounds.clear();
for (int i = 0; i <= _bounds.size(); i++){
for (int i = 0; i <= _bounds.size(); ++i) {
int r = (i == _bounds.size()) ? 0 : i;
std::string bodyfixed = "IAU_";
bool convert = (_frame.find(bodyfixed) == std::string::npos);
if (convert)
if (convert) {
bodyfixed = SpiceManager::ref().frameFromBody(_fovTarget);
else
}
else {
bodyfixed = _frame;
}
SpiceManager::SurfaceInterceptResult res =
SpiceManager::ref().surfaceIntercept(_fovTarget, _spacecraft,
@@ -506,24 +505,25 @@ void RenderableFov::computeIntercepts(const RenderData& data) {
_interceptTag[r] = res.interceptFound;
// if not found, use the orthogonal projected point
if (!_interceptTag[r])
if (!_interceptTag[r]) {
_projectionBounds[r] = orthogonalProjection(_bounds[r]);
}
glm::vec4 fovOrigin = glm::vec4(0); //This will have to be fixed once spacecraft is 1:1!
if (_interceptTag[r]){
if (_interceptTag[r]) {
_interceptVector = PowerScaledCoordinate::CreatePowerScaledCoordinate(ivec[0], ivec[1], ivec[2]);
_interceptVector[3] += 3;
// INTERCEPTIONS
insertPoint(_fovBounds, fovOrigin, col_start);
insertPoint(_fovBounds, _interceptVector.vec4(), col_end);
}
else if (_withinFOV){
else if (_withinFOV) {
// OBJECT IN FOV, NO INTERCEPT FOR THIS FOV-RAY
insertPoint(_fovBounds, fovOrigin, glm::vec4(0, 0, 1, 1));
insertPoint(_fovBounds, _projectionBounds[r].vec4(), col_blue);
}
else{
else {
glm::vec4 corner(_bounds[r][0], _bounds[r][1], _bounds[r][2], data.position[3] + 2);
corner = _spacecraftRotation*corner;
// NONE OF THE FOV-RAYS INTERCEPT AND NO OBJECT IN FOV
@@ -537,12 +537,14 @@ void RenderableFov::computeIntercepts(const RenderData& data) {
glm::vec3 aim = (_spacecraftRotation * glm::vec4(_boresight, 1)).xyz();
double lt;
glm::dvec3 position =
SpiceManager::ref().targetPosition(_fovTarget,
_spacecraft,
_frame,
_aberrationCorrection,
_time,
lt);
SpiceManager::ref().targetPosition(
_fovTarget,
_spacecraft,
_frame,
_aberrationCorrection,
_time,
lt
);
psc p = PowerScaledCoordinate::CreatePowerScaledCoordinate(position.x, position.y, position.z);
pss length = p.length();
if (length[0] < DBL_EPSILON) {
@@ -550,7 +552,7 @@ void RenderableFov::computeIntercepts(const RenderData& data) {
return;
}
//if aimed 80 deg away from target, dont draw white square
if (glm::dot(glm::normalize(aim), glm::normalize(p.vec3())) < 0.2){
if (glm::dot(glm::normalize(aim), glm::normalize(p.vec3())) < 0.2) {
_drawFOV = false;
}
}
@@ -565,8 +567,9 @@ void RenderableFov::render(const RenderData& data) {
_programObject->setUniform("ModelTransform", glm::mat4(1));
setPscUniforms(*_programObject.get(), data.camera, data.position);
if (openspace::ImageSequencer::ref().isReady())
if (openspace::ImageSequencer::ref().isReady()) {
_drawFOV = ImageSequencer::ref().instrumentActive(_instrumentID);
}
if (_drawFOV) {
// update only when time progresses.
@@ -585,7 +588,7 @@ void RenderableFov::render(const RenderData& data) {
glDrawArrays(_mode, 0, static_cast<int>(_vBoundsSize / Stride));
glBindVertexArray(0);
if (_drawFOV){
if (_drawFOV) {
glLineWidth(2.f);
glBindVertexArray(_fovPlaneVAO);
glDrawArrays(GL_LINE_LOOP, 0, static_cast<int>(_vPlaneSize / Stride));
@@ -599,12 +602,7 @@ void RenderableFov::render(const RenderData& data) {
void RenderableFov::update(const UpdateData& data) {
_time = data.time;
_stateMatrix = SpiceManager::ref().positionTransformMatrix(_instrumentID, _frame, data.time);
_spacecraftRotation = glm::mat4(1);
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
_spacecraftRotation[i][j] = static_cast<float>(_stateMatrix[i][j]);
}
}
_spacecraftRotation = glm::mat4(_stateMatrix);
}
} // namespace openspace