Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5584: Introduce ISurfaceRenderer interface to specifically deal with…
… oriented surfaces
- Loading branch information
1 parent
e1e6c65
commit f9151b6
Showing
18 changed files
with
493 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <limits> | ||
#include <cstdint> | ||
#include "render/ArbitraryMeshVertex.h" | ||
#include "math/Matrix4.h" | ||
|
||
namespace render | ||
{ | ||
|
||
/** | ||
* Surface type consisting of triangles, oriented by a transformation matrix. | ||
* | ||
* The ISurfaceRenderer will reacquire the transformation matrix each frame, | ||
* whereas the vertices and indices won't be requested every time. | ||
*/ | ||
class IRenderableSurface | ||
{ | ||
public: | ||
virtual ~IRenderableSurface() {} | ||
|
||
// Returns the vertex array of this surface | ||
virtual const std::vector<ArbitraryMeshVertex>& getVertices() = 0; | ||
|
||
// Returns the indices to render the triangle primitives | ||
virtual const std::vector<unsigned int>& getIndices() = 0; | ||
|
||
// The model view matrix used to render this surface | ||
virtual const Matrix4& getSurfaceTransform() = 0; | ||
}; | ||
|
||
/** | ||
* A surface renderer accepts a variable number of IRenderableSurfaces | ||
* each of which is oriented by its own transformation matrix. | ||
* | ||
* Surfaces are rendered separately, the transformation matrix is requested | ||
* each frame before drawing it. | ||
* The vertices and indices of the surface are buffered and won't be requested | ||
* every frame. Invoke the updateSurface() method to schedule an update. | ||
*/ | ||
class ISurfaceRenderer | ||
{ | ||
public: | ||
virtual ~ISurfaceRenderer() {} | ||
|
||
using Slot = std::uint64_t; | ||
static constexpr Slot InvalidSlot = std::numeric_limits<Slot>::max(); | ||
|
||
// Allocate a slot to hold the given surface data, indexed to render triangles. | ||
// Returns the handle which can be used to update or deallocate the data later | ||
virtual Slot addSurface(IRenderableSurface& surface) = 0; | ||
|
||
// Releases a previously allocated slot. This invalidates the handle. | ||
virtual void removeSurface(Slot slot) = 0; | ||
|
||
// Schedules an update of the vertex data contained in the surface. | ||
virtual void updateSurface(Slot slot) = 0; | ||
|
||
// Submits the surface of a single slot to GL | ||
virtual void renderSurface(Slot slot) = 0; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include "isurfacerenderer.h" | ||
#include "irender.h" | ||
|
||
namespace render | ||
{ | ||
|
||
/** | ||
* Surface base type, handling vertex data updates in combination | ||
* with an ISurfaceRenderer instance. | ||
* | ||
* It implements the OpenGLRenderable interface which will instruct | ||
* the shader to render just the surface managed by this object. | ||
* This is used to render highlights (such as selection overlays). | ||
*/ | ||
class RenderableSurface : | ||
public IRenderableSurface, | ||
public OpenGLRenderable | ||
{ | ||
private: | ||
ShaderPtr _shader; | ||
ISurfaceRenderer::Slot _surfaceSlot; | ||
|
||
std::size_t _lastVertexSize; // To detect size changes when updating surfaces | ||
std::size_t _lastIndexSize; // To detect size changes when updating surfaces | ||
|
||
protected: | ||
RenderableSurface() : | ||
_surfaceSlot(ISurfaceRenderer::InvalidSlot), | ||
_lastVertexSize(0), | ||
_lastIndexSize(0) | ||
{} | ||
|
||
public: | ||
// Noncopyable | ||
RenderableSurface(const RenderableSurface& other) = delete; | ||
RenderableSurface& operator=(const RenderableSurface& other) = delete; | ||
|
||
virtual ~RenderableSurface() | ||
{ | ||
clear(); | ||
} | ||
|
||
// (Non-virtual) update method handling any possible shader change | ||
// The surface is withdrawn from the given shader if it turns out | ||
// to be different from the last update. | ||
void update(const ShaderPtr& shader) | ||
{ | ||
bool shaderChanged = _shader != shader; | ||
auto currentVertexSize = getVertices().size(); | ||
auto currentIndexSize = getVertices().size(); | ||
|
||
bool sizeChanged = _lastIndexSize != currentIndexSize || _lastVertexSize != currentVertexSize; | ||
|
||
if (shaderChanged || sizeChanged) | ||
{ | ||
clear(); | ||
} | ||
|
||
// Update our local shader reference | ||
_shader = shader; | ||
|
||
if (_shader) | ||
{ | ||
_surfaceSlot = _shader->addSurface(*this); | ||
|
||
_lastVertexSize = currentVertexSize; | ||
_lastIndexSize = currentIndexSize; | ||
} | ||
} | ||
|
||
// Removes the surface and clears the shader reference | ||
void clear() | ||
{ | ||
if (_shader && _surfaceSlot != ISurfaceRenderer::InvalidSlot) | ||
{ | ||
_shader->removeSurface(_surfaceSlot); | ||
} | ||
|
||
_surfaceSlot = ISurfaceRenderer::InvalidSlot; | ||
_shader.reset(); | ||
} | ||
|
||
// Renders the surface stored in our single slot | ||
void render(const RenderInfo& info) const override | ||
{ | ||
if (_surfaceSlot != ISurfaceRenderer::InvalidSlot && _shader) | ||
{ | ||
_shader->renderSurface(_surfaceSlot); | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.