fixed line spacing and added BoundingBox support

This commit is contained in:
ZappoMan 2013-06-11 18:37:31 -07:00
parent 6d3ec662a1
commit b3ef9868e3
2 changed files with 157 additions and 138 deletions

View file

@ -1,87 +1,95 @@
// //
// VoxelProjectedShadow.cpp - The projected shadow (on the 2D view plane) for a voxel // VoxelProjectedShadow.cpp - The projected shadow (on the 2D view plane) for a voxel
// hifi // hifi
// //
// Added by Brad Hefta-Gaub on 06/11/13. // Added by Brad Hefta-Gaub on 06/11/13.
// //
#include "VoxelProjectedShadow.h" #include "VoxelProjectedShadow.h"
#include "GeometryUtil.h" #include "GeometryUtil.h"
void VoxelProjectedShadow::setVertex(int vertex, const glm::vec2& point) {
_vertices[vertex] = point; bool BoundingBox::contains(const BoundingBox& box) const {
return (
// keep track of our bounding box (box.corner.x >= corner.x) &&
if (point.x > _maxX) { (box.corner.y >= corner.y) &&
_maxX = point.x; (box.corner.x + box.size.x <= corner.x + size.x) &&
} (box.corner.y + box.size.y <= corner.y + size.y)
if (point.y > _maxY) { );
_maxY = point.y; };
}
if (point.x < _minX) { void VoxelProjectedShadow::setVertex(int vertex, const glm::vec2& point) {
_minX = point.x; _vertices[vertex] = point;
}
if (point.y < _minY) { // keep track of our bounding box
_minY = point.y; if (point.x > _maxX) {
} _maxX = point.x;
}
}; if (point.y > _maxY) {
_maxY = point.y;
bool VoxelProjectedShadow::occludes(const VoxelProjectedShadow& occludee) const { }
if (point.x < _minX) {
// first check the bounding boxes, the occludee mush be fully within the boounding box of this shadow _minX = point.x;
if ((occludee.getMaxX() > getMaxX()) || }
(occludee.getMaxY() > getMaxY()) || if (point.y < _minY) {
(occludee.getMinX() < getMinX()) || _minY = point.y;
(occludee.getMinY() < getMinY())) { }
return false;
} };
// if we got this far, then check each vertex of the occludee, if all those points bool VoxelProjectedShadow::occludes(const VoxelProjectedShadow& occludee) const {
// are inside our polygon, then the tested occludee is fully occluded
for(int i = 0; i < occludee.getVertexCount(); i++) { // first check the bounding boxes, the occludee mush be fully within the boounding box of this shadow
if (!pointInside(occludee.getVertex(i))) { if ((occludee.getMaxX() > getMaxX()) ||
return false; (occludee.getMaxY() > getMaxY()) ||
} (occludee.getMinX() < getMinX()) ||
} (occludee.getMinY() < getMinY())) {
return false;
// if we got this far, then indeed the occludee is fully occluded by us }
return true;
} // if we got this far, then check each vertex of the occludee, if all those points
// are inside our polygon, then the tested occludee is fully occluded
bool VoxelProjectedShadow::pointInside(const glm::vec2& point) const { for(int i = 0; i < occludee.getVertexCount(); i++) {
bool pointInside = false; // assume the worst if (!pointInside(occludee.getVertex(i))) {
return false;
// first check the bounding boxes, the point mush be fully within the boounding box of this shadow }
if ((point.x > getMaxX()) || }
(point.y > getMaxY()) ||
(point.x < getMinX()) || // if we got this far, then indeed the occludee is fully occluded by us
(point.y < getMinY())) { return true;
return false; }
}
bool VoxelProjectedShadow::pointInside(const glm::vec2& point) const {
float e = (getMaxX() - getMinX()) / 100.0f; // some epsilon // first check the bounding boxes, the point mush be fully within the boounding box of this shadow
if ((point.x > getMaxX()) ||
// We need to have one ray that goes from a known outside position to the point in question. We'll pick a (point.y > getMaxY()) ||
// start point just outside of our min X (point.x < getMinX()) ||
glm::vec2 r1p1(getMinX() - e, point.y); (point.y < getMinY())) {
glm::vec2 r1p2(point); return false;
}
glm::vec2 r2p1(getVertex(getVertexCount()-1)); // start with last vertex to first vertex
glm::vec2 r2p2; float e = (getMaxX() - getMinX()) / 100.0f; // some epsilon
// Test the ray against all sides // We need to have one ray that goes from a known outside position to the point in question. We'll pick a
int intersections = 0; // start point just outside of our min X
for (int i = 0; i < getVertexCount(); i++) { glm::vec2 r1p1(getMinX() - e, point.y);
r2p2 = getVertex(i); glm::vec2 r1p2(point);
if (doLineSegmentsIntersect(r1p1, r1p2, r2p1, r2p2)) {
intersections++; glm::vec2 r2p1(getVertex(getVertexCount()-1)); // start with last vertex to first vertex
} glm::vec2 r2p2;
r2p1 = r2p2; // set up for next side
} // Test the ray against all sides
int intersections = 0;
// If odd number of intersections, we're inside for (int i = 0; i < getVertexCount(); i++) {
return ((intersections & 1) == 1); r2p2 = getVertex(i);
} if (doLineSegmentsIntersect(r1p1, r1p2, r2p1, r2p2)) {
intersections++;
}
r2p1 = r2p2; // set up for next side
}
// If odd number of intersections, we're inside
return ((intersections & 1) == 1);
}

