Skip to content

Commit

Permalink
#149 Fix some glicht render on editor, add max light support
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jun 1, 2022
1 parent 561adbf commit cfc4f73
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ namespace Skylicht
std::vector<Lightmapper::CSH9> probes;
std::vector<core::vector3df> results;

CEntityManager* entityMgr = scene->getEntityManager();

if (probesComponent->getPositions(positions) > 0)
{
CEntityManager* entityMgr = scene->getEntityManager();
entityMgr->update();

Lightmapper::CLightmapper::getInstance()->initBaker(32);
Expand All @@ -108,6 +109,8 @@ namespace Skylicht
}

probesComponent->setSH(results);

entityMgr->setCamera(NULL);
}

bakeCameraObj->remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ namespace Skylicht

void CGSpotLight::updateComponent()
{
CCamera* camera = m_gameObject->getEntityManager()->getCamera();
if (camera == NULL)
return;

core::vector3df cameraPosition = camera->getGameObject()->getPosition();

// update color of light
SColor lightColor = m_spotLight->getColor().toSColor();
m_sprite->setColor(lightColor);
Expand All @@ -110,11 +104,9 @@ namespace Skylicht
core::quaternion q;
q.rotationFromTo(core::vector3df(0.0f, 1.0f, 0.0f), direction);

core::vector3df viewVector = position - cameraPosition;
viewVector.normalize();
std::vector<core::vector3df> bounds;

bool isBackFace = true;
core::vector3df r1, r2;
int count = CircleSegmentCount / 5;

for (int i = 0; i < CircleSegmentCount; i++)
{
Expand All @@ -129,28 +121,8 @@ namespace Skylicht
core::vector3df worldR = q * r;
worldR.normalize();

if (viewVector.dotProduct(worldR) > FLT_EPSILON)
{
// back face
if (i == 0)
isBackFace = true;
else if (!isBackFace)
{
r2 = worldR;
isBackFace = true;
}
}
else
{
// front face
if (i == 0)
isBackFace = false;
else if (isBackFace)
{
r1 = worldR;
isBackFace = false;
}
}
if (i % count == 0)
bounds.push_back(worldR);

m_circlePos[i] = end + worldR * maxOuterEdge;
}
Expand All @@ -159,8 +131,8 @@ namespace Skylicht
CHandles::getInstance()->drawPolyline(m_circlePos, CircleSegmentCount, true, lightColor);

// draw border edge
CHandles::getInstance()->drawLine(position, end + r1 * maxOuterEdge, lightColor);
CHandles::getInstance()->drawLine(position, end + r2 * maxOuterEdge, lightColor);
for (int i = 0, n = (int)bounds.size(); i < n; i++)
CHandles::getInstance()->drawLine(position, end + bounds[i] * maxOuterEdge, lightColor);

// update collision bbox
float boxScale = m_sprite->getViewScale() * 10.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Skylicht
CSpriteDrawData::CSpriteDrawData() :
Frame(NULL),
Scale(1.0f),
ViewScale(1.0f),
ViewScale(0.0f),
Color(255, 255, 255, 255),
Center(false),
Billboard(false),
Expand Down
13 changes: 7 additions & 6 deletions Projects/Skylicht/Engine/Source/Culling/CCullingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ namespace Skylicht

// camera
CCamera* camera = entityManager->getCamera();
u32 cameraCullingMask = camera->getCullingMask();
const core::aabbox3df& cameraBox = camera->getViewFrustum().getBoundingBox();

u32 numEntity = m_cullings.size();
for (u32 i = 0; i < numEntity; i++)
Expand All @@ -148,7 +150,7 @@ namespace Skylicht
culling->Visible = true;

// check camera mask culling
u32 test = camera->getCullingMask() & culling->CullingLayer;
u32 test = cameraCullingMask & culling->CullingLayer;
if (test == 0)
{
culling->Visible = false;
Expand Down Expand Up @@ -176,9 +178,7 @@ namespace Skylicht

transform->World.transformBoxEx(culling->BBox);

// 1. Detect by bounding box
SViewFrustum frust;

// 1. Detect by bounding box
if (rp->getType() == IRenderPipeline::ShadowMap)
{
CShadowMapRP* shadowMapRP = (CShadowMapRP*)rp;
Expand All @@ -189,8 +189,7 @@ namespace Skylicht
}
else
{
frust = camera->getViewFrustum();
culling->Visible = culling->BBox.intersectsWithBox(frust.getBoundingBox());
culling->Visible = culling->BBox.intersectsWithBox(cameraBox);
}

// 2. Detect algorithm
Expand All @@ -200,6 +199,8 @@ namespace Skylicht
{
// transform the frustum to the node's current absolute transformation
invTrans = invTransform->WorldInverse;

SViewFrustum frust = camera->getViewFrustum();
frust.transform(invTrans);

core::vector3df edges[8];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace Skylicht

CLightCullingData::CLightCullingData() :
Visible(true),
Light(NULL)
Light(NULL),
CameraDistance(0.0f)
{

}
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Engine/Source/Lighting/CLightCullingData.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Skylicht

CLight *Light;

float CameraDistance;

DECLARE_DATA_TYPE_INDEX;

public:
Expand Down
29 changes: 26 additions & 3 deletions Projects/Skylicht/Engine/Source/Lighting/CLightCullingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ namespace Skylicht

core::matrix4 invTrans;

CCamera* camera = entityManager->getCamera();
core::aabbox3df camBox = camera->getViewFrustum().getBoundingBox();
core::vector3df camPos = camera->getGameObject()->getPosition();

u32 numEntity = m_cullings.size();
for (u32 i = 0; i < numEntity; i++)
{
Expand All @@ -90,8 +94,8 @@ namespace Skylicht
transform->World.transformBoxEx(lightBox);

// 1. Detect by bounding box
CCamera* camera = entityManager->getCamera();
culling->Visible = lightBox.intersectsWithBox(camera->getViewFrustum().getBoundingBox());
culling->Visible = lightBox.intersectsWithBox(camBox);
culling->CameraDistance = transform->World.getTranslation().getDistanceFromSQ(camPos);

// 2. Detect algorithm
if (culling->Visible == true)
Expand Down Expand Up @@ -127,7 +131,26 @@ namespace Skylicht

// add list visible light
if (culling->Visible == true)
m_visible.push_back(culling);
{
int n = (int)m_visible.size();
if (n == 0 || culling->CameraDistance > m_visible[n - 1]->CameraDistance)
{
// add end
m_visible.push_back(culling);
}
else
{
// insert sort
for (int i = 0; i < n; i++)
{
if (culling->CameraDistance < m_visible[i]->CameraDistance)
{
m_visible.insert(culling, i);
break;
}
}
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions Projects/Skylicht/Engine/Source/RenderPipeline/CBaseRP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ namespace Skylicht
bool CBaseRP::s_bakeMode = false;
bool CBaseRP::s_bakeLMMode = false;
int CBaseRP::s_bakeBounce = 0;

int CBaseRP::s_maxLight = 8;

SColor CBaseRP::s_clearColor;

CBaseRP::CBaseRP() :
Expand Down
30 changes: 16 additions & 14 deletions Projects/Skylicht/Engine/Source/RenderPipeline/CBaseRP.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace Skylicht
class CBaseRP : public IRenderPipeline
{
protected:
IRenderPipeline *m_next;
IRenderPipeline* m_next;

CMeshBuffer<video::S3DVertex2TCoords> *m_drawBuffer;
CMeshBuffer<video::S3DVertex2TCoords>* m_drawBuffer;
IVertexBuffer* m_verticesImage;
IIndexBuffer* m_indicesImage;

Expand All @@ -52,38 +52,40 @@ namespace Skylicht

static SColor s_clearColor;

static int s_maxLight;

public:
CBaseRP();

virtual ~CBaseRP();

virtual bool canRenderMaterial(CMaterial *m);
virtual bool canRenderMaterial(CMaterial* m);

virtual void render(ITexture *target, CCamera *camera, CEntityManager *entityManager, const core::recti& vp) = 0;
virtual void render(ITexture* target, CCamera* camera, CEntityManager* entityManager, const core::recti& vp) = 0;

virtual void setCamera(CCamera *camera);
virtual void setCamera(CCamera* camera);

virtual void setNextPipeLine(IRenderPipeline *next);
virtual void setNextPipeLine(IRenderPipeline* next);

virtual void onNext(ITexture *target, CCamera *camera, CEntityManager* entity, const core::recti& vp);
virtual void onNext(ITexture* target, CCamera* camera, CEntityManager* entity, const core::recti& vp);

virtual void drawMeshBuffer(CMesh *mesh, int bufferID, CEntityManager* entity, int entityID, bool skinnedMesh);
virtual void drawMeshBuffer(CMesh* mesh, int bufferID, CEntityManager* entity, int entityID, bool skinnedMesh);

public:

void updateTextureResource(CMesh *mesh, int bufferID, CEntityManager* entity, int entityID);
void updateTextureResource(CMesh* mesh, int bufferID, CEntityManager* entity, int entityID);

void beginRender2D(float w, float h);

void renderBufferToTarget(float sx, float sy, float sw, float sh, SMaterial& material, bool flipY = true, bool flipX = false);

void renderBufferToTarget(float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh, SMaterial& material, bool flipY = true, bool flipX = false);

void renderEnvironment(CCamera *camera, CEntityManager *entityMgr, const core::vector3df& position, ITexture *texture[], int* face, int numFace);
void renderEnvironment(CCamera* camera, CEntityManager* entityMgr, const core::vector3df& position, ITexture* texture[], int* face, int numFace);

void renderCubeEnvironment(CCamera *camera, CEntityManager *entityMgr, const core::vector3df& position, ITexture *texture, int* face, int numFace);
void renderCubeEnvironment(CCamera* camera, CEntityManager* entityMgr, const core::vector3df& position, ITexture* texture, int* face, int numFace);

static void saveFBOToFile(ITexture *texture, const char *output);
static void saveFBOToFile(ITexture* texture, const char* output);

static void setBakeMode(bool b);

Expand All @@ -102,8 +104,8 @@ namespace Skylicht

protected:

void drawSceneToTexture(ITexture *target, CEntityManager *entityMgr);
void drawSceneToTexture(ITexture* target, CEntityManager* entityMgr);

void drawSceneToCubeTexture(ITexture *target, video::E_CUBEMAP_FACE faceID, CEntityManager *entityMgr);
void drawSceneToCubeTexture(ITexture* target, video::E_CUBEMAP_FACE faceID, CEntityManager* entityMgr);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ namespace Skylicht
if (lightCullingSystem != NULL)
{
core::array<CLightCullingData*>& listLight = lightCullingSystem->getLightVisible();
for (u32 i = 0, n = listLight.size(); i < n; i++)
for (u32 i = 0, n = listLight.size(); i < n && i < s_maxLight; i++)
{
CLight* light = listLight[i]->Light;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ namespace Skylicht
if (lightCullingSystem != NULL)
{
core::array<CLightCullingData*>& listLight = lightCullingSystem->getLightVisible();
for (u32 i = 0, n = listLight.size(); i < n; i++)
for (u32 i = 0, n = listLight.size(); i < n && i < s_maxLight; i++)
{
CLight* light = listLight[i]->Light;
CPointLight* pointLight = dynamic_cast<CPointLight*>(light);
Expand Down

0 comments on commit cfc4f73

Please sign in to comment.