semi-working fov.

This commit is contained in:
michal
2014-11-03 22:35:04 -05:00
parent 79b75aa572
commit f0b6b17554
3 changed files with 303 additions and 96 deletions
+24 -3
View File
@@ -55,12 +55,33 @@ public:
void allocateData();
double computeTargetLocalTime(PowerScaledScalar d);
psc orthogonalProjection(glm::dvec3 camvec);
int findIndx(unsigned int p1, unsigned int p2) const;
void insertBetween(int p1, int p2);
void hasIntercept(bool tag[]);
void printFovArray();
double distanceBetweenPoints(psc p1, psc p2);
double distanceBetweenPoints(glm::dvec3 p1, glm::dvec3 p2);
psc findSegmentIntercept(glm::dvec3 p1, glm::dvec3 p2, double tolerance);
glm::dvec3 _previousHalf;
void insertPoint(psc& p, glm::vec4& c);
int _nrInserted = 0;
void rebuildFOV(bool H[], std::vector<glm::dvec3> bounds);
int findIndx(unsigned int p1, unsigned int p2) const;
void insertPoint(int index, psc point); // extend later for multiple points
void getIntervalEndpoints(psc& p1, psc& p2, int index);
//void findSegmentSplit(bool tag[]);
psc pscInterpolate(psc p0, psc p1, float t);
glm::dvec3 interpolate(glm::dvec3 p0, glm::dvec3 p1, float t);
// spice
std::string _target;
+2 -2
View File
@@ -1,10 +1,10 @@
--openspace.setPropertyValue('Earth.renderable.colorTexture', '${OPENSPACE_DATA}/modules/mars/textures/mars.png')
openspace.time.setTime("2007-02-26T17:30:41")
openspace.time.setTime("2007-02-26T17:34:49")
--openspace.time.setTime("2006-08-22T20:00:00")
--openspace.time.setDeltaTime(200000.0)
--openspace.time.setDeltaTime(84600.00)
--openspace.time.setDeltaTime(3600)
openspace.time.setDeltaTime(100.0)
openspace.time.setDeltaTime(10.0)
-- print(openspace.time.currentTimeUTC())
+277 -91
View File
@@ -69,7 +69,6 @@ void RenderableFov::allocateData(){
for (int i = 0; i < points; i++){
for (int j = 0; j < 4; j++){
_varray1.push_back(0); // pos
}
for (int j = 0; j < 4; j++){
_varray1.push_back(0); // col
@@ -89,83 +88,36 @@ void RenderableFov::allocateData(){
_iarray2[i] = i;
}
_varray2.resize(40);
_vsize2 = 40;
_vsize2 = 40;
_vtotal2 = 5;
/*
// TESTING INDEX ROTATION ALGORITHM
for (int i = 0; i < 40; i++){
_varray2[i] = i;
}
// 0 1 2 3 4
// 0 1 2 3 4 (point nr)
// 0 1 2 3 | 4 5 6 7 | 8 9 10 11 | 12 13 14 15 | 16 17 18 19 | 20 21 22 23 | 24 25 26 27 | 28 29 30 31 | 32 33 34 35 | 36 37 38 39 |
// 8 16 24 32
insertBetween(1, 2);
// 8 16 24 32 (point index)
*/
}
psc RenderableFov::pscInterpolate(psc p0, psc p1, float t){
assert(t >= 0 && t <= 1);
float s = (1.f - t)*p0[3] + t*p1[3];
float x = ((1.f - t)*p0[0] + t*p1[0]);
float y = ((1.f - t)*p0[1] + t*p1[1]);
float z = ((1.f - t)*p0[2] + t*p1[2]);
return PowerScaledCoordinate::PowerScaledCoordinate(x,y,z,s);
}
void RenderableFov::hasIntercept(bool tag[]){
for (int i = 0; i < 4; i++){
if (tag[i] != tag[i + 1]){
std::cout << "intercept between pt " << i << " and " << i + 1 << std::endl;
}
}
std::cout << std::endl;
}
void RenderableFov::insertBetween(int p1, int p2){
int idx = findIndx(p1, p2);
std::rotate(_varray2.begin(), _varray2.begin() + idx, _varray2.end());
std::reverse(_varray2.begin(), _varray2.end());
for (int i = 0; i < 8; i++)
_varray2.push_back(0); // add new point
std::rotate(_varray2.begin(), _varray2.begin() + idx, _varray2.end());
std::reverse(_varray2.begin(), _varray2.end());
/*
int tmp = 0;
for (int i = 0; i < _varray2.size(); i++){
std::cout << _varray2[i] << " ";
tmp++;
if (tmp == 4){
std::cout << "| ";
tmp = 0;
}
}
std::cout << "\n";
*/
}
int RenderableFov::findIndx(unsigned int p1, unsigned int p2) const{
assert(p1 != p2 && _stride > 0);
int idx = (p1 > p2) ? p1*_stride : p2*_stride;
if (idx > _varray2.size()-_stride){
LERROR("Out of bounds for points " << p1 << " and " << p2
<< " will return index 0");
return 0;
}
return idx;
}
RenderableFov::~RenderableFov(){
deinitialize();
}
bool RenderableFov::initialize(){
bool completeSuccess = true;
if (_programObject == nullptr)
completeSuccess &= OsEng.ref().configurationManager().getValue("EphemerisProgram", _programObject);
allocateData();
sendToGPU();
return completeSuccess;
}
bool RenderableFov::deinitialize(){
delete _texture;
_texture = nullptr;
return true;
}
void RenderableFov::sendToGPU(){
// Initialize and upload to graphics card
glGenVertexArrays(1, &_vaoID1);
@@ -209,23 +161,256 @@ void RenderableFov::sendToGPU(){
}
bool RenderableFov::initialize(){
bool completeSuccess = true;
if (_programObject == nullptr)
completeSuccess &= OsEng.ref().configurationManager().getValue("EphemerisProgram", _programObject);
/*
void RenderableFov::findSegmentSplit(bool tag[]){
psc start, end, newPoint;
for (int i = 4; i > 0; i--){
if (tag[i] != tag[i - 1]){
int index = findIndx(i, i - 1);
getIntervalEndpoints(start, end, index);
newPoint = pscInterpolate(start, end, 0.5f); // point inbetween
insertPoint(index, newPoint);
}
}
}
*/
/*
void RenderableFov::getIntervalEndpoints(psc& p1, psc& p2, int index){
int secondIndex = index-_stride;
for (int i = 0; i < 4; i++){
p2[i] = _varray2[index];
p1[i] = _varray2[secondIndex];
index++;
secondIndex++;
}
}*/
/*
int RenderableFov::findIndx(unsigned int p1, unsigned int p2) const{
assert(p1 != p2 && _stride > 0);
int idx = (p1 > p2) ? p1*_stride : p2*_stride;
if (idx > _varray2.size()-_stride){
LERROR("Out of bounds for points " << p1 << " and " << p2
<< " will return index 0");
return 0;
}
return idx;
}
*/
void RenderableFov::insertPoint(int index, psc point){
std::rotate(_varray2.begin(), _varray2.begin() + index, _varray2.end());
std::reverse(_varray2.begin(), _varray2.end());
_varray2.push_back(1);
_varray2.push_back(0);
_varray2.push_back(0);
_varray2.push_back(1);
allocateData();
sendToGPU();
for (int i = 0; i < 4; i++){
_varray2.push_back(point[3-i]); // add new point
}
std::rotate(_varray2.begin(), _varray2.begin() + index, _varray2.end());
std::reverse(_varray2.begin(), _varray2.end());
return completeSuccess;
_vtotal2 += 1;
_isize2 += 1;
_vsize2 = _varray2.size();
//update size etc;
for (int i = 0; i < _isize2; i++){
_iarray2[i] = i;
}
glGenVertexArrays(1, &_vaoID2);
glGenBuffers(1, &_vboID2);
glGenBuffers(1, &_iboID2);
glBindVertexArray(_vaoID2);
glBindBuffer(GL_ARRAY_BUFFER, _vboID2);
glBufferData(GL_ARRAY_BUFFER, _vsize2 * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // orphaning the buffer, sending NULL data.
glBufferSubData(GL_ARRAY_BUFFER, 0, _vsize2 * sizeof(GLfloat), &_varray2[0]);
GLsizei st = sizeof(GLfloat) * _stride;
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, st, (void*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, st, (void*)(4 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboID2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _isize2 * sizeof(int), _iarray2, GL_STATIC_DRAW);
glBindVertexArray(0);
}
bool RenderableFov::deinitialize(){
delete _texture;
_texture = nullptr;
return true;
void RenderableFov::printFovArray(){
int tmp = 0;
for (int i = 0; i < _varray2.size(); i++){
std::cout << _varray2[i] << " ";
tmp++;
if (tmp == 4){
std::cout << std::endl;
tmp = 0;
}
}
std::cout << "\n";
}
void RenderableFov::insertPoint(psc& p, glm::vec4& c){
for (int i = 0; i < 4; i++){
_varray2.push_back(p[i]);
}
for (int i = 0; i < 4; i++){
_varray2.push_back(c[i]);
}
_nrInserted++;
}
double RenderableFov::distanceBetweenPoints(psc p1, psc p2){
PowerScaledScalar dist = (p1 - p2).length();
return dist[0] * pow(10, dist[1]);
}
double RenderableFov::distanceBetweenPoints(glm::dvec3 p1, glm::dvec3 p2){
glm::dvec3 tmp = p1 - p2;
return sqrt(tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2]);
}
psc RenderableFov::pscInterpolate(psc p0, psc p1, float t){
assert(t >= 0 && t <= 1);
float s = (1.f - t)*p0[3] + t*p1[3];
float x = ((1.f - t)*p0[0] + t*p1[0]);
float y = ((1.f - t)*p0[1] + t*p1[1]);
float z = ((1.f - t)*p0[2] + t*p1[2]);
return PowerScaledCoordinate::PowerScaledCoordinate(x, y, z, s);
}
glm::dvec3 RenderableFov::interpolate(glm::dvec3 p0, glm::dvec3 p1, float t){
assert(t >= 0 && t <= 1);
float x = ((1.f - t)*p0[0] + t*p1[0]);
float y = ((1.f - t)*p0[1] + t*p1[1]);
float z = ((1.f - t)*p0[2] + t*p1[2]);
return glm::dvec3(x, y, z);
}
psc RenderableFov::findSegmentIntercept(glm::dvec3 p1, glm::dvec3 p2, double tolerance){
glm::dvec3 half = interpolate(p1, p2, 0.5f);
//check if point is on surface
glm::dvec3 h(half[0], half[1], half[2]);
double targetEt;
glm::dvec3 ip, iv;
bool intercepted = openspace::SpiceManager::ref().getSurfaceIntercept("JUPITER", "NEW HORIZONS", "NH_LORRI",
"GALACTIC", "ELLIPSOID", "NONE", _time, targetEt, half, ip, iv);
if (distanceBetweenPoints(_previousHalf, half) < tolerance){
_previousHalf = glm::dvec3(0);
return orthogonalProjection(half);
}
_previousHalf = half;
//recursive search
if (intercepted == false){
return findSegmentIntercept(p1, half, tolerance);
}else{
return findSegmentIntercept(half, p2, tolerance);
}
}
/*
[3]---------[4]/[0]
| |
| FOV |
| |
[2]-----------[1]
CASE 1:
H[i] H[i+1]
[X]---------x...........[O]
| / :
| / :
| / :
x / :
/: :
/ : :
[O].....................[O]
*/
void RenderableFov::rebuildFOV(bool H[], std::vector<glm::dvec3> bounds){
_nrInserted = 0;
_varray2.clear();// empty the array
int isteps = 4;
for (int i = 0; i < 4; i++){
psc current = orthogonalProjection(bounds[i]);
insertPoint(current, glm::vec4(1));
int k = (i + 1 > 3) ? 0 : i + 1;
psc next = orthogonalProjection(bounds[k]);
// CASE 1
if (H[i] == true && H[i + 1] == false){
psc mid = findSegmentIntercept(bounds[i], bounds[k], 0.00001); // very low tolerance factor, might be precision issue
for (int j = 1; j < isteps; j++){
psc interpolated = pscInterpolate(current, mid, (double)j / isteps);
insertPoint(interpolated, glm::vec4(1));
}
}
if (H[i] == false && H[i+1] == true){
psc mid = findSegmentIntercept(bounds[k], bounds[i], 0.0000001);
for (int j = 1; j < isteps; j++){
psc interpolated = pscInterpolate(mid, next, (double)j / isteps);
insertPoint(interpolated, glm::vec4(1));
}
}
// just add remaining points for now...
if (H[i] == true && H[i + 1] == true){ // CASE 3
for (int j = 1; j < isteps; j++){
psc interpolated = pscInterpolate(current, next, (double)j / isteps);
insertPoint(interpolated, glm::vec4(1));
}
}
}
//last point add
psc last = orthogonalProjection(bounds[0]);
insertPoint(last, glm::vec4(1));
_vtotal2 = _nrInserted;
_isize2 = _nrInserted;
_vsize2 = _varray2.size();
//update size etc;
_iarray2 = new int[_isize2];
for (int i = 0; i < _isize2; i++){
_iarray2[i] = i;
}
//optimize later
glGenVertexArrays(1, &_vaoID2);
glGenBuffers(1, &_vboID2);
glGenBuffers(1, &_iboID2);
glBindVertexArray(_vaoID2);
glBindBuffer(GL_ARRAY_BUFFER, _vboID2);
glBufferData(GL_ARRAY_BUFFER, _vsize2 * sizeof(GLfloat), NULL, GL_STREAM_DRAW); // orphaning the buffer, sending NULL data.
glBufferSubData(GL_ARRAY_BUFFER, 0, _vsize2 * sizeof(GLfloat), &_varray2[0]);
GLsizei st = sizeof(GLfloat) * _stride;
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, st, (void*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, st, (void*)(4 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboID2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _isize2 * sizeof(int), _iarray2, GL_STATIC_DRAW);
glBindVertexArray(0);
}
void RenderableFov::updateData(){
glBindBuffer(GL_ARRAY_BUFFER, _vboID1);
glBufferSubData(GL_ARRAY_BUFFER, 0, _vsize * sizeof(GLfloat), &_varray1[0]);
@@ -234,15 +419,13 @@ void RenderableFov::updateData(){
glBufferSubData(GL_ARRAY_BUFFER, 0, _vsize2 * sizeof(GLfloat), &_varray2[0]);
}
psc RenderableFov::orthogonalProjection(glm::dvec3 camvec){
psc RenderableFov::orthogonalProjection(glm::dvec3 vecFov){
glm::dvec3 vecToTarget;
double lt;
SpiceManager::ref().getTargetPosition("JUPITER", "NEW HORIZONS", "GALACTIC", "XCN+S", _time, vecToTarget, lt);
glm::dvec3 b(camvec);
SpiceManager::ref().getTargetPosition("JUPITER", "NEW HORIZONS", "GALACTIC", "NONE", _time, vecToTarget, lt);
openspace::SpiceManager::ref().frameConversion(vecFov, "NH_LORRI", "GALACTIC", _time);
//might need time-offset dependant variant
openspace::SpiceManager::ref().frameConversion(b, "NH_LORRI", "GALACTIC", _time);
glm::dvec3 p = openspace::SpiceManager::ref().orthogonalProjection(vecToTarget, b);
glm::dvec3 p = openspace::SpiceManager::ref().orthogonalProjection(vecToTarget, vecFov);
psc projection = PowerScaledCoordinate::CreatePowerScaledCoordinate(p[0], p[1], p[2]);
projection[3] += 3;
@@ -338,7 +521,7 @@ void RenderableFov::render(const RenderData& data){
}
// COMPUTE ORTHOGONAL PROJECTION. -- have to make cosmetic changes to this later.
projectionBounds[i] = orthogonalProjection(bounds[i]);
//projectionBounds[i] = orthogonalProjection(bounds[i]);
/* // failed attempt at aberrated state.
openspace::SpiceManager::ref().getPositionTransformMatrix("NH_LORRI", "IAU_JUPITER", _time, targetEpoch, _stateMatrix);
@@ -350,12 +533,15 @@ void RenderableFov::render(const RenderData& data){
tags[4] = tags[0]; // 0 & 5 same point
hasIntercept(tags);
//findSegmentSplit(tags);
/*
// draworder r->g->b->y
glm::vec4 boundary_col[4] = { glm::vec4(1, 0, 0, 1), glm::vec4(0, 1, 0, 1), glm::vec4(0, 0, 1, 1), glm::vec4(1, 1, 0, 1) };
for (int i = 0; i < 4; i++){
memcpy(&_varray2[indx2], glm::value_ptr(projectionBounds[i].vec4()), size);
indx2 += 4;
memcpy(&_varray2[indx2], glm::value_ptr(col_proj), size);
memcpy(&_varray2[indx2], glm::value_ptr(boundary_col[i]), size);
indx2 += 4;
}
@@ -363,12 +549,12 @@ void RenderableFov::render(const RenderData& data){
psc lastpoint = orthogonalProjection(bounds[0]);
memcpy(&_varray2[indx2], glm::value_ptr(lastpoint.vec4()), size);
indx2 += 4;
memcpy(&_varray2[indx2], glm::value_ptr(col_proj), size);
memcpy(&_varray2[indx2], glm::value_ptr(boundary_col[0]), size);
indx2 += 4;
*/
//insert point between data
rebuildFOV(tags, bounds);
updateData();
}
_oldTime = _time;
@@ -388,7 +574,7 @@ void RenderableFov::render(const RenderData& data){
glDrawArrays(GL_LINE_STRIP, 0, _vtotal2);
glBindVertexArray(0);
glPointSize(4.f);
glPointSize(7.f);
glBindVertexArray(_vaoID2);
glDrawArrays(GL_POINTS, 0, _vtotal2);
glBindVertexArray(0);