View file

@ -1,51 +1,62 @@
// //
// VoxelProjectedShadow.h - The projected shadow (on the 2D view plane) for a voxel // VoxelProjectedShadow.h - The projected shadow (on the 2D view plane) for a voxel
// hifi // hifi
// //
// Added by Brad Hefta-Gaub on 06/11/13. // Added by Brad Hefta-Gaub on 06/11/13.
// //
#ifndef _VOXEL_PROJECTED_SHADOW_ #ifndef _VOXEL_PROJECTED_SHADOW_
#define _VOXEL_PROJECTED_SHADOW_ #define _VOXEL_PROJECTED_SHADOW_
#include <glm/glm.hpp> #include <glm/glm.hpp>
const int MAX_SHADOW_VERTEX_COUNT = 6; const int MAX_SHADOW_VERTEX_COUNT = 6;
typedef glm::vec2 ShadowVertices[MAX_SHADOW_VERTEX_COUNT]; typedef glm::vec2 ShadowVertices[MAX_SHADOW_VERTEX_COUNT];
class VoxelProjectedShadow class BoundingBox {
{ public:
BoundingBox(glm::vec2 corner, glm::vec2 size) : corner(corner), size(size) {};
public: glm::vec2 corner;
glm::vec2 size;
VoxelProjectedShadow() : _vertexCount(0), _maxX(0.0f), _maxY(0.0f), _minX(FLT_MAX), _minY(FLT_MAX) { }; bool contains(const BoundingBox& box) const;
VoxelProjectedShadow(int vertexCount) : _vertexCount(vertexCount), _maxX(0.0f), _maxY(0.0f), _minX(FLT_MAX), _minY(FLT_MAX) };
{ };
class VoxelProjectedShadow {
~VoxelProjectedShadow() { };
const ShadowVertices& getVerices() const { return _vertices; }; public:
const glm::vec2& getVertex(int i) const { return _vertices[i]; };
void setVertex(int vertex, const glm::vec2& point); VoxelProjectedShadow(int vertexCount = 0) :
int getVertexCount() const { return _vertexCount; }; _vertexCount(vertexCount), _maxX(-FLT_MAX), _maxY(-FLT_MAX), _minX(FLT_MAX), _minY(FLT_MAX)
void setVertexCount(int vertexCount) { _vertexCount = vertexCount; }; { };
bool occludes(const VoxelProjectedShadow& occludee) const; ~VoxelProjectedShadow() { };
bool pointInside(const glm::vec2& point) const; const ShadowVertices& getVerices() const { return _vertices; };
const glm::vec2& getVertex(int i) const { return _vertices[i]; };
float getMaxX() const { return _maxX; } void setVertex(int vertex, const glm::vec2& point);
float getMaxY() const { return _maxY; } int getVertexCount() const { return _vertexCount; };
float getMinX() const { return _minX; } void setVertexCount(int vertexCount) { _vertexCount = vertexCount; };
float getMinY() const { return _minY; }
bool occludes(const VoxelProjectedShadow& occludee) const;
private: bool pointInside(const glm::vec2& point) const;
int _vertexCount;
ShadowVertices _vertices; float getMaxX() const { return _maxX; }
float _maxX; float getMaxY() const { return _maxY; }
float _maxY; float getMinX() const { return _minX; }
float _minX; float getMinY() const { return _minY; }
float _minY;
}; BoundingBox getBoundingBox() const {
return BoundingBox(glm::vec2(_minX,_minY), glm::vec2(_maxX - _minX, _maxY - _minY));
};
#endif // _VOXEL_PROJECTED_SHADOW_
private:
int _vertexCount;
ShadowVertices _vertices;
float _maxX;
float _maxY;
float _minX;
float _minY;
};
#endif // _VOXEL_PROJECTED_SHADOW_