32#include "CEGUI/Logger.h"
33#include "CEGUI/RenderQueue.h"
34#include "CEGUI/PropertyHelper.h"
42#ifndef GLM_FORCE_DEPTH_ZERO_TO_ONE
43#define GLM_FORCE_DEPTH_ZERO_TO_ONE
47#include "glm/gtc/matrix_transform.hpp"
70 Divide::DIVIDE_UNEXPECTED_CALL();
93 const Sizef sz(
_area.getSize());
95 if (sz.d_width < 1.0f || sz.d_height < 1.0f)
105 thread_local size_t FBO_INDEX = 0u;
113 Divide::DIVIDE_UNEXPECTED_CALL();
136 Util::StringFormat( editorDesc.
_name,
"CEGUI_Target_{}", FBO_INDEX++ );
152 Sizef sz(
_area.getSize());
153 if (sz.d_width < 1.0f || sz.d_height < 1.0f)
158 else if (sz.d_width > (1 << 13) || sz.d_height > (1 << 13))
160 sz.d_width = 1 << 13;
161 sz.d_height = 1 << 13;
180 thread_local uint TEX_INDEX = 0;
182 String tmp(
"_ogl_tt_tex_" );
183 tmp.append( PropertyHelper<uint>::toString( TEX_INDEX++ ) );
199 RenderTargetEventArgs args(
this );
200 TextureTarget::fireEvent( RenderTarget::EventAreaChanged, args );
211 beginRenderPassCmd._name =
"Render CEGUI";
214 beginRenderPassCmd._clearDescriptor[to_base( RTColourAttachmentSlot::SLOT_0 )] = { VECTOR4_ZERO,
true};
217 beginRenderPassCmd._descriptor._drawMask[to_base( RTColourAttachmentSlot::SLOT_0 )] =
true;
218 GFX::EnqueueCommand( *cmdBuffer, beginRenderPassCmd );
231 Divide::GFX::EnqueueCommand<Divide::GFX::EndRenderPassCommand>(*
_owner.
cmdBuffer() );
250 double in_x = 0.0, in_y = 0.0, in_z = 0.0;
252 glm::ivec4 viewPort = glm::ivec4( vp[0], vp[1], vp[2], vp[3] );
253 const glm::mat4& projMatrix =
_matrix;
254 const glm::mat4& modelMatrix = gb.
getMatrix();
257 glm::vec3 unprojected1;
258 glm::vec3 unprojected2;
262 unprojected1 = glm::unProject( glm::vec3( in_x, in_y, in_z ), modelMatrix, projMatrix, viewPort );
264 in_y = vp[3] - p_in.d_y;
266 unprojected2 = glm::unProject( glm::vec3( in_x, in_y, in_z ), modelMatrix, projMatrix, viewPort );
269 glm::vec3 projected1;
270 glm::vec3 projected2;
271 glm::vec3 projected3;
274 projected1 = glm::project( glm::vec3( in_x, in_y, in_z ), modelMatrix, projMatrix, viewPort );
277 projected2 = glm::project( glm::vec3( in_x, in_y, in_z ), modelMatrix, projMatrix, viewPort );
280 projected3 = glm::project( glm::vec3( in_x, in_y, in_z ), modelMatrix, projMatrix, viewPort );
283 const glm::vec3 pv1 = projected2 - projected1;
284 const glm::vec3 pv2 = projected3 - projected1;
286 const glm::vec3 planeNormal = glm::cross( pv1, pv2 );
288 const glm::vec3 planeNormalNormalized = glm::normalize( planeNormal );
289 const double pl_d = -glm::dot( projected1, planeNormalNormalized );
291 const glm::vec3 rv = unprojected1 - unprojected2;
293 const double pn_dot_r1 = glm::dot( unprojected1, planeNormal );
294 const double pn_dot_rv = glm::dot( rv, planeNormal );
295 const double tmp1 = pn_dot_rv != 0.0 ? (pn_dot_r1 + pl_d) / pn_dot_rv : 0.0;
296 const double is_x = unprojected1.x - rv.x * tmp1;
297 const double is_y = unprojected1.y - rv.y * tmp1;
299 p_out.d_x =
static_cast<float>(is_x);
300 p_out.d_y =
static_cast<float>(is_y);
307 const float w =
_area.getWidth();
308 const float h =
_area.getHeight();
313 const bool widthAndHeightNotZero = w != 0.0f && h != 0.0f;
315 const float aspect = widthAndHeightNotZero ? w / h : 1.0f;
316 const float midx = widthAndHeightNotZero ? w * 0.5f : 0.5f;
317 const float midy = widthAndHeightNotZero ? h * 0.5f : 0.5f;
320 const glm::vec3 eye = glm::vec3( midx, midy,
float( -
_viewDistance ) );
321 const glm::vec3 center = glm::vec3( midx, midy, 1 );
322 const glm::vec3 up = glm::vec3( 0, -1, 0 );
325#if (GLM_VERSION_MAJOR == 0 && GLM_VERSION_MINOR <= 9 && GLM_VERSION_PATCH < 6) && (!defined(GLM_FORCE_RADIANS))
326 const glm::mat4 projectionMatrix = glm::perspective( 30.f, aspect,
float( d_viewDistance * 0.5 ),
float( d_viewDistance * 2.0 ) );
328 glm::mat4 projectionMatrix = glm::perspective( glm::radians( 30.f ), aspect,
float(
_viewDistance * 0.5 ),
float(
_viewDistance * 2.0 ) );
332 const glm::mat4 viewMatrix = glm::lookAt( eye, center, up );
334 _matrix = projectionMatrix * viewMatrix;
Texture & createTexture(const String &name) override
void destroyTexture(Texture &texture) override
void setActiveRenderTarget(RenderTarget *renderTarget)
void setViewProjectionMatrix(const glm::mat4 &viewProjectionMatrix)
Divide::GFX::CommandBuffer * cmdBuffer() const
Divide::GFXDevice & context()
const glm::mat4 & getMatrix() const
void setDVDTexture(Divide::Handle< Divide::Texture > tex, const Sizef &size)
set the Divide::Texture that this Texture is based on to the specified texture, with the specified si...
Divide::Handle< Divide::Texture > getAttachmentTex() const
Rectf _area
holds defined area for the RenderTarget
void updateMatrix() const
helper that initialises the cached matrix
virtual ~DVDTextureTarget() override
Divide::SamplerDescriptor _sampler
A Divide sampler hash used for sampling from this texture target.
void unprojectPoint(const GeometryBuffer &buff, const Vector2f &p_in, Vector2f &p_out) const override
Divide::RenderTargetHandle _renderTarget
A handle to a Divide render target used for rendering with this DVDTextureTarget.
void resizeRenderTexture()
resize the texture
void draw(const GeometryBuffer &buffer) override
void createCEGUITexture()
helper to create CEGUI::Texture d_CEGUITexture;
void initialiseRenderTexture()
allocate and set up the texture used with the FBO.
DVDTextureTarget(CEGUIRenderer &owner, const Sizef resolution)
static String GenerateTextureName()
helper to generate unique texture names
void deactivate() override
void setArea(const Rectf &area) override
DVDTexture * _CEGUITexture
we use this to wrap _texture so it can be used by the core CEGUI lib.
double _viewDistance
tracks viewing distance (this is set up at the same time as d_matrix)
bool _matrixValid
true if saved matrix is up to date
static constexpr float DEFAULT_SIZE
default size of created texture objects
CEGUIRenderer & _owner
CEGUIRenderer that created this object.
glm::mat4 _matrix
saved copy of projection matrix
void declareRenderSize(const Sizef &sz) override
bool _requiresClear
Because Divide command buffers are deferred, clearing should be queued instead of instant.
GFXRTPool & renderTargetPool() noexcept
RenderTargetHandle allocateRT(const RenderTargetDescriptor &descriptor)
bool deallocateRT(RenderTargetHandle &handle)
Handle< Texture > texture() const
RTAttachment * getAttachment(RTAttachmentType type, RTColourAttachmentSlot slot=RTColourAttachmentSlot::SLOT_0) const
bool resize(U16 width, U16 height)
Resize all attachments.
constexpr double S_YFOV_TAN
tangent of the y FOV half-angle; used to calculate viewing distance.
Handle console commands that start with a forward slash.
constexpr U16 to_U16(const T value)
FORCE_INLINE Handle< T > GetResourceRef(const Handle< T > handle)
constexpr RenderTargetID INVALID_RENDER_TARGET_ID
constexpr I32 to_I32(const T value)
MipMappingState _mipMappingState
InternalRTAttachmentDescriptors _attachments
TextureFilter _minFilter
Texture filtering mode.
TextureWrap _wrapU
Texture wrap mode (Or S-R-T)
U8 _anisotropyLevel
The value must be in the range [0...255] and is automatically clamped by the max HW supported level.
TextureMipSampling _mipSampling