diff --git a/modules/planetbrowsing/rendering/geometry.cpp b/modules/planetbrowsing/rendering/geometry.cpp index 824e592c47..8e99d0e53b 100644 --- a/modules/planetbrowsing/rendering/geometry.cpp +++ b/modules/planetbrowsing/rendering/geometry.cpp @@ -1,5 +1,4 @@ #include -#include namespace { const std::string _loggerCat = "Geometry"; @@ -8,17 +7,14 @@ namespace { namespace openspace { -Geometry::Geometry( - std::vector elements, - Positions usePositions, - Textures useTextures, - Normals useNormals) : - _vaoID(0), - _vertexBufferID(0), - _elementBufferID(0), - _usePositions(usePositions == Positions::Yes ? true : false), - _useTextures(useTextures == Textures::Yes ? true : false), - _useNormals(useNormals == Normals::Yes ? true : false) +Geometry::Geometry(std::vector elements, + Positions usePositions, Textures useTextures, Normals useNormals) + : _vaoID(0) + ,_vertexBufferID(0) + ,_elementBufferID(0) + ,_usePositions(usePositions == Positions::Yes) + ,_useTextures(useTextures == Textures::Yes) + ,_useNormals(useNormals == Normals::Yes) { setElementData(elements); } @@ -30,6 +26,7 @@ Geometry::~Geometry() { } void Geometry::setPositionData(std::vector positions) { + _vertexData.resize(positions.size()); for (size_t i = 0; i < positions.size(); i++) { _vertexData[i].position[0] = static_cast(positions[i].x); @@ -40,6 +37,7 @@ void Geometry::setPositionData(std::vector positions) { } void Geometry::setTextureData(std::vector textures) { + _vertexData.resize(textures.size()); for (size_t i = 0; i < textures.size(); i++) { _vertexData[i].texture[0] = static_cast(textures[i].s); @@ -48,6 +46,7 @@ void Geometry::setTextureData(std::vector textures) { } void Geometry::setNormalData(std::vector normals) { + _vertexData.resize(normals.size()); for (size_t i = 0; i < normals.size(); i++) { _vertexData[i].normal[0] = static_cast(normals[i].x); @@ -57,6 +56,7 @@ void Geometry::setNormalData(std::vector normals) { } void Geometry::setElementData(std::vector elements) { + _elementData.resize(elements.size()); for (size_t i = 0; i < elements.size(); i++) { _elementData[i] = static_cast(elements[i]); diff --git a/modules/planetbrowsing/rendering/geometry.h b/modules/planetbrowsing/rendering/geometry.h index 9768f7ce98..dd2ba89de1 100644 --- a/modules/planetbrowsing/rendering/geometry.h +++ b/modules/planetbrowsing/rendering/geometry.h @@ -26,6 +26,8 @@ #define __GEOMETRY_H__ #include +#include + #include #include @@ -47,9 +49,9 @@ public: Geometry( std::vector elements, // At least elements are required - Positions usePositions, - Textures useTextures, - Normals useNormals); + Positions usePositions = Positions::No, + Textures useTextures = Textures::No, + Normals useNormals = Normals::No); ~Geometry(); // Setters @@ -64,6 +66,14 @@ public: bool initialize(); void render() const; + +protected: + + // Determines what attribute data is in use + bool _usePositions; + bool _useTextures; + bool _useNormals; + private: typedef struct { GLfloat position[4]; @@ -80,10 +90,6 @@ private: GLuint _vertexBufferID; GLuint _elementBufferID; - // Determines what attribute data is in use - const bool _usePositions; - const bool _useTextures; - const bool _useNormals; }; } // namespace openspace diff --git a/modules/planetbrowsing/rendering/gridgeometry.cpp b/modules/planetbrowsing/rendering/gridgeometry.cpp new file mode 100644 index 0000000000..2b29e1ce52 --- /dev/null +++ b/modules/planetbrowsing/rendering/gridgeometry.cpp @@ -0,0 +1,165 @@ +#include + + + +namespace { + const std::string _loggerCat = "GridGeometry"; +} + +namespace openspace { + +GridGeometry::GridGeometry(unsigned int xRes, unsigned int yRes, Positions usePositions, Textures useTextures, Normals useNormals) + : Geometry(CreateElements(xRes, yRes), usePositions, useTextures, useNormals) +{ + if(_usePositions){ + setPositionData(CreatePositions(xRes, yRes)); + } + + if (_useTextures) { + setTextureData(CreateTextureCoordinates(xRes, yRes)); + } + + if (_useNormals) { + setNormalData(CreateNormals(xRes, yRes)); + } + +} + + +GridGeometry::~GridGeometry() +{ + +} + +void GridGeometry::validate(unsigned int xRes, unsigned int yRes) { + ghoul_assert(xRes > 0 && yRes > 0, + "Resolution must be larger than 1x1. (" << xRes << ", " << 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) { + //LDEBUG("CreateElements"); + + 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 v00---v10 x .. + // | / | + // x v01---v11 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(v01); + + // add lower triangle + elements.push_back(v01); + elements.push_back(v10); + elements.push_back(v11); + + //LDEBUG(v00 << ", " << v10 << ", " << v01); + //LDEBUG(v01 << ", " << v10 << ", " << v11); + } + } + + return elements; +} + +std::vector GridGeometry::CreatePositions(unsigned int xRes, unsigned int yRes, + float xSize, float ySize, float xOffset, float yOffset) +{ + //LDEBUG("CreatePositions"); + + validate(xRes, yRes); + std::vector positions; + positions.reserve(numVertices(xRes, yRes)); + + glm::vec2 delta( + xSize / static_cast(xRes-1), + ySize / static_cast(yRes-1) + ); + + for (unsigned int y = 0; y < yRes; y++) { + for (unsigned int x = 0; x < xRes; x++) { + positions.push_back(glm::vec4( + static_cast(x) * delta.x + xOffset, + static_cast(y) * delta.y + yOffset, + 0.0f, + 1.0f + )); + //const glm::vec4 v = positions.back(); + //LDEBUG(v.x << ", " << v.y); + } + } + + return positions; +} + + +std::vector GridGeometry::CreateTextureCoordinates(unsigned int xRes, unsigned int yRes){ + //LDEBUG("CreateTextureCoordinates"); + + validate(xRes, yRes); + std::vector textureCoordinates; + textureCoordinates.reserve(numVertices(xRes, yRes)); + + glm::vec2 delta( + 1.0f / static_cast(xRes - 1), + 1.0f / static_cast(yRes - 1) + ); + + for (unsigned int y = 0; y < yRes; y++) { + for (unsigned int x = 0; x < xRes; x++) { + textureCoordinates.push_back(glm::vec2( + static_cast(x) * delta.x, + static_cast(y) * delta.y + )); + //const glm::vec2 v = textureCoordinates.back(); + //LDEBUG(v.s << ", " << v.t); + } + } + + return textureCoordinates; +} + + + +std::vector GridGeometry::CreateNormals(unsigned int xRes, unsigned int yRes) { + //LDEBUG("CreateNormals"); + + 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)); + + //const glm::vec3 v = normals.back(); + //LDEBUG(v.x << ", " << v.y << ", " << v.z); + } + } + + return normals; +} + + +}// namespace openspace \ No newline at end of file diff --git a/modules/planetbrowsing/rendering/gridgeometry.h b/modules/planetbrowsing/rendering/gridgeometry.h new file mode 100644 index 0000000000..c2a26bcd9f --- /dev/null +++ b/modules/planetbrowsing/rendering/gridgeometry.h @@ -0,0 +1,65 @@ +/***************************************************************************************** +* * +* 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 __GRIDGEOMETRY_H__ +#define __GRIDGEOMETRY_H__ + + + +#include +#include + +#include + +#include + + +namespace openspace { + +class GridGeometry : public Geometry +{ +public: + GridGeometry(unsigned int xRes, unsigned int yRes, + Positions usePositions = Positions::No, + Textures useTextures = Textures::No, + Normals useNormals = Normals::No + ); + + ~GridGeometry(); + + inline static size_t numElements(unsigned int xRes, unsigned int yRes); + static size_t numVertices(unsigned int xRes, unsigned int yRes); + +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); + + static void validate(unsigned int xRes, unsigned int yRes); +}; +} // namespace openspace +#endif // __GRIDGEOMETRY_H__ \ No newline at end of file