File: RenderableSpacePartition.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (111 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma once

#include "ispacepartition.h"
#include "irender.h"
#include "irenderable.h"

#include "math/Matrix4.h"
#include "math/AABB.h"
#include "render/RenderableColouredBoundingBoxes.h"

namespace render
{

/**
 * greebo: This is a renderable helper object which can be used
 * to render any space partition system implementing the ISPacepartitionSystem interface.
 *
 * Instantiate such a class and pass the Shader and the SpacePartition system to the
 * setShader() and setSpacePartition() methods to enable rendering.
 *
 * This object can be directly attached to the GlobalRenderSystem().
 */
class RenderableSpacePartition :
	public Renderable
{
private:
	// The shader we're using
	ShaderPtr _shader;

	// The space partition to render
	scene::ISpacePartitionSystemPtr _spacePartition;

    std::vector<AABB> _spacePartitionNodes;
    std::vector<Vector4> _nodeColours;
    RenderableColouredBoundingBoxes _renderableBoxes;

public:
    RenderableSpacePartition() :
        _renderableBoxes(_spacePartitionNodes, _nodeColours)
    {}

	void setSpacePartition(const scene::ISpacePartitionSystemPtr& spacePartition)
	{
		_spacePartition = spacePartition;
	}

    void onPreRender(const VolumeTest& volume) override
    {
        if (!_spacePartition)
        {
            _renderableBoxes.clear();
            return;
        }

        // Accumulate the bounding boxes to render
        _spacePartitionNodes.clear();
        _nodeColours.clear();

        accumulateBoundingBoxes(_spacePartition->getRoot());

        // Update the renderable every frame
        _renderableBoxes.queueUpdate();
        _renderableBoxes.update(_shader);
    }

    void renderHighlights(IRenderableCollector& collector, const VolumeTest& volume) override
    {}

	void setRenderSystem(const RenderSystemPtr& renderSystem) override
	{
        _renderableBoxes.clear();

		if (renderSystem)
		{
			_shader = renderSystem->capture("[1 0 0]");
		}
		else
		{
			_shader.reset();
		}
	}

	std::size_t getHighlightFlags() override
	{
		return Highlight::NoHighlight; // never highlighted
	}

private:
	void accumulateBoundingBoxes(const scene::ISPNodePtr& node)
	{
		const auto& members = node->getMembers();

		float shade = members.size() > 2 ? 1 : (members.size() > 0 ? 0.6f : 0);

        _nodeColours.emplace_back(shade, shade, shade, 1);

		AABB rb(node->getBounds());

		// Extend the renderbounds *slightly* so that the lines don't overlap
		rb.extents *= 1.02f;

        _spacePartitionNodes.push_back(rb);

		for (auto child : node->getChildNodes())
		{
			accumulateBoundingBoxes(child);
		}
	}
};

} // namespace