From ae5b9fb1dad47637733f978976ee180052c1037a Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Wed, 20 Apr 2016 10:54:12 -0400 Subject: [PATCH 1/8] Started testing GDAL --- tests/main.cpp | 3 +- tests/test_gdalwms.inl | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/test_gdalwms.inl diff --git a/tests/main.cpp b/tests/main.cpp index c9ee1353c2..eda3332ea1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -35,11 +35,12 @@ //#include //#include //#include -#include +//#include //#include //#include //#include //#include +#include #include #include diff --git a/tests/test_gdalwms.inl b/tests/test_gdalwms.inl new file mode 100644 index 0000000000..9fe6be560c --- /dev/null +++ b/tests/test_gdalwms.inl @@ -0,0 +1,76 @@ +/***************************************************************************************** + * * + * 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 "gtest/gtest.h" + +#include "gdal.h" +#include "gdal_priv.h" +#include "cpl_conv.h" + +//#include "wms/wmsdriver.h" +//#include "wms/wmsmetadataset.h" + + +class GdalWmsTest : public testing::Test {}; + +TEST_F(GdalWmsTest, Simple) { + //GDALRegister_WMS(); + + const char* fileName = "C:/Users/kalbl_000/Documents/CPP/OpenSpace-Development/data/scene/debugglobe/textures/earth_bluemarble.jpg"; + + GDALDataset *poDataset; + GDALAllRegister(); + poDataset = (GDALDataset *)GDALOpen(fileName, GA_ReadOnly); + if (poDataset == NULL) + { + std::cout << "BAD GDAL OPEN" << std::endl; + return; + } + + double adfGeoTransform[6]; + printf("Driver: %s/%s\n", + poDataset->GetDriver()->GetDescription(), + poDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME)); + printf("Size is %dx%dx%d\n", + poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), + poDataset->GetRasterCount()); + if (poDataset->GetProjectionRef() != NULL) + printf("Projection is `%s'\n", poDataset->GetProjectionRef()); + if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None) + { + printf("Origin = (%.6f,%.6f)\n", + adfGeoTransform[0], adfGeoTransform[3]); + printf("Pixel Size = (%.6f,%.6f)\n", + adfGeoTransform[1], adfGeoTransform[5]); + } + + /* + GDALDataset *poDataset; + GDALAllRegister(); + poDataset = (GDALDataset *)GDALOpen("filename", GA_ReadOnly); + if (poDataset == NULL) + { + std::cout << "NULL" << std::endl; + }*/ +} From c355976b55fedf22e4d8e6f3bce42cc2f9771d76 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Wed, 20 Apr 2016 16:45:42 -0400 Subject: [PATCH 2/8] Removed old test code --- tests/test_gdalwms.inl | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/tests/test_gdalwms.inl b/tests/test_gdalwms.inl index 9fe6be560c..9ec559ac37 100644 --- a/tests/test_gdalwms.inl +++ b/tests/test_gdalwms.inl @@ -25,7 +25,12 @@ #include "gtest/gtest.h" #include "gdal.h" + #include "gdal_priv.h" + +#include + + #include "cpl_conv.h" //#include "wms/wmsdriver.h" @@ -36,34 +41,15 @@ class GdalWmsTest : public testing::Test {}; TEST_F(GdalWmsTest, Simple) { //GDALRegister_WMS(); - - const char* fileName = "C:/Users/kalbl_000/Documents/CPP/OpenSpace-Development/data/scene/debugglobe/textures/earth_bluemarble.jpg"; - GDALDataset *poDataset; - GDALAllRegister(); - poDataset = (GDALDataset *)GDALOpen(fileName, GA_ReadOnly); - if (poDataset == NULL) + + + GDALWMSDataset* wms_dataset = GDALOpen("../test.xml", GA_ReadOnly); + if (wms_dataset) { - std::cout << "BAD GDAL OPEN" << std::endl; - return; + ; } - double adfGeoTransform[6]; - printf("Driver: %s/%s\n", - poDataset->GetDriver()->GetDescription(), - poDataset->GetDriver()->GetMetadataItem(GDAL_DMD_LONGNAME)); - printf("Size is %dx%dx%d\n", - poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), - poDataset->GetRasterCount()); - if (poDataset->GetProjectionRef() != NULL) - printf("Projection is `%s'\n", poDataset->GetProjectionRef()); - if (poDataset->GetGeoTransform(adfGeoTransform) == CE_None) - { - printf("Origin = (%.6f,%.6f)\n", - adfGeoTransform[0], adfGeoTransform[3]); - printf("Pixel Size = (%.6f,%.6f)\n", - adfGeoTransform[1], adfGeoTransform[5]); - } /* GDALDataset *poDataset; From 17a83c02703a1f3e8b8b3bdc3a237413966fb8d5 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 21 Apr 2016 17:43:08 -0400 Subject: [PATCH 3/8] Convert from gdal format to Texture --- modules/globebrowsing/CMakeLists.txt | 2 + .../rendering/gdaldataconverter.cpp | 124 ++++++++++++++++++ .../rendering/gdaldataconverter.h | 53 ++++++++ .../rendering/texturetileset.cpp | 22 ++++ 4 files changed, 201 insertions(+) create mode 100644 modules/globebrowsing/rendering/gdaldataconverter.cpp create mode 100644 modules/globebrowsing/rendering/gdaldataconverter.h diff --git a/modules/globebrowsing/CMakeLists.txt b/modules/globebrowsing/CMakeLists.txt index 522e89b928..5b3eed88a9 100644 --- a/modules/globebrowsing/CMakeLists.txt +++ b/modules/globebrowsing/CMakeLists.txt @@ -41,6 +41,7 @@ set(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/texturetileset.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/twmstileprovider.h ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmappyramid.h + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/gdaldataconverter.h ${CMAKE_CURRENT_SOURCE_DIR}/datastructures/chunknode.h ${CMAKE_CURRENT_SOURCE_DIR}/datastructures/latlon.h @@ -62,6 +63,7 @@ set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/rendering/texturetileset.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/twmstileprovider.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rendering/clipmappyramid.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/rendering/gdaldataconverter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/datastructures/chunknode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/datastructures/latlon.cpp diff --git a/modules/globebrowsing/rendering/gdaldataconverter.cpp b/modules/globebrowsing/rendering/gdaldataconverter.cpp new file mode 100644 index 0000000000..b01b679c71 --- /dev/null +++ b/modules/globebrowsing/rendering/gdaldataconverter.cpp @@ -0,0 +1,124 @@ +/***************************************************************************************** +* * +* 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 = "GdalDataConverter"; +} + +namespace openspace { + + GdalDataConverter::GdalDataConverter() + { + + } + + GdalDataConverter::~GdalDataConverter() + { + + } + + std::shared_ptr GdalDataConverter::convertToOpenGLTexture(GDALDataset* dataSet) + { + int nCols = dataSet->GetRasterBand(1)->GetXSize(); + int nRows = dataSet->GetRasterBand(1)->GetYSize(); + + GDALRasterBand* redBand = dataSet->GetRasterBand(1); + GDALRasterBand* greenBand = dataSet->GetRasterBand(2); + GDALRasterBand* blueBand = dataSet->GetRasterBand(3); + + int blockSizeX; + int blockSizeY; + + redBand->GetBlockSize(&blockSizeX, &blockSizeY); + + int nBlocksX = nCols / blockSizeX; + int nBlocksY = nRows / blockSizeY; + + // A block where data is copied + GByte* blockR = (GByte*)CPLMalloc(sizeof(GByte) * blockSizeX * blockSizeY); + GByte* blockG = (GByte*)CPLMalloc(sizeof(GByte) * blockSizeX * blockSizeY); + GByte* blockB = (GByte*)CPLMalloc(sizeof(GByte) * blockSizeX * blockSizeY); + // The data that the texture should use + GLubyte* imageData = (GLubyte*)CPLMalloc(sizeof(GLubyte) * nCols * nRows * 4); + + // For each block + for (size_t blockY = 0; blockY < nBlocksY; blockY++) + { + for (size_t blockX = 0; blockX < nBlocksX; blockX++) + { + redBand->ReadBlock(blockX, blockY, blockR); + greenBand->ReadBlock(blockX, blockY, blockG); + blueBand->ReadBlock(blockX, blockY, blockB); + + size_t blockPixelIndexX = blockSizeX * blockX; + size_t blockPixelIndexY = blockSizeY * blockY; + + size_t startPixelIndex = blockPixelIndexY * nCols + blockPixelIndexX; + + + // For each pixel in each block + for (size_t yInBlock = 0; yInBlock < blockSizeY; yInBlock++) + { + for (size_t xInBlock = 0; xInBlock < blockSizeX; xInBlock++) + { + size_t pixelIndexInBlock = xInBlock + yInBlock * blockSizeX; + size_t globalPixelIndex = (startPixelIndex + xInBlock + yInBlock * nCols) * 4; + + GLubyte pixelR = blockR[pixelIndexInBlock]; + GLubyte pixelG = blockG[pixelIndexInBlock]; + GLubyte pixelB = blockB[pixelIndexInBlock]; + + imageData[globalPixelIndex + 0] = pixelR; + imageData[globalPixelIndex + 1] = pixelG; + imageData[globalPixelIndex + 2] = pixelB; + imageData[globalPixelIndex + 3] = 255; + } + } + } + } + + // The texture should take ownership of the data + std::shared_ptr texture = std::shared_ptr(new Texture( + static_cast(imageData), + glm::uvec3(nCols, nRows, 1), + Texture::Format::RGBA, + GL_RGBA, + GL_UNSIGNED_BYTE, + Texture::FilterMode::Linear, + Texture::WrappingMode::Repeat)); + + // Free the row + CPLFree(blockR); + CPLFree(blockG); + CPLFree(blockB); + + // Do not free imageData since the texture now has ownership of it + + return texture; + } + +} // namespace openspace diff --git a/modules/globebrowsing/rendering/gdaldataconverter.h b/modules/globebrowsing/rendering/gdaldataconverter.h new file mode 100644 index 0000000000..7e55bca08d --- /dev/null +++ b/modules/globebrowsing/rendering/gdaldataconverter.h @@ -0,0 +1,53 @@ +/***************************************************************************************** +* * +* 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 __GDALDATACONVERTER_H__ +#define __GDALDATACONVERTER_H__ + +#include +#include + +#include "gdal_priv.h" + +#include + +namespace openspace { + + using namespace ghoul::opengl; + + class GdalDataConverter + { + public: + GdalDataConverter(); + ~GdalDataConverter(); + + std::shared_ptr convertToOpenGLTexture(GDALDataset* dataSet); + + private: + + }; + +} // namespace openspace + +#endif // __GDALDATACONVERTER_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/texturetileset.cpp b/modules/globebrowsing/rendering/texturetileset.cpp index 29ee7b8bfb..404eb0aca1 100644 --- a/modules/globebrowsing/rendering/texturetileset.cpp +++ b/modules/globebrowsing/rendering/texturetileset.cpp @@ -24,12 +24,16 @@ #include +#include + #include #include #include #include +#include "gdal_priv.h" +#include "cpl_conv.h" // for CPLMalloc() #include @@ -45,6 +49,23 @@ namespace openspace { , _depth(depth) { + + // Read using GDAL + + /* + GDALDataset *poDataset; + GDALAllRegister(); + poDataset = (GDALDataset *)GDALOpen(absPath("textures/earth_bluemarble.jpg").c_str(), GA_ReadOnly); + assert(poDataset != NULL); + GdalDataConverter conv; + + _testTexture = conv.convertToOpenGLTexture(poDataset); + + _testTexture->uploadTexture(); + _testTexture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); + */ + + // Set e texture to test _testTexture = std::move(ghoul::io::TextureReader::ref().loadTexture(absPath("textures/earth_bluemarble.jpg"))); if (_testTexture) { @@ -56,6 +77,7 @@ namespace openspace { //_testTexture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap); _testTexture->setFilter(ghoul::opengl::Texture::FilterMode::Linear); } + } TextureTileSet::~TextureTileSet(){ From c7bfe0b2c04ea4c0a319bb5bf400943f8e8f6b99 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Thu, 21 Apr 2016 21:46:49 -0400 Subject: [PATCH 4/8] 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, From aaf86ca86fec689a758710591bfd5298d83521b7 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 22 Apr 2016 11:28:54 -0400 Subject: [PATCH 5/8] Add class InnerClipMapGrid which renders the last part of the clipmap. --- .../globebrowsing/rendering/clipmapglobe.cpp | 12 +- .../globebrowsing/rendering/clipmapglobe.h | 4 +- .../globebrowsing/rendering/clipmapgrid.cpp | 239 ++++++++++++++---- modules/globebrowsing/rendering/clipmapgrid.h | 55 +++- .../globebrowsing/rendering/patchrenderer.cpp | 15 +- .../globebrowsing/rendering/patchrenderer.h | 11 +- 6 files changed, 264 insertions(+), 72 deletions(-) diff --git a/modules/globebrowsing/rendering/clipmapglobe.cpp b/modules/globebrowsing/rendering/clipmapglobe.cpp index 5b87593773..8626e39105 100644 --- a/modules/globebrowsing/rendering/clipmapglobe.cpp +++ b/modules/globebrowsing/rendering/clipmapglobe.cpp @@ -71,10 +71,10 @@ namespace openspace { // --------- // init Renderer - auto patchRenderer = new ClipMapPatchRenderer(shared_ptr(new ClipMapGeometry(32))); - _patchRenderer.reset(patchRenderer); - auto smallestPatchRenderer = new ClipMapPatchRenderer(shared_ptr(new ClipMapGeometry(32))); - _smallestPatchRenderer.reset(smallestPatchRenderer); + auto outerPatchRenderer = new ClipMapPatchRenderer(shared_ptr(new OuterClipMapGrid(32))); + _outerPatchRenderer.reset(outerPatchRenderer); + auto innerPatchRenderer = new ClipMapPatchRenderer(shared_ptr(new InnerClipMapGrid(32))); + _innerPatchRenderer.reset(innerPatchRenderer); } ClipMapGlobe::~ClipMapGlobe() { @@ -102,10 +102,10 @@ namespace openspace { for (size_t i = minDepth; i < maxDepth; i++) { LatLon patchSize = _clipMapPyramid.getPatchSizeAtLevel(i); - _patchRenderer->renderPatch(patchSize, data, 6.3e6); + _outerPatchRenderer->renderPatch(patchSize, data, 6.3e6); } LatLon patchSize = _clipMapPyramid.getPatchSizeAtLevel(maxDepth); - _smallestPatchRenderer->renderPatch(patchSize, data, 6.3e6); + _innerPatchRenderer->renderPatch(patchSize, data, 6.3e6); } void ClipMapGlobe::update(const UpdateData& data) { diff --git a/modules/globebrowsing/rendering/clipmapglobe.h b/modules/globebrowsing/rendering/clipmapglobe.h index 093aefa256..ead63ca4cf 100644 --- a/modules/globebrowsing/rendering/clipmapglobe.h +++ b/modules/globebrowsing/rendering/clipmapglobe.h @@ -57,8 +57,8 @@ namespace openspace { void update(const UpdateData& data) override; private: - std::unique_ptr _patchRenderer; - std::unique_ptr _smallestPatchRenderer; + std::unique_ptr _outerPatchRenderer; + std::unique_ptr _innerPatchRenderer; ClipMapPyramid _clipMapPyramid; diff --git a/modules/globebrowsing/rendering/clipmapgrid.cpp b/modules/globebrowsing/rendering/clipmapgrid.cpp index 944f514cd6..42ecaeed2a 100644 --- a/modules/globebrowsing/rendering/clipmapgrid.cpp +++ b/modules/globebrowsing/rendering/clipmapgrid.cpp @@ -32,8 +32,39 @@ namespace { namespace openspace { -ClipMapGeometry::ClipMapGeometry(unsigned int resolution) +////////////////////////////////////////////////////////////////////////////////////////// +// CLIPMAP GRID (Abstract class) // +////////////////////////////////////////////////////////////////////////////////////////// + +ClipMapGrid::ClipMapGrid(unsigned int resolution) : Grid(resolution, resolution, Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No) +{ + +} + +ClipMapGrid::~ClipMapGrid() +{ + +} + +int ClipMapGrid::xResolution() const { + return resolution(); +} + +int ClipMapGrid::yResolution() const { + return resolution(); +} + +int ClipMapGrid::resolution() const { + return _xRes; +} + +////////////////////////////////////////////////////////////////////////////////////////// +// OUTER CLIPMAP GRID // +////////////////////////////////////////////////////////////////////////////////////////// + +OuterClipMapGrid::OuterClipMapGrid(unsigned int resolution) +: ClipMapGrid(resolution) { _geometry = std::unique_ptr(new Geometry( CreateElements(resolution, resolution), @@ -44,51 +75,39 @@ ClipMapGeometry::ClipMapGeometry(unsigned int resolution) _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(resolution, resolution)); } -ClipMapGeometry::~ClipMapGeometry() +OuterClipMapGrid::~OuterClipMapGrid() { } -int ClipMapGeometry::xResolution() const { - return resolution(); -} - -int ClipMapGeometry::yResolution() const { - return resolution(); -} - -int ClipMapGeometry::resolution() const { - return _xRes; -} - -size_t ClipMapGeometry::numElements(int resolution) +size_t OuterClipMapGrid::numElements(int resolution) { int numElementsInTotalSquare = 6 * (resolution + 1) * (resolution + 1); int numElementsInHole = 6 * (resolution / 4 * resolution / 4); return numElementsInTotalSquare - numElementsInHole; } -size_t ClipMapGeometry::numVerticesBottom(int resolution) +size_t OuterClipMapGrid::numVerticesBottom(int resolution) { return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); } -size_t ClipMapGeometry::numVerticesLeft(int resolution) +size_t OuterClipMapGrid::numVerticesLeft(int resolution) { return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); } -size_t ClipMapGeometry::numVerticesRight(int resolution) +size_t OuterClipMapGrid::numVerticesRight(int resolution) { return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); } -size_t ClipMapGeometry::numVerticesTop(int resolution) +size_t OuterClipMapGrid::numVerticesTop(int resolution) { return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); } -size_t ClipMapGeometry::numVertices(int resolution) +size_t OuterClipMapGrid::numVertices(int resolution) { return numVerticesBottom(resolution) + numVerticesLeft(resolution) + @@ -96,7 +115,7 @@ size_t ClipMapGeometry::numVertices(int resolution) numVerticesTop(resolution); } -void ClipMapGeometry::validate(int xRes, int yRes) { +void OuterClipMapGrid::validate(int xRes, int yRes) { ghoul_assert(xRes == yRes, "Resolution must be equal in x and in y. "); @@ -107,8 +126,7 @@ void ClipMapGeometry::validate(int xRes, int yRes) { "Resolution must be a power of 2. (" << resolution << ")"); } -std::vector ClipMapGeometry::CreateElements(int xRes, int yRes) { - int hej = 0; +std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { validate(xRes, yRes); int resolution = xRes; std::vector elements; @@ -215,7 +233,7 @@ std::vector ClipMapGeometry::CreateElements(int xRes, int yRes) { return elements; } -std::vector ClipMapGeometry::CreatePositions(int xRes, int yRes) +std::vector OuterClipMapGrid::CreatePositions(int xRes, int yRes) { validate(xRes, yRes); int resolution = xRes; @@ -237,65 +255,194 @@ std::vector ClipMapGeometry::CreatePositions(int xRes, int yRes) } -std::vector ClipMapGeometry::CreateTextureCoordinates(int xRes, int yRes){ +std::vector OuterClipMapGrid::CreateTextureCoordinates(int xRes, int yRes){ validate(xRes, yRes); int resolution = xRes; std::vector textureCoordinates; textureCoordinates.reserve(numVertices(resolution)); - // Resolution needs to be an int to compare with negative numbers - int intResolution = resolution; // Build the bottom part of the clipmap geometry - for (int y = -1; y < intResolution / 4 + 1; y++) { - for (int x = -1; x < intResolution + 2; x++) { + for (int y = -1; y < resolution / 4 + 1; y++) { + for (int x = -1; x < resolution + 2; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / intResolution, - static_cast(y) / intResolution)); + static_cast(x) / resolution, + static_cast(y) / resolution)); } } // Build the left part of the clipmap geometry - for (int y = intResolution / 4; y < 3 * intResolution / 4 + 1; y++) { - for (int x = -1; x < intResolution / 4 + 1; x++) { + for (int y = resolution / 4; y < 3 * resolution / 4 + 1; y++) { + for (int x = -1; x < resolution / 4 + 1; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / intResolution, - static_cast(y) / intResolution)); + static_cast(x) / resolution, + static_cast(y) / resolution)); } } // Build the right part of the clipmap geometry - for (int y = intResolution / 4; y < 3 * intResolution / 4 + 1; y++) { - for (int x = 3 * intResolution / 4; x < intResolution + 2; x++) { - float u = static_cast(x) / intResolution; - float v = static_cast(y) / intResolution; + for (int y = resolution / 4; y < 3 * resolution / 4 + 1; y++) { + for (int x = 3 * resolution / 4; x < resolution + 2; x++) { + float u = static_cast(x) / resolution; + float v = static_cast(y) / resolution; textureCoordinates.push_back(glm::vec2(u, v)); } } // Build the top part of the clipmap geometry - for (int y = 3 * intResolution / 4; y < intResolution + 2; y++) { - for (int x = -1; x < intResolution + 2; x++) { + for (int y = 3 * resolution / 4; y < resolution + 2; y++) { + for (int x = -1; x < resolution + 2; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / intResolution, - static_cast(y) / intResolution)); + static_cast(x) / resolution, + static_cast(y) / resolution)); } } return textureCoordinates; } -std::vector ClipMapGeometry::CreateNormals(int xRes, int yRes) { +std::vector OuterClipMapGrid::CreateNormals(int xRes, int yRes) { validate(xRes, yRes); int resolution = xRes; std::vector normals; normals.reserve(numVertices(resolution)); - for (unsigned int y = 0; y < resolution + 1; y++) { - for (unsigned int x = 0; x < resolution + 1; x++) { + for (int y = -1; y < resolution + 2; y++) { + for (int x = -1; x < resolution + 2; x++) { normals.push_back(glm::vec3(0, 0, 1)); } } return normals; } + +////////////////////////////////////////////////////////////////////////////////////////// +// INNER CLIPMAP GRID // +////////////////////////////////////////////////////////////////////////////////////////// + + +InnerClipMapGrid::InnerClipMapGrid(unsigned int resolution) + : ClipMapGrid(resolution) +{ + _geometry = std::unique_ptr(new Geometry( + CreateElements(resolution, resolution), + Geometry::Positions::No, + Geometry::TextureCoordinates::Yes, + Geometry::Normals::No)); + + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(resolution, resolution)); +} + +InnerClipMapGrid::~InnerClipMapGrid() +{ + +} + +size_t InnerClipMapGrid::numElements(int resolution) +{ + return resolution * resolution * 6; +} + +size_t InnerClipMapGrid::numVertices(int resolution) +{ + return (resolution + 1) * (resolution + 1); +} + +void InnerClipMapGrid::validate(int xRes, int yRes) { + + ghoul_assert(xRes == yRes, + "Resolution must be equal in x and in y. "); + int resolution = xRes; + ghoul_assert(resolution >= 1, + "Resolution must be at least 1. (" << resolution << ")"); +} + +std::vector InnerClipMapGrid::CreateElements(int xRes, int yRes) { + validate(xRes, yRes); + int resolution = xRes; + std::vector elements; + elements.reserve(numElements(resolution)); + + // x v01---v11 x .. + // | / | + // x v00---v10 x .. + // + // x x x x .. + // : : : : + + for (unsigned int y = 0; y < resolution + 2; y++) { + for (unsigned int x = 0; x < resolution + 2; x++) { + GLuint v00 = (y + 0) * (resolution + 3) + x + 0; + GLuint v10 = (y + 0) * (resolution + 3) + x + 1; + GLuint v01 = (y + 1) * (resolution + 3) + x + 0; + GLuint v11 = (y + 1) * (resolution + 3) + x + 1; + + elements.push_back(v00); + elements.push_back(v10); + elements.push_back(v11); + + elements.push_back(v00); + elements.push_back(v11); + elements.push_back(v01); + } + } + + return elements; +} + +std::vector InnerClipMapGrid::CreatePositions(int xRes, int yRes) +{ + validate(xRes, yRes); + int resolution = xRes; + std::vector positions; + positions.reserve(numVertices(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++) { + positions.push_back( + glm::vec4( + templateTextureCoords[i].x, + templateTextureCoords[i].y, + 0, + 1)); + } + + return positions; +} + + +std::vector InnerClipMapGrid::CreateTextureCoordinates(int xRes, int yRes) { + validate(xRes, yRes); + int resolution = xRes; + std::vector textureCoordinates; + textureCoordinates.reserve(numVertices(resolution)); + + // Build the bottom part of the clipmap geometry + for (int y = -1; y < resolution + 2; y++) { + for (int x = -1; x < resolution + 2; x++) { + textureCoordinates.push_back(glm::vec2( + static_cast(x) / resolution, + static_cast(y) / resolution)); + } + } + + return textureCoordinates; +} + +std::vector InnerClipMapGrid::CreateNormals(int xRes, int yRes) { + validate(xRes, yRes); + int resolution = xRes; + std::vector normals; + normals.reserve(numVertices(resolution)); + + for (int y = -1; y < resolution + 2; y++) { + for (int x = -1; x < resolution + 2; 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/clipmapgrid.h b/modules/globebrowsing/rendering/clipmapgrid.h index ed25505484..bb0faaac46 100644 --- a/modules/globebrowsing/rendering/clipmapgrid.h +++ b/modules/globebrowsing/rendering/clipmapgrid.h @@ -32,22 +32,38 @@ namespace openspace { -class ClipMapGeometry : public Grid +////////////////////////////////////////////////////////////////////////////////////////// +// CLIPMAP GRID (Abstract class) // +////////////////////////////////////////////////////////////////////////////////////////// + +class ClipMapGrid : public Grid { public: - ClipMapGeometry(unsigned int resolution); + ClipMapGrid(unsigned int resolution); - ~ClipMapGeometry(); + ~ClipMapGrid(); virtual int xResolution() const; virtual int yResolution() const; int resolution() const; +}; + +////////////////////////////////////////////////////////////////////////////////////////// +// OUTER CLIPMAP GRID // +////////////////////////////////////////////////////////////////////////////////////////// + +class OuterClipMapGrid : public ClipMapGrid +{ +public: + OuterClipMapGrid(unsigned int resolution); + + ~OuterClipMapGrid(); 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); + 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); private: void validate(int xRes, int yRes); @@ -60,5 +76,30 @@ private: static size_t numElements(int resolution); static size_t numVertices(int resolution); }; + +////////////////////////////////////////////////////////////////////////////////////////// +// INNER CLIPMAP GRID // +////////////////////////////////////////////////////////////////////////////////////////// + +class InnerClipMapGrid : public ClipMapGrid +{ +public: + InnerClipMapGrid(unsigned int resolution); + + ~InnerClipMapGrid(); + +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); + +private: + void validate(int xRes, int yRes); + + 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/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index c2d0cddb5c..b33252ffb3 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -55,9 +55,8 @@ namespace openspace { ////////////////////////////////////////////////////////////////////////////////////// // PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// - PatchRenderer::PatchRenderer(shared_ptr geometry) - : _grid(geometry) - , _tileSet(LatLon(M_PI, M_PI * 2), LatLon(M_PI / 2, - M_PI), 0) + PatchRenderer::PatchRenderer() + : _tileSet(LatLon(M_PI, M_PI * 2), LatLon(M_PI / 2, -M_PI), 0) { } @@ -76,7 +75,8 @@ namespace openspace { // LATLON PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// LatLonPatchRenderer::LatLonPatchRenderer(shared_ptr grid) - : PatchRenderer(grid) + : PatchRenderer() + , _grid(grid) { _programObject = OsEng.renderEngine().buildRenderProgram( "LatLonSphereMappingProgram", @@ -154,8 +154,9 @@ namespace openspace { ////////////////////////////////////////////////////////////////////////////////////// // CLIPMAP PATCH RENDERER // ////////////////////////////////////////////////////////////////////////////////////// - ClipMapPatchRenderer::ClipMapPatchRenderer(shared_ptr grid) - : PatchRenderer(grid) + ClipMapPatchRenderer::ClipMapPatchRenderer(shared_ptr grid) + : PatchRenderer() + , _grid(grid) { _programObject = OsEng.renderEngine().buildRenderProgram( "LatLonSphereMappingProgram", @@ -182,7 +183,7 @@ namespace openspace { mat4 modelTransform = translate(mat4(1), data.position.vec3()); // Snap patch position - int segmentsPerPatch = 32; + int segmentsPerPatch = _grid->resolution(); LatLon stepSize = LatLon( patchSize.lat / segmentsPerPatch, patchSize.lon / segmentsPerPatch); diff --git a/modules/globebrowsing/rendering/patchrenderer.h b/modules/globebrowsing/rendering/patchrenderer.h index a7b1827f1d..28ec03718e 100644 --- a/modules/globebrowsing/rendering/patchrenderer.h +++ b/modules/globebrowsing/rendering/patchrenderer.h @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -55,14 +56,12 @@ namespace openspace { class PatchRenderer { public: - PatchRenderer(shared_ptr); + PatchRenderer(); ~PatchRenderer(); protected: unique_ptr _programObject; - shared_ptr _grid; - TextureTileSet _tileSet; }; @@ -85,18 +84,22 @@ namespace openspace { const RenderData& data, double radius, const TileIndex& ti); + private: + shared_ptr _grid; }; class ClipMapPatchRenderer : public PatchRenderer { public: - ClipMapPatchRenderer(shared_ptr grid); + ClipMapPatchRenderer(shared_ptr grid); void renderPatch( const LatLon& patchSize, const RenderData& data, double radius); + private: + shared_ptr _grid; }; } // namespace openspace From 47da09064ee472948b1129253cc5670f6d92161c Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 22 Apr 2016 11:53:52 -0400 Subject: [PATCH 6/8] Redefine x resolution and y resolution to be the number of grid cells in each direction. --- modules/globebrowsing/rendering/basicgrid.cpp | 28 +++++++++---------- modules/globebrowsing/rendering/basicgrid.h | 21 +++++++++++++- modules/globebrowsing/rendering/clipmapgrid.h | 28 +++++++++++++++++-- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/modules/globebrowsing/rendering/basicgrid.cpp b/modules/globebrowsing/rendering/basicgrid.cpp index ffe95c6fb2..18e5092a95 100644 --- a/modules/globebrowsing/rendering/basicgrid.cpp +++ b/modules/globebrowsing/rendering/basicgrid.cpp @@ -79,11 +79,11 @@ void BasicGrid::validate(int xRes, int yRes) { } inline size_t BasicGrid::numElements(int xRes, int yRes){ - return 3 * 2 * (xRes - 1)*(yRes - 1); + return 3 * 2 * xRes * yRes; } inline size_t BasicGrid::numVertices(int xRes, int yRes) { - return xRes * yRes; + return (xRes + 1) * (yRes + 1); } std::vector BasicGrid::CreateElements(int xRes, int yRes) { @@ -91,8 +91,8 @@ std::vector BasicGrid::CreateElements(int xRes, int 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++) { + for (unsigned int y = 0; y < yRes; y++) { + for (unsigned int x = 0; x < xRes; x++) { // x v01---v11 x .. // | / | @@ -101,10 +101,10 @@ std::vector BasicGrid::CreateElements(int xRes, int yRes) { // 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; + GLuint v00 = (y + 0) * (xRes + 1) + x + 0; + GLuint v10 = (y + 0) * (xRes + 1) + x + 1; + GLuint v01 = (y + 1) * (xRes + 1) + x + 0; + GLuint v11 = (y + 1) * (xRes + 1) + x + 1; // add upper triangle elements.push_back(v00); @@ -147,11 +147,11 @@ std::vector BasicGrid::CreateTextureCoordinates(int xRes, int 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++) { + for (unsigned int y = 0; y < yRes + 1; y++) { + for (unsigned int x = 0; x < xRes + 1; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / static_cast(xRes - 1), - static_cast(y) / static_cast(yRes - 1) + static_cast(x) / static_cast(xRes), + static_cast(y) / static_cast(yRes) )); } } @@ -163,8 +163,8 @@ std::vector BasicGrid::CreateNormals(int xRes, int 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++) { + for (unsigned int y = 0; y < yRes + 1; y++) { + for (unsigned int x = 0; x < xRes + 1; x++) { normals.push_back(glm::vec3(0, 0, 1)); } } diff --git a/modules/globebrowsing/rendering/basicgrid.h b/modules/globebrowsing/rendering/basicgrid.h index 1ff22010de..27059ea458 100644 --- a/modules/globebrowsing/rendering/basicgrid.h +++ b/modules/globebrowsing/rendering/basicgrid.h @@ -37,6 +37,16 @@ namespace openspace { class BasicGrid : public Grid { public: + /** + \param xRes is the number of grid cells in the x direction. + \param yRes is the number of grid cells in the y direction. + \param usePositions determines whether or not to upload any vertex position data + to the GPU. + \param useTextureCoordinates determines whether or not to upload any vertex texture + coordinate data to the GPU. + \param useNormals determines whether or not to upload any vertex normal data + to the GPU. + */ BasicGrid( unsigned int xRes, unsigned int yRes, @@ -45,10 +55,19 @@ public: Geometry::Normals useNormals); ~BasicGrid(); + /** + Returns the number of grid cells in the x direction. Hence the number of vertices + in the x direction is xResolution + 1. + */ virtual int xResolution() const; + + /** + Returns the number of grid cells in the y direction. Hence the number of vertices + in the y direction is xResolution + 1. + */ virtual int yResolution() const; -protected: +private: virtual std::vector CreateElements( int xRes, int yRes); virtual std::vector CreatePositions( int xRes, int yRes); virtual std::vector CreateTextureCoordinates( int xRes, int yRes); diff --git a/modules/globebrowsing/rendering/clipmapgrid.h b/modules/globebrowsing/rendering/clipmapgrid.h index bb0faaac46..d37d8d9e66 100644 --- a/modules/globebrowsing/rendering/clipmapgrid.h +++ b/modules/globebrowsing/rendering/clipmapgrid.h @@ -36,6 +36,15 @@ namespace openspace { // CLIPMAP GRID (Abstract class) // ////////////////////////////////////////////////////////////////////////////////////////// +/** +This class defines a grid used for the layers of a geometry clipmap. A geometry +clipmap is built up from a pyramid of clipmaps so the majority of the grids used +are of the class OuterClipMapGrid. The vertex positions and texture coordinated are +defined differently than for a normal BasicGrid. Other than having the basic grid +vertices it also creates padding of one grid cell on the perimeter. This padding can be +used when rendering a ClipMapGrid so that an inner layer of the pyramid can move and +snap to the outer layers grid cells. +*/ class ClipMapGrid : public Grid { public: @@ -45,6 +54,12 @@ public: virtual int xResolution() const; virtual int yResolution() const; + + /** + Returns the resolution of the grid. A ClipMapGrid must have the resolution in x and + y direction equal so this function works as a wrapper for xResolution() and + yResolution(). + */ int resolution() const; }; @@ -52,6 +67,11 @@ public: // OUTER CLIPMAP GRID // ////////////////////////////////////////////////////////////////////////////////////////// +/** +The outer layers of a geometry clipmap pyramid can be built up by grids with sizes +increasing with the power of 2. The OuterClipMapGrid has a whole in the middle where +a smaller ClipMapGrid of half the size can fit. +*/ class OuterClipMapGrid : public ClipMapGrid { public: @@ -81,6 +101,11 @@ private: // INNER CLIPMAP GRID // ////////////////////////////////////////////////////////////////////////////////////////// +/** +The InnerClipMapGrid can be used for the inner most (smallest) grid of a geometry clipmap +pyramid. The only difference from a OuterClipMapGrid is that this grid does not have +a whole where a smaller ClipMapGrid can be positioned. +*/ class InnerClipMapGrid : public ClipMapGrid { public: @@ -88,13 +113,12 @@ public: ~InnerClipMapGrid(); -protected: +private: 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); -private: void validate(int xRes, int yRes); static size_t numElements(int resolution); From 3c8434bee0aa379481df1be88ccc886823ff7dd5 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 22 Apr 2016 12:14:37 -0400 Subject: [PATCH 7/8] Rename resolution to segments in grid classes. --- modules/globebrowsing/rendering/basicgrid.cpp | 90 +++---- modules/globebrowsing/rendering/basicgrid.h | 28 +-- .../globebrowsing/rendering/clipmapgrid.cpp | 238 +++++++++--------- modules/globebrowsing/rendering/clipmapgrid.h | 54 ++-- modules/globebrowsing/rendering/grid.cpp | 8 +- modules/globebrowsing/rendering/grid.h | 29 ++- .../globebrowsing/rendering/patchrenderer.cpp | 2 +- 7 files changed, 225 insertions(+), 224 deletions(-) diff --git a/modules/globebrowsing/rendering/basicgrid.cpp b/modules/globebrowsing/rendering/basicgrid.cpp index 18e5092a95..801985607b 100644 --- a/modules/globebrowsing/rendering/basicgrid.cpp +++ b/modules/globebrowsing/rendering/basicgrid.cpp @@ -31,32 +31,32 @@ namespace { namespace openspace { BasicGrid::BasicGrid( - unsigned int xRes, - unsigned int yRes, + unsigned int xSegments, + unsigned int ySegments, Geometry::Positions usePositions, Geometry::TextureCoordinates useTextureCoordinates, Geometry::Normals useNormals) : Grid( - xRes, - yRes, + xSegments, + ySegments, usePositions, useTextureCoordinates, useNormals) { _geometry = std::unique_ptr(new Geometry( - CreateElements(xRes, yRes), + CreateElements(xSegments, ySegments), usePositions, useTextureCoordinates, useNormals)); if (usePositions == Geometry::Positions::Yes) { - _geometry->setVertexPositions(CreatePositions(_xRes, _yRes)); + _geometry->setVertexPositions(CreatePositions(_xSegments, _ySegments)); } if (useTextureCoordinates == Geometry::TextureCoordinates::Yes) { - _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(_xRes, _yRes)); + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(_xSegments, _ySegments)); } if (useNormals == Geometry::Normals::Yes) { - _geometry->setVertexNormals(CreateNormals(_xRes, _yRes)); + _geometry->setVertexNormals(CreateNormals(_xSegments, _ySegments)); } } @@ -65,34 +65,34 @@ BasicGrid::~BasicGrid() } -int BasicGrid::xResolution() const { - return _xRes; +int BasicGrid::xSegments() const { + return _xSegments; } -int BasicGrid::yResolution() const { - return _yRes; +int BasicGrid::ySegments() const { + return _ySegments; } -void BasicGrid::validate(int xRes, int yRes) { - ghoul_assert(xRes > 0 && yRes > 0, - "Resolution must be at least 1x1. (" << xRes << ", " << yRes << ")"); +void BasicGrid::validate(int xSegments, int ySegments) { + ghoul_assert(xSegments > 0 && ySegments > 0, + "Resolution must be at least 1x1. (" << xSegments << ", " << ySegments << ")"); } -inline size_t BasicGrid::numElements(int xRes, int yRes){ - return 3 * 2 * xRes * yRes; +inline size_t BasicGrid::numElements(int xSegments, int ySegments){ + return 3 * 2 * xSegments * ySegments; } -inline size_t BasicGrid::numVertices(int xRes, int yRes) { - return (xRes + 1) * (yRes + 1); +inline size_t BasicGrid::numVertices(int xSegments, int ySegments) { + return (xSegments + 1) * (ySegments + 1); } -std::vector BasicGrid::CreateElements(int xRes, int yRes) { - validate(xRes, yRes); +std::vector BasicGrid::CreateElements(int xSegments, int ySegments) { + validate(xSegments, ySegments); std::vector elements; - elements.reserve(numElements(xRes, yRes)); - for (unsigned int y = 0; y < yRes; y++) { - for (unsigned int x = 0; x < xRes; x++) { + elements.reserve(numElements(xSegments, ySegments)); + for (unsigned int y = 0; y < ySegments; y++) { + for (unsigned int x = 0; x < xSegments; x++) { // x v01---v11 x .. // | / | @@ -101,10 +101,10 @@ std::vector BasicGrid::CreateElements(int xRes, int yRes) { // x x x x .. // : : : : - GLuint v00 = (y + 0) * (xRes + 1) + x + 0; - GLuint v10 = (y + 0) * (xRes + 1) + x + 1; - GLuint v01 = (y + 1) * (xRes + 1) + x + 0; - GLuint v11 = (y + 1) * (xRes + 1) + x + 1; + GLuint v00 = (y + 0) * (xSegments + 1) + x + 0; + GLuint v10 = (y + 0) * (xSegments + 1) + x + 1; + GLuint v01 = (y + 1) * (xSegments + 1) + x + 0; + GLuint v11 = (y + 1) * (xSegments + 1) + x + 1; // add upper triangle elements.push_back(v00); @@ -122,15 +122,15 @@ std::vector BasicGrid::CreateElements(int xRes, int yRes) { } std::vector BasicGrid::CreatePositions( - int xRes, - int yRes) + int xSegments, + int ySegments) { - validate(xRes, yRes); + validate(xSegments, ySegments); std::vector positions; - positions.reserve(numVertices(xRes, yRes)); + positions.reserve(numVertices(xSegments, ySegments)); // Copy from 2d texture coordinates and use as template to create positions - std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); + std::vector templateTextureCoords = CreateTextureCoordinates(xSegments, ySegments); for (unsigned int i = 0; i < templateTextureCoords.size(); i++) { positions.push_back(glm::vec4( @@ -142,29 +142,29 @@ std::vector BasicGrid::CreatePositions( return positions; } -std::vector BasicGrid::CreateTextureCoordinates(int xRes, int yRes){ - validate(xRes, yRes); +std::vector BasicGrid::CreateTextureCoordinates(int xSegments, int ySegments){ + validate(xSegments, ySegments); std::vector textureCoordinates; - textureCoordinates.reserve(numVertices(xRes, yRes)); + textureCoordinates.reserve(numVertices(xSegments, ySegments)); - for (unsigned int y = 0; y < yRes + 1; y++) { - for (unsigned int x = 0; x < xRes + 1; x++) { + for (unsigned int y = 0; y < ySegments + 1; y++) { + for (unsigned int x = 0; x < xSegments + 1; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / static_cast(xRes), - static_cast(y) / static_cast(yRes) + static_cast(x) / static_cast(xSegments), + static_cast(y) / static_cast(ySegments) )); } } return textureCoordinates; } -std::vector BasicGrid::CreateNormals(int xRes, int yRes) { - validate(xRes, yRes); +std::vector BasicGrid::CreateNormals(int xSegments, int ySegments) { + validate(xSegments, ySegments); std::vector normals; - normals.reserve(numVertices(xRes, yRes)); + normals.reserve(numVertices(xSegments, ySegments)); - for (unsigned int y = 0; y < yRes + 1; y++) { - for (unsigned int x = 0; x < xRes + 1; x++) { + for (unsigned int y = 0; y < ySegments + 1; y++) { + for (unsigned int x = 0; x < xSegments + 1; x++) { normals.push_back(glm::vec3(0, 0, 1)); } } diff --git a/modules/globebrowsing/rendering/basicgrid.h b/modules/globebrowsing/rendering/basicgrid.h index 27059ea458..a2f1fd76ef 100644 --- a/modules/globebrowsing/rendering/basicgrid.h +++ b/modules/globebrowsing/rendering/basicgrid.h @@ -38,8 +38,8 @@ class BasicGrid : public Grid { public: /** - \param xRes is the number of grid cells in the x direction. - \param yRes is the number of grid cells in the y direction. + \param xSegments is the number of grid cells in the x direction. + \param ySegments is the number of grid cells in the y direction. \param usePositions determines whether or not to upload any vertex position data to the GPU. \param useTextureCoordinates determines whether or not to upload any vertex texture @@ -48,24 +48,16 @@ public: to the GPU. */ BasicGrid( - unsigned int xRes, - unsigned int yRes, + unsigned int xSegments, + unsigned int ySegments, Geometry::Positions usePositions, Geometry::TextureCoordinates useTextureCoordinates, Geometry::Normals useNormals); ~BasicGrid(); - /** - Returns the number of grid cells in the x direction. Hence the number of vertices - in the x direction is xResolution + 1. - */ - virtual int xResolution() const; - - /** - Returns the number of grid cells in the y direction. Hence the number of vertices - in the y direction is xResolution + 1. - */ - virtual int yResolution() const; + + virtual int xSegments() const; + virtual int ySegments() const; private: virtual std::vector CreateElements( int xRes, int yRes); @@ -73,10 +65,10 @@ private: virtual std::vector CreateTextureCoordinates( int xRes, int yRes); virtual std::vector CreateNormals( int xRes, int yRes); - void validate(int xRes, int yRes); + void validate(int xSegments, int ySegments); - inline size_t numElements(int xRes, int yRes); - inline size_t numVertices(int xRes, int yRes); + inline size_t numElements(int xSegments, int ySegments); + inline size_t numVertices(int xSegments, int ySegments); }; } // namespace openspace #endif // __BASICGRIDGEOMETRY_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/clipmapgrid.cpp b/modules/globebrowsing/rendering/clipmapgrid.cpp index 42ecaeed2a..b535c872d8 100644 --- a/modules/globebrowsing/rendering/clipmapgrid.cpp +++ b/modules/globebrowsing/rendering/clipmapgrid.cpp @@ -36,8 +36,8 @@ namespace openspace { // CLIPMAP GRID (Abstract class) // ////////////////////////////////////////////////////////////////////////////////////////// -ClipMapGrid::ClipMapGrid(unsigned int resolution) - : Grid(resolution, resolution, Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No) +ClipMapGrid::ClipMapGrid(unsigned int segments) + : Grid(segments, segments, Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No) { } @@ -47,32 +47,32 @@ ClipMapGrid::~ClipMapGrid() } -int ClipMapGrid::xResolution() const { - return resolution(); +int ClipMapGrid::xSegments() const { + return segments(); } -int ClipMapGrid::yResolution() const { - return resolution(); +int ClipMapGrid::ySegments() const { + return segments(); } -int ClipMapGrid::resolution() const { - return _xRes; +int ClipMapGrid::segments() const { + return _xSegments; } ////////////////////////////////////////////////////////////////////////////////////////// // OUTER CLIPMAP GRID // ////////////////////////////////////////////////////////////////////////////////////////// -OuterClipMapGrid::OuterClipMapGrid(unsigned int resolution) -: ClipMapGrid(resolution) +OuterClipMapGrid::OuterClipMapGrid(unsigned int segments) +: ClipMapGrid(segments) { _geometry = std::unique_ptr(new Geometry( - CreateElements(resolution, resolution), + CreateElements(segments, segments), Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No)); - _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(resolution, resolution)); + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(segments, segments)); } OuterClipMapGrid::~OuterClipMapGrid() @@ -80,57 +80,57 @@ OuterClipMapGrid::~OuterClipMapGrid() } -size_t OuterClipMapGrid::numElements(int resolution) +size_t OuterClipMapGrid::numElements(int segments) { - int numElementsInTotalSquare = 6 * (resolution + 1) * (resolution + 1); - int numElementsInHole = 6 * (resolution / 4 * resolution / 4); + int numElementsInTotalSquare = 6 * (segments + 1) * (segments + 1); + int numElementsInHole = 6 * (segments / 4 * segments / 4); return numElementsInTotalSquare - numElementsInHole; } -size_t OuterClipMapGrid::numVerticesBottom(int resolution) +size_t OuterClipMapGrid::numVerticesBottom(int segments) { - return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); + return (segments + 1 + 2) * (segments / 4 + 1 + 1); } -size_t OuterClipMapGrid::numVerticesLeft(int resolution) +size_t OuterClipMapGrid::numVerticesLeft(int segments) { - return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); + return (segments / 4 + 1 + 1) * (segments / 2 + 1); } -size_t OuterClipMapGrid::numVerticesRight(int resolution) +size_t OuterClipMapGrid::numVerticesRight(int segments) { - return (resolution / 4 + 1 + 1) * (resolution / 2 + 1); + return (segments / 4 + 1 + 1) * (segments / 2 + 1); } -size_t OuterClipMapGrid::numVerticesTop(int resolution) +size_t OuterClipMapGrid::numVerticesTop(int segments) { - return (resolution + 1 + 2) * (resolution / 4 + 1 + 1); + return (segments + 1 + 2) * (segments / 4 + 1 + 1); } -size_t OuterClipMapGrid::numVertices(int resolution) +size_t OuterClipMapGrid::numVertices(int segments) { - return numVerticesBottom(resolution) + - numVerticesLeft(resolution) + - numVerticesRight(resolution) + - numVerticesTop(resolution); + return numVerticesBottom(segments) + + numVerticesLeft(segments) + + numVerticesRight(segments) + + numVerticesTop(segments); } void OuterClipMapGrid::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 << ")"); + "segments must be equal in x and in y. "); + int segments = xRes; + ghoul_assert(segments >= 8, + "segments must be at least 8. (" << segments << ")"); + ghoul_assert(segments == pow(2, int(log2(segments))), + "segments must be a power of 2. (" << segments << ")"); } std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector elements; - elements.reserve(numElements(resolution)); + elements.reserve(numElements(segments)); // The clipmap geometry is built up by four parts as follows: // 0 = Bottom part @@ -155,17 +155,17 @@ std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { unsigned int previousVerts[4]; previousVerts[0] = 0; - previousVerts[1] = previousVerts[0] + numVerticesBottom(resolution); - previousVerts[2] = previousVerts[1] + numVerticesLeft(resolution); - previousVerts[3] = previousVerts[2] + numVerticesRight(resolution); + previousVerts[1] = previousVerts[0] + numVerticesBottom(segments); + previousVerts[2] = previousVerts[1] + numVerticesLeft(segments); + previousVerts[3] = previousVerts[2] + numVerticesRight(segments); // Build the bottom part of the clipmap geometry - for (unsigned int y = 0; y < resolution / 4 + 1; y++) { - for (unsigned int x = 0; x < resolution + 2; x++) { - GLuint v00 = previousVerts[0] + (y + 0) * (resolution + 3) + x + 0; - GLuint v10 = previousVerts[0] + (y + 0) * (resolution + 3) + x + 1; - GLuint v01 = previousVerts[0] + (y + 1) * (resolution + 3) + x + 0; - GLuint v11 = previousVerts[0] + (y + 1) * (resolution + 3) + x + 1; + for (unsigned int y = 0; y < segments / 4 + 1; y++) { + for (unsigned int x = 0; x < segments + 2; x++) { + GLuint v00 = previousVerts[0] + (y + 0) * (segments + 3) + x + 0; + GLuint v10 = previousVerts[0] + (y + 0) * (segments + 3) + x + 1; + GLuint v01 = previousVerts[0] + (y + 1) * (segments + 3) + x + 0; + GLuint v11 = previousVerts[0] + (y + 1) * (segments + 3) + x + 1; elements.push_back(v00); elements.push_back(v10); @@ -178,12 +178,12 @@ std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { } // Build the left part of the clipmap geometry - for (unsigned int y = 0; y < resolution / 2; y++) { - for (unsigned int x = 0; x < resolution / 4 + 1; x++) { - GLuint v00 = previousVerts[1] + (y + 0) * (resolution / 4 + 2) + x + 0; - GLuint v10 = previousVerts[1] + (y + 0) * (resolution / 4 + 2) + x + 1; - GLuint v01 = previousVerts[1] + (y + 1) * (resolution / 4 + 2) + x + 0; - GLuint v11 = previousVerts[1] + (y + 1) * (resolution / 4 + 2) + x + 1; + for (unsigned int y = 0; y < segments / 2; y++) { + for (unsigned int x = 0; x < segments / 4 + 1; x++) { + GLuint v00 = previousVerts[1] + (y + 0) * (segments / 4 + 2) + x + 0; + GLuint v10 = previousVerts[1] + (y + 0) * (segments / 4 + 2) + x + 1; + GLuint v01 = previousVerts[1] + (y + 1) * (segments / 4 + 2) + x + 0; + GLuint v11 = previousVerts[1] + (y + 1) * (segments / 4 + 2) + x + 1; elements.push_back(v00); elements.push_back(v10); @@ -196,12 +196,12 @@ std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { } // Build the left part of the clipmap geometry - for (unsigned int y = 0; y < resolution / 2; y++) { - for (unsigned int x = 0; x < resolution / 4 + 1; x++) { - GLuint v00 = previousVerts[2] + (y + 0) * (resolution / 4 + 2) + x + 0; - GLuint v10 = previousVerts[2] + (y + 0) * (resolution / 4 + 2) + x + 1; - GLuint v01 = previousVerts[2] + (y + 1) * (resolution / 4 + 2) + x + 0; - GLuint v11 = previousVerts[2] + (y + 1) * (resolution / 4 + 2) + x + 1; + for (unsigned int y = 0; y < segments / 2; y++) { + for (unsigned int x = 0; x < segments / 4 + 1; x++) { + GLuint v00 = previousVerts[2] + (y + 0) * (segments / 4 + 2) + x + 0; + GLuint v10 = previousVerts[2] + (y + 0) * (segments / 4 + 2) + x + 1; + GLuint v01 = previousVerts[2] + (y + 1) * (segments / 4 + 2) + x + 0; + GLuint v11 = previousVerts[2] + (y + 1) * (segments / 4 + 2) + x + 1; elements.push_back(v00); elements.push_back(v10); @@ -214,12 +214,12 @@ std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { } // Build the left part of the clipmap geometry - for (unsigned int y = 0; y < resolution / 4 + 1; y++) { - for (unsigned int x = 0; x < resolution + 2; x++) { - GLuint v00 = previousVerts[3] + (y + 0) * (resolution + 3) + x + 0; - GLuint v10 = previousVerts[3] + (y + 0) * (resolution + 3) + x + 1; - GLuint v01 = previousVerts[3] + (y + 1) * (resolution + 3) + x + 0; - GLuint v11 = previousVerts[3] + (y + 1) * (resolution + 3) + x + 1; + for (unsigned int y = 0; y < segments / 4 + 1; y++) { + for (unsigned int x = 0; x < segments + 2; x++) { + GLuint v00 = previousVerts[3] + (y + 0) * (segments + 3) + x + 0; + GLuint v10 = previousVerts[3] + (y + 0) * (segments + 3) + x + 1; + GLuint v01 = previousVerts[3] + (y + 1) * (segments + 3) + x + 0; + GLuint v11 = previousVerts[3] + (y + 1) * (segments + 3) + x + 1; elements.push_back(v00); elements.push_back(v10); @@ -236,9 +236,9 @@ std::vector OuterClipMapGrid::CreateElements(int xRes, int yRes) { std::vector OuterClipMapGrid::CreatePositions(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector positions; - positions.reserve(numVertices(resolution)); + positions.reserve(numVertices(segments)); std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); // Copy from 2d texture coordinates and use as template to create positions @@ -257,43 +257,43 @@ std::vector OuterClipMapGrid::CreatePositions(int xRes, int yRes) std::vector OuterClipMapGrid::CreateTextureCoordinates(int xRes, int yRes){ validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector textureCoordinates; - textureCoordinates.reserve(numVertices(resolution)); + textureCoordinates.reserve(numVertices(segments)); // Build the bottom part of the clipmap geometry - for (int y = -1; y < resolution / 4 + 1; y++) { - for (int x = -1; x < resolution + 2; x++) { + for (int y = -1; y < segments / 4 + 1; y++) { + for (int x = -1; x < segments + 2; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / resolution, - static_cast(y) / resolution)); + static_cast(x) / segments, + static_cast(y) / segments)); } } // Build the left part of the clipmap geometry - for (int y = resolution / 4; y < 3 * resolution / 4 + 1; y++) { - for (int x = -1; x < resolution / 4 + 1; x++) { + for (int y = segments / 4; y < 3 * segments / 4 + 1; y++) { + for (int x = -1; x < segments / 4 + 1; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / resolution, - static_cast(y) / resolution)); + static_cast(x) / segments, + static_cast(y) / segments)); } } // Build the right part of the clipmap geometry - for (int y = resolution / 4; y < 3 * resolution / 4 + 1; y++) { - for (int x = 3 * resolution / 4; x < resolution + 2; x++) { - float u = static_cast(x) / resolution; - float v = static_cast(y) / resolution; + for (int y = segments / 4; y < 3 * segments / 4 + 1; y++) { + for (int x = 3 * segments / 4; x < segments + 2; x++) { + float u = static_cast(x) / segments; + float v = static_cast(y) / segments; textureCoordinates.push_back(glm::vec2(u, v)); } } // Build the top part of the clipmap geometry - for (int y = 3 * resolution / 4; y < resolution + 2; y++) { - for (int x = -1; x < resolution + 2; x++) { + for (int y = 3 * segments / 4; y < segments + 2; y++) { + for (int x = -1; x < segments + 2; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / resolution, - static_cast(y) / resolution)); + static_cast(x) / segments, + static_cast(y) / segments)); } } @@ -302,12 +302,12 @@ std::vector OuterClipMapGrid::CreateTextureCoordinates(int xRes, int std::vector OuterClipMapGrid::CreateNormals(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector normals; - normals.reserve(numVertices(resolution)); + normals.reserve(numVertices(segments)); - for (int y = -1; y < resolution + 2; y++) { - for (int x = -1; x < resolution + 2; x++) { + for (int y = -1; y < segments + 2; y++) { + for (int x = -1; x < segments + 2; x++) { normals.push_back(glm::vec3(0, 0, 1)); } } @@ -320,16 +320,16 @@ std::vector OuterClipMapGrid::CreateNormals(int xRes, int yRes) { ////////////////////////////////////////////////////////////////////////////////////////// -InnerClipMapGrid::InnerClipMapGrid(unsigned int resolution) - : ClipMapGrid(resolution) +InnerClipMapGrid::InnerClipMapGrid(unsigned int segments) + : ClipMapGrid(segments) { _geometry = std::unique_ptr(new Geometry( - CreateElements(resolution, resolution), + CreateElements(segments, segments), Geometry::Positions::No, Geometry::TextureCoordinates::Yes, Geometry::Normals::No)); - _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(resolution, resolution)); + _geometry->setVertexTextureCoordinates(CreateTextureCoordinates(segments, segments)); } InnerClipMapGrid::~InnerClipMapGrid() @@ -337,30 +337,30 @@ InnerClipMapGrid::~InnerClipMapGrid() } -size_t InnerClipMapGrid::numElements(int resolution) +size_t InnerClipMapGrid::numElements(int segments) { - return resolution * resolution * 6; + return segments * segments * 6; } -size_t InnerClipMapGrid::numVertices(int resolution) +size_t InnerClipMapGrid::numVertices(int segments) { - return (resolution + 1) * (resolution + 1); + return (segments + 1) * (segments + 1); } void InnerClipMapGrid::validate(int xRes, int yRes) { ghoul_assert(xRes == yRes, - "Resolution must be equal in x and in y. "); - int resolution = xRes; - ghoul_assert(resolution >= 1, - "Resolution must be at least 1. (" << resolution << ")"); + "segments must be equal in x and in y. "); + int segments = xRes; + ghoul_assert(segments >= 1, + "segments must be at least 1. (" << segments << ")"); } std::vector InnerClipMapGrid::CreateElements(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector elements; - elements.reserve(numElements(resolution)); + elements.reserve(numElements(segments)); // x v01---v11 x .. // | / | @@ -369,12 +369,12 @@ std::vector InnerClipMapGrid::CreateElements(int xRes, int yRes) { // x x x x .. // : : : : - for (unsigned int y = 0; y < resolution + 2; y++) { - for (unsigned int x = 0; x < resolution + 2; x++) { - GLuint v00 = (y + 0) * (resolution + 3) + x + 0; - GLuint v10 = (y + 0) * (resolution + 3) + x + 1; - GLuint v01 = (y + 1) * (resolution + 3) + x + 0; - GLuint v11 = (y + 1) * (resolution + 3) + x + 1; + for (unsigned int y = 0; y < segments + 2; y++) { + for (unsigned int x = 0; x < segments + 2; x++) { + GLuint v00 = (y + 0) * (segments + 3) + x + 0; + GLuint v10 = (y + 0) * (segments + 3) + x + 1; + GLuint v01 = (y + 1) * (segments + 3) + x + 0; + GLuint v11 = (y + 1) * (segments + 3) + x + 1; elements.push_back(v00); elements.push_back(v10); @@ -392,9 +392,9 @@ std::vector InnerClipMapGrid::CreateElements(int xRes, int yRes) { std::vector InnerClipMapGrid::CreatePositions(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector positions; - positions.reserve(numVertices(resolution)); + positions.reserve(numVertices(segments)); std::vector templateTextureCoords = CreateTextureCoordinates(xRes, yRes); // Copy from 2d texture coordinates and use as template to create positions @@ -413,16 +413,16 @@ std::vector InnerClipMapGrid::CreatePositions(int xRes, int yRes) std::vector InnerClipMapGrid::CreateTextureCoordinates(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector textureCoordinates; - textureCoordinates.reserve(numVertices(resolution)); + textureCoordinates.reserve(numVertices(segments)); // Build the bottom part of the clipmap geometry - for (int y = -1; y < resolution + 2; y++) { - for (int x = -1; x < resolution + 2; x++) { + for (int y = -1; y < segments + 2; y++) { + for (int x = -1; x < segments + 2; x++) { textureCoordinates.push_back(glm::vec2( - static_cast(x) / resolution, - static_cast(y) / resolution)); + static_cast(x) / segments, + static_cast(y) / segments)); } } @@ -431,12 +431,12 @@ std::vector InnerClipMapGrid::CreateTextureCoordinates(int xRes, int std::vector InnerClipMapGrid::CreateNormals(int xRes, int yRes) { validate(xRes, yRes); - int resolution = xRes; + int segments = xRes; std::vector normals; - normals.reserve(numVertices(resolution)); + normals.reserve(numVertices(segments)); - for (int y = -1; y < resolution + 2; y++) { - for (int x = -1; x < resolution + 2; x++) { + for (int y = -1; y < segments + 2; y++) { + for (int x = -1; x < segments + 2; x++) { normals.push_back(glm::vec3(0, 0, 1)); } } diff --git a/modules/globebrowsing/rendering/clipmapgrid.h b/modules/globebrowsing/rendering/clipmapgrid.h index d37d8d9e66..f3e650a2cd 100644 --- a/modules/globebrowsing/rendering/clipmapgrid.h +++ b/modules/globebrowsing/rendering/clipmapgrid.h @@ -48,19 +48,19 @@ snap to the outer layers grid cells. class ClipMapGrid : public Grid { public: - ClipMapGrid(unsigned int resolution); + ClipMapGrid(unsigned int segments); ~ClipMapGrid(); - virtual int xResolution() const; - virtual int yResolution() const; + virtual int xSegments() const; + virtual int ySegments() const; /** - Returns the resolution of the grid. A ClipMapGrid must have the resolution in x and - y direction equal so this function works as a wrapper for xResolution() and - yResolution(). + Returns the segments of the grid. A ClipMapGrid must have the segments in x and + y direction equal so this function works as a wrapper for xSegments() and + ySegments(). */ - int resolution() const; + int segments() const; }; ////////////////////////////////////////////////////////////////////////////////////////// @@ -75,26 +75,26 @@ a smaller ClipMapGrid of half the size can fit. class OuterClipMapGrid : public ClipMapGrid { public: - OuterClipMapGrid(unsigned int resolution); + OuterClipMapGrid(unsigned int segments); ~OuterClipMapGrid(); 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); + virtual std::vector CreateElements(int xSegments, int ySegments); + virtual std::vector CreatePositions(int xSegments, int ySegments); + virtual std::vector CreateTextureCoordinates(int xSegments, int ySegments); + virtual std::vector CreateNormals(int xSegments, int ySegments); private: - void validate(int xRes, int yRes); + void validate(int xSegments, int ySegments); - static size_t numVerticesBottom(int resolution); - static size_t numVerticesLeft(int resolution); - static size_t numVerticesRight(int resolution); - static size_t numVerticesTop(int resolution); + static size_t numVerticesBottom(int segments); + static size_t numVerticesLeft(int segments); + static size_t numVerticesRight(int segments); + static size_t numVerticesTop(int segments); - static size_t numElements(int resolution); - static size_t numVertices(int resolution); + static size_t numElements(int segments); + static size_t numVertices(int segments); }; ////////////////////////////////////////////////////////////////////////////////////////// @@ -109,20 +109,20 @@ a whole where a smaller ClipMapGrid can be positioned. class InnerClipMapGrid : public ClipMapGrid { public: - InnerClipMapGrid(unsigned int resolution); + InnerClipMapGrid(unsigned int segments); ~InnerClipMapGrid(); private: - 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); + virtual std::vector CreateElements( int xSegments, int ySegments); + virtual std::vector CreatePositions(int xSegments, int ySegments); + virtual std::vector CreateTextureCoordinates(int xSegments, int ySegments); + virtual std::vector CreateNormals(int xSegments, int ySegments); - void validate(int xRes, int yRes); + void validate(int xSegments, int ySegments); - static size_t numElements(int resolution); - static size_t numVertices(int resolution); + static size_t numElements(int segments); + static size_t numVertices(int segments); }; } // namespace openspace diff --git a/modules/globebrowsing/rendering/grid.cpp b/modules/globebrowsing/rendering/grid.cpp index fcf82baae7..fa112d4e82 100644 --- a/modules/globebrowsing/rendering/grid.cpp +++ b/modules/globebrowsing/rendering/grid.cpp @@ -31,13 +31,13 @@ namespace { namespace openspace { Grid::Grid( - int xRes, - int yRes, + int xSegments, + int ySegments, Geometry::Positions usePositions, Geometry::TextureCoordinates useTextures, Geometry::Normals useNormals) - : _xRes(xRes) - , _yRes(yRes) + : _xSegments(xSegments) + , _ySegments(ySegments) { } diff --git a/modules/globebrowsing/rendering/grid.h b/modules/globebrowsing/rendering/grid.h index 2942faf9fb..496319b47a 100644 --- a/modules/globebrowsing/rendering/grid.h +++ b/modules/globebrowsing/rendering/grid.h @@ -38,8 +38,8 @@ class Grid { public: Grid( - int xRes, - int yRes, + int xSegments, + int ySegments, Geometry::Positions usePositions = Geometry::Positions::No, Geometry::TextureCoordinates useTextures = Geometry::TextureCoordinates::No, Geometry::Normals useNormals = Geometry::Normals::No); @@ -47,19 +47,28 @@ public: Geometry& geometry(); - virtual int xResolution() const = 0; - virtual int yResolution() const = 0; + /** + Returns the number of grid cells in the x direction. Hence the number of vertices + in the x direction is xResolution + 1. + */ + virtual int xSegments() const = 0; + + /** + Returns the number of grid cells in the y direction. Hence the number of vertices + in the y direction is xResolution + 1. + */ + virtual int ySegments() const = 0; 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; + virtual std::vector CreateElements( int xSegments, int ySegments) = 0; + virtual std::vector CreatePositions( int xSegments, int ySegments) = 0; + virtual std::vector CreateTextureCoordinates( int xSegments, int ySegments) = 0; + virtual std::vector CreateNormals( int xSegments, int ySegments) = 0; std::unique_ptr _geometry; - const int _xRes; - const int _yRes; + const int _xSegments; + const int _ySegments; }; } // namespace openspace #endif // __GRIDGEOMETRY_H__ \ No newline at end of file diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index b33252ffb3..06317110f6 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -183,7 +183,7 @@ namespace openspace { mat4 modelTransform = translate(mat4(1), data.position.vec3()); // Snap patch position - int segmentsPerPatch = _grid->resolution(); + int segmentsPerPatch = _grid->segments(); LatLon stepSize = LatLon( patchSize.lat / segmentsPerPatch, patchSize.lon / segmentsPerPatch); From 198c284e23094f3b9a82ffc4f7364445d1e8a0d6 Mon Sep 17 00:00:00 2001 From: Kalle Bladin Date: Fri, 22 Apr 2016 12:38:13 -0400 Subject: [PATCH 8/8] Using number of segments per patch in rendering. --- modules/globebrowsing/rendering/patchrenderer.cpp | 2 ++ modules/globebrowsing/shaders/simple_fs.glsl | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/globebrowsing/rendering/patchrenderer.cpp b/modules/globebrowsing/rendering/patchrenderer.cpp index 06317110f6..13c012c2ce 100644 --- a/modules/globebrowsing/rendering/patchrenderer.cpp +++ b/modules/globebrowsing/rendering/patchrenderer.cpp @@ -133,6 +133,7 @@ namespace openspace { _programObject->setUniform("uvTransformPatchToTile", uvTransform); LatLon swCorner = patch.southWestCorner(); + _programObject->setUniform("segmentsPerPatch", _grid->xSegments()); _programObject->setUniform("modelViewProjectionTransform", modelViewProjectionTransform); _programObject->setUniform("minLatLon", vec2(swCorner.toLonLatVec2())); _programObject->setUniform("lonLatScalingFactor", vec2(patch.size().toLonLatVec2())); @@ -224,6 +225,7 @@ namespace openspace { _programObject->setUniform( "modelViewProjectionTransform", data.camera.projectionMatrix() * viewTransform * modelTransform); + _programObject->setUniform("segmentsPerPatch", segmentsPerPatch); _programObject->setUniform("minLatLon", vec2(newPatch.southWestCorner().toLonLatVec2())); _programObject->setUniform("lonLatScalingFactor", vec2(patchSize.toLonLatVec2())); _programObject->setUniform("globeRadius", float(radius)); diff --git a/modules/globebrowsing/shaders/simple_fs.glsl b/modules/globebrowsing/shaders/simple_fs.glsl index fb4bad1a48..00549ccd48 100644 --- a/modules/globebrowsing/shaders/simple_fs.glsl +++ b/modules/globebrowsing/shaders/simple_fs.glsl @@ -38,6 +38,8 @@ uniform sampler2D nightTex; uniform sampler2D textureSampler; uniform mat3 uvTransformPatchToTile; +uniform int segmentsPerPatch; + in vec4 vs_position; in vec2 vs_uv; @@ -49,7 +51,7 @@ Fragment getFragment() { Fragment frag; frag.color = texture(textureSampler, vec2(uvTransformPatchToTile * vec3(vs_uv.s, vs_uv.t, 1))); - frag.color = frag.color * 1.0 + vec4(fract(vs_uv * 32), 0.4,1) * 0.0; + frag.color = frag.color * 1.0 + vec4(fract(vs_uv * segmentsPerPatch), 0.4,1) * 0.4; frag.depth = pscDepth(vs_position); return frag;