Skip to content

Commit

Permalink
Merge branch 'orbweaver/master' into 'greebo/master'
Browse files Browse the repository at this point in the history
This includes the recent changes to light rendering in the 3D view, making
CamRenderer responsible for lighting intersection calculations.
  • Loading branch information
Matthew Mott committed Nov 27, 2020
2 parents 4645016 + faca571 commit 9934407
Show file tree
Hide file tree
Showing 35 changed files with 818 additions and 789 deletions.
4 changes: 3 additions & 1 deletion include/irender.h
Expand Up @@ -4,6 +4,7 @@
#include <functional>

#include "math/Vector3.h"
#include "math/AABB.h"

#include "ShaderLayer.h"
#include <sigc++/signal.h>
Expand Down Expand Up @@ -231,7 +232,8 @@ typedef std::shared_ptr<RendererLight> RendererLightPtr;
/// Debug stream insertion for RendererLight
inline std::ostream& operator<< (std::ostream& os, const RendererLight& l)
{
return os << "RendererLight(origin=" << l.worldOrigin() << ")";
return os << "RendererLight(origin=" << l.worldOrigin().pp()
<< ", lightAABB=" << l.lightAABB() << ")";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/irenderable.h
Expand Up @@ -80,7 +80,7 @@ class RenderableCollector
*
*/
virtual void addLitRenderable(Shader& shader,
OpenGLRenderable& renderable,
const OpenGLRenderable& renderable,
const Matrix4& localToWorld,
const LitObject& litObject,
const IRenderEntity* entity = nullptr) = 0;
Expand Down
46 changes: 0 additions & 46 deletions libs/entitylib.h
Expand Up @@ -15,52 +15,6 @@
#include <list>
#include <set>

/* greebo: draws a pyramid defined by 5 vertices
* points[0] is the top of the pyramid
* points[1] to points[4] is the base rectangle
*/
inline void drawPyramid(const Vector3 points[5]) {
typedef unsigned int index_t;
index_t indices[16] = {
0, 1, // top to first
0, 2, // top to second
0, 3, // top to third
0, 4, // top to fourth
1, 2, // first to second
2, 3, // second to third
3, 4, // third to second
4, 1, // fourth to first
};
glVertexPointer(3, GL_DOUBLE, 0, points);
glDrawElements(GL_LINES, sizeof(indices)/sizeof(index_t), GL_UNSIGNED_INT, indices);
}

/* greebo: draws a frustum defined by 8 vertices
* points[0] to points[3] define the top area vertices (clockwise starting from the "upper right" corner)
* points[4] to points[7] define the base rectangle (clockwise starting from the "upper right" corner)
*/
inline void drawFrustum(const Vector3 points[8]) {
typedef unsigned int index_t;
index_t indices[24] = {
0, 4, // top up right to bottom up right
1, 5, // top down right to bottom down right
2, 6, // top down left to bottom down left
3, 7, // top up left to bottom up left

0, 1, // top up right to top down right
1, 2, // top down right to top down left
2, 3, // top down left to top up left
3, 0, // top up left to top up right

4, 5, // bottom up right to bottom down right
5, 6, // bottom down right to bottom down left
6, 7, // bottom down left to bottom up left
7, 4, // bottom up left to bottom up right
};
glVertexPointer(3, GL_DOUBLE, 0, points);
glDrawElements(GL_LINES, sizeof(indices)/sizeof(index_t), GL_UNSIGNED_INT, indices);
}

inline void arrow_draw(const Vector3& origin, const Vector3& direction)
{
Vector3 up(0, 0, 1);
Expand Down
30 changes: 7 additions & 23 deletions libs/math/AABB.h
Expand Up @@ -25,16 +25,13 @@ class AABB
/// The symmetrical extents in 3 dimensions.
Vector3 extents;

/** Construct an AABB with default origin and invalid extents.
*/
/// Construct an AABB with default origin and invalid extents.
AABB() :
origin(0, 0, 0),
extents(-1,-1,-1)
{}

/** Construct an AABB with the provided origin and extents
* vectors.
*/
/// Construct an AABB with the provided origin and extents vectors.
AABB(const Vector3& origin_, const Vector3& extents_) :
origin(origin_),
extents(extents_)
Expand Down Expand Up @@ -97,20 +94,10 @@ class AABB
*/
float getRadius() const;

/** Expand this AABB in-place to include the given point in
* world space.
*
* @param point
* Vector3 representing the point to include.
*/
/// Expand this AABB in-place to include the given point
void includePoint(const Vector3& point);

/** Expand this AABB in-place to include the given AABB in
* world space.
*
* @param other
* The other AABB to include.
*/
/// Expand this AABB in-place to include the given AABB
void includeAABB(const AABB& other);

/**
Expand Down Expand Up @@ -294,14 +281,11 @@ inline void AABB::getPlanes(Plane3 planes[6]) const
planes[5] = Plane3(-g_vector3_axes[2], -(origin[2] - extents[2]));
}

/**
* Stream insertion for AABB class.
*/
/// Stream insertion for AABB class.
inline std::ostream& operator<< (std::ostream& os, const AABB& aabb)
{
os << "AABB { origin=" << aabb.getOrigin() << ", extents=" << aabb.getExtents() << " }";

return os;
return os << "AABB(origin=" << aabb.getOrigin().pp()
<< ", extents=" << aabb.getExtents().pp() << ")";
}

class AABBExtendByPoint
Expand Down
169 changes: 114 additions & 55 deletions libs/math/Frustum.h
@@ -1,10 +1,13 @@
#pragma once

#include <cassert>

/// \file
/// \brief View-frustum data types and related operations.

#include "math/Matrix4.h"
#include "math/Segment.h"
#include "math/AABB.h"

#include "VolumeIntersectionValue.h"

Expand All @@ -14,66 +17,122 @@ class Plane3;
/**
* \brief
* Object representing a frustum, defined by six planes.
*
* A frustum is used to represent both the view volume and the volume of a
* projected light. It typically takes the form of a truncated pyramid although
* it need not be symmetrical.
*
* The six planes defining the frustum are named according to their use with an
* OpenGL camera: "front" is the near clip plane (the smaller plane near the
* top of the pyramid) and "back" is the far clip plane (the larger plane at
* the bottom of the pyramid). In the case of a projected light the "front" is
* the plane nearest the light source and the "back" is the far end of the
* light projection.
*/
class Frustum
{
public:
Plane3 right, left, bottom, top, back, front;

Frustum()
{}

Frustum(const Plane3& _right, const Plane3& _left,
const Plane3& _bottom, const Plane3& _top,
const Plane3& _back, const Plane3& _front) :
right(_right),
left(_left),
bottom(_bottom),
top(_top),
back(_back),
front(_front)
{}

/**
* Construct the frustum planes from the given projection matrix.
*/
static Frustum createFromViewproj(const Matrix4& viewproj);

/**
* \brief
* Normalise all planes in the frustum.
*/
Plane3 right, left, bottom, top, back, front;

Frustum()
{}

/// Construct a Frustum with six explicit planes
Frustum(const Plane3& _right, const Plane3& _left,
const Plane3& _bottom, const Plane3& _top,
const Plane3& _back, const Plane3& _front)
: right(_right), left(_left), bottom(_bottom), top(_top),
back(_back), front(_front)
{}

/// Construct the frustum planes from the given projection matrix.
static Frustum createFromViewproj(const Matrix4& viewproj);

/// Normalise all planes in the frustum.
void normalisePlanes();

/**
* \brief
* Get the projection matrix corresponding to the planes of this frustum.
*/
/// Get the projection matrix corresponding to the planes of this frustum.
Matrix4 getProjectionMatrix() const;

/**
* \brief
* Return a copy of this frustum transformed by the given matrix.
*/
/// Return a copy of this frustum transformed by the given matrix.
Frustum getTransformedBy(const Matrix4& transform) const;

/**
* \brief
* Test the intersection of this frustum with an AABB.
*/
/// Test the intersection of this frustum with an AABB.
VolumeIntersectionValue testIntersection(const AABB& aabb) const;

/**
* Test the intersection of this frustum with a transformed AABB.
*/
VolumeIntersectionValue testIntersection(const AABB& aabb, const Matrix4& localToWorld) const;

/**
* Returns true if the given point is contained in this frustum.
*/
bool testPoint(const Vector3& point) const;

bool testLine(const Segment& segment) const;
/// Test the intersection of this frustum with a transformed AABB.
VolumeIntersectionValue testIntersection(const AABB& aabb, const Matrix4& localToWorld) const;

/// Enum representing the corner points of each end plane
enum Corner
{
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
};

/// Enum representing each end plane
enum EndPlane
{
FRONT, BACK
};

/// Return the position of the given corner point
Vector3 getCornerPoint(EndPlane plane, Corner point) const
{
if (plane == FRONT)
{
switch (point)
{
case TOP_LEFT:
return Plane3::intersect(left, top, front);
case TOP_RIGHT:
return Plane3::intersect(right, top, front);
case BOTTOM_LEFT:
return Plane3::intersect(left, bottom, front);
case BOTTOM_RIGHT:
return Plane3::intersect(right, bottom, front);
}
}
else
{
switch (point)
{
case TOP_LEFT:
return Plane3::intersect(left, top, back);
case TOP_RIGHT:
return Plane3::intersect(right, top, back);
case BOTTOM_LEFT:
return Plane3::intersect(left, bottom, back);
case BOTTOM_RIGHT:
return Plane3::intersect(right, bottom, back);
}
}

// All cases should be handled above
assert(false);
return Vector3();
}

/// Return an AABB enclosing this frustum
AABB getAABB() const
{
// The AABB of a frustum is simply the AABB which includes all eight
// corner points.
AABB result;
result.includePoint(getCornerPoint(FRONT, TOP_LEFT));
result.includePoint(getCornerPoint(FRONT, BOTTOM_LEFT));
result.includePoint(getCornerPoint(FRONT, TOP_RIGHT));
result.includePoint(getCornerPoint(FRONT, BOTTOM_RIGHT));
result.includePoint(getCornerPoint(BACK, TOP_LEFT));
result.includePoint(getCornerPoint(BACK, BOTTOM_LEFT));
result.includePoint(getCornerPoint(BACK, TOP_RIGHT));
result.includePoint(getCornerPoint(BACK, BOTTOM_RIGHT));
return result;
}

/// Returns true if the given point is contained in this frustum.
bool testPoint(const Vector3& point) const;

bool testLine(const Segment& segment) const;
};

inline Frustum Frustum::createFromViewproj(const Matrix4& viewproj)
Expand All @@ -94,8 +153,8 @@ inline Frustum Frustum::createFromViewproj(const Matrix4& viewproj)

inline bool Frustum::testPoint(const Vector3& point) const
{
return !right.testPoint(point) && !left.testPoint(point) &&
!bottom.testPoint(point) && !top.testPoint(point) &&
return !right.testPoint(point) && !left.testPoint(point) &&
!bottom.testPoint(point) && !top.testPoint(point) &&
!back.testPoint(point) && !front.testPoint(point);
}

Expand All @@ -106,11 +165,11 @@ inline bool plane3_test_line(const Plane3& plane, const Segment& segment)

inline bool Frustum::testLine(const Segment& segment) const
{
return !plane3_test_line(right, segment) &&
!plane3_test_line(left, segment) &&
!plane3_test_line(bottom, segment) &&
!plane3_test_line(top, segment) &&
!plane3_test_line(back, segment) &&
return !plane3_test_line(right, segment) &&
!plane3_test_line(left, segment) &&
!plane3_test_line(bottom, segment) &&
!plane3_test_line(top, segment) &&
!plane3_test_line(back, segment) &&
!plane3_test_line(front, segment);
}

Expand Down
32 changes: 32 additions & 0 deletions libs/math/Matrix4.h
Expand Up @@ -392,7 +392,10 @@ class Matrix4
void invertFull();

/**
* \brief
* Returns the given 3-component point transformed by this matrix.
*
* The point is assumed to have a W component of 1.
*/
template<typename Element>
BasicVector3<Element> transformPoint(const BasicVector3<Element>& point) const;
Expand Down Expand Up @@ -677,6 +680,35 @@ class Matrix4
std::size_t clipTriangle(const Vector3& p0, const Vector3& p1, const Vector3& p2, Vector4 clipped[9]) const;
};

// ===========================================================================
// Operators
// ===========================================================================

/**
* \brief
* Multiply a 4-component vector by this matrix.
*
* Equivalent to m.transform(v).
*/
template<typename T>
BasicVector4<T> operator* (const Matrix4& m, const BasicVector4<T>& v)
{
return m.transform(v);
}

/**
* \brief
* Multiply a 3-component vector by this matrix.
*
* The vector is upgraded to a 4-component vector with a W component of 1, i.e.
* equivalent to m.transformPoint(v).
*/
template<typename T>
BasicVector3<T> operator* (const Matrix4& m, const BasicVector3<T>& v)
{
return m.transformPoint(v);
}

// =========================================================================================
// Inlined member definitions
// =========================================================================================
Expand Down

0 comments on commit 9934407

Please sign in to comment.