Skip to content

Commit

Permalink
#5584: WIP commit, supporting oriented surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Dec 24, 2021
1 parent 01b5971 commit e1e6c65
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 137 deletions.
7 changes: 5 additions & 2 deletions include/igeometryrenderer.h
Expand Up @@ -4,6 +4,7 @@
#include <limits>
#include <cstdint>
#include "render/ArbitraryMeshVertex.h"
#include "math/Matrix4.h"

namespace render
{
Expand All @@ -13,6 +14,7 @@ enum class GeometryType
Triangles,
Quads,
Lines,
OrientedSurface, // Triangles with a per-surface transformation matrix
};

/**
Expand All @@ -34,9 +36,10 @@ class IGeometryRenderer
// Allocate a slot to hold the given indexed vertex data.
// Returns the handle which can be used to update or deallocate the data later
// The indexType determines the primitive GLenum that is chosen to render this surface
virtual Slot addGeometry(GeometryType indexType,
virtual Slot addGeometry(GeometryType indexType,
const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices) = 0;
const std::vector<unsigned int>& indices,
const std::function<const Matrix4& ()>& getTransformCallback = std::function<const Matrix4&()>()) = 0;

// Releases a previously allocated slot. This invalidates the handle.
virtual void removeGeometry(Slot slot) = 0;
Expand Down
117 changes: 89 additions & 28 deletions libs/render/RenderableBox.h
Expand Up @@ -14,40 +14,40 @@ inline std::vector<ArbitraryMeshVertex> getFillBoxVertices(const Vector3& min, c
return
{
// Bottom quad
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,0,-1}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,0,-1}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,0,-1}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,0,-1}, {0,1}, colour),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,0,-1}, {1,1}, colour),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,0,-1}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,0,-1}, {0,0}, colour),

// Top quad
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,0,+1}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,0,+1}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,0,+1}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,0,+1}, {0,1}, colour),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,0,+1}, {0,1}, colour),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,0,+1}, {1,1}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,0,+1}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,0,+1}, {0,0}, colour),

// Front quad
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,-1,0}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,-1,0}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,-1,0}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,-1,0}, {0,1}, colour),
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,-1,0}, {0,1}, colour),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,-1,0}, {1,1}, colour),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,-1,0}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,-1,0}, {0,0}, colour),

// Back quad
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,+1,0}, {0,0}, colour),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,+1,0}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,+1,0}, {1,1}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,+1,0}, {0,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,+1,0}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,+1,0}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,+1,0}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,+1,0}, {0,1}, colour),

// Right quad
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {+1,0,0}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {+1,0,0}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {+1,0,0}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {+1,0,0}, {0,1}, colour),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {+1,0,0}, {1,1}, colour),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {+1,0,0}, {1,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {+1,0,0}, {0,0}, colour),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {+1,0,0}, {0,1}, colour),

// Left quad
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {-1,0,0}, {0,0}, colour),
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {-1,0,0}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {-1,0,0}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {-1,0,0}, {0,1}, colour),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {-1,0,0}, {0,1}, colour),
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {-1,0,0}, {1,1}, colour),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {-1,0,0}, {1,0}, colour),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {-1,0,0}, {0,0}, colour),
};
}

Expand Down Expand Up @@ -100,10 +100,24 @@ inline std::vector<unsigned int> generateFillBoxIndices()
3, 2, 1, 0, // bottom rectangle
7, 6, 5, 4, // top rectangle

4, 5, 1, 0, // sides
5, 6, 2, 1,
6, 7, 3, 2,
7, 4, 0, 3,
11, 10, 9, 8, // sides
15, 14, 13, 12,
19, 18, 17, 16,
23, 22, 21, 20,
};
};

inline std::vector<unsigned int> generateTriangleBoxIndices()
{
return
{
3, 2, 1, 3, 1, 0, // bottom rectangle
7, 6, 5, 7, 5, 4, // top rectangle

11, 10, 9, 11, 9, 8, // sides
15, 14, 13, 15, 13, 12,
19, 18, 17, 19, 17, 16,
23, 22, 21, 23, 21, 20,
};
};

Expand Down Expand Up @@ -154,7 +168,7 @@ class RenderableBox :

static Vector3 Origin(0, 0, 0);

// Calculate the corner vertices of this bounding box, plus the mid-point
// Calculate the corner vertices of this bounding box
Vector3 max(Origin + _bounds.extents);
Vector3 min(Origin - _bounds.extents);

Expand Down Expand Up @@ -184,4 +198,51 @@ class RenderableBox :
}
};

class RenderableBoxSurface :
public render::RenderableGeometry
{
private:
const AABB& _bounds;
const Matrix4& _orientation;
bool _needsUpdate;

public:
RenderableBoxSurface(const AABB& bounds, const Matrix4& orientation) :
_bounds(bounds),
_orientation(orientation),
_needsUpdate(true)
{}

void queueUpdate()
{
_needsUpdate = true;
}

virtual void updateGeometry() override
{
if (!_needsUpdate) return;

_needsUpdate = false;

static Vector3 Origin(0, 0, 0);

// Calculate the corner vertices of this bounding box
Vector3 max(Origin + _bounds.extents);
Vector3 min(Origin - _bounds.extents);

auto vertices = detail::getFillBoxVertices(min, max, { 1, 1, 1, 1 });

static auto Indices = detail::generateTriangleBoxIndices();

RenderableGeometry::updateGeometry(render::GeometryType::OrientedSurface, vertices, Indices,
std::bind(&RenderableBoxSurface::getOrientation, this));
}

private:
const Matrix4& getOrientation() const
{
return _orientation;
}
};

}
5 changes: 3 additions & 2 deletions libs/render/RenderableGeometry.h
Expand Up @@ -103,7 +103,8 @@ class RenderableGeometry :
// to ensure that the _shader reference is already up to date.
void updateGeometry(GeometryType type,
const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices)
const std::vector<unsigned int>& indices,
const std::function<const Matrix4& ()>& getTransformCallback = std::function<const Matrix4&()>())
{
// Size changes require removal of the geometry before update
if (_lastVertexSize != vertices.size() || _lastIndexSize != indices.size())
Expand All @@ -116,7 +117,7 @@ class RenderableGeometry :

if (_surfaceSlot == IGeometryRenderer::InvalidSlot)
{
_surfaceSlot = _shader->addGeometry(type, vertices, indices);
_surfaceSlot = _shader->addGeometry(type, vertices, indices, getTransformCallback);
}
else
{
Expand Down
36 changes: 1 addition & 35 deletions radiantcore/model/NullModel.cpp
Expand Up @@ -20,31 +20,8 @@ const AABB& NullModel::localAABB() const {
return _aabbLocal;
}

void NullModel::renderSolid(IRenderableCollector& collector,
const VolumeTest& volume, const Matrix4& localToWorld) const
void NullModel::testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
{
collector.addRenderable(*_state, _aabbSolid, localToWorld);
}

void NullModel::renderWireframe(IRenderableCollector& collector,
const VolumeTest& volume, const Matrix4& localToWorld) const
{
collector.addRenderable(*_state, _aabbWire, localToWorld);
}

void NullModel::setRenderSystem(const RenderSystemPtr& renderSystem)
{
if (renderSystem)
{
_state = renderSystem->capture("");
}
else
{
_state.reset();
}
}

void NullModel::testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld) {
test.BeginMesh(localToWorld);

SelectionIntersection best;
Expand Down Expand Up @@ -97,15 +74,4 @@ const StringList& NullModel::getActiveMaterials() const {
return _dummyMaterials;
}

void NullModel::render(const RenderInfo& info) const {
if (info.checkFlag(RENDER_TEXTURE_2D))
{
aabb_draw_solid(_aabbLocal, info.getFlags());
}
else
{
aabb_draw_wire(_aabbLocal);
}
}

} // namespace model
11 changes: 3 additions & 8 deletions radiantcore/model/NullModel.h
@@ -1,5 +1,4 @@
#ifndef _NULLMODEL_H_
#define _NULLMODEL_H_
#pragma once

#include "imodel.h"
#include "math/AABB.h"
Expand All @@ -23,9 +22,6 @@ class NullModel :

const AABB& localAABB() const;

void renderSolid(IRenderableCollector& collector, const VolumeTest& volume, const Matrix4& localToWorld) const;
void renderWireframe(IRenderableCollector& collector, const VolumeTest& volume, const Matrix4& localToWorld) const;
void setRenderSystem(const RenderSystemPtr& renderSystem);
void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld);

// IModel implementation
Expand All @@ -45,10 +41,9 @@ class NullModel :
virtual const std::vector<std::string>& getActiveMaterials() const;

// OpenGLRenderable implementation
void render(const RenderInfo& info) const;
void render(const RenderInfo& info) const
{}
};
typedef std::shared_ptr<NullModel> NullModelPtr;

} // namespace model

#endif /* _NULLMODEL_H_ */

0 comments on commit e1e6c65

Please sign in to comment.