Clean up in the geometry classes.

This commit is contained in:
kbladin
2016-04-13 10:23:42 -04:00
parent 23b28f9024
commit eeaa4925ba
6 changed files with 98 additions and 110 deletions
@@ -62,7 +62,9 @@ const unsigned int ClipMapGeometry::resolution() const {
size_t ClipMapGeometry::numElements(unsigned int resolution)
{
return 6 * ((resolution + 1) * (resolution + 1) - (resolution / 4 * resolution / 4));
int numElementsInTotalSquare = 6 * (resolution + 1) * (resolution + 1);
int numElementsInHole = 6 * (resolution / 4 * resolution / 4);
return numElementsInTotalSquare - numElementsInHole;
}
size_t ClipMapGeometry::numVerticesBottom(unsigned int resolution)
@@ -105,6 +107,20 @@ std::vector<GLuint> ClipMapGeometry::CreateElements(unsigned int resolution) {
std::vector<GLuint> elements;
elements.reserve(numElements(resolution));
// The clipmap geometry is built up by four parts as follows:
// 0 = Bottom part
// 1 = Left part
// 2 = Right part
// 3 = Top part
//
// 33333333
// 33333333
// 11 22
// 11 22
// 00000000
// 00000000
// x v01---v11 x ..
// | / |
// x v00---v10 x ..
@@ -197,67 +213,68 @@ std::vector<glm::vec4> ClipMapGeometry::CreatePositions(unsigned int resolution)
validate(resolution);
std::vector<glm::vec4> positions;
positions.reserve(numVertices(resolution));
std::vector<glm::vec2> textureCoordinates = CreateTextureCoordinates(resolution);
std::vector<glm::vec2> templateTextureCoords = CreateTextureCoordinates(resolution);
// Copy from texture coordinates
for (unsigned int i = 0; i < textureCoordinates.size(); i++) {
// 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(
textureCoordinates[i].x,
textureCoordinates[i].y,
templateTextureCoords[i].x,
templateTextureCoords[i].y,
0,
1));
}
return positions;
}
std::vector<glm::vec2> ClipMapGeometry::CreateTextureCoordinates(int resolution){
std::vector<glm::vec2> ClipMapGeometry::CreateTextureCoordinates(unsigned int resolution){
validate(resolution);
std::vector<glm::vec2> 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 < resolution / 4 + 1; y++) {
for (int x = -1; x < resolution + 2; x++) {
for (int y = -1; y < intResolution / 4 + 1; y++) {
for (int x = -1; x < intResolution + 2; x++) {
textureCoordinates.push_back(glm::vec2(
static_cast<float>(x) / resolution,
static_cast<float>(y) / resolution));
static_cast<float>(x) / intResolution,
static_cast<float>(y) / intResolution));
}
}
// 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 = intResolution / 4; y < 3 * intResolution / 4 + 1; y++) {
for (int x = -1; x < intResolution / 4 + 1; x++) {
textureCoordinates.push_back(glm::vec2(
static_cast<float>(x) / resolution,
static_cast<float>(y) / resolution));
static_cast<float>(x) / intResolution,
static_cast<float>(y) / intResolution));
}
}
// 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<float>(x) / resolution;
float v = static_cast<float>(y) / resolution;
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<float>(x) / intResolution;
float v = static_cast<float>(y) / intResolution;
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 * intResolution / 4; y < intResolution + 2; y++) {
for (int x = -1; x < intResolution + 2; x++) {
textureCoordinates.push_back(glm::vec2(
static_cast<float>(x) / resolution,
static_cast<float>(y) / resolution));
static_cast<float>(x) / intResolution,
static_cast<float>(y) / intResolution));
}
}
return textureCoordinates;
}
std::vector<glm::vec3> ClipMapGeometry::CreateNormals(unsigned int resolution) {
validate(resolution);
std::vector<glm::vec3> normals;
@@ -38,7 +38,7 @@ public:
ClipMapGeometry(
unsigned int resolution,
Positions usePositions = Positions::No,
TextureCoordinates useTextures = TextureCoordinates::No,
TextureCoordinates useTextures = TextureCoordinates::Yes,
Normals useNormals = Normals::No
);
@@ -54,15 +54,16 @@ public:
static size_t numElements(unsigned int resolution);
static size_t numVertices(unsigned int resolution);
private:
static std::vector<GLuint> CreateElements(unsigned int resoluion);
static std::vector<glm::vec4> CreatePositions(unsigned int resolution);
static std::vector<glm::vec2> CreateTextureCoordinates(int resolution);
static std::vector<glm::vec3> CreateNormals(unsigned int resolution);
static std::vector<GLuint> CreateElements(unsigned int resoluion);
static std::vector<glm::vec4> CreatePositions(unsigned int resolution);
static std::vector<glm::vec2> CreateTextureCoordinates(unsigned int resolution);
static std::vector<glm::vec3> CreateNormals(unsigned int resolution);
static void validate(unsigned 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;
};
} // namespace openspace
@@ -8,28 +8,27 @@ namespace {
namespace openspace {
GridGeometry::GridGeometry(unsigned int xRes, unsigned int yRes, Positions usePositions, TextureCoordinates useTextures, Normals useNormals)
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;
_yRes = yRes;
}
GridGeometry::~GridGeometry()
{
@@ -43,7 +42,6 @@ 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 << ")");
@@ -63,10 +61,7 @@ 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;
@@ -74,96 +69,75 @@ std::vector<GLuint> GridGeometry::CreateElements(unsigned int xRes, unsigned int
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 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;
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);
// add lower triangle
elements.push_back(v00);
elements.push_back(v11);
elements.push_back(v01);
}
}
return elements;
}
std::vector<glm::vec4> GridGeometry::CreatePositions(unsigned int xRes, unsigned int yRes,
float xSize, float ySize, float xOffset, float yOffset)
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
// Copy from 2d texture coordinates and use as template to create positions
std::vector<glm::vec2> templateTextureCoords = CreateTextureCoordinates(xRes, yRes);
for (unsigned int i = 0; i < templateTextureCoords.size(); i++)
{
positions.push_back(glm::vec4(
templateTextureCoords[i],
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
static_cast<float>(x) / static_cast<float>(xRes - 1),
static_cast<float>(y) / static_cast<float>(yRes - 1)
));
//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));
@@ -171,14 +145,10 @@ std::vector<glm::vec3> GridGeometry::CreateNormals(unsigned int xRes, unsigned i
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
+13 -7
View File
@@ -26,8 +26,6 @@
#ifndef __GRIDGEOMETRY_H__
#define __GRIDGEOMETRY_H__
#include <ghoul/opengl/ghoul_gl.h>
#include <glm/glm.hpp>
@@ -35,13 +33,14 @@
#include <vector>
namespace openspace {
class GridGeometry : public Geometry
{
public:
GridGeometry(unsigned int xRes, unsigned int yRes,
GridGeometry(
unsigned int xRes,
unsigned int yRes,
Positions usePositions = Positions::No,
TextureCoordinates useTextures = TextureCoordinates::No,
Normals useNormals = Normals::No
@@ -57,9 +56,16 @@ public:
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::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);
inline static void validate(unsigned int xRes, unsigned int yRes);
@@ -143,11 +143,7 @@ namespace openspace {
// CLIPMAP PATCH RENDERER //
//////////////////////////////////////////////////////////////////////////////////////
ClipMapPatchRenderer::ClipMapPatchRenderer()
: PatchRenderer(shared_ptr<ClipMapGeometry>(new ClipMapGeometry(
32,
Geometry::Positions::No,
Geometry::TextureCoordinates::Yes,
Geometry::Normals::No)))
: PatchRenderer(shared_ptr<ClipMapGeometry>(new ClipMapGeometry(32)))
{
_programObject = OsEng.renderEngine().buildRenderProgram(
"LatLonSphereMappingProgram",
@@ -68,11 +68,9 @@ namespace openspace {
if (_target != "")
setBody(_target);
// Mainly for debugging purposes @AA
addProperty(_rotation);
addSwitchValue(std::shared_ptr<ClipMapGlobe>(new ClipMapGlobe(dictionary)), 1e9);
//addSwitchValue(std::shared_ptr<ChunkLodGlobe>(new ChunkLodGlobe(dictionary)), 1e9);
addSwitchValue(std::shared_ptr<GlobeMesh>(new GlobeMesh(dictionary)), 1e10);