From c7bfe0b2c04ea4c0a319bb5bf400943f8e8f6b99 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 21 Apr 2016 21:46:49 -0400 Subject: [PATCH] Changed structure for grid classes. --- modules/globebrowsing/CMakeLists.txt | 10 +- modules/globebrowsing/rendering/basicgrid.cpp | 175 ++++++++++++++++++ modules/globebrowsing/rendering/basicgrid.h | 63 +++++++ .../globebrowsing/rendering/chunklodglobe.cpp | 6 +- .../globebrowsing/rendering/clipmapglobe.cpp | 10 +- .../globebrowsing/rendering/clipmapglobe.h | 2 +- .../{clipmapgeometry.cpp => clipmapgrid.cpp} | 82 ++++---- .../{clipmapgeometry.h => clipmapgrid.h} | 42 ++--- modules/globebrowsing/rendering/globemesh.cpp | 2 +- modules/globebrowsing/rendering/globemesh.h | 4 +- modules/globebrowsing/rendering/grid.cpp | 55 ++++++ .../rendering/{gridgeometry.h => grid.h} | 51 ++--- .../globebrowsing/rendering/gridgeometry.cpp | 154 --------------- .../globebrowsing/rendering/patchrenderer.cpp | 20 +- .../globebrowsing/rendering/patchrenderer.h | 10 +- 15 files changed, 408 insertions(+), 278 deletions(-) create mode 100644 modules/globebrowsing/rendering/basicgrid.cpp create mode 100644 modules/globebrowsing/rendering/basicgrid.h rename modules/globebrowsing/rendering/{clipmapgeometry.cpp => clipmapgrid.cpp} (83%) rename modules/globebrowsing/rendering/{clipmapgeometry.h => clipmapgrid.h} (65%) create mode 100644 modules/globebrowsing/rendering/grid.cpp rename modules/globebrowsing/rendering/{gridgeometry.h => grid.h} (67%) delete mode 100644 modules/globebrowsing/rendering/gridgeometry.cpp diff --git a/modules/globebrowsing/CMakeLists.txt b/modules/globebrowsing/CMakeLists.txt index 5b3eed88a9..59b0f4db1e 100644 --- a/modules/globebrowsing/CMakeLists.txt +++ b/modules/globebrowsing/CMakeLists.txt @@ -31,8 +31,9 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderableglobe.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/distanceswitch.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/geometry.h - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/gridgeometry.h - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapgeometry.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/grid.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/basicgrid.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapgrid.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/globemesh.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/patchrenderer.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapglobe.h @@ -53,8 +54,9 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/renderableglobe.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/distanceswitch.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/geometry.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/gridgeometry.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapgeometry.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/grid.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/basicgrid.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapgrid.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/globemesh.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/patchrenderer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmapglobe.cpp diff --git a/modules/globebrowsing/rendering/basicgrid.cpp b/modules/globebrowsing/rendering/basicgrid.cpp new file mode 100644 index 0000000000..ffe95c6fb2 --- /dev/null +++ b/modules/globebrowsing/rendering/basicgrid.cpp @@ -0,0 +1,175 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 = "BasicGrid"; +} + +namespace openspace { + +BasicGrid::BasicGrid( + unsigned int xRes, + unsigned int yRes, + Geometry::Positions usePositions, + Geometry::TextureCoordinates useTextureCoordinates, + Geometry::Normals useNormals) + : Grid( + xRes, + yRes, + usePositions, + useTextureCoordinates, + useNormals) +{ + _geometry = std::unique_ptr(new Geometry( + CreateElements(xRes, yRes), + usePositions, + useTextureCoordinates, + useNormals)); + + if (usePositions == Geometry::Positions::Yes) { + _geometry->setVertexPositions(CreatePositions(_xRes, _yRes)); + } + if (useTextureCoordinates == Geometry::TextureCoordinates::Yes) { + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(_xRes, _yRes)); + } + if (useNormals == Geometry::Normals::Yes) { + _geometry->setVertexNormals(CreateNormals(_xRes, _yRes)); + } +} + +BasicGrid::~BasicGrid() +{ + +} + +int BasicGrid::xResolution() const { + return _xRes; +} + +int BasicGrid::yResolution() const { + return _yRes; +} + +void BasicGrid::validate(int xRes, int yRes) { + ghoul_assert(xRes > 0 && yRes > 0, + "Resolution must be at least 1x1. (" << xRes << ", " << yRes << ")"); +} + +inline size_t BasicGrid::numElements(int xRes, int yRes){ + return 3 * 2 * (xRes - 1)*(yRes - 1); +} + +inline size_t BasicGrid::numVertices(int xRes, int yRes) { + return xRes * yRes; +} + +std::vector BasicGrid::CreateElements(int xRes, int yRes) { + validate(xRes, yRes); + + std::vector elements; + elements.reserve(numElements(xRes, yRes)); + for (unsigned int y = 0; y < yRes-1; y++) { + for (unsigned int x = 0; x < xRes-1; x++) { + + // x v01---v11 x .. + // | / | + // x v00---v10 x .. + // + // x x x x .. + // : : : : + + GLuint v00 = (y + 0) * xRes + x + 0; + GLuint v10 = (y + 0) * xRes + x + 1; + GLuint v01 = (y + 1) * xRes + x + 0; + GLuint v11 = (y + 1) * xRes + x + 1; + + // add upper triangle + elements.push_back(v00); + elements.push_back(v10); + elements.push_back(v11); + + // add lower triangle + elements.push_back(v00); + elements.push_back(v11); + elements.push_back(v01); + } + } + + return elements; +} + +std::vector BasicGrid::CreatePositions( + int xRes, + int yRes) +{ + validate(xRes, yRes); + std::vector positions; + positions.reserve(numVertices(xRes, yRes)); + + // Copy from 2d texture coordinates and use as template to create positions + std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); + for (unsigned int i = 0; i < templateTextureCoords.size(); i++) + { + positions.push_back(glm::vec4( + templateTextureCoords[i], + 0.0f, + 1.0f + )); + } + return positions; +} + +std::vector BasicGrid::CreateTextureCoordinates(int xRes, int yRes){ + validate(xRes, yRes); + std::vector textureCoordinates; + textureCoordinates.reserve(numVertices(xRes, yRes)); + + for (unsigned int y = 0; y < yRes; y++) { + for (unsigned int x = 0; x < xRes; x++) { + textureCoordinates.push_back(glm::vec2( + static_cast(x) / static_cast(xRes - 1), + static_cast(y) / static_cast(yRes - 1) + )); + } + } + return textureCoordinates; +} + +std::vector BasicGrid::CreateNormals(int xRes, int yRes) { + validate(xRes, yRes); + std::vector normals; + normals.reserve(numVertices(xRes, yRes)); + + for (unsigned int y = 0; y < yRes; y++) { + for (unsigned int x = 0; x < xRes; x++) { + normals.push_back(glm::vec3(0, 0, 1)); + } + } + + return normals; +} + +}// namespace openspace \ No newline at end of file diff --git a/modules/globebrowsing/rendering/basicgrid.h b/modules/globebrowsing/rendering/basicgrid.h new file mode 100644 index 0000000000..1ff22010de --- /dev/null +++ b/modules/globebrowsing/rendering/basicgrid.h @@ -0,0 +1,63 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 __BASICGRIDGEOMETRY_H__ +#define __BASICGRIDGEOMETRY_H__ + +#include + +#include + +#include + +namespace openspace { + +class BasicGrid : public Grid +{ +public: + BasicGrid( + unsigned int xRes, + unsigned int yRes, + Geometry::Positions usePositions, + Geometry::TextureCoordinates useTextureCoordinates, + Geometry::Normals useNormals); + ~BasicGrid(); + + virtual int xResolution() const; + virtual int yResolution() const; + +protected: + virtual std::vector CreateElements( int xRes, int yRes); + virtual std::vector CreatePositions( int xRes, int yRes); + virtual std::vector CreateTextureCoordinates( int xRes, int yRes); + virtual std::vector CreateNormals( int xRes, int yRes); + + void validate(int xRes, int yRes); + + inline size_t numElements(int xRes, int yRes); + inline size_t numVertices(int xRes, int yRes); +}; +} // namespace openspace +#endif // __BASICGRIDGEOMETRY_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/chunklodglobe.cpp b/modules/globebrowsing/rendering/chunklodglobe.cpp index afd0018dcf..e63a3c494c 100644 --- a/modules/globebrowsing/rendering/chunklodglobe.cpp +++ b/modules/globebrowsing/rendering/chunklodglobe.cpp @@ -24,6 +24,8 @@ #include +#include + // open space includes #include #include @@ -79,7 +81,9 @@ namespace openspace { // --------- // init Renderer - auto geometry = std::shared_ptr(new GridGeometry(10, 10, + auto geometry = std::shared_ptr(new BasicGrid( + 10, + 10, Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No)); diff --git a/modules/globebrowsing/rendering/clipmapglobe.cpp b/modules/globebrowsing/rendering/clipmapglobe.cpp index f740bad7ea..5b87593773 100644 --- a/modules/globebrowsing/rendering/clipmapglobe.cpp +++ b/modules/globebrowsing/rendering/clipmapglobe.cpp @@ -28,7 +28,7 @@ #include -#include +#include // open space includes #include @@ -73,13 +73,7 @@ namespace openspace { // init Renderer auto patchRenderer = new ClipMapPatchRenderer(shared_ptr(new ClipMapGeometry(32))); _patchRenderer.reset(patchRenderer); - auto smallestPatchRenderer = new ClipMapPatchRenderer(shared_ptr( - new GridGeometry( - 32, - 32, - Geometry::Positions::No, - Geometry::TextureCoordinates::Yes, - Geometry::Normals::No))); + auto smallestPatchRenderer = new ClipMapPatchRenderer(shared_ptr(new ClipMapGeometry(32))); _smallestPatchRenderer.reset(smallestPatchRenderer); } diff --git a/modules/globebrowsing/rendering/clipmapglobe.h b/modules/globebrowsing/rendering/clipmapglobe.h index 6fe483c457..093aefa256 100644 --- a/modules/globebrowsing/rendering/clipmapglobe.h +++ b/modules/globebrowsing/rendering/clipmapglobe.h @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include #include diff --git a/modules/globebrowsing/rendering/clipmapgeometry.cpp b/modules/globebrowsing/rendering/clipmapgrid.cpp similarity index 83% rename from modules/globebrowsing/rendering/clipmapgeometry.cpp rename to modules/globebrowsing/rendering/clipmapgrid.cpp index a33a1fd2f3..944f514cd6 100644 --- a/modules/globebrowsing/rendering/clipmapgeometry.cpp +++ b/modules/globebrowsing/rendering/clipmapgrid.cpp @@ -22,33 +22,26 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#include +#include #include namespace { - const std::string _loggerCat = "ClipMapGeometry"; + const std::string _loggerCat = "ClipMapGrid"; } namespace openspace { -ClipMapGeometry::ClipMapGeometry( - unsigned int resolution, - Positions usePositions, - TextureCoordinates useTextures, - Normals useNormals) - : Geometry(CreateElements(resolution), usePositions, useTextures, useNormals) - , _resolution(resolution) +ClipMapGeometry::ClipMapGeometry(unsigned int resolution) + : Grid(resolution, resolution, Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No) { - if(_useVertexPositions){ - setVertexPositions(CreatePositions(_resolution)); - } - if (_useTextureCoordinates) { - setVertexTextureCoordinates(CreateTextureCoordinates(_resolution)); - } - if (_useVertexNormals) { - setVertexNormals(CreateNormals(_resolution)); - } + _geometry = std::unique_ptr(new Geometry( + CreateElements(resolution, resolution), + Geometry::Positions::No, + Geometry::TextureCoordinates::Yes, + Geometry::Normals::No)); + + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(resolution, resolution)); } ClipMapGeometry::~ClipMapGeometry() @@ -56,38 +49,46 @@ ClipMapGeometry::~ClipMapGeometry() } -const unsigned int ClipMapGeometry::resolution() const { - return _resolution; +int ClipMapGeometry::xResolution() const { + return resolution(); } -size_t ClipMapGeometry::numElements(unsigned int resolution) +int ClipMapGeometry::yResolution() const { + return resolution(); +} + +int ClipMapGeometry::resolution() const { + return _xRes; +} + +size_t ClipMapGeometry::numElements(int resolution) { int numElementsInTotalSquare = 6 * (resolution + 1) * (resolution + 1); int numElementsInHole = 6 * (resolution / 4 * resolution / 4); return numElementsInTotalSquare - numElementsInHole; } -size_t ClipMapGeometry::numVerticesBottom(unsigned int resolution) +size_t ClipMapGeometry::numVerticesBottom(int resolution) { return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); } -size_t ClipMapGeometry::numVerticesLeft(unsigned int resolution) +size_t ClipMapGeometry::numVerticesLeft(int resolution) { return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); } -size_t ClipMapGeometry::numVerticesRight(unsigned int resolution) +size_t ClipMapGeometry::numVerticesRight(int resolution) { return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); } -size_t ClipMapGeometry::numVerticesTop(unsigned int resolution) +size_t ClipMapGeometry::numVerticesTop(int resolution) { return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); } -size_t ClipMapGeometry::numVertices(unsigned int resolution) +size_t ClipMapGeometry::numVertices(int resolution) { return numVerticesBottom(resolution) + numVerticesLeft(resolution) + @@ -95,15 +96,21 @@ size_t ClipMapGeometry::numVertices(unsigned int resolution) numVerticesTop(resolution); } -void ClipMapGeometry::validate(unsigned int resolution) { +void ClipMapGeometry::validate(int xRes, int yRes) { + + ghoul_assert(xRes == yRes, + "Resolution must be equal in x and in y. "); + int resolution = xRes; ghoul_assert(resolution >= 8, "Resolution must be at least 8. (" << resolution << ")"); ghoul_assert(resolution == pow(2, int(log2(resolution))), "Resolution must be a power of 2. (" << resolution << ")"); } -std::vector ClipMapGeometry::CreateElements(unsigned int resolution) { - validate(resolution); +std::vector ClipMapGeometry::CreateElements(int xRes, int yRes) { + int hej = 0; + validate(xRes, yRes); + int resolution = xRes; std::vector elements; elements.reserve(numElements(resolution)); @@ -208,12 +215,13 @@ std::vector ClipMapGeometry::CreateElements(unsigned int resolution) { return elements; } -std::vector ClipMapGeometry::CreatePositions(unsigned int resolution) +std::vector ClipMapGeometry::CreatePositions(int xRes, int yRes) { - validate(resolution); + validate(xRes, yRes); + int resolution = xRes; std::vector positions; positions.reserve(numVertices(resolution)); - std::vector templateTextureCoords = CreateTextureCoordinates(resolution); + std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); // Copy from 2d texture coordinates and use as template to create positions for (unsigned int i = 0; i < templateTextureCoords.size(); i++) { @@ -229,8 +237,9 @@ std::vector ClipMapGeometry::CreatePositions(unsigned int resolution) } -std::vector ClipMapGeometry::CreateTextureCoordinates(unsigned int resolution){ - validate(resolution); +std::vector ClipMapGeometry::CreateTextureCoordinates(int xRes, int yRes){ + validate(xRes, yRes); + int resolution = xRes; std::vector textureCoordinates; textureCoordinates.reserve(numVertices(resolution)); @@ -275,8 +284,9 @@ std::vector ClipMapGeometry::CreateTextureCoordinates(unsigned int re return textureCoordinates; } -std::vector ClipMapGeometry::CreateNormals(unsigned int resolution) { - validate(resolution); +std::vector ClipMapGeometry::CreateNormals(int xRes, int yRes) { + validate(xRes, yRes); + int resolution = xRes; std::vector normals; normals.reserve(numVertices(resolution)); diff --git a/modules/globebrowsing/rendering/clipmapgeometry.h b/modules/globebrowsing/rendering/clipmapgrid.h similarity index 65% rename from modules/globebrowsing/rendering/clipmapgeometry.h rename to modules/globebrowsing/rendering/clipmapgrid.h index 056af90f04..ed25505484 100644 --- a/modules/globebrowsing/rendering/clipmapgeometry.h +++ b/modules/globebrowsing/rendering/clipmapgrid.h @@ -25,46 +25,40 @@ #ifndef __CLIPMAPGEOMETRY_H__ #define __CLIPMAPGEOMETRY_H__ -#include +#include #include #include namespace openspace { -class ClipMapGeometry : public Geometry +class ClipMapGeometry : public Grid { public: - ClipMapGeometry( - unsigned int resolution, - Positions usePositions = Positions::No, - TextureCoordinates useTextures = TextureCoordinates::Yes, - Normals useNormals = Normals::No - ); + ClipMapGeometry(unsigned int resolution); ~ClipMapGeometry(); - const unsigned int resolution() const; + virtual int xResolution() const; + virtual int yResolution() const; + int resolution() const; - static size_t numVerticesBottom(unsigned int resolution); - static size_t numVerticesLeft(unsigned int resolution); - static size_t numVerticesRight(unsigned int resolution); - static size_t numVerticesTop(unsigned int resolution); +protected: + virtual std::vector CreateElements( int xRes, int yRes); + virtual std::vector CreatePositions( int xRes, int yRes); + virtual std::vector CreateTextureCoordinates( int xRes, int yRes); + virtual std::vector CreateNormals( int xRes, int yRes); - static size_t numElements(unsigned int resolution); - static size_t numVertices(unsigned int resolution); private: - static std::vector CreateElements(unsigned int resoluion); - static std::vector CreatePositions(unsigned int resolution); - static std::vector CreateTextureCoordinates(unsigned int resolution); - static std::vector CreateNormals(unsigned int resolution); + void validate(int xRes, int yRes); - static void validate(unsigned int resolution); + static size_t numVerticesBottom(int resolution); + static size_t numVerticesLeft(int resolution); + static size_t numVerticesRight(int resolution); + static size_t numVerticesTop(int resolution); - // _resolution defines how many grid squares the geometry has in one direction. - // In its uncontracted state, the clipmap geometry will have two extra grid squares - // in each direction. - unsigned int _resolution; + static size_t numElements(int resolution); + static size_t numVertices(int resolution); }; } // namespace openspace #endif // __CLIPMAPGEOMETRY_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/globemesh.cpp b/modules/globebrowsing/rendering/globemesh.cpp index 6f68d56796..ee70305d99 100644 --- a/modules/globebrowsing/rendering/globemesh.cpp +++ b/modules/globebrowsing/rendering/globemesh.cpp @@ -134,7 +134,7 @@ namespace openspace { //glCullFace(GL_BACK); // render - _grid.drawUsingActiveProgram(); + _grid.geometry().drawUsingActiveProgram(); // disable shader _programObject->deactivate(); diff --git a/modules/globebrowsing/rendering/globemesh.h b/modules/globebrowsing/rendering/globemesh.h index bf435a2b7a..ebe36625b7 100644 --- a/modules/globebrowsing/rendering/globemesh.h +++ b/modules/globebrowsing/rendering/globemesh.h @@ -32,7 +32,7 @@ #include #include -#include +#include #include namespace ghoul { @@ -59,7 +59,7 @@ namespace openspace { std::unique_ptr _programObject; //std::unique_ptr _testGeometry; - GridGeometry _grid; + BasicGrid _grid; properties::IntProperty _rotation; diff --git a/modules/globebrowsing/rendering/grid.cpp b/modules/globebrowsing/rendering/grid.cpp new file mode 100644 index 0000000000..fcf82baae7 --- /dev/null +++ b/modules/globebrowsing/rendering/grid.cpp @@ -0,0 +1,55 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 = "Grid"; +} + +namespace openspace { + + Grid::Grid( + int xRes, + int yRes, + Geometry::Positions usePositions, + Geometry::TextureCoordinates useTextures, + Geometry::Normals useNormals) + : _xRes(xRes) + , _yRes(yRes) +{ + +} + +Grid::~Grid() +{ + +} + +Geometry& Grid::geometry() +{ + return *_geometry; +} + +}// namespace openspace \ No newline at end of file diff --git a/modules/globebrowsing/rendering/gridgeometry.h b/modules/globebrowsing/rendering/grid.h similarity index 67% rename from modules/globebrowsing/rendering/gridgeometry.h rename to modules/globebrowsing/rendering/grid.h index ed2e0cc669..2942faf9fb 100644 --- a/modules/globebrowsing/rendering/gridgeometry.h +++ b/modules/globebrowsing/rendering/grid.h @@ -22,7 +22,6 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ - #ifndef __GRIDGEOMETRY_H__ #define __GRIDGEOMETRY_H__ @@ -35,44 +34,32 @@ namespace openspace { -class GridGeometry : public Geometry +class Grid { public: - GridGeometry( - unsigned int xRes, - unsigned int yRes, - Positions usePositions = Positions::No, - TextureCoordinates useTextures = TextureCoordinates::No, - Normals useNormals = Normals::No - ); + Grid( + int xRes, + int yRes, + Geometry::Positions usePositions = Geometry::Positions::No, + Geometry::TextureCoordinates useTextures = Geometry::TextureCoordinates::No, + Geometry::Normals useNormals = Geometry::Normals::No); + ~Grid(); - ~GridGeometry(); + Geometry& geometry(); - inline const unsigned int xResolution() const; - inline const unsigned int yResolution() const; + virtual int xResolution() const = 0; + virtual int yResolution() const = 0; - inline static size_t numElements(unsigned int xRes, unsigned int yRes); - static size_t numVertices(unsigned int xRes, unsigned int yRes); +protected: + virtual std::vector CreateElements( int xRes, int yRes) = 0; + virtual std::vector CreatePositions( int xRes, int yRes) = 0; + virtual std::vector CreateTextureCoordinates( int xRes, int yRes) = 0; + virtual std::vector CreateNormals( int xRes, int yRes) = 0; -private: - static std::vector CreateElements(unsigned int xRes, unsigned int yRes); - static std::vector CreatePositions( - unsigned int xRes, - unsigned int yRes, - float xSize = 1.0f, - float ySize = 1.0f, - float xOffset = 0.0f, - float yOffset = 0.0f); - static std::vector CreateTextureCoordinates( - unsigned int xRes, - unsigned int yRes); - static std::vector CreateNormals(unsigned int xRes, unsigned int yRes); + std::unique_ptr _geometry; - inline static void validate(unsigned int xRes, unsigned int yRes); - inline void validateIndices(unsigned int x, unsigned int y); - - unsigned int _xRes; - unsigned int _yRes; + const int _xRes; + const int _yRes; }; } // namespace openspace #endif // __GRIDGEOMETRY_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/gridgeometry.cpp b/modules/globebrowsing/rendering/gridgeometry.cpp deleted file mode 100644 index 5b065b32b5..0000000000 --- a/modules/globebrowsing/rendering/gridgeometry.cpp +++ /dev/null @@ -1,154 +0,0 @@ -#include - - - -namespace { - const std::string _loggerCat = "GridGeometry"; -} - -namespace openspace { - -GridGeometry::GridGeometry( - unsigned int xRes, - unsigned int yRes, - Positions usePositions, - TextureCoordinates useTextures, - Normals useNormals) - : Geometry(CreateElements(xRes, yRes), usePositions, useTextures, useNormals) -{ - if(_useVertexPositions){ - setVertexPositions(CreatePositions(xRes, yRes)); - } - if (_useTextureCoordinates) { - setVertexTextureCoordinates(CreateTextureCoordinates(xRes, yRes)); - } - if (_useVertexNormals) { - setVertexNormals(CreateNormals(xRes, yRes)); - } - _xRes = xRes; - _yRes = yRes; -} - -GridGeometry::~GridGeometry() -{ - -} - -const unsigned int GridGeometry::xResolution() const { - return _xRes; -} - -const unsigned int GridGeometry::yResolution() const { - return _yRes; -} - -void GridGeometry::validate(unsigned int xRes, unsigned int yRes) { - ghoul_assert(xRes > 0 && yRes > 0, - "Resolution must be at least 1x1. (" << xRes << ", " << yRes << ")"); -} - -void GridGeometry::validateIndices(unsigned int x, unsigned int y) { - validate(x, y); - ghoul_assert(x < _xRes, "x index is outside range: x = " << x << " xRes = " << _xRes); - ghoul_assert(y < _yRes, "y index is outside range: y = " << y << " yRes = " << _yRes); -} - -inline size_t GridGeometry::numElements(unsigned int xRes, unsigned int yRes){ - return 3 * 2 * (xRes - 1)*(yRes - 1); -} - -inline size_t GridGeometry::numVertices(unsigned int xRes, unsigned int yRes) { - return xRes * yRes; -} - -std::vector GridGeometry::CreateElements(unsigned int xRes, unsigned int yRes) { - validate(xRes, yRes); - - std::vector elements; - elements.reserve(numElements(xRes, yRes)); - for (unsigned int y = 0; y < yRes-1; y++) { - for (unsigned int x = 0; x < xRes-1; x++) { - - // x v01---v11 x .. - // | / | - // x v00---v10 x .. - // - // x x x x .. - // : : : : - - GLuint v00 = (y + 0) * xRes + x + 0; - GLuint v10 = (y + 0) * xRes + x + 1; - GLuint v01 = (y + 1) * xRes + x + 0; - GLuint v11 = (y + 1) * xRes + x + 1; - - // add upper triangle - elements.push_back(v00); - elements.push_back(v10); - elements.push_back(v11); - - // add lower triangle - elements.push_back(v00); - elements.push_back(v11); - elements.push_back(v01); - } - } - - return elements; -} - -std::vector GridGeometry::CreatePositions( - unsigned int xRes, - unsigned int yRes, - float xSize, - float ySize, - float xOffset, - float yOffset) -{ - validate(xRes, yRes); - std::vector positions; - positions.reserve(numVertices(xRes, yRes)); - - // Copy from 2d texture coordinates and use as template to create positions - std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); - for (unsigned int i = 0; i < templateTextureCoords.size(); i++) - { - positions.push_back(glm::vec4( - templateTextureCoords[i], - 0.0f, - 1.0f - )); - } - return positions; -} - -std::vector GridGeometry::CreateTextureCoordinates(unsigned int xRes, unsigned int yRes){ - validate(xRes, yRes); - std::vector textureCoordinates; - textureCoordinates.reserve(numVertices(xRes, yRes)); - - for (unsigned int y = 0; y < yRes; y++) { - for (unsigned int x = 0; x < xRes; x++) { - textureCoordinates.push_back(glm::vec2( - static_cast(x) / static_cast(xRes - 1), - static_cast(y) / static_cast(yRes - 1) - )); - } - } - return textureCoordinates; -} - -std::vector GridGeometry::CreateNormals(unsigned int xRes, unsigned int yRes) { - validate(xRes, yRes); - std::vector normals; - normals.reserve(numVertices(xRes, yRes)); - - for (unsigned int y = 0; y < yRes; y++) { - for (unsigned int x = 0; x < xRes; x++) { - normals.push_back(glm::vec3(0, 0, 1)); - } - } - - return normals; -} - -}// namespace openspace \ No newline at end of file diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 25236bc5cd..c2d0cddb5c 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -24,7 +24,7 @@ #include -#include +#include // open space includes #include @@ -41,7 +41,7 @@ #include namespace { - const std::string _loggerCat = "LatLonPatch"; + const std::string _loggerCat = "PatchRenderer"; const std::string keyFrame = "Frame"; const std::string keyGeometry = "Geometry"; @@ -55,8 +55,8 @@ namespace openspace { ////////////////////////////////////////////////////////////////////////////////////// // PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// - PatchRenderer::PatchRenderer(shared_ptr geometry) - : _geometry(geometry) + PatchRenderer::PatchRenderer(shared_ptr geometry) + : _grid(geometry) , _tileSet(LatLon(M_PI, M_PI * 2), LatLon(M_PI / 2, - M_PI), 0) { @@ -75,8 +75,8 @@ namespace openspace { ////////////////////////////////////////////////////////////////////////////////////// // LATLON PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// - LatLonPatchRenderer::LatLonPatchRenderer(shared_ptr geometry) - : PatchRenderer(geometry) + LatLonPatchRenderer::LatLonPatchRenderer(shared_ptr grid) + : PatchRenderer(grid) { _programObject = OsEng.renderEngine().buildRenderProgram( "LatLonSphereMappingProgram", @@ -143,7 +143,7 @@ namespace openspace { glCullFace(GL_BACK); // render - _geometry->drawUsingActiveProgram(); + _grid->geometry().drawUsingActiveProgram(); // disable shader _programObject->deactivate(); @@ -154,8 +154,8 @@ namespace openspace { ////////////////////////////////////////////////////////////////////////////////////// // CLIPMAP PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// - ClipMapPatchRenderer::ClipMapPatchRenderer(shared_ptr geometry) - : PatchRenderer(geometry) + ClipMapPatchRenderer::ClipMapPatchRenderer(shared_ptr grid) + : PatchRenderer(grid) { _programObject = OsEng.renderEngine().buildRenderProgram( "LatLonSphereMappingProgram", @@ -233,7 +233,7 @@ namespace openspace { glCullFace(GL_BACK); // render - _geometry->drawUsingActiveProgram(); + _grid->geometry().drawUsingActiveProgram(); // disable shader _programObject->deactivate(); diff --git a/modules/globebrowsing/rendering/patchrenderer.h b/modules/globebrowsing/rendering/patchrenderer.h index 4dfa8246b3..a7b1827f1d 100644 --- a/modules/globebrowsing/rendering/patchrenderer.h +++ b/modules/globebrowsing/rendering/patchrenderer.h @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -55,13 +55,13 @@ namespace openspace { class PatchRenderer { public: - PatchRenderer(shared_ptr); + PatchRenderer(shared_ptr); ~PatchRenderer(); protected: unique_ptr _programObject; - shared_ptr _geometry; + shared_ptr _grid; TextureTileSet _tileSet; }; @@ -73,7 +73,7 @@ namespace openspace { class LatLonPatchRenderer : public PatchRenderer { public: - LatLonPatchRenderer(shared_ptr); + LatLonPatchRenderer(shared_ptr grid); void renderPatch( const LatLonPatch& patch, @@ -91,7 +91,7 @@ namespace openspace { class ClipMapPatchRenderer : public PatchRenderer { public: - ClipMapPatchRenderer(shared_ptr geometry); + ClipMapPatchRenderer(shared_ptr grid); void renderPatch( const LatLon& patchSize,