qzdoom/src/gl/dynlights/gl_lightbsp.h

90 lines
2.5 KiB
C
Raw Normal View History

2017-03-01 01:46:03 +00:00
#pragma once
2017-03-07 23:34:08 +00:00
#include "vectors.h"
#include <memory>
// Node in a binary AABB tree
struct AABBTreeNode
2017-03-01 01:46:03 +00:00
{
AABBTreeNode(const FVector2 &aabb_min, const FVector2 &aabb_max, int line_index) : aabb_left(aabb_min.X), aabb_top(aabb_min.Y), aabb_right(aabb_max.X), aabb_bottom(aabb_max.Y), left_node(-1), right_node(-1), line_index(line_index) { }
AABBTreeNode(const FVector2 &aabb_min, const FVector2 &aabb_max, int left, int right) : aabb_left(aabb_min.X), aabb_top(aabb_min.Y), aabb_right(aabb_max.X), aabb_bottom(aabb_max.Y), left_node(left), right_node(right), line_index(-1) { }
// Axis aligned bounding box for the node
float aabb_left, aabb_top;
float aabb_right, aabb_bottom;
// Children node indices
int left_node;
int right_node;
// AABBTreeLine index if it is a leaf node. Index is -1 if it is not.
int line_index;
// Padding to keep 16-byte length (this structure is uploaded to the GPU)
int padding;
2017-03-01 01:46:03 +00:00
};
// Line segment for leaf nodes in an AABB tree
struct AABBTreeLine
2017-03-01 01:46:03 +00:00
{
float x, y;
float dx, dy;
};
// Axis aligned bounding box tree used for ray testing lines.
class LevelAABBTree
{
public:
// Constructs a tree for the current level
LevelAABBTree();
// Nodes in the AABB tree. Last node is the root node.
TArray<AABBTreeNode> nodes;
// Line segments for the leaf nodes in the tree.
TArray<AABBTreeLine> lines;
// Shoot a ray from ray_start to ray_end and return the first hit as a fractional value between 0 and 1. Returns 1 if no line was hit.
2017-03-07 23:34:08 +00:00
double RayTest(const DVector3 &ray_start, const DVector3 &ray_end);
private:
// Test if a ray overlaps an AABB node or not
bool OverlapRayAABB(const DVector2 &ray_start2d, const DVector2 &ray_end2d, const AABBTreeNode &node);
// Intersection test between a ray and a line segment
2017-03-07 23:34:08 +00:00
double IntersectRayLine(const DVector2 &ray_start, const DVector2 &ray_end, int line_index, const DVector2 &raydelta, double rayd, double raydist2);
// Generate a tree node and its children recursively
int GenerateTreeNode(int *lines, int num_lines, const FVector2 *centroids, int *work_buffer);
2017-03-01 01:46:03 +00:00
};
class FLightBSP
{
public:
FLightBSP() { }
~FLightBSP() { Clear(); }
int GetNodesBuffer();
int GetLinesBuffer();
2017-03-01 01:46:03 +00:00
void Clear();
2017-03-07 23:34:08 +00:00
bool ShadowTest(const DVector3 &light, const DVector3 &pos);
2017-03-01 01:46:03 +00:00
private:
void UpdateBuffers();
void GenerateBuffers();
void UploadNodes();
void UploadSegs();
FLightBSP(const FLightBSP &) = delete;
FLightBSP &operator=(FLightBSP &) = delete;
int NodesBuffer = 0;
int LinesBuffer = 0;
2017-03-01 01:46:03 +00:00
int NumNodes = 0;
int NumSegs = 0;
std::unique_ptr<LevelAABBTree> Shape;
2017-03-01 01:46:03 +00:00
};