1#include <Engine/Rendering/RenderObject.hpp>
3#include <Engine/RadiumEngine.hpp>
5#include <Engine/Data/DisplayableObject.hpp>
6#include <Engine/Data/Material.hpp>
7#include <Engine/Data/RenderParameters.hpp>
8#include <Engine/OpenGL.hpp>
9#include <Engine/Rendering/RenderObjectManager.hpp>
10#include <Engine/Scene/Component.hpp>
14#include <Engine/Scene/Entity.hpp>
16#include <Core/Containers/MakeShared.hpp>
17#include <Engine/Data/BlinnPhongMaterial.hpp>
18#include <Engine/Data/ShaderProgram.hpp>
19#include <Engine/Data/SimpleMaterial.hpp>
20#include <Engine/Data/ViewingParameters.hpp>
35 m_lifetime { lifetime },
36 m_hasLifetime { lifetime > 0 } {}
38RenderObject::~RenderObject() {
40 m_mesh->getAbstractGeometry().getAabbObservable().detach( m_aabbObserverIndex );
51 obj->setVisible(
true );
53 obj->setRenderTechnique( rt );
61 if ( m_renderTechnique ) { m_renderTechnique->updateGL(); }
63 if ( m_mesh ) { m_mesh->updateGL(); }
80void RenderObject::setVisible(
bool visible ) {
84void RenderObject::toggleVisible() {
85 m_visible = !m_visible;
88bool RenderObject::isVisible()
const {
92void RenderObject::setPickable(
bool pickable ) {
93 m_pickable = pickable;
96void RenderObject::togglePickable() {
97 m_pickable = !m_pickable;
100bool RenderObject::isPickable()
const {
104void RenderObject::setXRay(
bool xray ) {
108void RenderObject::toggleXRay() {
112bool RenderObject::isXRay()
const {
116void RenderObject::setTransparent(
bool transparent ) {
117 m_transparent = transparent;
120void RenderObject::toggleTransparent() {
121 m_transparent = !m_transparent;
124bool RenderObject::isTransparent()
const {
125 return m_transparent;
129 if ( m_material ) { m_material->setColoredByVertexAttrib( state ); }
134 m_material->setColoredByVertexAttrib( !m_material->isColoredByVertexAttrib() );
139 if ( m_material ) {
return m_material->isColoredByVertexAttrib(); }
143bool RenderObject::isDirty()
const {
156 CORE_ASSERT( technique,
"Passing a nullptr as render technique" );
157 m_renderTechnique = technique;
161 return m_renderTechnique;
165 return m_renderTechnique;
169 m_material = material;
183 m_mesh->getAbstractGeometry().getAabbObservable().detach( m_aabbObserverIndex );
188 m_aabbObserverIndex = m_mesh->getAbstractGeometry().getAabbObservable().attach(
189 [
this]() { this->invalidateAabb(); } );
202Core::Transform RenderObject::getTransform()
const {
203 return m_component->
getEntity()->getTransform() * m_localTransform;
206Core::Matrix4 RenderObject::getTransformAsMatrix()
const {
207 return getTransform().matrix();
210Core::Aabb RenderObject::computeAabb() {
211 if ( !m_isAabbValid ) {
212 auto aabb = m_mesh->getAbstractGeometry().computeAabb();
213 if ( !aabb.isEmpty() ) {
216 for (
int i = 0; i < 8; ++i ) {
217 result.extend( getTransform() * aabb.corner( Core::Aabb::CornerType( i ) ) );
221 m_isAabbValid =
true;
225 m_isAabbValid =
true;
232void RenderObject::setLocalTransform(
const Core::Transform& transform ) {
237void RenderObject::setLocalTransform(
const Core::Matrix4& transform ) {
238 m_localTransform = Core::Transform( transform );
242const Core::Transform& RenderObject::getLocalTransform()
const {
243 return m_localTransform;
246const Core::Matrix4& RenderObject::getLocalTransformAsMatrix()
const {
247 return m_localTransform.matrix();
251 if ( m_hasLifetime ) {
252 if ( --m_lifetime <= 0 ) {
253 RadiumEngine::getInstance()->getRenderObjectManager()->renderObjectExpired(
m_idx );
259 m_component->notifyRenderObjectExpired(
m_idx );
264 m_hasLifetime =
true;
271 if ( !m_visible || !shader ) {
return; }
273 Core::Matrix4 modelMatrix = getTransformAsMatrix();
274 Core::Matrix4 normalMatrix = modelMatrix.inverse().transpose();
277 shader->
setUniform(
"transform.proj", viewParams.projMatrix );
278 shader->
setUniform(
"transform.view", viewParams.viewMatrix );
279 shader->
setUniform(
"transform.model", modelMatrix );
280 shader->
setUniform(
"transform.worldNormal", normalMatrix );
281 lightParams.
bind( shader );
282 shaderParams.
bind( shader );
286 if ( viewParams.viewMatrix.determinant() < 0 ) { glFrontFace( GL_CW ); }
287 else { glFrontFace( GL_CCW ); }
288 m_mesh->render( shader );
293 Core::Utils::Index passId ) {
295 auto shader = getRenderTechnique()->getShader( passId );
296 if ( !shader ) {
return; }
298 auto paramsProvider = getRenderTechnique()->getParametersProvider( passId );
300 if ( paramsProvider !=
nullptr ) {
301 render( lightParams, viewParams, shader, paramsProvider->getParameters() );
303 else {
render( lightParams, viewParams, shader, {} ); }
307void RenderObject::invalidateAabb() {
308 m_isAabbValid =
false;
309 m_component->invalidateAabb();
Management of shader parameters with automatic binding of a named parameter to the corresponding glsl...
void bind(const Data::ShaderProgram *shader) const
Bind the parameter uniform on the shader program.
void setUniform(const char *name, const T &value) const
Uniform setters.
void hasBeenRenderedOnce()
void render(const Data::RenderParameters &lightParams, const Data::ViewingParameters &viewParams, const Data::ShaderProgram *shader, const Data::RenderParameters &shaderParams)
bool isColoredByVertexAttrib() const
manage usage of VERTEX_COLOR attribute by the material
void toggleColoredByVertexAttrib()
manage usage of VERTEX_COLOR attribute by the material
void setLifetime(int t)
Set a lifetime to an existing RenderObject.
const std::string & getName() const
RenderObject(const std::string &name, Scene::Component *comp, const RenderObjectType &type, int lifetime=-1)
void hasExpired()
Notify component that the render object has expired.
void setColoredByVertexAttrib(bool state)
manage usage of VERTEX_COLOR attribute by the material
static RenderObject * createRenderObject(const std::string &name, Scene::Component *comp, const RenderObjectType &type, std::shared_ptr< Data::Displayable > mesh, const RenderTechnique &techniqueConfig=RenderTechnique::createDefaultRenderTechnique())
A component is an element that can be updated by a system. It is also linked to some other components...
virtual Entity * getEntity() const
Return the entity the component belongs to.
std::shared_ptr< T > make_shared(Args &&... args)
hepler function to manage enum as underlying types in VariableSet
the set of viewing parameters extracted from the camera and given to the renderer