Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/includes/pipelinetasks/shadowmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ShadowMap : public PipelineTask {
void exec(PipelineContext *context) override;

void areaLightUpdate(PipelineContext *context, AreaLight *light, list<Renderable *> &components);
void directLightUpdate(PipelineContext *context, DirectLight *light, list<Renderable *> &components, Camera &camera);
void directLightUpdate(PipelineContext *context, DirectLight *light, list<Renderable *> &components, const Camera &camera);
void pointLightUpdate(PipelineContext *context, PointLight *light, list<Renderable *> &components);
void spotLightUpdate(PipelineContext *context, SpotLight *light, list<Renderable *> &components);

Expand Down
49 changes: 26 additions & 23 deletions engine/src/pipelinetasks/shadowmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void ShadowMap::areaLightUpdate(PipelineContext *context, AreaLight *light, list

buffer->setRenderTarget(shadowTarget);
for(int32_t i = 0; i < m_directions.size(); i++) {
Matrix4 mat = (wp * Matrix4(m_directions[i].toMatrix())).inverse();
Matrix4 mat((wp * Matrix4(m_directions[i].toMatrix())).inverse());
matrix[i] = m_scale * crop * mat;

tiles[i] = Vector4(static_cast<float>(x[i]) / pageSize,
Expand Down Expand Up @@ -142,7 +142,7 @@ void ShadowMap::areaLightUpdate(PipelineContext *context, AreaLight *light, list
}
}

void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light, list<Renderable *> &components, Camera &camera) {
void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light, list<Renderable *> &components, const Camera &camera) {
CommandBuffer *buffer = context->buffer();
Vector4 distance;

Expand All @@ -166,12 +166,12 @@ void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light,
}

Transform *lightTransform = light->transform();
Quaternion q = lightTransform->worldQuaternion();
Matrix4 rot = Matrix4(q.toMatrix()).inverse();
Quaternion lightRot = lightTransform->worldQuaternion();
Matrix4 rot = Matrix4(lightRot.toMatrix()).inverse();

Transform *cameraTransform = camera.transform();
Vector3 wPosition = cameraTransform->worldPosition();
Quaternion wRotation = cameraTransform->worldQuaternion();
Vector3 cameraPos = cameraTransform->worldPosition();
Quaternion cameraRot = cameraTransform->worldQuaternion();

bool orthographic = camera.orthographic();
float sigma = (camera.orthographic()) ? camera.orthoSize() : camera.fov();
Expand All @@ -188,26 +188,26 @@ void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light,
buffer->setRenderTarget(shadowTarget);
for(int32_t lod = 0; lod < MAX_LODS; lod++) {
float dist = distance[lod];
auto points = Camera::frustumCorners(orthographic, sigma, ratio, wPosition, wRotation, nearPlane, dist);
auto points = Camera::frustumCorners(orthographic, sigma, ratio, cameraPos, cameraRot, nearPlane, dist);

nearPlane = dist;

AABBox box;
box.setBox(points.data(), 8);
box *= rot;
box *= rot.rotation();

AABBox bb;
auto corners = Camera::frustumCorners(true, box.extent.y * 2.0f, 1.0f, box.center, q, -FLT_MAX, FLT_MAX);
RenderList filter = context->frustumCulling(corners, components, bb);
auto corners = Camera::frustumCorners(true, box.extent.y * 2.0f, 1.0f, box.center, lightRot, -FLT_MAX, FLT_MAX);
RenderList filter(context->frustumCulling(corners, components, bb));

float radius = MAX(box.radius, bb.radius);

Matrix4 m;
m.translate(-box.center - q * Vector3(0.0f, 0.0f, radius));
Matrix4 view = rot * m;
Matrix4 crop = Matrix4::ortho(-box.extent.x, box.extent.x,
-box.extent.y, box.extent.y,
0.0f, radius * 2.0f);
m.translate(-box.center - lightRot * Vector3(0.0f, 0.0f, radius));
Matrix4 view(rot * m);
Matrix4 crop(Matrix4::ortho(-box.extent.x, box.extent.x,
-box.extent.y, box.extent.y,
0.0f, radius * 2.0f));

uint32_t pageSize = Texture::maxTextureSize();

Expand All @@ -233,7 +233,7 @@ void ShadowMap::directLightUpdate(PipelineContext *context, DirectLight *light,

auto instance = light->material();
if(instance) {
Vector3 direction(q * Vector3(0.0f, 0.0f, 1.0f));
Vector3 direction(lightRot * Vector3(0.0f, 0.0f, 1.0f));
Vector4 bias(m_bias);
float shadows = light->castShadows() ? 1.0f : 0.0;

Expand Down Expand Up @@ -275,7 +275,7 @@ void ShadowMap::pointLightUpdate(PipelineContext *context, PointLight *light, li

buffer->setRenderTarget(shadowTarget);
for(int32_t i = 0; i < m_directions.size(); i++) {
Matrix4 mat = (wp * Matrix4(m_directions[i].toMatrix())).inverse();
Matrix4 mat((wp * Matrix4(m_directions[i].toMatrix())).inverse());
matrix[i] = m_scale * crop * mat;

tiles[i] = Vector4(static_cast<float>(x[i]) / pageSize,
Expand Down Expand Up @@ -329,15 +329,18 @@ void ShadowMap::spotLightUpdate(PipelineContext *context, SpotLight *light, list
float zFar = light->attenuationDistance();
Matrix4 crop = Matrix4::perspective(light->outerAngle() * 2.0f, 1.0f, zNear, zFar);

int32_t x, y, w, h;
int32_t x = 0;
int32_t y = 0;
int32_t w = 0;
int32_t h = 0;
RenderTarget *shadowTarget = requestShadowTiles(light->uuid(), 1, &x, &y, &w, &h, 1);

uint32_t pageSize = Texture::maxTextureSize();
Matrix4 matrix = m_scale * crop * rot;
Vector4 tiles = Vector4(static_cast<float>(x) / pageSize,
static_cast<float>(y) / pageSize,
static_cast<float>(w) / pageSize,
static_cast<float>(h) / pageSize);
Matrix4 matrix(m_scale * crop * rot);
Vector4 tiles(static_cast<float>(x) / pageSize,
static_cast<float>(y) / pageSize,
static_cast<float>(w) / pageSize,
static_cast<float>(h) / pageSize);

buffer->setRenderTarget(shadowTarget);
buffer->enableScissor(x, y, w, h);
Expand Down
3 changes: 3 additions & 0 deletions thirdparty/next/inc/math/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "vector3.h"
#include "plane.h"

class Matrix3;
class Matrix4;

class NEXT_LIBRARY_EXPORT AABBox {
Expand All @@ -39,8 +40,10 @@ class NEXT_LIBRARY_EXPORT AABBox {

const AABBox operator*(areal factor) const;
const AABBox operator*(const Vector3 &vector) const;
const AABBox operator*(const Matrix3 &matrix) const;
const AABBox operator*(const Matrix4 &matrix) const;

AABBox &operator*=(const Matrix3 &matrix);
AABBox &operator*=(const Matrix4 &matrix);

void encapsulate(const Vector3 &position, areal radius = 0.0f);
Expand Down
37 changes: 25 additions & 12 deletions thirdparty/next/src/math/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,32 +152,45 @@ const AABBox AABBox::operator*(const Vector3 &vector) const {
return AABBox(center * vector, extent * vector);
}
/*!
Returns a copy of this box, multiplied by the given \a matrix.
Returns a copy of this box, multiplied by the given rotation \a matrix.
*/
const AABBox AABBox::operator*(const Matrix4 &matrix) const {
const AABBox AABBox::operator*(const Matrix3 &matrix) const {
AABBox result;

Vector3 min = -extent;
Vector3 max = extent;

Matrix3 rot = matrix.rotation();
Vector3 rotPoints[4] = {
(rot * Vector3(min.x, max.y, min.z)).abs(),
(rot * Vector3(min.x, max.y, max.z)).abs(),
(rot * Vector3(max.x, max.y, max.z)).abs(),
(rot * Vector3(max.x, max.y, min.z)).abs()
(matrix * Vector3(-extent.x, extent.y, -extent.z)).abs(),
(matrix * Vector3(-extent.x, extent.y, extent.z)).abs(),
(matrix * Vector3( extent.x, extent.y, extent.z)).abs(),
(matrix * Vector3( extent.x, extent.y, -extent.z)).abs()
};

result.center = matrix * center;
result.center = center;
result.extent = Vector3(MAX(rotPoints[0].x, MAX(rotPoints[1].x, MAX(rotPoints[2].x, rotPoints[3].x))),
MAX(rotPoints[0].y, MAX(rotPoints[1].y, MAX(rotPoints[2].y, rotPoints[3].y))),
MAX(rotPoints[0].z, MAX(rotPoints[1].z, MAX(rotPoints[2].z, rotPoints[3].z))));

result.radius = result.extent.length();

return result;
}
/*!
Multiplies this box by the given \a matrix, and returns a reference to this vector.
Returns a copy of this box, multiplied by the given transform \a matrix.
*/
const AABBox AABBox::operator*(const Matrix4 &matrix) const {
AABBox result = *this * matrix.rotation();

result.center = matrix * center;

return result;
}
/*!
Multiplies this box by the given rotation \a matrix, and returns a reference to this vector.
*/
AABBox &AABBox::operator*=(const Matrix3 &matrix) {
return *this = *this * matrix;
}
/*!
Multiplies this box by the given transform \a matrix, and returns a reference to this vector.
*/
AABBox &AABBox::operator*=(const Matrix4 &matrix) {
return *this = *this * matrix;
Expand Down