| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400 |
- #include "RendererScene.h"
- #include "RendererSystem.h"
- RendererScene::RendererScene(RendererSystem *p_system, SceneLoader *p_sceneLoader) : SystemScene(p_system, p_sceneLoader)
- {
- m_renderTask = new RenderTask(this, p_system->getRenderer());
- m_camera = nullptr;
- m_skybox = nullptr;
- }
- RendererScene::~RendererScene()
- {
- delete m_camera;
- }
- ErrorCode RendererScene::init()
- {
- ErrorCode returnError = ErrorCode::Success;
-
- // Create a default camera, in case it is not created upon loading a scene
- m_camera = new CameraObject(this, "Default Camera");
- // Create a default directional light, in case it is not created upon loading a scene
- m_directionalLight = new DirectionalLightObject(this, "Default Directional Light", DirectionalLightDataSet());
-
- // Create a default static environment map, so it can be used while a real one hasn't been loaded yet
- //m_sceneObjects.m_staticSkybox = new EnvironmentMapObject(this, "default", Loaders::textureCubemap().load());
- return returnError;
- }
- ErrorCode RendererScene::setup(const PropertySet &p_properties)
- {
- // Get default object pool size
- decltype(m_graphicsObjPool.getPoolSize()) objectPoolSize = Config::objectPoolVar().object_pool_size;
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::ObjectPoolSize:
- objectPoolSize = p_properties[i].getInt();
- break;
- }
- }
- // Initialize object pools
- m_graphicsObjPool.init(objectPoolSize);
- m_objectsLoadingToMemory.reserve(objectPoolSize);
- m_objectsToDestroy.reserve(objectPoolSize);
- return ErrorCode::Success;
- }
- ErrorCode RendererScene::preload()
- {
- // Implementation note: use number of allocated objects as an early bail - this method is most
- // likely called after populating pools, which means objects are lined at the start of the pools)
- /*/ Start loading Model Objects
- for(decltype(m_modelObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_modelObjPool.getNumAllocated(),
- size = m_modelObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // If current object is allocated and is not loaded to memory already
- if(m_modelObjPool[i].allocated() && !(m_modelObjPool[i].getObject()->isLoadedToMemory()))
- {
- // Add the object to be loaded later
- //m_objectsBeingLoaded.push_back(LoadableGraphicsObjectAndIndex(m_modelObjPool[i].getObject(), m_modelObjPool[i].getIndex()));
- m_objectsBeingLoaded.emplace_back(m_modelObjPool[i].getObject(), m_modelObjPool[i].getIndex());
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- }
- }
- // Start loading ShaderModel Objects
- for(decltype(m_shaderObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_shaderObjPool.getNumAllocated(),
- size = m_shaderObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // If current object is allocated and is not loaded to memory already
- if(m_shaderObjPool[i].allocated() && !(m_shaderObjPool[i].getObject()->isLoadedToMemory()))
- {
- // Add the object to be loaded later
- //m_objectsBeingLoaded.push_back(LoadableGraphicsObjectAndIndex(m_shaderObjPool[i].getObject(), m_shaderObjPool[i].getIndex()));
- m_objectsBeingLoaded.emplace_back(m_shaderObjPool[i].getObject(), m_shaderObjPool[i].getIndex());
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- }
- }
-
- // Start loading Environment Map Objects
- for(decltype(m_envMapPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_envMapPool.getNumAllocated(),
- size = m_envMapPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // If current object is allocated and is not loaded to memory already
- if (m_envMapPool[i].allocated() && !(m_envMapPool[i].getObject()->isLoadedToMemory()))
- {
- // Add the object to be loaded later
- //m_objectsBeingLoaded.push_back(LoadableGraphicsObjectAndIndex(m_envMapPool[i].getObject(), m_shaderObjPool[i].getIndex()));
- m_objectsBeingLoaded.emplace_back(m_envMapPool[i].getObject(), m_shaderObjPool[i].getIndex());
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- }
- }
- // Load every object to memory. It still works in parallel, however,
- // it returns only when all objects have finished loading (simulating sequential call)
- TaskManagerLocator::get().parallelFor(size_t(0), m_objectsBeingLoaded.size(), size_t(1), [=](size_t i)
- {
- m_objectsBeingLoaded[i].LoadToMemory();
- });*/
- // Go over each graphics object and add it to the loading array
- for(decltype(m_graphicsObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_graphicsObjPool.getNumAllocated(),
- size = m_graphicsObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // Check if the graphics object is allocated inside the pool container
- if(m_graphicsObjPool[i].allocated())
- {
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- auto graphicsObject = m_graphicsObjPool[i].getObject();
- m_objectsLoadingToMemory.push_back(graphicsObject);
- }
- }
- // Load every object to memory. It still works in parallel, however,
- // it returns only when all objects have finished loading (simulating sequential call)
- TaskManagerLocator::get().parallelFor(size_t(0), m_objectsLoadingToMemory.size(), size_t(1), [=](size_t i)
- {
- m_objectsLoadingToMemory[i]->loadToMemory();
- });
- return ErrorCode::Success;
- }
- void RendererScene::loadInBackground()
- {
- // Implementation note: use number of allocated objects as an early bail - this method is most
- // likely called after populating pools, which means objects are lined up at the start of the pools)
- // Go over each graphics object
- for(decltype(m_graphicsObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_graphicsObjPool.getNumAllocated(),
- size = m_graphicsObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // Check if the graphics object is allocated inside the pool container
- if(m_graphicsObjPool[i].allocated())
- {
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- auto graphicsObject = m_graphicsObjPool[i].getObject();
- // Add the graphics object to the loading array and start loading it in the background
- m_objectsLoadingToMemory.push_back(graphicsObject);
- TaskManagerLocator::get().startBackgroundThread(std::bind(&GraphicsObject::loadToMemory, graphicsObject));
- }
- }
- }
- PropertySet RendererScene::exportObject()
- {
- // Create the root property set
- PropertySet propertySet(Properties::Graphics);
- /*
- // Add root property set for scene values
- auto &scene = propertySet.addPropertySet(Properties::Scene);
- // Add individual scene values
- scene.addProperty(Properties::ModelPoolSize, (int)m_modelObjPool.getPoolSize());
- scene.addProperty(Properties::PointLightPoolSize, (int)m_pointLightPool.getPoolSize());
- scene.addProperty(Properties::SpotLightPoolSize, (int)m_spotLightPool.getPoolSize());
- // Add root property set for all the objects
- auto &objects = propertySet.addPropertySet(Properties::Objects);
- // Add camera object property set
- objects.addPropertySet(m_camera->exportObject());
- // Add directional light property set
- objects.addPropertySet(m_directionalLight->exportObject());
- // Add static environment map property set
- objects.addPropertySet(m_skybox->exportObject());
- // Add model object property sets
- for(decltype(m_modelObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_modelObjPool.getNumAllocated(),
- size = m_modelObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_modelObjPool[i].allocated())
- objects.addPropertySet(m_modelObjPool[i].getObject()->exportObject());
- }
- // Add custom shader object property sets
- for(decltype(m_shaderObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_shaderObjPool.getNumAllocated(),
- size = m_shaderObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_shaderObjPool[i].allocated())
- objects.addPropertySet(m_shaderObjPool[i].getObject()->exportObject());
- }
- // Add point light property sets
- for(decltype(m_pointLightPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_pointLightPool.getNumAllocated(),
- size = m_pointLightPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_pointLightPool[i].allocated())
- objects.addPropertySet(m_pointLightPool[i].getObject()->exportObject());
- }
- // Add spot light property sets
- for(decltype(m_spotLightPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_spotLightPool.getNumAllocated(),
- size = m_spotLightPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_spotLightPool[i].allocated())
- objects.addPropertySet(m_spotLightPool[i].getObject()->exportObject());
- }
- */
- return propertySet;
- }
- void RendererScene::update(const float p_deltaTime)
- {
- // Clear variables
- m_sceneObjects.m_staticSkybox = nullptr;
- m_sceneObjects.m_directionalLight = nullptr;
- // Clear arrays from previous frame
- m_sceneObjects.m_pointLights.clear();
- m_sceneObjects.m_spotLights.clear();
- m_sceneObjects.m_modelData.clear();
- m_sceneObjects.m_modelDataWithShaders.clear();
- m_sceneObjects.m_loadToVideoMemory.clear();
- // _______________________________
- // | |
- // | Objects to be destroyed |
- // |_______________________________|
- //
- // Iterate over objects that are queued to be destroyed
- if(!m_objectsToDestroy.empty())
- for(decltype(m_objectsToDestroy.size()) i = 0, size = m_objectsToDestroy.size(); i < size;)
- {
- // Check if the object isn't being currently loaded
- if(getCurrentlyLoadingObject(*m_objectsToDestroy[i]) == nullptr)
- {
- // Delete the object as it's not being loaded = not in use
- delete m_objectsToDestroy[i];
- // Remove the object from the destroy list
- m_objectsToDestroy.erase(m_objectsToDestroy.begin() + i);
- }
- // If current object is still loading, advance the index
- else
- i++;
- }
- // _______________________________
- // | |
- // | Currently Loading Objects |
- // |_______________________________|
- //
- // Iterate over currently loading objects
- for(decltype(m_objectsLoadingToMemory.size()) i = 0, size = m_objectsLoadingToMemory.size(),
- maxObjects = Config::rendererVar().objects_loaded_per_frame; i < size;)
- {
- // Perform a check that marks an object if it is loaded to memory
- m_objectsLoadingToMemory[i]->performCheckIsLoadedToMemory();
- // If the object has loaded to memory already, add to load queue
- if(m_objectsLoadingToMemory[i]->isLoadedToMemory())
- {
- // If object should be activated after loading (for example wasn't set to be deleted while loading)
- //if(m_objectsBeingLoaded[i].isActivatedAfterLoading())
- //{
- // Make object active, so it is passed to the renderer for drawing
- m_objectsLoadingToMemory[i]->setActive(true);
- if(m_objectsLoadingToMemory[i]->modelComponentPresent())
- {
- // Get all loadable objects from the model component
- auto loadableObjectsFromModel = m_objectsLoadingToMemory[i]->getModelComponent()->getLoadableObjects();
- // Iterate over all loadable objects from the model component, and if any of them are not loaded to video memory already, add them to the to-load list
- for(decltype(loadableObjectsFromModel.size()) size = loadableObjectsFromModel.size(), i = 0; i < size; i++)
- if(!loadableObjectsFromModel[i].isLoadedToVideoMemory())
- m_sceneObjects.m_loadToVideoMemory.emplace_back(loadableObjectsFromModel[i]);
- }
- if(m_objectsLoadingToMemory[i]->shaderComponentPresent())
- {
- // Get all loadable objects from the model component
- auto loadableObjectsFromShader = m_objectsLoadingToMemory[i]->getShaderComponent()->getLoadableObjects();
- // Iterate over all loadable objects from the model component, and if any of them are not loaded to video memory already, add them to the to-load list
- for(decltype(loadableObjectsFromShader.size()) size = loadableObjectsFromShader.size(), i = 0; i < size; i++)
- if(!loadableObjectsFromShader[i].isLoadedToVideoMemory())
- m_sceneObjects.m_loadToVideoMemory.emplace_back(loadableObjectsFromShader[i]);
- }
- // Remove the object from the current list
- m_objectsLoadingToMemory.erase(m_objectsLoadingToMemory.begin() + i);
-
- // If the max number of object to be processed per frame has been reached, break from the loop
- if(--maxObjects == 0)
- break;
- }
- // If current object is still loading, advance the index
- else
- i++;
- }
- // ___________________________
- // | |
- // | Graphics Objects |
- // |___________________________|
- //
- // Iterate over all graphics object and process them to be rendered
- for(decltype(m_graphicsObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_graphicsObjPool.getNumAllocated(),
- size = m_graphicsObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // Check if the graphics object is allocated inside the pool container
- if(m_graphicsObjPool[i].allocated())
- {
- auto *graphicsObject = m_graphicsObjPool[i].getObject();
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- // Check if the graphics object is enabled
- if(graphicsObject->isObjectActive())
- {
- // Update the object
- graphicsObject->update(p_deltaTime);
- // Check if the graphics object is already loaded to video memory (GPU)
- if(graphicsObject->isLoadedToVideoMemory())
- {
- // Check if the graphics object contains a model component
- if(graphicsObject->modelComponentPresent())
- {
- auto modelComponent = graphicsObject->getModelComponent();
- // Check if the graphics object contains a shader component
- if(graphicsObject->shaderComponentPresent())
- {
- // Loop over each model and add it to the render list of models with custom shaders
- for(decltype(modelComponent->m_modelData.size()) size = modelComponent->m_modelData.size(), i = 0; i < size; i++)
- m_sceneObjects.m_modelDataWithShaders.emplace_back(modelComponent->m_modelData[i], graphicsObject->getShaderComponent()->m_shaderData->m_shader, graphicsObject->getSpatialDataManagerReference().getWorldSpaceData().m_transformMat);
- }
- else
- {
- // Loop over each model and add it to the render list of models with default shaders
- for(decltype(modelComponent->m_modelData.size()) size = modelComponent->m_modelData.size(), i = 0; i < size; i++)
- m_sceneObjects.m_modelData.emplace_back(modelComponent->m_modelData[i], graphicsObject->getSpatialDataManagerReference().getWorldSpaceData().m_transformMat);
- }
- }
- // Check if the graphics object contains a light component
- if(graphicsObject->lightComponentPresent())
- {
- auto lightComponent = graphicsObject->getLightComponent();
- // Check if the light is enabled
- if(lightComponent->isObjectActive())
- {
- // Add the light data to the corresponding array, based on the light type
- switch(lightComponent->getLightType())
- {
- case LightComponent::LightComponentType_point:
- m_sceneObjects.m_pointLights.push_back(*lightComponent->getPointLight());
- break;
- case LightComponent::LightComponentType_spot:
- m_sceneObjects.m_spotLights.push_back(*lightComponent->getSpotLight());
- break;
- case LightComponent::LightComponentType_directional:
- m_sceneObjects.m_directionalLight = lightComponent->getDirectionalLight();
- break;
- }
- }
- }
- // Check if the graphics object contains a camera component
- if(graphicsObject->cameraComponentPresent())
- {
- m_sceneObjects.m_camera.m_viewData = graphicsObject->getSpatialDataManagerReference().getWorldSpaceData();
- /*Math::Vec3f m_positionVec(0.0f, 0.0f, 0.0f);
- Math::Vec3f m_targetVec(0.0f, 0.0f, 0.0f);
- Math::Vec3f m_upVector(0.0f, 1.0f, 0.0f);
- Math::Vec3f m_horizontalVec(0.0f, 0.0f, 0.0f);
- float m_verticalAngle = 0.5f;
- float m_horizontalAngle = 3.14f;
- // Calculate camera's rotation
- m_targetVec.target(m_verticalAngle, m_horizontalAngle);
- m_horizontalVec.horizontal(m_horizontalAngle);
- // Calculate camera's position based on the pressed movement keys
- m_upVector = Math::cross(m_horizontalVec, m_targetVec);
- m_sceneObjects.m_camera.m_viewData.m_transformMat.initCamera(m_positionVec, m_targetVec + m_positionVec, m_upVector);
- // Set the target vector variable, so it can be retrieved later by listeners
- m_targetVec = Math::Vec3f(0.0f);
- m_targetVec.y = m_verticalAngle;
- m_targetVec.z = m_horizontalAngle;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_position = m_positionVec;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler = m_targetVec;*/
- //m_sceneObjects.m_camera.m_viewData.m_transformMat.initCamera(m_positionVec, m_targetVec + m_positionVec, m_upVector);
- //m_sceneObjects.m_camera.m_viewData.m_transformMat.initCamera(m_positionVec, m_targetVec + m_positionVec, m_upVector);
- }
- }
- }
- else
- {
- }
- }
- }
- /*/ ___________________________
- // | |
- // | Model Objects |
- // |___________________________|
- //
- // Update objects and put them into scene object list (use number of allocated objects as early bail)
- for(decltype(m_modelObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_modelObjPool.getNumAllocated(),
- size = m_modelObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_modelObjPool[i].allocated())
- {
- auto *object = m_modelObjPool[i].getObject();
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- // Check if object is active, if so, update it and assign it to 'to-be-rendered' array
- if(object->isObjectActive())
- {
- // Update object
- object->update(p_deltaTime);
- // Place the object to the appropriate array
- if(object->isAffectedByLighting())
- m_sceneObjects.m_modelObjects.push_back(&object->getRenderableObjectData());
- else
- m_sceneObjects.m_postLightingObjects.push_back(&object->getRenderableObjectData());
- // Put the object in the appropriate array
- //m_sceneObjects.m_modelObjects.push_back(&object->getRenderableObjectData());
- }
- }
- }
- // ___________________________
- // | |
- // | Shader Objects |
- // |___________________________|
- //
- // Update custom shader objects and put them into scene object list (use number of allocated objects as early bail)
- for(decltype(m_shaderObjPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_shaderObjPool.getNumAllocated(),
- size = m_shaderObjPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_shaderObjPool[i].allocated())
- {
- auto *object = m_shaderObjPool[i].getObject();
- // Increment the number of allocated objects (early bail mechanism)
- numAllocObjecs++;
- // Check if object is active, if so, update it and assign it to 'to-be-rendered' array
- if(object->isObjectActive())
- {
- // Update object
- object->update(p_deltaTime);
- // Place the object to the appropriate array
- if(object->isAffectedByLighting())
- m_sceneObjects.m_customShaderObjects.push_back(&object->getRenderableObjectData());
- else
- m_sceneObjects.m_postLightingObjects.push_back(&object->getRenderableObjectData());
- }
- }
- }
- // TODO make isActive a part of system object
- if(m_directionalLight->active())
- m_directionalLight->update(p_deltaTime);
- // ___________________________
- // | |
- // | Point Lights |
- // |___________________________|
- //
- // Put all the active point lights into scene object list
- for(decltype(m_pointLightPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_pointLightPool.getNumAllocated(),
- size = m_pointLightPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_pointLightPool[i].allocated())
- {
- auto *light = m_pointLightPool[i].getObject();
- // Increment the number of allocated lights (early bail mechanism)
- numAllocObjecs++;
- if(light->active())
- {
- m_sceneObjects.m_pointLights.push_back(light->getLightDataSet());
- }
- }
- }
- // ___________________________
- // | |
- // | Spot Lights |
- // |___________________________|
- //
- // Put all the active spot lights into scene object list
- for(decltype(m_spotLightPool.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_spotLightPool.getNumAllocated(),
- size = m_spotLightPool.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- if(m_spotLightPool[i].allocated())
- {
- auto *light = m_spotLightPool[i].getObject();
- // Increment the number of allocated lights (early bail mechanism)
- numAllocObjecs++;
- if(light->active())
- {
- m_sceneObjects.m_spotLights.push_back(light->getLightDataSet());
- }
- }
- }*/
- /*/ ___________________________
- // | |
- // | Light Components |
- // |___________________________|
- //
- // Put all the active lights into scene object lists, separate by light-type
- for(decltype(m_lightComponents.getPoolSize()) i = 0, numAllocObjecs = 0, totalNumAllocObjs = m_lightComponents.getNumAllocated(),
- size = m_lightComponents.getPoolSize(); i < size && numAllocObjecs < totalNumAllocObjs; i++)
- {
- // Check if the light component object is allocated inside the pool container
- if(m_lightComponents[i].allocated())
- {
- auto* lightComponent = m_lightComponents[i].getObject();
- // Increment the number of allocated lights (early bail mechanism)
- numAllocObjecs++;
- // Check if the light is enabled
- if(lightComponent->isObjectActive())
- {
- // Add the light data to the corresponding array, based on the light type
- switch(lightComponent->getLightType())
- {
- case LightComponent::LightComponentType_point:
- m_sceneObjects.m_pointLights.push_back(*lightComponent->getPointLight());
- break;
- case LightComponent::LightComponentType_spot:
- m_sceneObjects.m_spotLights.push_back(*lightComponent->getSpotLight());
- break;
- case LightComponent::LightComponentType_directional:
- m_sceneObjects.m_directionalLight = lightComponent->getDirectionalLight();
- break;
- }
- }
- }
- }*/
- // Update camera spatial data
- calculateCamera(m_sceneObjects.m_camera.m_viewData);
- //std::cout << GetString(static_cast<Properties::PropertyID>(10)) << std::endl;
-
- //m_sceneObjects.m_camera.m_spatialData.m_rotationEuler = Math::toRadian(p_viewData.m_spatialData.m_rotationEuler);
- /*m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.y = 0.5f;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.z = 3.14f;
- //const Math::Vec3f upVector = Math::cross(m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.z, m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.y);
- //const Math::Vec3f targetVector(0.0f, m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.y, m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler.z);
- //m_sceneObjects.m_camera.m_viewData.m_transformMat.initCamera(m_sceneObjects.m_camera.m_viewData.m_spatialData.m_position, targetVector + m_sceneObjects.m_camera.m_viewData.m_spatialData.m_position, upVector);
- Math::Vec3f m_positionVec(0.0f, 0.0f, 0.0f);
- Math::Vec3f m_targetVec(0.0f, 0.0f, 0.0f);
- Math::Vec3f m_upVector(0.0f, 1.0f, 0.0f);
- Math::Vec3f m_horizontalVec(0.0f, 0.0f, 0.0f);
- float m_verticalAngle = 0.0f;
- float m_horizontalAngle = 3.14f;
- // Calculate camera's rotation
- m_targetVec.target(m_verticalAngle, m_horizontalAngle);
- m_horizontalVec.horizontal(m_horizontalAngle);
- // Calculate camera's position based on the pressed movement keys
- m_upVector = Math::cross(m_horizontalVec, m_targetVec);
- m_sceneObjects.m_camera.m_viewData.m_transformMat.initCamera(m_positionVec, m_targetVec + m_positionVec, m_upVector);
- // Set the target vector variable, so it can be retrieved later by listeners
- m_targetVec = Math::Vec3f(0.0f);
- m_targetVec.y = m_verticalAngle;
- m_targetVec.z = m_horizontalAngle;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_position = m_positionVec;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler = m_targetVec;
- m_sceneObjects.m_camera.m_viewData.m_spatialData.m_rotationEuler = Math::Vec3f(0.0f);
- m_sceneObjects.m_camera.m_viewData.m_transformMat = Math::createTransformMat(
- Math::Vec3f(0.0f),
- Math::Vec3f(0.0f, 0.0f, 45.0f),
- Math::Vec3f(1.0f));
- Math::Vec3f rotation(30.0f, 30.0f, 0.0f);
- rotation = Math::toRadian(rotation);
- float cosY = cosf(rotation.y); // Yaw
- float sinY = sinf(rotation.y);
- float cosP = cosf(rotation.x); // Pitch
- float sinP = sinf(rotation.x);
- float cosR = cosf(rotation.z); // Roll
- float sinR = sinf(rotation.z);
- Math::Mat4f mat;
- mat.identity();
- mat.m[0] = cosY * cosR + sinY * sinP * sinR;
- mat.m[1] = cosR * sinY * sinP - sinR * cosY;
- mat.m[2] = cosP * sinY;
- mat.m[4] = cosP * sinR;
- mat.m[5] = cosR * cosP;
- mat.m[6] = -sinP;
- mat.m[8] = sinR * cosY * sinP - sinY * cosR;
- mat.m[9] = sinY * sinR + cosR * cosY * sinP;
- mat.m[10] = cosP * cosY;
- m_sceneObjects.m_camera.m_viewData.m_transformMat = mat * Math::Mat4f();*/
- m_sceneObjects.m_staticSkybox = m_skybox;
- m_sceneObjects.m_directionalLight = &m_directionalLight->getLightDataSet();
- }
- SystemObject *RendererScene::createObject(const PropertySet &p_properties)
- {
- // Check if property set node is present
- if(p_properties)
- {
- // Check if the rendering property is present
- auto &renderingProperty = p_properties.getPropertySetByID(Properties::Rendering);
- if(renderingProperty)
- {
- // Get the object name
- auto &nameProperty = p_properties.getPropertyByID(Properties::Name);
- // Find a place for the new object in the pool
- auto graphicsObjectFromPool = m_graphicsObjPool.newObject();
- // Check if the pool wasn't full
- if(graphicsObjectFromPool != nullptr)
- {
- std::string name;
- // If the name property is missing, generate a unique name based on the object's index in the pool
- if(nameProperty)
- name = nameProperty.getString();
- else
- name = GetString(Properties::GraphicsObject) + Utilities::toString(graphicsObjectFromPool->getIndex());
-
- // Construct the GraphicsObject
- graphicsObjectFromPool->construct(this, name);
- auto graphicsObject = graphicsObjectFromPool->getObject();
- //graphicsObject->importObject(renderingProperty);
- // Start importing the newly created object in a background thread
- //TaskManagerLocator::get().startBackgroundThread(std::bind(&GraphicsObject::importObject, graphicsObject, renderingProperty));
- graphicsObject->importObject(renderingProperty);
-
- return graphicsObject;
- }
- else
- {
- ErrHandlerLoc::get().log(ErrorCode::ObjectPool_full, ErrorSource::Source_RendererScene, "Failed to add GraphicsObject - \'" + nameProperty.getString() + "\'");
- }
- }
- }
- // If valid type was not specified, or object creation failed, return a null object instead
- return g_nullSystemBase.getScene()->createObject(p_properties);
- }
- ErrorCode RendererScene::destroyObject(SystemObject *p_systemObject)
- {
- // Check if object is valid and belongs to graphics system
- if(p_systemObject != nullptr && p_systemObject->getSystemType() == Systems::Graphics)
- {
- // Cast the system object to graphics object, as it belongs to the renderer scene
- GraphicsObject *objectToDestroy = static_cast<GraphicsObject *>(p_systemObject);
- // Check if the object is being currently loaded
- auto loadingObject = getCurrentlyLoadingObject(*p_systemObject);
- if(loadingObject != nullptr)
- {
- // If it is currently being loaded, add it to the destroy list, as it cannot be deleted now while it is being used
- m_objectsToDestroy.push_back(objectToDestroy);
- return ErrorCode::Success;
- }
- else
- {
- // Try to destroy the object; return success if it succeeds
- if(removeObjectFromPool(*objectToDestroy))
- return ErrorCode::Success;
- }
- }
- // If this point is reached, no object was found, return an appropriate error
- return ErrorCode::Destroy_obj_not_found;
- }
- void RendererScene::changeOccurred(ObservedSubject *p_subject, BitMask p_changeType)
- {
- //std::cout << "change occurred" << std::endl;
- }
- BitMask RendererScene::getDesiredSystemChanges()
- {
- return Systems::Changes::Generic::All;
- }
- BitMask RendererScene::getPotentialSystemChanges()
- {
- return Systems::Changes::None;
- }
- MaterialData RendererScene::loadMaterialData(PropertySet &p_materialProperty, Model::MaterialArrays &p_materialArraysFromModel, MaterialType p_materialType, std::size_t p_meshIndex)
- {
- // Declare the material data that is to be returned and a flag showing whether the material data was loaded successfully
- MaterialData newMaterialData;
- bool materialWasLoaded = false;
- /*/ Try to load the material from the filename retrieved from properties
- if (p_materialProperty)
- {
- // Get texture filename property, check if it is valid
- auto filenameProperty = p_materialProperty.getPropertyByID(Properties::Filename);
- if (filenameProperty.isVariableTypeString())
- {
- // Get texture filename string, check if it is valid
- auto filename = filenameProperty.getString();
- if (!filename.empty())
- {
- // Get the texture and load it to memory
- auto materialHandle = Loaders::texture2D().load(filename, p_materialType, false);
- auto materialLoadError = materialHandle.loadToMemory();
- // Check if the texture was loaded successfully
- if (materialLoadError == ErrorCode::Success)
- {
- newMaterialData.m_texture = materialHandle;
- materialWasLoaded = true;
- }
- else
- {
- ErrHandlerLoc::get().log(materialLoadError, ErrorSource::Source_Renderer);
- }
- }
- }
- }
- // Try to load the material from the filename retrieved from the model
- if (!materialWasLoaded)
- {
- // Check if there are enough materials, and if the material isn't default
- if (p_materialArraysFromModel.m_numMaterials > p_meshIndex
- && !p_materialArraysFromModel.m_materials[p_materialType][p_meshIndex].isEmpty()
- && !p_materialArraysFromModel.m_materials[p_materialType][p_meshIndex].isDefaultMaterial())
- {
- // Get the texture and load it to memory
- auto materialHandle = Loaders::texture2D().load(p_materialArraysFromModel.m_materials[p_materialType][p_meshIndex].m_filename, p_materialType, false);
- auto materialLoadError = materialHandle.loadToMemory();
- // Check if the texture was loaded successfully
- if (materialLoadError == ErrorCode::Success)
- {
- newMaterialData.m_texture = materialHandle;
- materialWasLoaded = true;
- }
- else
- {
- ErrHandlerLoc::get().log(materialLoadError, ErrorSource::Source_Renderer);
- }
- }
- }
- // All attempts to load the material were unsuccessful, so load a default material
- if (!materialWasLoaded)
- {
- newMaterialData.m_texture = Loaders::texture2D().getDefaultTexture(p_materialType);
- }
- */
- // Return the newly loaded material data
- return newMaterialData;
- }
- ModelComponent *RendererScene::loadModelComponent(const PropertySet &p_properties)
- {
- ModelComponent *newComponent = nullptr;
-
- // Check if models node is present
- if(p_properties)
- {
- // Create the model component
- newComponent = new ModelComponent(this, "");
- // Loop over each model entry in the node
- for(decltype(p_properties.getNumProperties()) iModel = 0, numModels = p_properties.getNumProperties(); iModel < numModels; iModel++)
- {
- // Get model filename
- auto modelName = p_properties.getPropertySet(iModel).getPropertyByID(Properties::Filename).getString();
- // Add a new model data entry, and get a reference to it
- newComponent->m_modelData.push_back(ModelData(Loaders::model().load(modelName, false)));
- auto &newModelData = newComponent->m_modelData.back();
- // Load the model to memory, to be able to access all of its meshes
- newModelData.m_model.loadToMemory();
- // Get the meshes array
- auto meshesInModelArray = newModelData.m_model.getMeshArray();
- // Get the meshes array
- auto meshesProperty = p_properties.getPropertySet(iModel).getPropertySetByID(Properties::Meshes);
- // Check if the meshes array node is present;
- // If it is present, only add the meshes included in the meshes node
- // If it is not present, add all the meshes included in the model
- if(meshesProperty)
- {
- // Loop over each mesh entry in the model node
- for(decltype(meshesProperty.getNumProperties()) iMesh = 0, numMeshes = meshesProperty.getNumProperties(); iMesh < numMeshes; iMesh++)
- {
- // Try to get the mesh index property node and check if it is present
- auto meshIndexProperty = meshesProperty.getPropertySet(iMesh).getPropertyByID(Properties::Index);
- if(meshIndexProperty)
- {
- // Get the mesh index, check if it is valid and within the range of mesh array that was loaded from the model
- const int meshDataIndex = meshIndexProperty.getInt();
- if(meshDataIndex > 0 && meshDataIndex < meshesInModelArray.size())
- {
- // Get material properties
- auto materialsProperty = meshesProperty.getPropertySet(iMesh).getPropertySetByID(Properties::Materials);
- // Define material data and material properties
- MaterialData materials[MaterialType::MaterialType_NumOfTypes];
- PropertySet materialProperties[MaterialType::MaterialType_NumOfTypes] =
- {
- materialsProperty.getPropertySetByID(Properties::Diffuse),
- materialsProperty.getPropertySetByID(Properties::Normal),
- materialsProperty.getPropertySetByID(Properties::Emissive),
- materialsProperty.getPropertySetByID(Properties::RMHAO)
- };
-
- // Go over each material type
- for(unsigned int iMatType = 0; iMatType < MaterialType::MaterialType_NumOfTypes; iMatType++)
- {
- // Check if an entry for the current material type was present within the properties
- if(materialProperties[iMatType])
- {
- // Load the material data
- materials[iMatType] = loadMaterialData(materialProperties[iMatType], newModelData.m_model.getMaterialArrays(), static_cast<MaterialType>(iMatType), meshDataIndex);
- }
- }
- /*if(materialsProperty)
- {
- auto diffuseMatProperty = materialsProperty.getPropertySetByID(Properties::Diffuse);
- materials[MaterialType_Diffuse] = loadMaterialData(diffuseMatProperty, newModelData.m_model.getMaterialArrays(), MaterialType_Diffuse, meshDataIndex);
- //newModelData.m_model.getMaterialArrays()
- //materials[MaterialType_Diffuse] = &materialProperty.getPropertySetByID(Properties::Diffuse);
- //materials[MaterialType_Normal] = &materialProperty.getPropertySetByID(Properties::Normal);
- //materials[MaterialType_Emissive] = &materialProperty.getPropertySetByID(Properties::Emissive);
- //materials[MaterialType_Combined] = &materialProperty.getPropertySetByID(Properties::RMHAO);
- }*/
-
- newModelData.m_meshes.push_back(MeshData(meshesInModelArray[iMesh], materials));
- }
- }
- }
- }
- else
- {
- // Get the material arrays that were loaded from the model file
- auto &materialArrayFromModel = newModelData.m_model.getMaterialArrays();
- // Iterate over every mesh in the model
- for(decltype(meshesInModelArray.size()) iMesh = 0, numMeshes = meshesInModelArray.size(); iMesh < numMeshes; iMesh++)
- {
- // Define material data and material properties
- MaterialData materials[MaterialType::MaterialType_NumOfTypes];
- // Go over each mesh in the model
- if(iMesh > materialArrayFromModel.m_numMaterials)
- {
- // Go over each material type
- for(unsigned int iMatType = 0; iMatType < MaterialType::MaterialType_NumOfTypes; iMatType++)
- {
- // Get the texture filename and load it to memory
- auto textureFromModel = Loaders::texture2D().load(materialArrayFromModel.m_materials[iMatType][iMesh].m_filename, static_cast<MaterialType>(iMatType), false);
- auto materialLoadError = textureFromModel.loadToMemory();
-
- // Check if the texture was loaded successfully
- if(materialLoadError == ErrorCode::Success)
- {
- materials[MaterialType::MaterialType_Diffuse].m_texture = textureFromModel;
- }
- else
- {
- ErrHandlerLoc::get().log(materialLoadError, ErrorSource::Source_Renderer);
- }
- }
-
- // Add the data for this mesh. Include materials loaded from the model itself, if they were present, otherwise, include default textures instead
- newModelData.m_meshes.push_back(MeshData(meshesInModelArray[iMesh], materials));
- }
- }
- }
- }
- }
- return newComponent;
- }
- ShaderComponent *RendererScene::loadShaderComponent(const PropertySet &p_properties)
- {
- ShaderComponent *newComponent = nullptr;
- // Check if shaders node is valid
- if(p_properties)
- {
- // Get nodes for different shader types
- auto fragmentShaderNode = p_properties.getPropertyByID(Properties::FragmentShader);
- auto vertexShaderNode = p_properties.getPropertyByID(Properties::VertexShader);
- // Check if any of the shader nodes are present
- if(fragmentShaderNode || vertexShaderNode)
- {
- // Load shader program
- auto shaderProgram = Loaders::shader().load(p_properties);
- // If shader is not default (i.e. at least one of the shader types was loaded)
- if(!shaderProgram->isDefaultProgram())
- {
- // Load the shader to memory and assign it to the new shader component
- shaderProgram->loadToMemory();
- newComponent = new ShaderComponent(this, "", *shaderProgram);
- }
- }
- }
- return newComponent;
- }
- LightComponent *RendererScene::loadLightComponent(const PropertySet &p_properties)
- {
- LightComponent *newComponent = nullptr;
- // Check if the property node is valid
- if(p_properties)
- {
- Math::Vec3f color;
- float intensity = 0.0f,
- cutoffAngle = 0.0f;
- Properties::PropertyID type = Properties::PropertyID::Null;
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::Color:
- color = p_properties[i].getVec3f();
- break;
- case Properties::CutoffAngle:
- // Convert to radians
- cutoffAngle = cosf(Math::toRadian(p_properties[i].getFloat()));
- break;
- case Properties::Intensity:
- intensity = p_properties[i].getFloat();
- break;
- case Properties::Type:
- type = p_properties[i].getID();
- break;
- }
- }
- // Create light component based on light type
- switch(type)
- {
- case Properties::DirectionalLight:
- {
- // Create and setup the directional light data set
- DirectionalLightDataSet dirLightDataSet;
- dirLightDataSet.m_color = color;
- dirLightDataSet.m_intensity = intensity;
- // Create the component of the directional light type
- newComponent = new LightComponent(this, "", dirLightDataSet);
- }
- break;
- case Properties::PointLight:
- {
- // Create and setup the point light data set
- PointLightDataSet pointLightDataSet;
- pointLightDataSet.m_color = color;
- pointLightDataSet.m_intensity = intensity;
- // Create the component of the point light type
- newComponent = new LightComponent(this, "",pointLightDataSet);
- }
- break;
- case Properties::SpotLight:
- {
- // Create and setup the spot light data set
- SpotLightDataSet spotLightDataSet;
- spotLightDataSet.m_color = color;
- spotLightDataSet.m_cutoffAngle = cutoffAngle;
- spotLightDataSet.m_intensity = intensity;
- // Create the component of the spot light type
- newComponent = new LightComponent(this, "", spotLightDataSet);
- }
- break;
- }
- }
- return newComponent;
- }
- ModelObject *RendererScene::loadModelObject(const PropertySet &p_properties)
- {
- // Get model properties
- auto &models = p_properties.getPropertySetByID(Properties::Models);
- auto &shaderProperty = p_properties.getPropertySetByID(Properties::Shaders);
- ErrorCode objPoolError = ErrorCode::Failure;
- ModelObject *newObject = nullptr;
- /*
- // If shaders are present
- if(shaderProperty)
- {
- // Try to add a new object to the pool
- objPoolError = m_shaderObjPool.add(
- this, p_properties.getPropertyByID(Properties::Name).getString(),
- Loaders::model().load(models.getPropertyByID(Properties::Filename).getString(), false),
- Loaders::shader().load(shaderProperty));
- // The newly added object in the pool
- newObject = m_shaderObjPool.getLastRawObject();
- newObject->getRenderableObjectData().m_shader->loadToMemory();
- }
- else
- {
- // Try to add a new object to the pool
- objPoolError = m_modelObjPool.add(
- this, p_properties.getPropertyByID(Properties::Name).getString(),
- Loaders::model().load(models.getPropertyByID(Properties::Filename).getString(), false));
- // The newly added object in the pool
- newObject = m_modelObjPool.getLastRawObject();
- }
- // If adding a new object was successful, continue to load data
- if(objPoolError == ErrorCode::Success)
- {
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::OffsetPosition:
- newObject->setOffsetPosition(p_properties[i].getVec3f());
- break;
- case Properties::OffsetRotation:
- newObject->setOffsetRotation(p_properties[i].getVec3f());
- break;
- case Properties::LocalPosition:
- newObject->setPosition(p_properties[i].getVec3f());
- break;
- case Properties::LocalRotation:
- newObject->setRotation(p_properties[i].getVec3f());
- break;
- case Properties::LocalScale:
- newObject->setScale(p_properties[i].getVec3f());
- break;
- case Properties::Lighting:
- newObject->setLighting(p_properties[i].getBool());
- break;
- case Properties::AlphaThreshold:
- newObject->setAlphaThreshold(p_properties[i].getFloat());
- break;
- case Properties::HeightScale:
- newObject->setHeightScale(p_properties[i].getFloat());
- break;
- case Properties::TextureTilingFactor:
- newObject->setTextureTilingFactor(p_properties[i].getFloat());
- break;
- }
- }
- // Adjust the position and rotation by offset
- newObject->setPosition(newObject->getBaseObjectData().m_position + newObject->getBaseObjectData().m_offsetPosition);
- newObject->setRotation(newObject->getBaseObjectData().m_rotation + newObject->getBaseObjectData().m_offsetRotation);
- // Get material parent property
- auto &materialProperty = p_properties.getPropertySetByID(Properties::Materials);
- // Get material properties
- const PropertySet *materials[MaterialType_NumOfTypes];
- materials[MaterialType_Diffuse] = &materialProperty.getPropertySetByID(Properties::Diffuse);
- materials[MaterialType_Normal] = &materialProperty.getPropertySetByID(Properties::Normal);
- materials[MaterialType_Emissive] = &materialProperty.getPropertySetByID(Properties::Emissive);
- materials[MaterialType_Combined] = &materialProperty.getPropertySetByID(Properties::RMHAO);
-
- // Process all materials
- // For every type of material
- for(unsigned int matType = 0; matType < MaterialType_NumOfTypes; matType++)
- // Check if material property is valid
- if(*materials[matType])
- // For every property set in the material property
- for(decltype(materials[matType]->getNumPropertySets()) i = 0, size = materials[matType]->getNumPropertySets(); i < size; i++)
- // Add the material to the new object
- newObject->addMaterial(static_cast<MaterialType>(matType),
- materials[matType]->getPropertySet(i).getPropertyByID(Properties::Filename).getString(),
- materials[matType]->getPropertySet(i).getPropertyByID(Properties::Index).getInt());
-
- // Get default material properties
- // Default materials replace any missing materials from the model file
- auto &defaultMaterials = materialProperty.getPropertySetByID(Properties::Default);
- if(defaultMaterials)
- {
- // Get individual default materials
- const PropertySet *defaulMaterials[MaterialType_NumOfTypes];
- defaulMaterials[MaterialType_Diffuse] = &defaultMaterials.getPropertySetByID(Properties::Diffuse);
- defaulMaterials[MaterialType_Normal] = &defaultMaterials.getPropertySetByID(Properties::Normal);
- defaulMaterials[MaterialType_Emissive] = &defaultMaterials.getPropertySetByID(Properties::Emissive);
- defaulMaterials[MaterialType_Combined] = &defaultMaterials.getPropertySetByID(Properties::RMHAO);
- // Process default materials by assigning them to the model object
- for(unsigned int matType = 0; matType < MaterialType_NumOfTypes; matType++)
- if(*defaulMaterials[matType])
- newObject->setDefaultMaterial(matType, defaulMaterials[matType]->getPropertyByID(Properties::Filename).getString());
- }
- return newObject;
- }
- // If adding a new object failed, log an error and return a nullptr
- else
- {
- ErrHandlerLoc::get().log(objPoolError, ErrorSource::Source_SceneLoader);
- return nullptr;
- }*/
- return newObject;
- }
- CameraObject *RendererScene::loadCameraObject(const PropertySet & p_properties)
- {
- // Delete an existing camera
- if(m_camera != nullptr)
- delete m_camera;
- // Create a new camera
- m_camera = new CameraObject(this, p_properties.getPropertyByID(Properties::Name).getString());
-
- return m_camera;
- }
- EnvironmentMapObject *RendererScene::loadEnvironmentMap(const PropertySet &p_properties)
- {
- EnvironmentMapObject *newObject = nullptr;
- /*ErrorCode objPoolError = ErrorCode::Failure;
- bool staticEnvMap = false;
- auto &materials = p_properties.getPropertySetByID(Properties::Materials);
- std::string filenames[CubemapFace_NumOfFaces];
- if(materials.getNumPropertySets() >= CubemapFace_NumOfFaces)
- for(unsigned int face = CubemapFace_PositiveX; face < CubemapFace_NumOfFaces; face++)
- filenames[face] = materials.getPropertySet(face).getPropertyByID(Properties::Filename).getString();
- // Check if the environment map should be a static one
- staticEnvMap = p_properties.getPropertyByID(Properties::Static).getBool();
- if(staticEnvMap)
- {
- //newObject = new EnvironmentMapObject(this,
- // p_properties.getPropertyByID(Properties::Name).getString(),
- // Loaders::textureCubemap().load(filenames, false));
- m_skybox = newObject;
- }
- else
- {
- objPoolError = m_envMapPool.add(this,
- p_properties.getPropertyByID(Properties::Name).getString(),
- Loaders::textureCubemap().load(filenames, false));
- // If adding a new object failed, log an error and return a nullptr
- if (objPoolError == ErrorCode::Success)
- {
- ErrHandlerLoc::get().log(objPoolError, ErrorSource::Source_SceneLoader);
- return nullptr;
- }
- }
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- //switch(p_properties[i].getPropertyID())
- //{
- //case Properties::Position:
- // newObject->setPosition(p_properties[i].getVec3f());
- // break;
- //}
- }*/
- return newObject;
- }
- DirectionalLightObject *RendererScene::loadDirectionalLight(const PropertySet &p_properties)
- {
- // Since only one directional light is supported at a time, delete the old one first
- delete m_directionalLight;
- m_directionalLight = new DirectionalLightObject(this,
- p_properties.getPropertyByID(Properties::Name).getString(),
- DirectionalLightDataSet());
-
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::Color:
- m_directionalLight->setColor(p_properties[i].getVec3f());
- break;
- case Properties::Direction:
- // Need to normalize the light direction
- m_directionalLight->setDirection(Math::normalize(p_properties[i].getVec3f()));
- break;
- case Properties::Intensity:
- m_directionalLight->setIntensity(p_properties[i].getFloat());
- break;
- }
- }
- m_directionalLight->init();
- return m_directionalLight;
- }
- PointLightObject *RendererScene::loadPointLight(const PropertySet &p_properties)
- {
- ErrorCode objPoolError = ErrorCode::Failure;
- PointLightObject *newObject = nullptr;
- /*
- // Try to add a new object to the pool
- objPoolError = m_pointLightPool.add(
- this, p_properties.getPropertyByID(Properties::Name).getString(), PointLightDataSet());
- // Get the newly added object in the pool
- newObject = m_pointLightPool.getLastRawObject();
- // If adding a new object was successful, continue to load data
- if(objPoolError == ErrorCode::Success)
- {
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::Attenuation:
- newObject->setAttenuation(p_properties[i].getVec3f());
- break;
- case Properties::Color:
- newObject->setColor(p_properties[i].getVec3f());
- break;
- case Properties::Intensity:
- newObject->setIntensity(p_properties[i].getFloat());
- break;
- case Properties::OffsetPosition:
- newObject->setOffsetPosition(p_properties[i].getVec3f());
- break;
- case Properties::Position:
- newObject->setPosition(p_properties[i].getVec3f());
- break;
- }
- }
- // Adjust the position and rotation by offset
- newObject->setPosition(newObject->getLightDataSet().m_position + newObject->getOffsetPosition());
- }
- // If adding a new object failed, log an error and return a nullptr
- else
- {
- ErrHandlerLoc::get().log(objPoolError, ErrorSource::Source_SceneLoader);
- return nullptr;
- }
- */
- return newObject;
- }
- SpotLightObject *RendererScene::loadSpotLight(const PropertySet &p_properties)
- {
- ErrorCode objPoolError = ErrorCode::Failure;
- SpotLightObject *newObject = nullptr;
- /*
- // Try to add a new object to the pool
- objPoolError = m_spotLightPool.add(
- this, p_properties.getPropertyByID(Properties::Name).getString(), SpotLightDataSet());
- // Get the newly added object in the pool
- newObject = m_spotLightPool.getLastRawObject();
- // If adding a new object was successful, continue to load data
- if(objPoolError == ErrorCode::Success)
- {
- // Load property data
- for(decltype(p_properties.getNumProperties()) i = 0, size = p_properties.getNumProperties(); i < size; i++)
- {
- switch(p_properties[i].getPropertyID())
- {
- case Properties::Attenuation:
- newObject->setAttenuation(p_properties[i].getVec3f());
- break;
- case Properties::CutoffAngle:
- // Convert to radians
- newObject->setCutoffAngle(cosf(Math::toRadian(p_properties[i].getFloat())));
- break;
- case Properties::Color:
- newObject->setColor(p_properties[i].getVec3f());
- break;
- case Properties::Direction:
- newObject->setDirection(p_properties[i].getVec3f());
- break;
- case Properties::Intensity:
- newObject->setIntensity(p_properties[i].getFloat());
- break;
- case Properties::OffsetPosition:
- newObject->setOffsetPosition(p_properties[i].getVec3f());
- break;
- case Properties::OffsetRotation:
- newObject->setOffsetRotation(p_properties[i].getVec3f());
- break;
- case Properties::Position:
- newObject->setPosition(p_properties[i].getVec3f());
- break;
- }
- }
- // Adjust the position and rotation by offset (and normalize the direction)
- newObject->setPosition(newObject->getLightDataSet().m_position + newObject->getOffsetPosition());
- newObject->setDirection(Math::normalize(newObject->getLightDataSet().m_direction + newObject->getOffsetRotation()));
- }
- // If adding a new object failed, log an error and return a nullptr
- else
- {
- ErrHandlerLoc::get().log(objPoolError, ErrorSource::Source_SceneLoader);
- return nullptr;
- }
- */
- return newObject;
- }
|