From 1a590f83e042d575e49b9bda3d12971ea7604a95 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 00:18:37 +0100 Subject: [PATCH 01/11] Initial commit of constallation bounds --- .../stars/renderableconstallationbounds.h | 125 ++++ openspace-data | 2 +- shaders/constellationbounds_fs.glsl | 45 ++ shaders/constellationbounds_vs.glsl | 44 ++ .../stars/renderableconstellationbounds.cpp | 670 ++++++++++++++++++ src/util/factorymanager.cpp | 9 +- 6 files changed, 890 insertions(+), 5 deletions(-) create mode 100644 include/openspace/rendering/stars/renderableconstallationbounds.h create mode 100644 shaders/constellationbounds_fs.glsl create mode 100644 shaders/constellationbounds_vs.glsl create mode 100644 src/rendering/stars/renderableconstellationbounds.cpp diff --git a/include/openspace/rendering/stars/renderableconstallationbounds.h b/include/openspace/rendering/stars/renderableconstallationbounds.h new file mode 100644 index 0000000000..34f7368481 --- /dev/null +++ b/include/openspace/rendering/stars/renderableconstallationbounds.h @@ -0,0 +1,125 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#ifndef __RENDERABLECONSTELLATIONBOUNDS_H__ +#define __RENDERABLECONSTELLATIONBOUNDS_H__ + +#include + +#include + +namespace openspace { + +class RenderableConstellationBounds : public Renderable { +public: + RenderableConstellationBounds(const ghoul::Dictionary& dictionary); + + bool initialize() override; + bool deinitialize() override; + + bool isReady() const override; + + void render(const RenderData& data) override; + void update(const UpdateData& data) override; + +private: + std::string _filename; + + ghoul::opengl::ProgramObject* _program; + bool _programIsDirty; + + std::vector _data; + + GLuint _vao; + GLuint _vbo; +}; + + +} // namespace openspace + +#endif // __RENDERABLECONSTELLATIONBOUNDS_H__ + +//#ifndef __RENDERABLESTARS_H__ +//#define __RENDERABLESTARS_H__ +// +//#include +//#include +//#include +// +//#include +//#include +// +//namespace openspace { +// +//class RenderableStars : public Renderable { +//public: +// RenderableStars(const ghoul::Dictionary& dictionary); +// +// bool initialize() override; +// bool deinitialize() override; +// +// bool isReady() const override; +// +// void render(const RenderData& data) override; +// void update(const UpdateData& data) override; +// +//private: +// enum ColorOption { +// Color = 0, +// Velocity = 1, +// Speed = 2 +// }; +// +// void createDataSlice(ColorOption option); +// +// bool loadData(); +// bool readSpeckFile(); +// bool loadCachedFile(const std::string& file); +// bool saveCachedFile(const std::string& file) const; +// +// properties::StringProperty _colorTexturePath; +// ghoul::opengl::Texture* _texture; +// bool _textureIsDirty; +// +// properties::OptionProperty _colorOption; +// bool _dataIsDirty; +// +// properties::FloatProperty _spriteSize; +// +// ghoul::opengl::ProgramObject* _program; +// bool _programIsDirty; +// +// std::string _speckFile; +// +// std::vector _slicedData; +// std::vector _fullData; +// int _nValuesPerStar; +// +// GLuint _vao; +// GLuint _vbo; +//}; +// +//} // namespace openspace +// +//#endif // __RENDERABLESTARS_H__ \ No newline at end of file diff --git a/openspace-data b/openspace-data index 48daa03562..ec8fded580 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit 48daa03562e2bdc42a9088c724792ac57832ab92 +Subproject commit ec8fded58055f279da1f23091f247daa70384a62 diff --git a/shaders/constellationbounds_fs.glsl b/shaders/constellationbounds_fs.glsl new file mode 100644 index 0000000000..803e0bd79b --- /dev/null +++ b/shaders/constellationbounds_fs.glsl @@ -0,0 +1,45 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#version __CONTEXT__ + +uniform vec4 campos; +uniform vec4 objpos; +//uniform vec3 camdir; // add this for specular + +in vec4 vs_position; + +#include "ABuffer/abufferStruct.hglsl" +#include "ABuffer/abufferAddToBuffer.hglsl" +#include "PowerScaling/powerScaling_fs.hglsl" + +//#include "PowerScaling/powerScaling_vs.hglsl" +void main() +{ + vec4 position = vs_position; + float depth = pscDepth(position); + + ABufferStruct_t frag = createGeometryFragment(vec4(1.0, 0.0, 0.0, 1.0), position, depth); + addToBuffer(frag); +} \ No newline at end of file diff --git a/shaders/constellationbounds_vs.glsl b/shaders/constellationbounds_vs.glsl new file mode 100644 index 0000000000..d6e1d9d99f --- /dev/null +++ b/shaders/constellationbounds_vs.glsl @@ -0,0 +1,44 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#version __CONTEXT__ + +uniform mat4 ViewProjection; +uniform mat4 ModelTransform; + +layout(location = 0) in vec4 in_position; +out vec4 vs_position; + +#include "PowerScaling/powerScaling_vs.hglsl" + +void main() +{ + vs_position = in_position; + vec4 tmp = in_position; + + vec4 position = pscTransform(tmp, ModelTransform); + vs_position = tmp; + position = ViewProjection * position; + gl_Position = z_normalization(position); +} \ No newline at end of file diff --git a/src/rendering/stars/renderableconstellationbounds.cpp b/src/rendering/stars/renderableconstellationbounds.cpp new file mode 100644 index 0000000000..fe4f2b5141 --- /dev/null +++ b/src/rendering/stars/renderableconstellationbounds.cpp @@ -0,0 +1,670 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#include + +#include +#include + +#define _USE_MATH_DEFINES +#include + +#include + +namespace { + const std::string _loggerCat = "RenderableConstellationBounds"; +} + +namespace openspace { + +RenderableConstellationBounds::RenderableConstellationBounds( + const ghoul::Dictionary& dictionary) + : Renderable(dictionary) + , _programIsDirty(false) + , _vao(0) + , _vbo(0) +{ + dictionary.getValue("File", _filename); +} + +bool RenderableConstellationBounds::initialize() { + struct Point{ + float ra; + float dec; + }; + + _program = ghoul::opengl::ProgramObject::Build("Star", + "${SHADERS}/constellationbounds_vs.glsl", + "${SHADERS}/constellationbounds_fs.glsl"); + _program->setProgramObjectCallback([&](ghoul::opengl::ProgramObject*){ this->_programIsDirty = true; }); + + + std::vector points; + + points.push_back({12.83333f, -53.f}); + points.push_back({22.83333f, -55.f}); + points.push_back({22.83333f, -84.f}); + points.push_back({12.83333f, -84.f}); + points.push_back({12.83333f, -53.f}); + + if (_vao == 0) { + glGenVertexArrays(1, &_vao); + LDEBUG("Generating Vertex Array id '" << _vao << "'"); + } + if (_vbo == 0) { + glGenBuffers(1, &_vbo); + LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); + } + + auto conv = [](float& ra, float& dec) { + ra = ((ra * 15) / 360.f) * 2.f * M_PI; + dec = (dec / 360.f) * 2.f * M_PI; + }; + + //std::vector positions; + for (auto p : points) { + SpiceDouble values[3]; + + conv(p.ra, p.dec); + radrec_c(1.0, p.ra, p.dec, values); + + _data.push_back(values[0]); + _data.push_back(values[1]); + _data.push_back(values[2]); + _data.push_back(15.f); + } + + + glBindVertexArray(_vao); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, + _data.size() * sizeof(float), + &_data[0], + GL_STATIC_DRAW); + + GLint positionAttrib = _program->attributeLocation("in_position"); + glEnableVertexAttribArray(positionAttrib); + + + glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + return true; +} + +bool RenderableConstellationBounds::deinitialize() { + return true; +} + +bool RenderableConstellationBounds::isReady() const { + return (_vao != 0) && (_vbo != 0); +} + +void RenderableConstellationBounds::render(const RenderData& data) { + _program->activate(); + + // @Check overwriting the scaling from the camera; error as parsec->meter conversion + // is done twice? ---abock + //glm::vec2 scaling = glm::vec2(1, -19); + + glm::mat4 modelMatrix = data.camera.modelMatrix(); + glm::mat4 viewMatrix = data.camera.viewMatrix(); + glm::mat4 projectionMatrix = data.camera.projectionMatrix(); + + _program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); + _program->setUniform("ModelTransform", glm::mat4(1)); + + //_program->setUniform("ModelTransform", modelMatrix); + //_program->setUniform("view", viewMatrix); + //_program->setUniform("projection", projectionMatrix); + + setPscUniforms(_program, &data.camera, data.position); + //_program->setUniform("scaling", scaling); + + //ghoul::opengl::TextureUnit unit; + //unit.activate(); + //_texture->bind(); + //_program->setIgnoreUniformLocationError(true); + //_program->setUniform("texture1", unit); + //_program->setIgnoreUniformLocationError(false); + + glBindVertexArray(_vao); + //const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); + //glDrawArrays(GL_POINTS, 0, nStars); + glDrawArrays(GL_LINE_STRIP, 0, _data.size() - 1); + glBindVertexArray(0); + _program->deactivate(); +} + +void RenderableConstellationBounds::update(const UpdateData& data) { + if (_programIsDirty) { + _program->rebuildFromFile(); + _programIsDirty = false; + } +} + +} // namespace openspace + +//#include +// +//#include +// +//#include +//#include +//#include +//#include +// +//#include +//#include +//#include +// +//namespace { +// const std::string _loggerCat = "RenderableStars"; +// +// const int8_t CurrentCacheVersion = 1; +// +// struct ColorVBOLayout { +// std::array position; // (x,y,z,e) +// +// float bvColor; // B-V color value +// float luminance; +// float absoluteMagnitude; +// }; +// +// struct VelocityVBOLayout { +// std::array position; // (x,y,z,e) +// +// float bvColor; // B-V color value +// float luminance; +// float absoluteMagnitude; +// +// float vx; // v_x +// float vy; // v_y +// float vz; // v_z +// }; +// +// struct SpeedVBOLayout { +// std::array position; // (x,y,z,e) +// +// float bvColor; // B-V color value +// float luminance; +// float absoluteMagnitude; +// +// float speed; +// }; +//} +// +//namespace openspace { +// +//RenderableStars::RenderableStars(const ghoul::Dictionary& dictionary) +// : Renderable(dictionary) +// , _colorTexturePath("colorTexture", "Color Texture") +// , _texture(nullptr) +// , _textureIsDirty(true) +// , _colorOption("colorOption", "Color Option") +// , _dataIsDirty(true) +// , _spriteSize("spriteSize", "Sprite Size", 0.0000005f, 0.f, 1.f) +// , _program(nullptr) +// , _programIsDirty(false) +// , _speckFile("") +// , _nValuesPerStar(0) +// , _vao(0) +// , _vbo(0) +//{ +// std::string texturePath = ""; +// if (dictionary.hasKey(constants::renderablestars::keyTexture)) { +// dictionary.getValue(constants::renderablestars::keyTexture, texturePath); +// _colorTexturePath = absPath(texturePath); +// } +// +// bool success = dictionary.getValue(constants::renderablestars::keyFile, _speckFile); +// if (!success) { +// LERROR("SpeckDataSource did not contain key '" << +// constants::renderablestars::keyFile << "'"); +// return; +// } +// _speckFile = absPath(_speckFile); +// +// _colorOption.addOption({ColorOption::Color, "Color"}); +// _colorOption.addOption({ColorOption::Velocity, "Velocity"}); +// _colorOption.addOption({ColorOption::Speed, "Speed"}); +// addProperty(_colorOption); +// _colorOption.onChange([&]{ _dataIsDirty = true;}); +// +// addProperty(_spriteSize); +// +// addProperty(_colorTexturePath); +// _colorTexturePath.onChange([&]{ _textureIsDirty = true;}); +//} +// +//bool RenderableStars::isReady() const { +// return (_program != nullptr) && (_fullData.size() > 0); +//} +// +//bool RenderableStars::initialize() { +// bool completeSuccess = true; +// +// _program = ghoul::opengl::ProgramObject::Build("Star", +// "${SHADERS}/star_vs.glsl", +// "${SHADERS}/star_fs.glsl", +// "${SHADERS}/star_ge.glsl"); +// completeSuccess = (_program != nullptr); +// _program->setProgramObjectCallback([&](ghoul::opengl::ProgramObject*){ _programIsDirty = true; }); +// completeSuccess &= loadData(); +// completeSuccess &= (_texture != nullptr); +// +// return completeSuccess; +//} +// +//bool RenderableStars::deinitialize() { +// glDeleteBuffers(1, &_vbo); +// _vbo = 0; +// glDeleteVertexArrays(1, &_vao); +// _vao = 0; +// +// delete _texture; +// _texture = nullptr; +// +// delete _program; +// _program = nullptr; +// return true; +//} +// +//void RenderableStars::render(const RenderData& data) { +// _program->activate(); +// +// // @Check overwriting the scaling from the camera; error as parsec->meter conversion +// // is done twice? ---abock +// glm::vec2 scaling = glm::vec2(1, -19); +// +// glm::mat4 modelMatrix = data.camera.modelMatrix(); +// glm::mat4 viewMatrix = data.camera.viewMatrix(); +// glm::mat4 projectionMatrix = data.camera.projectionMatrix(); +// +// _program->setUniform("model", modelMatrix); +// _program->setUniform("view", viewMatrix); +// _program->setUniform("projection", projectionMatrix); +// +// _program->setUniform("colorOption", _colorOption); +// +// setPscUniforms(_program, &data.camera, data.position); +// _program->setUniform("scaling", scaling); +// +// _program->setUniform("spriteSize", _spriteSize); +// +// ghoul::opengl::TextureUnit unit; +// unit.activate(); +// _texture->bind(); +// _program->setIgnoreUniformLocationError(true); +// _program->setUniform("texture1", unit); +// _program->setIgnoreUniformLocationError(false); +// +// glBindVertexArray(_vao); +// const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); +// glDrawArrays(GL_POINTS, 0, nStars); +// glBindVertexArray(0); +// _program->deactivate(); +//} +// +//void RenderableStars::update(const UpdateData& data) { +// if (_programIsDirty) { +// _program->rebuildFromFile(); +// _dataIsDirty = true; +// _programIsDirty = false; +// } +// +// if (_dataIsDirty) { +// const int value = _colorOption; +// LDEBUG("Regenerating data"); +// +// createDataSlice(ColorOption(value)); +// +// int size = static_cast(_slicedData.size()); +// +// if (_vao == 0) { +// glGenVertexArrays(1, &_vao); +// LDEBUG("Generating Vertex Array id '" << _vao << "'"); +// } +// if (_vbo == 0) { +// glGenBuffers(1, &_vbo); +// LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); +// } +// glBindVertexArray(_vao); +// glBindBuffer(GL_ARRAY_BUFFER, _vbo); +// glBufferData(GL_ARRAY_BUFFER, +// size*sizeof(GLfloat), +// &_slicedData[0], +// GL_STATIC_DRAW); +// +// GLint positionAttrib = _program->attributeLocation("in_position"); +// GLint brightnessDataAttrib = _program->attributeLocation("in_brightness"); +// +// const size_t nStars = _fullData.size() / _nValuesPerStar; +// const size_t nValues = _slicedData.size() / nStars; +// +// GLsizei stride = static_cast(sizeof(GLfloat) * nValues); +// +// glEnableVertexAttribArray(positionAttrib); +// glEnableVertexAttribArray(brightnessDataAttrib); +// const int colorOption = _colorOption; +// switch (colorOption) { +// case ColorOption::Color: +// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(ColorVBOLayout, position))); +// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(ColorVBOLayout, bvColor))); +// +// break; +// case ColorOption::Velocity: +// { +// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(VelocityVBOLayout, position))); +// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(VelocityVBOLayout, bvColor))); +// +// GLint velocityAttrib = _program->attributeLocation("in_velocity"); +// glEnableVertexAttribArray(velocityAttrib); +// glVertexAttribPointer(velocityAttrib, 3, GL_FLOAT, GL_TRUE, stride, +// reinterpret_cast(offsetof(VelocityVBOLayout, vx))); +// +// break; +// } +// case ColorOption::Speed: +// { +// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(SpeedVBOLayout, position))); +// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, +// reinterpret_cast(offsetof(SpeedVBOLayout, bvColor))); +// +// GLint speedAttrib = _program->attributeLocation("in_speed"); +// glEnableVertexAttribArray(speedAttrib); +// glVertexAttribPointer(speedAttrib, 1, GL_FLOAT, GL_TRUE, stride, +// reinterpret_cast(offsetof(SpeedVBOLayout, speed))); +// +// } +// } +// +// glBindBuffer(GL_ARRAY_BUFFER, 0); +// glBindVertexArray(0); +// +// _dataIsDirty = false; +// } +// +// if (_textureIsDirty) { +// LDEBUG("Reloading texture"); +// delete _texture; +// _texture = nullptr; +// if (_colorTexturePath.value() != "") { +// _texture = ghoul::opengl::loadTexture(absPath(_colorTexturePath)); +// if (_texture) { +// LDEBUG("Loaded texture from '" << absPath(_colorTexturePath) << "'"); +// _texture->uploadTexture(); +// } +// } +// _textureIsDirty = false; +// } +//} +// +//bool RenderableStars::loadData() { +// std::string _file = _speckFile; +// std::string cachedFile = ""; +// FileSys.cacheManager()->getCachedFile(_file, cachedFile, true); +// +// bool hasCachedFile = FileSys.fileExists(cachedFile); +// if (hasCachedFile) { +// LINFO("Cached file '" << cachedFile << "' used for Speck file '" << _file << "'"); +// +// bool success = loadCachedFile(cachedFile); +// if (success) +// return true; +// else +// FileSys.cacheManager()->removeCacheFile(_file); +// // Intentional fall-through to the 'else' computation to generate the cache +// // file for the next run +// } +// else { +// LINFO("Cache for Speck file '" << _file << "' not found"); +// } +// LINFO("Loading Speck file '" << _file << "'"); +// +// bool success = readSpeckFile(); +// if (!success) +// return false; +// +// LINFO("Saving cache"); +// success = saveCachedFile(cachedFile); +// +// return success; +//} +// +//bool RenderableStars::readSpeckFile() { +// std::string _file = _speckFile; +// std::ifstream file(_file); +// if (!file.good()) { +// LERROR("Failed to open Speck file '" << _file << "'"); +// return false; +// } +// +// _nValuesPerStar = 0; +// +// // The beginning of the speck file has a header that either contains comments +// // (signaled by a preceding '#') or information about the structure of the file +// // (signaled by the keywords 'datavar', 'texturevar', and 'texture') +// std::string line = ""; +// while (true) { +// std::ifstream::streampos position = file.tellg(); +// std::getline(file, line); +// +// if (line[0] == '#') +// continue; +// +// if (line.substr(0, 7) != "datavar" && +// line.substr(0, 10) != "texturevar" && +// line.substr(0, 7) != "texture") +// { +// // we read a line that doesn't belong to the header, so we have to jump back +// // before the beginning of the current line +// file.seekg(position); +// break; +// } +// +// if (line.substr(0, 7) == "datavar") { +// // datavar lines are structured as follows: +// // datavar # description +// // where # is the index of the data variable; so if we repeatedly overwrite +// // the 'nValues' variable with the latest index, we will end up with the total +// // number of values (+3 since X Y Z are not counted in the Speck file index) +// std::stringstream str(line); +// +// std::string dummy; +// str >> dummy; +// str >> _nValuesPerStar; +// _nValuesPerStar += 1; // We want the number, but the index is 0 based +// } +// } +// +// _nValuesPerStar += 3; // X Y Z are not counted in the Speck file indices +// +// do { +// std::vector values(_nValuesPerStar); +// +// std::getline(file, line); +// std::stringstream str(line); +// +// for (int i = 0; i < _nValuesPerStar; ++i) +// str >> values[i]; +// +// _fullData.insert(_fullData.end(), values.begin(), values.end()); +// } while (!file.eof()); +// +// return true; +//} +// +//bool RenderableStars::loadCachedFile(const std::string& file) { +// std::ifstream fileStream(file, std::ifstream::binary); +// if (fileStream.good()) { +// int8_t version = 0; +// fileStream.read(reinterpret_cast(&version), sizeof(int8_t)); +// if (version != CurrentCacheVersion) { +// LINFO("The format of the cached file has changed, deleted old cache"); +// fileStream.close(); +// FileSys.deleteFile(file); +// return false; +// } +// +// int32_t nValues = 0; +// fileStream.read(reinterpret_cast(&nValues), sizeof(int32_t)); +// fileStream.read(reinterpret_cast(&_nValuesPerStar), sizeof(int32_t)); +// +// _fullData.resize(nValues); +// fileStream.read(reinterpret_cast(&_fullData[0]), +// nValues * sizeof(_fullData[0])); +// +// bool success = fileStream.good(); +// return success; +// } +// else { +// LERROR("Error opening file '" << file << "' for loading cache file"); +// return false; +// } +//} +// +//bool RenderableStars::saveCachedFile(const std::string& file) const { +// std::ofstream fileStream(file, std::ofstream::binary); +// if (fileStream.good()) { +// fileStream.write(reinterpret_cast(&CurrentCacheVersion), +// sizeof(int8_t)); +// +// int32_t nValues = static_cast(_fullData.size()); +// if (nValues == 0) { +// LERROR("Error writing cache: No values were loaded"); +// return false; +// } +// fileStream.write(reinterpret_cast(&nValues), sizeof(int32_t)); +// +// int32_t nValuesPerStar = static_cast(_nValuesPerStar); +// fileStream.write(reinterpret_cast(&nValuesPerStar), sizeof(int32_t)); +// +// size_t nBytes = nValues * sizeof(_fullData[0]); +// fileStream.write(reinterpret_cast(&_fullData[0]), nBytes); +// +// bool success = fileStream.good(); +// return success; +// } +// else { +// LERROR("Error opening file '" << file << "' for save cache file"); +// return false; +// } +//} +// +//void RenderableStars::createDataSlice(ColorOption option) { +// _slicedData.clear(); +// for (size_t i = 0; i < _fullData.size(); i+=_nValuesPerStar) { +// psc position = PowerScaledCoordinate::CreatePowerScaledCoordinate( +// _fullData[i + 0], +// _fullData[i + 1], +// _fullData[i + 2] +// ); +// // Convert parsecs -> meter +// PowerScaledScalar parsecsToMetersFactor = PowerScaledScalar(0.308567758f, 17.f); +// position[0] *= parsecsToMetersFactor[0]; +// position[1] *= parsecsToMetersFactor[0]; +// position[2] *= parsecsToMetersFactor[0]; +// position[3] += parsecsToMetersFactor[1]; +// +// switch (option) { +// case ColorOption::Color: +// { +// union { +// ColorVBOLayout value; +// std::array data; +// } layout; +// +// layout.value.position = { { +// position[0], position[1], position[2], position[3] +// } }; +// +// layout.value.bvColor = _fullData[i + 3]; +// layout.value.luminance = _fullData[i + 4]; +// layout.value.absoluteMagnitude = _fullData[i + 5]; +// +// _slicedData.insert(_slicedData.end(), +// layout.data.begin(), +// layout.data.end()); +// +// break; +// } +// case ColorOption::Velocity: +// { +// union { +// VelocityVBOLayout value; +// std::array data; +// } layout; +// +// layout.value.position = { { +// position[0], position[1], position[2], position[3] +// } }; +// +// layout.value.bvColor = _fullData[i + 3]; +// layout.value.luminance = _fullData[i + 4]; +// layout.value.absoluteMagnitude = _fullData[i + 5]; +// +// layout.value.vx = _fullData[i + 12]; +// layout.value.vy = _fullData[i + 13]; +// layout.value.vz = _fullData[i + 14]; +// +// _slicedData.insert(_slicedData.end(), +// layout.data.begin(), +// layout.data.end()); +// break; +// } +// case ColorOption::Speed: +// { +// union { +// SpeedVBOLayout value; +// std::array data; +// } layout; +// +// layout.value.position = { { +// position[0], position[1], position[2], position[3] +// } }; +// +// layout.value.bvColor = _fullData[i + 3]; +// layout.value.luminance = _fullData[i + 4]; +// layout.value.absoluteMagnitude = _fullData[i + 5]; +// +// layout.value.speed = _fullData[i + 15]; +// +// _slicedData.insert(_slicedData.end(), +// layout.data.begin(), +// layout.data.end()); +// break; +// } +// } +// } +//} +// +//} // namespace openspace diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index f169fa226c..c225a29efc 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -26,6 +26,7 @@ // renderables #include +#include #include #include #include @@ -62,10 +63,10 @@ void FactoryManager::initialize() // TODO: This has to be moved into a sort of module structure (ab) // Add Renderables _manager->addFactory(new ghoul::TemplateFactory); - _manager->factory()->registerClass( - "RenderablePlanet"); - _manager->factory()->registerClass( - "RenderableStars"); + _manager->factory()->registerClass("RenderablePlanet"); + _manager->factory()->registerClass("RenderableStars"); + _manager->factory()->registerClass + ("RenderableConstellationBounds"); _manager->factory()->registerClass( "RenderableEphemeris"); //will replace ephemeris class soon... From 5d168c18e5851edfb1e1bbf0082e023e65c1ea8d Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 00:55:25 +0100 Subject: [PATCH 02/11] First working version --- .../stars/renderableconstallationbounds.h | 24 ++- openspace-data | 2 +- .../stars/renderableconstellationbounds.cpp | 150 ++++++++++++------ 3 files changed, 123 insertions(+), 53 deletions(-) diff --git a/include/openspace/rendering/stars/renderableconstallationbounds.h b/include/openspace/rendering/stars/renderableconstallationbounds.h index 34f7368481..3cb2bc7a24 100644 --- a/include/openspace/rendering/stars/renderableconstallationbounds.h +++ b/include/openspace/rendering/stars/renderableconstallationbounds.h @@ -29,6 +29,8 @@ #include +#include + namespace openspace { class RenderableConstellationBounds : public Renderable { @@ -44,18 +46,36 @@ public: void update(const UpdateData& data) override; private: + struct ConstellationBound { + typedef std::array Point; + std::string constellation; + std::vector points; + GLuint vao; + }; + + float deg2rad(float deg) { + return (deg / 360.f) * 2.f * 3.1415926f; + } + float convertHrsToRadians(float rightAscension) { + // 360 degrees / 24h = 15 degrees/h + return deg2rad(rightAscension * 15); + } + + + bool loadFile(); + + std::string _filename; ghoul::opengl::ProgramObject* _program; bool _programIsDirty; - std::vector _data; + std::vector _bounds; GLuint _vao; GLuint _vbo; }; - } // namespace openspace #endif // __RENDERABLECONSTELLATIONBOUNDS_H__ diff --git a/openspace-data b/openspace-data index ec8fded580..d098f15135 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit ec8fded58055f279da1f23091f247daa70384a62 +Subproject commit d098f15135a462fad3df6b846da05059111d28ec diff --git a/src/rendering/stars/renderableconstellationbounds.cpp b/src/rendering/stars/renderableconstellationbounds.cpp index fe4f2b5141..f2c4f51838 100644 --- a/src/rendering/stars/renderableconstellationbounds.cpp +++ b/src/rendering/stars/renderableconstellationbounds.cpp @@ -25,11 +25,14 @@ #include #include + +#include #include #define _USE_MATH_DEFINES #include +#include #include namespace { @@ -49,68 +52,59 @@ RenderableConstellationBounds::RenderableConstellationBounds( } bool RenderableConstellationBounds::initialize() { - struct Point{ - float ra; - float dec; - }; - _program = ghoul::opengl::ProgramObject::Build("Star", "${SHADERS}/constellationbounds_vs.glsl", "${SHADERS}/constellationbounds_fs.glsl"); _program->setProgramObjectCallback([&](ghoul::opengl::ProgramObject*){ this->_programIsDirty = true; }); - std::vector points; - - points.push_back({12.83333f, -53.f}); - points.push_back({22.83333f, -55.f}); - points.push_back({22.83333f, -84.f}); - points.push_back({12.83333f, -84.f}); - points.push_back({12.83333f, -53.f}); - if (_vao == 0) { glGenVertexArrays(1, &_vao); LDEBUG("Generating Vertex Array id '" << _vao << "'"); } - if (_vbo == 0) { - glGenBuffers(1, &_vbo); - LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); + //if (_vbo == 0) { + // glGenBuffers(1, &_vbo); + // LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); + //} + + + loadFile(); + + for (ConstellationBound& bound : _bounds) { + glGenVertexArrays(1, &bound.vao); + glBindVertexArray(bound.vao); + + GLuint vbo; + glGenBuffers(1, &vbo); + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, + bound.points.size() * 4 * sizeof(float), + &bound.points[0], + GL_STATIC_DRAW + ); + + GLint positionAttrib = _program->attributeLocation("in_position"); + glEnableVertexAttribArray(positionAttrib); + glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } - auto conv = [](float& ra, float& dec) { - ra = ((ra * 15) / 360.f) * 2.f * M_PI; - dec = (dec / 360.f) * 2.f * M_PI; - }; + //glBindBuffer(GL_ARRAY_BUFFER, _vbo); + //glBufferData(GL_ARRAY_BUFFER, + // _data.size() * sizeof(float), + // &_data[0], + // GL_STATIC_DRAW); - //std::vector positions; - for (auto p : points) { - SpiceDouble values[3]; + //GLint positionAttrib = _program->attributeLocation("in_position"); + //glEnableVertexAttribArray(positionAttrib); - conv(p.ra, p.dec); - radrec_c(1.0, p.ra, p.dec, values); + //glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); - _data.push_back(values[0]); - _data.push_back(values[1]); - _data.push_back(values[2]); - _data.push_back(15.f); - } - - - glBindVertexArray(_vao); - glBindBuffer(GL_ARRAY_BUFFER, _vbo); - glBufferData(GL_ARRAY_BUFFER, - _data.size() * sizeof(float), - &_data[0], - GL_STATIC_DRAW); - - GLint positionAttrib = _program->attributeLocation("in_position"); - glEnableVertexAttribArray(positionAttrib); - - - glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); + //glBindBuffer(GL_ARRAY_BUFFER, 0); + //glBindVertexArray(0); return true; } @@ -120,7 +114,8 @@ bool RenderableConstellationBounds::deinitialize() { } bool RenderableConstellationBounds::isReady() const { - return (_vao != 0) && (_vbo != 0); + //return (_vao != 0) && (_vbo != 0); + return true; } void RenderableConstellationBounds::render(const RenderData& data) { @@ -151,10 +146,18 @@ void RenderableConstellationBounds::render(const RenderData& data) { //_program->setUniform("texture1", unit); //_program->setIgnoreUniformLocationError(false); - glBindVertexArray(_vao); + for (auto bound : _bounds) { + glBindVertexArray(bound.vao); + glDrawArrays(GL_LINE_STRIP, 0, bound.points.size()); + } + + //glBindVertexArray(_vao); //const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); //glDrawArrays(GL_POINTS, 0, nStars); - glDrawArrays(GL_LINE_STRIP, 0, _data.size() - 1); + //glDrawArrays(GL_LINE_STRIP, 0, _data.size() - 1); + + + glBindVertexArray(0); _program->deactivate(); } @@ -166,6 +169,53 @@ void RenderableConstellationBounds::update(const UpdateData& data) { } } +bool RenderableConstellationBounds::loadFile() { + std::string fileName = absPath(_filename); + std::ifstream file(fileName); + + ConstellationBound currentBound; + currentBound.constellation = ""; + std::string line; + while (file.good()) { + std::getline(file, line); + + // @CHECK: Is this the best way of doing this? ---abock + float ra; + float dec; + std::string constellation; + std::stringstream s(line); + s >> ra; + s >> dec; + s >> constellation; + + if (constellation != currentBound.constellation) { + _bounds.push_back(currentBound); + currentBound = ConstellationBound(); + currentBound.constellation = constellation; + } + + ra = convertHrsToRadians(ra); + dec = deg2rad(dec); + + SpiceDouble values[3]; + radrec_c(1.0, ra, dec, values); + + std::array position; + position[0] = values[0]; + position[1] = values[1]; + position[2] = values[2]; + position[3] = 15.f; + currentBound.points.push_back(position); + } + + // remove the first one + _bounds.erase(_bounds.begin()); + + return true; +} + + + } // namespace openspace //#include From e735e8f90a920cf4a36b2d7e903ca930984582e6 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 16:55:26 +0100 Subject: [PATCH 03/11] Update glslLangDef file --- ext/ghoul | 2 +- openspace-data | 2 +- support/cloc/glslLangDef | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ext/ghoul b/ext/ghoul index e84d7baf27..4aae49c3e0 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit e84d7baf27de3ff4e2831bc8bf59136cdc24bc30 +Subproject commit 4aae49c3e034313defdc407b15acfd89bcb2f9c5 diff --git a/openspace-data b/openspace-data index 48daa03562..d098f15135 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit 48daa03562e2bdc42a9088c724792ac57832ab92 +Subproject commit d098f15135a462fad3df6b846da05059111d28ec diff --git a/support/cloc/glslLangDef b/support/cloc/glslLangDef index ccc4f70900..d4f8610ae6 100644 --- a/support/cloc/glslLangDef +++ b/support/cloc/glslLangDef @@ -4,4 +4,3 @@ GLSL filter remove_inline //.*$ extension glsl 3rd_gen_scale 5 - end_of_line_continuation \\$ \ No newline at end of file From 38b1c00d3dd9c209185a5bc1c4565021ac0f36b1 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 18:05:13 +0100 Subject: [PATCH 04/11] Fix RenderableConstellationBounds name Make rendering more efficient --- ...unds.h => renderableconstellationbounds.h} | 11 +- .../stars/renderableconstellationbounds.cpp | 103 ++++++++++++------ src/util/factorymanager.cpp | 2 +- 3 files changed, 75 insertions(+), 41 deletions(-) rename include/openspace/rendering/stars/{renderableconstallationbounds.h => renderableconstellationbounds.h} (95%) diff --git a/include/openspace/rendering/stars/renderableconstallationbounds.h b/include/openspace/rendering/stars/renderableconstellationbounds.h similarity index 95% rename from include/openspace/rendering/stars/renderableconstallationbounds.h rename to include/openspace/rendering/stars/renderableconstellationbounds.h index 3cb2bc7a24..90e253ec0f 100644 --- a/include/openspace/rendering/stars/renderableconstallationbounds.h +++ b/include/openspace/rendering/stars/renderableconstellationbounds.h @@ -47,10 +47,9 @@ public: private: struct ConstellationBound { - typedef std::array Point; std::string constellation; - std::vector points; - GLuint vao; + int startIndex; + int nVertices; }; float deg2rad(float deg) { @@ -70,7 +69,11 @@ private: ghoul::opengl::ProgramObject* _program; bool _programIsDirty; - std::vector _bounds; + std::vector _constellationBounds; + typedef std::array Vertex; + std::vector _vertexValues; + + glm::dmat3 _stateMatrix; GLuint _vao; GLuint _vbo; diff --git a/src/rendering/stars/renderableconstellationbounds.cpp b/src/rendering/stars/renderableconstellationbounds.cpp index f2c4f51838..9f6d5b26cb 100644 --- a/src/rendering/stars/renderableconstellationbounds.cpp +++ b/src/rendering/stars/renderableconstellationbounds.cpp @@ -22,9 +22,10 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#include +#include #include +#include #include #include @@ -62,35 +63,50 @@ bool RenderableConstellationBounds::initialize() { glGenVertexArrays(1, &_vao); LDEBUG("Generating Vertex Array id '" << _vao << "'"); } - //if (_vbo == 0) { - // glGenBuffers(1, &_vbo); - // LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); - //} - + if (_vbo == 0) { + glGenBuffers(1, &_vbo); + LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); + } loadFile(); - for (ConstellationBound& bound : _bounds) { - glGenVertexArrays(1, &bound.vao); - glBindVertexArray(bound.vao); - - GLuint vbo; - glGenBuffers(1, &vbo); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, - bound.points.size() * 4 * sizeof(float), - &bound.points[0], + glBindVertexArray(_vao); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, + _vertexValues.size() * 4 * sizeof(float), + &_vertexValues[0], GL_STATIC_DRAW ); - GLint positionAttrib = _program->attributeLocation("in_position"); - glEnableVertexAttribArray(positionAttrib); - glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); + GLint positionAttrib = _program->attributeLocation("in_position"); + glEnableVertexAttribArray(positionAttrib); + glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + + //for (ConstellationBound& bound : _bounds) { + // glGenVertexArrays(1, &bound.vao); + // glBindVertexArray(bound.vao); + + // GLuint vbo; + // glGenBuffers(1, &vbo); + + // glBindBuffer(GL_ARRAY_BUFFER, vbo); + // glBufferData(GL_ARRAY_BUFFER, + // bound.points.size() * 4 * sizeof(float), + // &bound.points[0], + // GL_STATIC_DRAW + // ); + + // GLint positionAttrib = _program->attributeLocation("in_position"); + // glEnableVertexAttribArray(positionAttrib); + // glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); + + // glBindBuffer(GL_ARRAY_BUFFER, 0); + // glBindVertexArray(0); + //} //glBindBuffer(GL_ARRAY_BUFFER, _vbo); //glBufferData(GL_ARRAY_BUFFER, @@ -129,8 +145,15 @@ void RenderableConstellationBounds::render(const RenderData& data) { glm::mat4 viewMatrix = data.camera.viewMatrix(); glm::mat4 projectionMatrix = data.camera.projectionMatrix(); + glm::mat4 transform = glm::mat4(1); + for (int i = 0; i < 3; i++){ + for (int j = 0; j < 3; j++){ + transform[i][j] = _stateMatrix[i][j]; + } + } + _program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - _program->setUniform("ModelTransform", glm::mat4(1)); + _program->setUniform("ModelTransform", transform); //_program->setUniform("ModelTransform", modelMatrix); //_program->setUniform("view", viewMatrix); @@ -146,9 +169,10 @@ void RenderableConstellationBounds::render(const RenderData& data) { //_program->setUniform("texture1", unit); //_program->setIgnoreUniformLocationError(false); - for (auto bound : _bounds) { - glBindVertexArray(bound.vao); - glDrawArrays(GL_LINE_STRIP, 0, bound.points.size()); + glBindVertexArray(_vao); + for (auto bound : _constellationBounds) { + glDrawArrays(GL_LINE_STRIP, bound.startIndex , bound.nVertices); + //glDrawArrays(GL_LINE_STRIP, 0, bound.points.size()); } //glBindVertexArray(_vao); @@ -163,16 +187,19 @@ void RenderableConstellationBounds::render(const RenderData& data) { } void RenderableConstellationBounds::update(const UpdateData& data) { - if (_programIsDirty) { + if (_programIsDirty) { _program->rebuildFromFile(); _programIsDirty = false; } + + openspace::SpiceManager::ref().getPositionTransformMatrix("J2000", "GALACTIC", data.time, _stateMatrix); } bool RenderableConstellationBounds::loadFile() { std::string fileName = absPath(_filename); std::ifstream file(fileName); + ConstellationBound currentBound; currentBound.constellation = ""; std::string line; @@ -189,9 +216,12 @@ bool RenderableConstellationBounds::loadFile() { s >> constellation; if (constellation != currentBound.constellation) { - _bounds.push_back(currentBound); + // @CHECK: Does this work? ---abock + currentBound.nVertices = (_vertexValues.size() - currentBound.startIndex); + _constellationBounds.push_back(currentBound); currentBound = ConstellationBound(); currentBound.constellation = constellation; + currentBound.startIndex = _vertexValues.size(); } ra = convertHrsToRadians(ra); @@ -200,16 +230,17 @@ bool RenderableConstellationBounds::loadFile() { SpiceDouble values[3]; radrec_c(1.0, ra, dec, values); - std::array position; - position[0] = values[0]; - position[1] = values[1]; - position[2] = values[2]; - position[3] = 15.f; - currentBound.points.push_back(position); + std::array v; + v[0] = values[0]; + v[1] = values[1]; + v[2] = values[2]; + v[3] = 15.f; //@TODO Make a user-changeable values ---abock + + _vertexValues.push_back(v); } // remove the first one - _bounds.erase(_bounds.begin()); + _constellationBounds.erase(_constellationBounds.begin()); return true; } diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index c225a29efc..72e99876e7 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -26,7 +26,7 @@ // renderables #include -#include +#include #include #include #include From 3f35b0a948ef859f0121fe9922b41a78597d7e5c Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 18:37:15 +0100 Subject: [PATCH 05/11] More optimizations for RenderableConstellationBounds --- .../stars/renderableconstellationbounds.h | 18 +- shaders/constellationbounds_vs.glsl | 8 +- .../stars/renderableconstellationbounds.cpp | 660 ++---------------- 3 files changed, 84 insertions(+), 602 deletions(-) diff --git a/include/openspace/rendering/stars/renderableconstellationbounds.h b/include/openspace/rendering/stars/renderableconstellationbounds.h index 90e253ec0f..7e955974fa 100644 --- a/include/openspace/rendering/stars/renderableconstellationbounds.h +++ b/include/openspace/rendering/stars/renderableconstellationbounds.h @@ -26,6 +26,7 @@ #define __RENDERABLECONSTELLATIONBOUNDS_H__ #include +#include #include @@ -52,27 +53,22 @@ private: int nVertices; }; - float deg2rad(float deg) { - return (deg / 360.f) * 2.f * 3.1415926f; - } - float convertHrsToRadians(float rightAscension) { - // 360 degrees / 24h = 15 degrees/h - return deg2rad(rightAscension * 15); - } - - bool loadFile(); - + std::string _filename; ghoul::opengl::ProgramObject* _program; bool _programIsDirty; std::vector _constellationBounds; - typedef std::array Vertex; + + typedef std::array Vertex; std::vector _vertexValues; + properties::FloatProperty _distance; + + std::string _originReferenceFrame; glm::dmat3 _stateMatrix; GLuint _vao; diff --git a/shaders/constellationbounds_vs.glsl b/shaders/constellationbounds_vs.glsl index d6e1d9d99f..f78b55555d 100644 --- a/shaders/constellationbounds_vs.glsl +++ b/shaders/constellationbounds_vs.glsl @@ -27,15 +27,17 @@ uniform mat4 ViewProjection; uniform mat4 ModelTransform; -layout(location = 0) in vec4 in_position; +uniform float exponent; + +layout(location = 0) in vec3 in_position; out vec4 vs_position; #include "PowerScaling/powerScaling_vs.hglsl" void main() { - vs_position = in_position; - vec4 tmp = in_position; + vec4 tmp = vec4(in_position, exponent); + vs_position = tmp; vec4 position = pscTransform(tmp, ModelTransform); vs_position = tmp; diff --git a/src/rendering/stars/renderableconstellationbounds.cpp b/src/rendering/stars/renderableconstellationbounds.cpp index 9f6d5b26cb..d9cd904a65 100644 --- a/src/rendering/stars/renderableconstellationbounds.cpp +++ b/src/rendering/stars/renderableconstellationbounds.cpp @@ -38,6 +38,17 @@ namespace { const std::string _loggerCat = "RenderableConstellationBounds"; + + const std::string keyFile = "File"; + const std::string keyOriginalReferenceFrame = "OriginalReferenceFrame"; + + float deg2rad(float deg) { + return (deg / 360.f) * 2.f * 3.1415926f; + } + float convertHrsToRadians(float rightAscension) { + // 360 degrees / 24h = 15 degrees/h + return deg2rad(rightAscension * 15); + } } namespace openspace { @@ -45,17 +56,34 @@ namespace openspace { RenderableConstellationBounds::RenderableConstellationBounds( const ghoul::Dictionary& dictionary) : Renderable(dictionary) + , _filename("") , _programIsDirty(false) + , _distance("distance", "Distance to the celestial Sphere", 15.f, 0.f, 30.f) + , _originReferenceFrame("") , _vao(0) , _vbo(0) { - dictionary.getValue("File", _filename); + bool success = dictionary.getValue(keyFile, _filename); + if (!success) { + LERROR("RenderableConstellationBounds did not contain a key '" << + keyFile << "'"); + } + + success = dictionary.getValue(keyOriginalReferenceFrame, _originReferenceFrame); + if (!success) { + LERROR("RenderableConstellationBounds did not contain a key '" << + keyOriginalReferenceFrame << "'"); + } + + addProperty(_distance); } bool RenderableConstellationBounds::initialize() { - _program = ghoul::opengl::ProgramObject::Build("Star", + _program = ghoul::opengl::ProgramObject::Build("ConstellationBounds", "${SHADERS}/constellationbounds_vs.glsl", "${SHADERS}/constellationbounds_fs.glsl"); + if (!_program) + return false; _program->setProgramObjectCallback([&](ghoul::opengl::ProgramObject*){ this->_programIsDirty = true; }); @@ -68,120 +96,59 @@ bool RenderableConstellationBounds::initialize() { LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); } - loadFile(); + bool loadSuccess = loadFile(); + if (!loadSuccess) + return false; glBindVertexArray(_vao); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, - _vertexValues.size() * 4 * sizeof(float), + _vertexValues.size() * 3 * sizeof(float), &_vertexValues[0], GL_STATIC_DRAW ); GLint positionAttrib = _program->attributeLocation("in_position"); glEnableVertexAttribArray(positionAttrib); - glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - - //for (ConstellationBound& bound : _bounds) { - // glGenVertexArrays(1, &bound.vao); - // glBindVertexArray(bound.vao); - - // GLuint vbo; - // glGenBuffers(1, &vbo); - - // glBindBuffer(GL_ARRAY_BUFFER, vbo); - // glBufferData(GL_ARRAY_BUFFER, - // bound.points.size() * 4 * sizeof(float), - // &bound.points[0], - // GL_STATIC_DRAW - // ); - - // GLint positionAttrib = _program->attributeLocation("in_position"); - // glEnableVertexAttribArray(positionAttrib); - // glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); - - // glBindBuffer(GL_ARRAY_BUFFER, 0); - // glBindVertexArray(0); - //} - - //glBindBuffer(GL_ARRAY_BUFFER, _vbo); - //glBufferData(GL_ARRAY_BUFFER, - // _data.size() * sizeof(float), - // &_data[0], - // GL_STATIC_DRAW); - - //GLint positionAttrib = _program->attributeLocation("in_position"); - //glEnableVertexAttribArray(positionAttrib); - - //glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, 0, 0); - - //glBindBuffer(GL_ARRAY_BUFFER, 0); - //glBindVertexArray(0); - return true; } bool RenderableConstellationBounds::deinitialize() { + glDeleteBuffers(1, &_vbo); + _vbo = 0; + glDeleteVertexArrays(1, &_vao); + _vao = 0; + + delete _program; + _program = nullptr; return true; } bool RenderableConstellationBounds::isReady() const { - //return (_vao != 0) && (_vbo != 0); - return true; + return (_vao != 0) && (_vbo != 0); } void RenderableConstellationBounds::render(const RenderData& data) { _program->activate(); - // @Check overwriting the scaling from the camera; error as parsec->meter conversion - // is done twice? ---abock - //glm::vec2 scaling = glm::vec2(1, -19); - glm::mat4 modelMatrix = data.camera.modelMatrix(); glm::mat4 viewMatrix = data.camera.viewMatrix(); glm::mat4 projectionMatrix = data.camera.projectionMatrix(); - glm::mat4 transform = glm::mat4(1); - for (int i = 0; i < 3; i++){ - for (int j = 0; j < 3; j++){ - transform[i][j] = _stateMatrix[i][j]; - } - } - - _program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - _program->setUniform("ModelTransform", transform); - - //_program->setUniform("ModelTransform", modelMatrix); - //_program->setUniform("view", viewMatrix); - //_program->setUniform("projection", projectionMatrix); - setPscUniforms(_program, &data.camera, data.position); - //_program->setUniform("scaling", scaling); - //ghoul::opengl::TextureUnit unit; - //unit.activate(); - //_texture->bind(); - //_program->setIgnoreUniformLocationError(true); - //_program->setUniform("texture1", unit); - //_program->setIgnoreUniformLocationError(false); + _program->setUniform("exponent", _distance); + _program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); + _program->setUniform("ModelTransform", glm::mat4(glm::dmat4(_stateMatrix))); glBindVertexArray(_vao); - for (auto bound : _constellationBounds) { + for (auto bound : _constellationBounds) glDrawArrays(GL_LINE_STRIP, bound.startIndex , bound.nVertices); - //glDrawArrays(GL_LINE_STRIP, 0, bound.points.size()); - } - - //glBindVertexArray(_vao); - //const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); - //glDrawArrays(GL_POINTS, 0, nStars); - //glDrawArrays(GL_LINE_STRIP, 0, _data.size() - 1); - - - glBindVertexArray(0); _program->deactivate(); } @@ -192,19 +159,33 @@ void RenderableConstellationBounds::update(const UpdateData& data) { _programIsDirty = false; } - openspace::SpiceManager::ref().getPositionTransformMatrix("J2000", "GALACTIC", data.time, _stateMatrix); + SpiceManager::ref().getPositionTransformMatrix( + _originReferenceFrame, + "GALACTIC", + data.time, + _stateMatrix + ); } bool RenderableConstellationBounds::loadFile() { + if (_filename.empty()) + return false; + std::string fileName = absPath(_filename); std::ifstream file(fileName); - + if (!file.good()) { + LERROR("Could not open file '" << fileName << "' for reading"); + return false; + } ConstellationBound currentBound; currentBound.constellation = ""; std::string line; + int lineNumber = 1; while (file.good()) { std::getline(file, line); + if (line.empty()) + continue; // @CHECK: Is this the best way of doing this? ---abock float ra; @@ -215,6 +196,12 @@ bool RenderableConstellationBounds::loadFile() { s >> dec; s >> constellation; + if (!s.good()) { + // If this evaluates to true, the stream was not completely filled, which + // means that the line was incomplete, so there was an error + LERROR("Error reading file '" << fileName << "' at line #" << lineNumber); + } + if (constellation != currentBound.constellation) { // @CHECK: Does this work? ---abock currentBound.nVertices = (_vertexValues.size() - currentBound.startIndex); @@ -230,13 +217,13 @@ bool RenderableConstellationBounds::loadFile() { SpiceDouble values[3]; radrec_c(1.0, ra, dec, values); - std::array v; + std::array v; v[0] = values[0]; v[1] = values[1]; v[2] = values[2]; - v[3] = 15.f; //@TODO Make a user-changeable values ---abock - _vertexValues.push_back(v); + _vertexValues.push_back(v); + ++lineNumber; } // remove the first one @@ -245,507 +232,4 @@ bool RenderableConstellationBounds::loadFile() { return true; } - - } // namespace openspace - -//#include -// -//#include -// -//#include -//#include -//#include -//#include -// -//#include -//#include -//#include -// -//namespace { -// const std::string _loggerCat = "RenderableStars"; -// -// const int8_t CurrentCacheVersion = 1; -// -// struct ColorVBOLayout { -// std::array position; // (x,y,z,e) -// -// float bvColor; // B-V color value -// float luminance; -// float absoluteMagnitude; -// }; -// -// struct VelocityVBOLayout { -// std::array position; // (x,y,z,e) -// -// float bvColor; // B-V color value -// float luminance; -// float absoluteMagnitude; -// -// float vx; // v_x -// float vy; // v_y -// float vz; // v_z -// }; -// -// struct SpeedVBOLayout { -// std::array position; // (x,y,z,e) -// -// float bvColor; // B-V color value -// float luminance; -// float absoluteMagnitude; -// -// float speed; -// }; -//} -// -//namespace openspace { -// -//RenderableStars::RenderableStars(const ghoul::Dictionary& dictionary) -// : Renderable(dictionary) -// , _colorTexturePath("colorTexture", "Color Texture") -// , _texture(nullptr) -// , _textureIsDirty(true) -// , _colorOption("colorOption", "Color Option") -// , _dataIsDirty(true) -// , _spriteSize("spriteSize", "Sprite Size", 0.0000005f, 0.f, 1.f) -// , _program(nullptr) -// , _programIsDirty(false) -// , _speckFile("") -// , _nValuesPerStar(0) -// , _vao(0) -// , _vbo(0) -//{ -// std::string texturePath = ""; -// if (dictionary.hasKey(constants::renderablestars::keyTexture)) { -// dictionary.getValue(constants::renderablestars::keyTexture, texturePath); -// _colorTexturePath = absPath(texturePath); -// } -// -// bool success = dictionary.getValue(constants::renderablestars::keyFile, _speckFile); -// if (!success) { -// LERROR("SpeckDataSource did not contain key '" << -// constants::renderablestars::keyFile << "'"); -// return; -// } -// _speckFile = absPath(_speckFile); -// -// _colorOption.addOption({ColorOption::Color, "Color"}); -// _colorOption.addOption({ColorOption::Velocity, "Velocity"}); -// _colorOption.addOption({ColorOption::Speed, "Speed"}); -// addProperty(_colorOption); -// _colorOption.onChange([&]{ _dataIsDirty = true;}); -// -// addProperty(_spriteSize); -// -// addProperty(_colorTexturePath); -// _colorTexturePath.onChange([&]{ _textureIsDirty = true;}); -//} -// -//bool RenderableStars::isReady() const { -// return (_program != nullptr) && (_fullData.size() > 0); -//} -// -//bool RenderableStars::initialize() { -// bool completeSuccess = true; -// -// _program = ghoul::opengl::ProgramObject::Build("Star", -// "${SHADERS}/star_vs.glsl", -// "${SHADERS}/star_fs.glsl", -// "${SHADERS}/star_ge.glsl"); -// completeSuccess = (_program != nullptr); -// _program->setProgramObjectCallback([&](ghoul::opengl::ProgramObject*){ _programIsDirty = true; }); -// completeSuccess &= loadData(); -// completeSuccess &= (_texture != nullptr); -// -// return completeSuccess; -//} -// -//bool RenderableStars::deinitialize() { -// glDeleteBuffers(1, &_vbo); -// _vbo = 0; -// glDeleteVertexArrays(1, &_vao); -// _vao = 0; -// -// delete _texture; -// _texture = nullptr; -// -// delete _program; -// _program = nullptr; -// return true; -//} -// -//void RenderableStars::render(const RenderData& data) { -// _program->activate(); -// -// // @Check overwriting the scaling from the camera; error as parsec->meter conversion -// // is done twice? ---abock -// glm::vec2 scaling = glm::vec2(1, -19); -// -// glm::mat4 modelMatrix = data.camera.modelMatrix(); -// glm::mat4 viewMatrix = data.camera.viewMatrix(); -// glm::mat4 projectionMatrix = data.camera.projectionMatrix(); -// -// _program->setUniform("model", modelMatrix); -// _program->setUniform("view", viewMatrix); -// _program->setUniform("projection", projectionMatrix); -// -// _program->setUniform("colorOption", _colorOption); -// -// setPscUniforms(_program, &data.camera, data.position); -// _program->setUniform("scaling", scaling); -// -// _program->setUniform("spriteSize", _spriteSize); -// -// ghoul::opengl::TextureUnit unit; -// unit.activate(); -// _texture->bind(); -// _program->setIgnoreUniformLocationError(true); -// _program->setUniform("texture1", unit); -// _program->setIgnoreUniformLocationError(false); -// -// glBindVertexArray(_vao); -// const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); -// glDrawArrays(GL_POINTS, 0, nStars); -// glBindVertexArray(0); -// _program->deactivate(); -//} -// -//void RenderableStars::update(const UpdateData& data) { -// if (_programIsDirty) { -// _program->rebuildFromFile(); -// _dataIsDirty = true; -// _programIsDirty = false; -// } -// -// if (_dataIsDirty) { -// const int value = _colorOption; -// LDEBUG("Regenerating data"); -// -// createDataSlice(ColorOption(value)); -// -// int size = static_cast(_slicedData.size()); -// -// if (_vao == 0) { -// glGenVertexArrays(1, &_vao); -// LDEBUG("Generating Vertex Array id '" << _vao << "'"); -// } -// if (_vbo == 0) { -// glGenBuffers(1, &_vbo); -// LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); -// } -// glBindVertexArray(_vao); -// glBindBuffer(GL_ARRAY_BUFFER, _vbo); -// glBufferData(GL_ARRAY_BUFFER, -// size*sizeof(GLfloat), -// &_slicedData[0], -// GL_STATIC_DRAW); -// -// GLint positionAttrib = _program->attributeLocation("in_position"); -// GLint brightnessDataAttrib = _program->attributeLocation("in_brightness"); -// -// const size_t nStars = _fullData.size() / _nValuesPerStar; -// const size_t nValues = _slicedData.size() / nStars; -// -// GLsizei stride = static_cast(sizeof(GLfloat) * nValues); -// -// glEnableVertexAttribArray(positionAttrib); -// glEnableVertexAttribArray(brightnessDataAttrib); -// const int colorOption = _colorOption; -// switch (colorOption) { -// case ColorOption::Color: -// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(ColorVBOLayout, position))); -// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(ColorVBOLayout, bvColor))); -// -// break; -// case ColorOption::Velocity: -// { -// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(VelocityVBOLayout, position))); -// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(VelocityVBOLayout, bvColor))); -// -// GLint velocityAttrib = _program->attributeLocation("in_velocity"); -// glEnableVertexAttribArray(velocityAttrib); -// glVertexAttribPointer(velocityAttrib, 3, GL_FLOAT, GL_TRUE, stride, -// reinterpret_cast(offsetof(VelocityVBOLayout, vx))); -// -// break; -// } -// case ColorOption::Speed: -// { -// glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(SpeedVBOLayout, position))); -// glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, -// reinterpret_cast(offsetof(SpeedVBOLayout, bvColor))); -// -// GLint speedAttrib = _program->attributeLocation("in_speed"); -// glEnableVertexAttribArray(speedAttrib); -// glVertexAttribPointer(speedAttrib, 1, GL_FLOAT, GL_TRUE, stride, -// reinterpret_cast(offsetof(SpeedVBOLayout, speed))); -// -// } -// } -// -// glBindBuffer(GL_ARRAY_BUFFER, 0); -// glBindVertexArray(0); -// -// _dataIsDirty = false; -// } -// -// if (_textureIsDirty) { -// LDEBUG("Reloading texture"); -// delete _texture; -// _texture = nullptr; -// if (_colorTexturePath.value() != "") { -// _texture = ghoul::opengl::loadTexture(absPath(_colorTexturePath)); -// if (_texture) { -// LDEBUG("Loaded texture from '" << absPath(_colorTexturePath) << "'"); -// _texture->uploadTexture(); -// } -// } -// _textureIsDirty = false; -// } -//} -// -//bool RenderableStars::loadData() { -// std::string _file = _speckFile; -// std::string cachedFile = ""; -// FileSys.cacheManager()->getCachedFile(_file, cachedFile, true); -// -// bool hasCachedFile = FileSys.fileExists(cachedFile); -// if (hasCachedFile) { -// LINFO("Cached file '" << cachedFile << "' used for Speck file '" << _file << "'"); -// -// bool success = loadCachedFile(cachedFile); -// if (success) -// return true; -// else -// FileSys.cacheManager()->removeCacheFile(_file); -// // Intentional fall-through to the 'else' computation to generate the cache -// // file for the next run -// } -// else { -// LINFO("Cache for Speck file '" << _file << "' not found"); -// } -// LINFO("Loading Speck file '" << _file << "'"); -// -// bool success = readSpeckFile(); -// if (!success) -// return false; -// -// LINFO("Saving cache"); -// success = saveCachedFile(cachedFile); -// -// return success; -//} -// -//bool RenderableStars::readSpeckFile() { -// std::string _file = _speckFile; -// std::ifstream file(_file); -// if (!file.good()) { -// LERROR("Failed to open Speck file '" << _file << "'"); -// return false; -// } -// -// _nValuesPerStar = 0; -// -// // The beginning of the speck file has a header that either contains comments -// // (signaled by a preceding '#') or information about the structure of the file -// // (signaled by the keywords 'datavar', 'texturevar', and 'texture') -// std::string line = ""; -// while (true) { -// std::ifstream::streampos position = file.tellg(); -// std::getline(file, line); -// -// if (line[0] == '#') -// continue; -// -// if (line.substr(0, 7) != "datavar" && -// line.substr(0, 10) != "texturevar" && -// line.substr(0, 7) != "texture") -// { -// // we read a line that doesn't belong to the header, so we have to jump back -// // before the beginning of the current line -// file.seekg(position); -// break; -// } -// -// if (line.substr(0, 7) == "datavar") { -// // datavar lines are structured as follows: -// // datavar # description -// // where # is the index of the data variable; so if we repeatedly overwrite -// // the 'nValues' variable with the latest index, we will end up with the total -// // number of values (+3 since X Y Z are not counted in the Speck file index) -// std::stringstream str(line); -// -// std::string dummy; -// str >> dummy; -// str >> _nValuesPerStar; -// _nValuesPerStar += 1; // We want the number, but the index is 0 based -// } -// } -// -// _nValuesPerStar += 3; // X Y Z are not counted in the Speck file indices -// -// do { -// std::vector values(_nValuesPerStar); -// -// std::getline(file, line); -// std::stringstream str(line); -// -// for (int i = 0; i < _nValuesPerStar; ++i) -// str >> values[i]; -// -// _fullData.insert(_fullData.end(), values.begin(), values.end()); -// } while (!file.eof()); -// -// return true; -//} -// -//bool RenderableStars::loadCachedFile(const std::string& file) { -// std::ifstream fileStream(file, std::ifstream::binary); -// if (fileStream.good()) { -// int8_t version = 0; -// fileStream.read(reinterpret_cast(&version), sizeof(int8_t)); -// if (version != CurrentCacheVersion) { -// LINFO("The format of the cached file has changed, deleted old cache"); -// fileStream.close(); -// FileSys.deleteFile(file); -// return false; -// } -// -// int32_t nValues = 0; -// fileStream.read(reinterpret_cast(&nValues), sizeof(int32_t)); -// fileStream.read(reinterpret_cast(&_nValuesPerStar), sizeof(int32_t)); -// -// _fullData.resize(nValues); -// fileStream.read(reinterpret_cast(&_fullData[0]), -// nValues * sizeof(_fullData[0])); -// -// bool success = fileStream.good(); -// return success; -// } -// else { -// LERROR("Error opening file '" << file << "' for loading cache file"); -// return false; -// } -//} -// -//bool RenderableStars::saveCachedFile(const std::string& file) const { -// std::ofstream fileStream(file, std::ofstream::binary); -// if (fileStream.good()) { -// fileStream.write(reinterpret_cast(&CurrentCacheVersion), -// sizeof(int8_t)); -// -// int32_t nValues = static_cast(_fullData.size()); -// if (nValues == 0) { -// LERROR("Error writing cache: No values were loaded"); -// return false; -// } -// fileStream.write(reinterpret_cast(&nValues), sizeof(int32_t)); -// -// int32_t nValuesPerStar = static_cast(_nValuesPerStar); -// fileStream.write(reinterpret_cast(&nValuesPerStar), sizeof(int32_t)); -// -// size_t nBytes = nValues * sizeof(_fullData[0]); -// fileStream.write(reinterpret_cast(&_fullData[0]), nBytes); -// -// bool success = fileStream.good(); -// return success; -// } -// else { -// LERROR("Error opening file '" << file << "' for save cache file"); -// return false; -// } -//} -// -//void RenderableStars::createDataSlice(ColorOption option) { -// _slicedData.clear(); -// for (size_t i = 0; i < _fullData.size(); i+=_nValuesPerStar) { -// psc position = PowerScaledCoordinate::CreatePowerScaledCoordinate( -// _fullData[i + 0], -// _fullData[i + 1], -// _fullData[i + 2] -// ); -// // Convert parsecs -> meter -// PowerScaledScalar parsecsToMetersFactor = PowerScaledScalar(0.308567758f, 17.f); -// position[0] *= parsecsToMetersFactor[0]; -// position[1] *= parsecsToMetersFactor[0]; -// position[2] *= parsecsToMetersFactor[0]; -// position[3] += parsecsToMetersFactor[1]; -// -// switch (option) { -// case ColorOption::Color: -// { -// union { -// ColorVBOLayout value; -// std::array data; -// } layout; -// -// layout.value.position = { { -// position[0], position[1], position[2], position[3] -// } }; -// -// layout.value.bvColor = _fullData[i + 3]; -// layout.value.luminance = _fullData[i + 4]; -// layout.value.absoluteMagnitude = _fullData[i + 5]; -// -// _slicedData.insert(_slicedData.end(), -// layout.data.begin(), -// layout.data.end()); -// -// break; -// } -// case ColorOption::Velocity: -// { -// union { -// VelocityVBOLayout value; -// std::array data; -// } layout; -// -// layout.value.position = { { -// position[0], position[1], position[2], position[3] -// } }; -// -// layout.value.bvColor = _fullData[i + 3]; -// layout.value.luminance = _fullData[i + 4]; -// layout.value.absoluteMagnitude = _fullData[i + 5]; -// -// layout.value.vx = _fullData[i + 12]; -// layout.value.vy = _fullData[i + 13]; -// layout.value.vz = _fullData[i + 14]; -// -// _slicedData.insert(_slicedData.end(), -// layout.data.begin(), -// layout.data.end()); -// break; -// } -// case ColorOption::Speed: -// { -// union { -// SpeedVBOLayout value; -// std::array data; -// } layout; -// -// layout.value.position = { { -// position[0], position[1], position[2], position[3] -// } }; -// -// layout.value.bvColor = _fullData[i + 3]; -// layout.value.luminance = _fullData[i + 4]; -// layout.value.absoluteMagnitude = _fullData[i + 5]; -// -// layout.value.speed = _fullData[i + 15]; -// -// _slicedData.insert(_slicedData.end(), -// layout.data.begin(), -// layout.data.end()); -// break; -// } -// } -// } -//} -// -//} // namespace openspace From 89890f66edd5039230891efcdc70b3510f864ed6 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 18:38:17 +0100 Subject: [PATCH 06/11] Use correct openspace-data --- openspace-data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openspace-data b/openspace-data index d098f15135..7abb15d06f 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit d098f15135a462fad3df6b846da05059111d28ec +Subproject commit 7abb15d06fe6e81a78a3cd0b4cda729a63786c04 From fe9db283104742b580a8581e07701e99bac08aa7 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 19:12:31 +0100 Subject: [PATCH 07/11] Finished with current version of ConstellationBounds --- .../stars/renderableconstellationbounds.h | 109 ++++++------------ openspace-data | 2 +- .../stars/renderableconstellationbounds.cpp | 83 ++++++++----- 3 files changed, 89 insertions(+), 105 deletions(-) diff --git a/include/openspace/rendering/stars/renderableconstellationbounds.h b/include/openspace/rendering/stars/renderableconstellationbounds.h index 7e955974fa..9e57b54618 100644 --- a/include/openspace/rendering/stars/renderableconstellationbounds.h +++ b/include/openspace/rendering/stars/renderableconstellationbounds.h @@ -27,13 +27,29 @@ #include #include - #include - #include namespace openspace { +/** + * This class renders the constellation bounds as defined in + * http://cdsarc.u-strasbg.fr/viz-bin/Cat?cat=VI%2F49. It contains the bounds on the + * celestial sky for the different constellations and is used to determine in which region + * of the sky a specific object is located. + * The bounds are drawn as lines on a sphere with variable radius, set by the + * _distance property. Currently, all constellation bounds are lines, which + * leads to artifacts if the radius is very small. + * Renderable configuration attributes: + * File [string] (required): The file that contains the bounds and the + * abbreviations for the different constellations + * ReferenceFrame [string]: The reference frame in which the points contained + * in the File are stored in. Defaults to J2000 + * + * @TODO Add a method to load (and access) a table translating from abbreviations to + * full names ---abock + * @TODO Make it possible to only show a subset of constellation bounds ---abock + */ class RenderableConstellationBounds : public Renderable { public: RenderableConstellationBounds(const ghoul::Dictionary& dictionary); @@ -47,28 +63,39 @@ public: void update(const UpdateData& data) override; private: + /// Stores the constellation bounds struct ConstellationBound { - std::string constellation; - int startIndex; - int nVertices; + std::string constellation; ///< The abbreviation of the constellation + size_t startIndex; ///< The index of the first vertex describing the bounds + size_t nVertices; ///< The number of vertices describing the bounds }; + /** + * Loads the file specified in _filename and fills the + * _constellationBounds variable, as well as the + * _vertexValues list. If this method fails, the content of either + * destination is undefined. + * \return true if the loading succeeded, false otherwise + */ bool loadFile(); - - std::string _filename; + std::string _filename; ///< The filename containing the constellation bounds ghoul::opengl::ProgramObject* _program; bool _programIsDirty; + /// The list of all loaded constellation bounds std::vector _constellationBounds; typedef std::array Vertex; - std::vector _vertexValues; + std::vector _vertexValues; ///< A list of all vertices of all bounds + /// The radius of the celestial sphere onto which the bounds are drawn properties::FloatProperty _distance; - std::string _originReferenceFrame; + std::string _originReferenceFrame; ///< Reference frame in which bounds are defined + + /// Used to translate between the origin reference frame and the target frame glm::dmat3 _stateMatrix; GLuint _vao; @@ -78,67 +105,3 @@ private: } // namespace openspace #endif // __RENDERABLECONSTELLATIONBOUNDS_H__ - -//#ifndef __RENDERABLESTARS_H__ -//#define __RENDERABLESTARS_H__ -// -//#include -//#include -//#include -// -//#include -//#include -// -//namespace openspace { -// -//class RenderableStars : public Renderable { -//public: -// RenderableStars(const ghoul::Dictionary& dictionary); -// -// bool initialize() override; -// bool deinitialize() override; -// -// bool isReady() const override; -// -// void render(const RenderData& data) override; -// void update(const UpdateData& data) override; -// -//private: -// enum ColorOption { -// Color = 0, -// Velocity = 1, -// Speed = 2 -// }; -// -// void createDataSlice(ColorOption option); -// -// bool loadData(); -// bool readSpeckFile(); -// bool loadCachedFile(const std::string& file); -// bool saveCachedFile(const std::string& file) const; -// -// properties::StringProperty _colorTexturePath; -// ghoul::opengl::Texture* _texture; -// bool _textureIsDirty; -// -// properties::OptionProperty _colorOption; -// bool _dataIsDirty; -// -// properties::FloatProperty _spriteSize; -// -// ghoul::opengl::ProgramObject* _program; -// bool _programIsDirty; -// -// std::string _speckFile; -// -// std::vector _slicedData; -// std::vector _fullData; -// int _nValuesPerStar; -// -// GLuint _vao; -// GLuint _vbo; -//}; -// -//} // namespace openspace -// -//#endif // __RENDERABLESTARS_H__ \ No newline at end of file diff --git a/openspace-data b/openspace-data index 7abb15d06f..20da238d5a 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit 7abb15d06fe6e81a78a3cd0b4cda729a63786c04 +Subproject commit 20da238d5a5d903918e8346bff40f1ae9ac5ea59 diff --git a/src/rendering/stars/renderableconstellationbounds.cpp b/src/rendering/stars/renderableconstellationbounds.cpp index d9cd904a65..6036bc3a34 100644 --- a/src/rendering/stars/renderableconstellationbounds.cpp +++ b/src/rendering/stars/renderableconstellationbounds.cpp @@ -24,7 +24,6 @@ #include -#include #include #include @@ -34,16 +33,17 @@ #include #include -#include namespace { const std::string _loggerCat = "RenderableConstellationBounds"; const std::string keyFile = "File"; - const std::string keyOriginalReferenceFrame = "OriginalReferenceFrame"; + const std::string keyReferenceFrame = "ReferenceFrame"; + + const std::string defaultReferenceFrame = "J2000"; float deg2rad(float deg) { - return (deg / 360.f) * 2.f * 3.1415926f; + return static_cast((deg / 360.f) * 2.f * M_PI); } float convertHrsToRadians(float rightAscension) { // 360 degrees / 24h = 15 degrees/h @@ -69,10 +69,9 @@ RenderableConstellationBounds::RenderableConstellationBounds( keyFile << "'"); } - success = dictionary.getValue(keyOriginalReferenceFrame, _originReferenceFrame); + success = dictionary.getValue(keyReferenceFrame, _originReferenceFrame); if (!success) { - LERROR("RenderableConstellationBounds did not contain a key '" << - keyOriginalReferenceFrame << "'"); + _originReferenceFrame = defaultReferenceFrame; } addProperty(_distance); @@ -148,7 +147,11 @@ void RenderableConstellationBounds::render(const RenderData& data) { glBindVertexArray(_vao); for (auto bound : _constellationBounds) - glDrawArrays(GL_LINE_STRIP, bound.startIndex , bound.nVertices); + glDrawArrays( + GL_LINE_STRIP, + static_cast(bound.startIndex), + static_cast(bound.nVertices) + ); glBindVertexArray(0); _program->deactivate(); } @@ -180,53 +183,71 @@ bool RenderableConstellationBounds::loadFile() { ConstellationBound currentBound; currentBound.constellation = ""; - std::string line; - int lineNumber = 1; + + std::string currentLine; + int currentLineNumber = 1; + + float ra; + float dec; + std::string constellationName; + SpiceDouble rectangularValues[3]; + + // Overview of the reading algorithm: + // We keep an active ConstellationBound (currentBound) and update it until we read + // a new constellation name, at which point the currentBound is stored away, a new, + // empty ConstellationBound is created and set at the currentBound while (file.good()) { - std::getline(file, line); - if (line.empty()) + std::getline(file, currentLine); + if (currentLine.empty()) continue; // @CHECK: Is this the best way of doing this? ---abock - float ra; - float dec; - std::string constellation; - std::stringstream s(line); + std::stringstream s(currentLine); s >> ra; s >> dec; - s >> constellation; + s >> constellationName; if (!s.good()) { // If this evaluates to true, the stream was not completely filled, which // means that the line was incomplete, so there was an error - LERROR("Error reading file '" << fileName << "' at line #" << lineNumber); + LERROR("Error reading file '" << fileName << "' at line #" << currentLineNumber); + break; } - if (constellation != currentBound.constellation) { - // @CHECK: Does this work? ---abock + // Did we arrive at a new constellation? + if (constellationName != currentBound.constellation) { + // Store how many vertices we read during the active time of the constellation currentBound.nVertices = (_vertexValues.size() - currentBound.startIndex); + // Store the constellation and start a new one _constellationBounds.push_back(currentBound); currentBound = ConstellationBound(); - currentBound.constellation = constellation; + currentBound.constellation = constellationName; currentBound.startIndex = _vertexValues.size(); } + // The file format stores the right ascension in hours, while SPICE expects them + // to be in radians ra = convertHrsToRadians(ra); + + // Likewise, the declination is stored in degrees and needs to be converted dec = deg2rad(dec); - SpiceDouble values[3]; - radrec_c(1.0, ra, dec, values); + // Convert the (right ascension, declination) to rectangular coordinates) + // The 1.0 is the distance of the celestial sphere, we will scale that in the + // render function + radrec_c(1.0, ra, dec, rectangularValues); - std::array v; - v[0] = values[0]; - v[1] = values[1]; - v[2] = values[2]; - - _vertexValues.push_back(v); - ++lineNumber; + // Add the new vertex to our list of vertices + _vertexValues.push_back({{ + static_cast(rectangularValues[0]), + static_cast(rectangularValues[1]), + static_cast(rectangularValues[2]) + }}); + ++currentLineNumber; } - // remove the first one + // Due to the way we read the file, the first (empty) constellation bounds will not + // contain any valid values. So we have to remove it _constellationBounds.erase(_constellationBounds.begin()); return true; From 612e48455de29beb665382f778108bc10b3c15a9 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 19:20:55 +0100 Subject: [PATCH 08/11] Update ghoul reference --- ext/ghoul | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/ghoul b/ext/ghoul index 4aae49c3e0..e041712349 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 4aae49c3e034313defdc407b15acfd89bcb2f9c5 +Subproject commit e0417123496d5888a066896142c1e7489c433f5f From 4ba81a5d6cd627923a4f394d62ac8a9869413c82 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 21:29:25 +0100 Subject: [PATCH 09/11] Add SelectionProperty --- include/openspace/properties/optionproperty.h | 2 +- .../openspace/properties/selectionproperty.h | 40 ++++++++++ src/properties/selectionproperty.cpp | 80 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 include/openspace/properties/selectionproperty.h create mode 100644 src/properties/selectionproperty.cpp diff --git a/include/openspace/properties/optionproperty.h b/include/openspace/properties/optionproperty.h index ae7f36125e..7cd3ca2443 100644 --- a/include/openspace/properties/optionproperty.h +++ b/include/openspace/properties/optionproperty.h @@ -31,7 +31,7 @@ namespace openspace { namespace properties { - + /** * The OptionProperty is a property that provides a number of predefined (using the * addOption method) options consisting of a description and a diff --git a/include/openspace/properties/selectionproperty.h b/include/openspace/properties/selectionproperty.h new file mode 100644 index 0000000000..c260043f74 --- /dev/null +++ b/include/openspace/properties/selectionproperty.h @@ -0,0 +1,40 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#ifndef __OPTIONPROPERTY_H__ +#define __OPTIONPROPERTY_H__ + +#include + +#include + +namespace openspace { +namespace properties { + +REGISTER_TEMPLATEPROPERTY_HEADER(SelectionProperty, std::vector); + +} // namespace properties +} // namespace openspace + +#endif // __STRINGPROPERTY_H__ \ No newline at end of file diff --git a/src/properties/selectionproperty.cpp b/src/properties/selectionproperty.cpp new file mode 100644 index 0000000000..d366b84b33 --- /dev/null +++ b/src/properties/selectionproperty.cpp @@ -0,0 +1,80 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014 * +* * +* Permission is hereby granted, free of charge, to any person obtaining a copy of this * +* software and associated documentation files (the "Software"), to deal in the Software * +* without restriction, including without limitation the rights to use, copy, modify, * +* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * +* permit persons to whom the Software is furnished to do so, subject to the following * +* conditions: * +* * +* The above copyright notice and this permission notice shall be included in all copies * +* or substantial portions of the Software. * +* * +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * +* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * +* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * +* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * +* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * +****************************************************************************************/ + +#include + +namespace { + const std::string _loggerCat = "SelectionProperty"; +} + +namespace openspace { +namespace properties { + +REGISTER_TEMPLATEPROPERTY_SOURCE(SelectionProperty, std::vector, std::vector(), + [](lua_State* state, bool& success) -> std::vector { + static const int KEY = -2; + static const int VAL = -1; + + std::vector result; + + if (!lua_istable(state, -1)) { + LERROR("Parameter passed to the property is not a table"); + success = false; + return result; + } + + lua_pushnil(state); + while (lua_next(state, -2) != 0) { + int valueType = lua_type(state, VAL); + + if (lua_isnumber(state, VAL)) { + int number = static_cast(lua_tonumber(state, VAL)); + result.push_back(number); + } + else { + success = false; + return std::vector(); + } + + lua_pop(state, 1); + } + + success = true; + return result; + }, + [](lua_State* state, const std::vector& value) -> bool { + //@NOTE Untested ---abock + lua_newtable(state); + for (int i = 0; i < value.size(); ++i) { + int v = value[i]; + lua_pushinteger(state, v); + lua_setfield(state, -2, std::to_string(i).c_str()); + } + return true; + }, LUA_TTABLE +); + + +} // namespace properties +} // namespace openspace From 08378888a6375d6751db0c917fd6c42dd7d59eaa Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 23:04:54 +0100 Subject: [PATCH 10/11] Fixed the SelectionProperty --- .../openspace/properties/selectionproperty.h | 42 ++++- src/properties/selectionproperty.cpp | 155 +++++++++++++----- 2 files changed, 156 insertions(+), 41 deletions(-) diff --git a/include/openspace/properties/selectionproperty.h b/include/openspace/properties/selectionproperty.h index c260043f74..59f2f6eb17 100644 --- a/include/openspace/properties/selectionproperty.h +++ b/include/openspace/properties/selectionproperty.h @@ -22,8 +22,8 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#ifndef __OPTIONPROPERTY_H__ -#define __OPTIONPROPERTY_H__ +#ifndef __SELECTIONPROPERTY_H__ +#define __SELECTIONPROPERTY_H__ #include @@ -32,9 +32,43 @@ namespace openspace { namespace properties { -REGISTER_TEMPLATEPROPERTY_HEADER(SelectionProperty, std::vector); +//REGISTER_TEMPLATEPROPERTY_HEADER(SelectionProperty, std::vector); + +class SelectionProperty : public TemplateProperty> { +public: + struct Option { + int value; + std::string description; + }; + + SelectionProperty(std::string identifier, std::string guiName); + + void addOption(Option option); + const std::vector