Added subclass GridGeometry, and made minor changes to the base class Geometry

This commit is contained in:
Erik Broberg
2016-03-31 12:34:36 -04:00
parent c31d21fb63
commit 83eb210f4a
4 changed files with 255 additions and 19 deletions

View File

@@ -1,5 +1,4 @@
#include <modules/planetbrowsing/rendering/geometry.h>
#include <ghoul/logging/logmanager.h>
namespace {
const std::string _loggerCat = "Geometry";
@@ -8,17 +7,14 @@ namespace {
namespace openspace
{
Geometry::Geometry(
std::vector<unsigned int> 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<unsigned int> 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<glm::vec4> positions) {
_vertexData.resize(positions.size());
for (size_t i = 0; i < positions.size(); i++)
{
_vertexData[i].position[0] = static_cast<GLfloat>(positions[i].x);
@@ -40,6 +37,7 @@ void Geometry::setPositionData(std::vector<glm::vec4> positions) {
}
void Geometry::setTextureData(std::vector<glm::vec2> textures) {
_vertexData.resize(textures.size());
for (size_t i = 0; i < textures.size(); i++)
{
_vertexData[i].texture[0] = static_cast<GLfloat>(textures[i].s);
@@ -48,6 +46,7 @@ void Geometry::setTextureData(std::vector<glm::vec2> textures) {
}
void Geometry::setNormalData(std::vector<glm::vec3> normals) {
_vertexData.resize(normals.size());
for (size_t i = 0; i < normals.size(); i++)
{
_vertexData[i].normal[0] = static_cast<GLfloat>(normals[i].x);
@@ -57,6 +56,7 @@ void Geometry::setNormalData(std::vector<glm::vec3> normals) {
}
void Geometry::setElementData(std::vector<unsigned int> elements) {
_elementData.resize(elements.size());
for (size_t i = 0; i < elements.size(); i++)
{
_elementData[i] = static_cast<GLuint>(elements[i]);

View File

@@ -26,6 +26,8 @@
#define __GEOMETRY_H__
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/logging/logmanager.h>
#include <glm/glm.hpp>
#include <vector>
@@ -47,9 +49,9 @@ public:
Geometry(
std::vector<unsigned int> 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

View File

@@ -0,0 +1,165 @@
#include <modules/planetbrowsing/rendering/gridgeometry.h>
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<GLuint> GridGeometry::CreateElements(unsigned int xRes, unsigned int yRes) {
//LDEBUG("CreateElements");
validate(xRes, yRes);
std::vector<GLuint> 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<glm::vec4> GridGeometry::CreatePositions(unsigned int xRes, unsigned int yRes,
float xSize, float ySize, float xOffset, float yOffset)
{
//LDEBUG("CreatePositions");
validate(xRes, yRes);
std::vector<glm::vec4> positions;
positions.reserve(numVertices(xRes, yRes));
glm::vec2 delta(
xSize / static_cast<float>(xRes-1),
ySize / static_cast<float>(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<float>(x) * delta.x + xOffset,
static_cast<float>(y) * delta.y + yOffset,
0.0f,
1.0f
));
//const glm::vec4 v = positions.back();
//LDEBUG(v.x << ", " << v.y);
}
}
return positions;
}
std::vector<glm::vec2> GridGeometry::CreateTextureCoordinates(unsigned int xRes, unsigned int yRes){
//LDEBUG("CreateTextureCoordinates");
validate(xRes, yRes);
std::vector<glm::vec2> textureCoordinates;
textureCoordinates.reserve(numVertices(xRes, yRes));
glm::vec2 delta(
1.0f / static_cast<float>(xRes - 1),
1.0f / static_cast<float>(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<float>(x) * delta.x,
static_cast<float>(y) * delta.y
));
//const glm::vec2 v = textureCoordinates.back();
//LDEBUG(v.s << ", " << v.t);
}
}
return textureCoordinates;
}
std::vector<glm::vec3> GridGeometry::CreateNormals(unsigned int xRes, unsigned int yRes) {
//LDEBUG("CreateNormals");
validate(xRes, yRes);
std::vector<glm::vec3> 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

View File

@@ -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 <ghoul/opengl/ghoul_gl.h>
#include <glm/glm.hpp>
#include <modules/planetbrowsing/rendering/geometry.h>
#include <vector>
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<GLuint> CreateElements(unsigned int xRes, unsigned int yRes);
static std::vector<glm::vec4> 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<glm::vec2> CreateTextureCoordinates(unsigned int xRes, unsigned int yRes);
static std::vector<glm::vec3> CreateNormals(unsigned int xRes, unsigned int yRes);
static void validate(unsigned int xRes, unsigned int yRes);
};
} // namespace openspace
#endif // __GRIDGEOMETRY_H__