From 38b1c00d3dd9c209185a5bc1c4565021ac0f36b1 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 4 Dec 2014 18:05:13 +0100 Subject: [PATCH] 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