diff --git a/data/scene/grids/grids.mod b/data/scene/grids/grids.mod index 04a9b783bf..a9ec4af6a4 100644 --- a/data/scene/grids/grids.mod +++ b/data/scene/grids/grids.mod @@ -5,37 +5,34 @@ return { Parent = "Root", Renderable = { Type = "RenderableSphericalGrid", - GridType = "ECLIPJ2000", GridColor = { 0.75, 0.0, 0.0, 1.0}, - LineWidth = 2.0, + LineWidth = 0.75, GridMatrix = { -0.05487554, 0.4941095, -0.8676661 , 0.0, -0.9938214 , -0.1109906, -0.0003515167, 0.0, -0.09647644, 0.8622859, 0.4971472 , 0.0, 0.0 , 0.0 , 0.0 , 1.0 } } }, - { + { Name = "Equatorial Grid", Parent = "Root", Renderable = { Type = "RenderableSphericalGrid", - GridType = "ICRF", GridColor = { 0.0, 0.0, 0.75, 1.0}, - LineWidth = 2.0, + LineWidth = 0.75, GridMatrix = { -0.05487554, 0.4941095, -0.8676661, 0.0, -0.8734371 , -0.4448296, -0.1980764, 0.0, -0.483835 , 0.7469823, 0.4559838, 0.0, 0.0 , 0.0 , 0.0 , 1.0 } } }, - { + { Name = "Galactic Grid", Parent = "SolarSystem", Renderable = { Type = "RenderableSphericalGrid", - GridType = "GALACTIC", - LineWidth = 2.0, + LineWidth = 0.75, GridColor = { 0.0, 0.75, 0.75, 1.0} } - } + } } diff --git a/modules/base/rendering/renderablesphericalgrid.cpp b/modules/base/rendering/renderablesphericalgrid.cpp index 31d9894aa3..041ef3a45c 100644 --- a/modules/base/rendering/renderablesphericalgrid.cpp +++ b/modules/base/rendering/renderablesphericalgrid.cpp @@ -81,7 +81,7 @@ documentation::Documentation RenderableSphericalGrid::Documentation() { { GridMatrixInfo.identifier, new DoubleMatrix4x4Verifier, - Optional::No, + Optional::Yes, GridMatrixInfo.description }, { @@ -123,9 +123,10 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio glm::vec4(0.f), glm::vec4(1.f) ) - , _segments(SegmentsInfo, 36, 4, 10000) + , _segments(SegmentsInfo, 36, 4, 200) , _lineWidth(LineWidthInfo, 0.5f, 0.f, 20.f) , _radius(RadiusInfo, 1e20f, 1.f, 1e35f) + , _gridIsDirty(true) , _vaoID(0) , _vBufferID(0) , _iBufferID(0) @@ -137,7 +138,9 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio "RenderableSphericalGrid" ); - _gridMatrix = dictionary.value(GridMatrixInfo.identifier); + if (dictionary.hasKey(GridMatrixInfo.identifier)) { + _gridMatrix = dictionary.value(GridMatrixInfo.identifier); + } addProperty(_gridMatrix); if (dictionary.hasKey(GridColorInfo.identifier)) { @@ -149,7 +152,8 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio if (dictionary.hasKey(SegmentsInfo.identifier)) { _segments = static_cast(dictionary.value(SegmentsInfo.identifier)); } - //addProperty(_segments); + _segments.onChange([&]() { _gridIsDirty = true; }); + addProperty(_segments); if (dictionary.hasKey(LineWidthInfo.identifier)) { _lineWidth = static_cast( @@ -163,61 +167,8 @@ RenderableSphericalGrid::RenderableSphericalGrid(const ghoul::Dictionary& dictio dictionary.value(RadiusInfo.identifier) ); } - - _isize = int(6 * _segments * _segments); - _vsize = int((_segments + 1) * (_segments + 1)); - _varray.resize(_vsize); - _iarray.resize(_isize); - - int nr = 0; - const float fsegments = static_cast(_segments); - const float r = _radius; - - for (int nSegment = 0; nSegment <= _segments; ++nSegment) { - // define an extra vertex around the y-axis due to texture mapping - for (int j = 0; j <= _segments; j++) { - const float fi = static_cast(nSegment); - const float fj = static_cast(j); - - // inclination angle (north to south) - const float theta = fi * float(M_PI) / fsegments*2.f; // 0 -> PI - - // azimuth angle (east to west) - const float phi = fj * float(M_PI) * 2.0f / fsegments; // 0 -> 2*PI - - const float x = r * sin(phi) * sin(theta); // - const float y = r * cos(theta); // up - const float z = r * cos(phi) * sin(theta); // - - glm::vec3 normal = glm::vec3(x, y, z); - if (!(x == 0.f && y == 0.f && z == 0.f)) - normal = glm::normalize(normal); - - //const float t1 = fj / fsegments; - const float t2 = fi / fsegments; - - glm::vec4 tmp (x, y, z, 1); - glm::mat4 rot = glm::rotate(glm::mat4(1), static_cast(M_PI_2), glm::vec3(1, 0, 0)); - tmp = glm::vec4(_gridMatrix.value() * glm::dmat4(rot) * glm::dvec4(tmp)); - - for (int i = 0; i < 3; i++){ - _varray[nr].location[i] = tmp[i]; - } - ++nr; - } - } - nr = 0; - // define indices for all triangles - for (int i = 1; i <= _segments; ++i) { - for (int j = 0; j < _segments; ++j) { - const int t = _segments + 1; - _iarray[nr] = t * (i - 1) + j + 0; ++nr; - _iarray[nr] = t * (i + 0) + j + 0; ++nr; - _iarray[nr] = t * (i + 0) + j + 1; ++nr; - _iarray[nr] = t * (i - 1) + j + 1; ++nr; - _iarray[nr] = t * (i - 1) + j + 0; ++nr; - } - } + _radius.onChange([&]() { _gridIsDirty = true; }); + addProperty(_radius); } RenderableSphericalGrid::~RenderableSphericalGrid() {} @@ -242,34 +193,23 @@ bool RenderableSphericalGrid::deinitialize() { } bool RenderableSphericalGrid::initialize() { - bool completeSuccess = true; - _gridProgram = OsEng.renderEngine().buildRenderProgram( "GridProgram", "${MODULE_BASE}/shaders/grid_vs.glsl", "${MODULE_BASE}/shaders/grid_fs.glsl" ); - // Initialize and upload to graphics card glGenVertexArrays(1, &_vaoID); glGenBuffers(1, &_vBufferID); glGenBuffers(1, &_iBufferID); - // First VAO setup glBindVertexArray(_vaoID); glBindBuffer(GL_ARRAY_BUFFER, _vBufferID); - glBufferData(GL_ARRAY_BUFFER, _vsize * sizeof(Vertex), _varray.data(), GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), - reinterpret_cast(offsetof(Vertex, location))); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iBufferID); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, _isize * sizeof(int), _iarray.data(), GL_STATIC_DRAW); - + glEnableVertexAttribArray(0); glBindVertexArray(0); - return completeSuccess; + return true; } void RenderableSphericalGrid::render(const RenderData& data, RendererTasks&){ @@ -300,4 +240,73 @@ void RenderableSphericalGrid::render(const RenderData& data, RendererTasks&){ _gridProgram->deactivate(); } +void RenderableSphericalGrid::update(const UpdateData& data) { + if (_gridIsDirty) { + _isize = int(6 * _segments * _segments); + _vsize = int((_segments + 1) * (_segments + 1)); + _varray.resize(_vsize); + _iarray.resize(_isize); + + int nr = 0; + const float fsegments = static_cast(_segments); + const float r = _radius; + + for (int nSegment = 0; nSegment <= _segments; ++nSegment) { + // define an extra vertex around the y-axis due to texture mapping + for (int j = 0; j <= _segments; j++) { + const float fi = static_cast(nSegment); + const float fj = static_cast(j); + + // inclination angle (north to south) + const float theta = fi * float(M_PI) / fsegments*2.f; // 0 -> PI + + // azimuth angle (east to west) + const float phi = fj * float(M_PI) * 2.0f / fsegments; // 0 -> 2*PI + + const float x = r * sin(phi) * sin(theta); // + const float y = r * cos(theta); // up + const float z = r * cos(phi) * sin(theta); // + + glm::vec3 normal = glm::vec3(x, y, z); + if (!(x == 0.f && y == 0.f && z == 0.f)) + normal = glm::normalize(normal); + + //const float t1 = fj / fsegments; + const float t2 = fi / fsegments; + + glm::vec4 tmp(x, y, z, 1); + glm::mat4 rot = glm::rotate(glm::mat4(1), static_cast(M_PI_2), glm::vec3(1, 0, 0)); + tmp = glm::vec4(_gridMatrix.value() * glm::dmat4(rot) * glm::dvec4(tmp)); + + for (int i = 0; i < 3; i++) { + _varray[nr].location[i] = tmp[i]; + } + ++nr; + } + } + nr = 0; + // define indices for all triangles + for (int i = 1; i <= _segments; ++i) { + for (int j = 0; j < _segments; ++j) { + const int t = _segments + 1; + _iarray[nr] = t * (i - 1) + j + 0; ++nr; + _iarray[nr] = t * (i + 0) + j + 0; ++nr; + _iarray[nr] = t * (i + 0) + j + 1; ++nr; + _iarray[nr] = t * (i - 1) + j + 1; ++nr; + _iarray[nr] = t * (i - 1) + j + 0; ++nr; + } + } + + glBindVertexArray(_vaoID); + glBindBuffer(GL_ARRAY_BUFFER, _vBufferID); + glBufferData(GL_ARRAY_BUFFER, _vsize * sizeof(Vertex), _varray.data(), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), + reinterpret_cast(offsetof(Vertex, location))); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iBufferID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, _isize * sizeof(int), _iarray.data(), GL_STATIC_DRAW); + } +} + } // namespace openspace diff --git a/modules/base/rendering/renderablesphericalgrid.h b/modules/base/rendering/renderablesphericalgrid.h index c36fae8a78..9d4c0ef789 100644 --- a/modules/base/rendering/renderablesphericalgrid.h +++ b/modules/base/rendering/renderablesphericalgrid.h @@ -54,6 +54,7 @@ public: bool isReady() const override; void render(const RenderData& data, RendererTasks& rendererTask) override; + void update(const UpdateData& data); static documentation::Documentation Documentation(); @@ -70,6 +71,8 @@ protected: properties::FloatProperty _lineWidth; properties::FloatProperty _radius; + bool _gridIsDirty; + GLuint _vaoID; GLuint _vBufferID; GLuint _iBufferID;