Skip to content

Commit

Permalink
configurable lights limit; fix spot light
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Nov 18, 2024
1 parent 9900787 commit aaa1ff7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
10 changes: 3 additions & 7 deletions sources/simple/lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <cage-engine/guiComponents.h>
#include <cage-engine/highPerformanceGpuHint.h>
#include <cage-engine/scene.h>
#include <cage-engine/sceneScreenSpaceEffects.h>
#include <cage-engine/window.h>
#include <cage-simple/engine.h>
#include <cage-simple/fpsCamera.h>
Expand Down Expand Up @@ -148,11 +147,7 @@ int main(int argc, char *args[])
{
try
{
// log to console
Holder<Logger> log1 = newLogger();
log1->format.bind<logFormatConsole>();
log1->output.bind<logOutputStdOut>();

initializeConsoleLogger();
engineInitialize(EngineCreateConfig());

// events
Expand Down Expand Up @@ -180,6 +175,7 @@ int main(int argc, char *args[])
{ // spot lights
Entity *e = ents->create(5 + i);
e->value<RenderComponent>().object = HashString("cage-tests/bottle/other.obj?arrow");
//e->value<RenderComponent>().object = HashString("cage/model/axes.obj");
LightComponent &l = e->value<LightComponent>();
l.lightType = LightTypeEnum::Spot;
l.spotAngle = Degs(40);
Expand All @@ -198,7 +194,7 @@ int main(int argc, char *args[])
CameraComponent &c = e->value<CameraComponent>();
c.near = 0.1;
c.far = 1000;
e->value<ScreenSpaceEffectsComponent>();
//e->value<ScreenSpaceEffectsComponent>();
LightComponent &l = e->value<LightComponent>();
l.lightType = LightTypeEnum::Directional;
l.intensity = 1;
Expand Down
113 changes: 93 additions & 20 deletions sources/simple/manyLights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,40 @@
#include <cage-core/entitiesVisitor.h>
#include <cage-core/hashString.h>
#include <cage-core/logger.h>
#include <cage-engine/guiBuilder.h>
#include <cage-engine/highPerformanceGpuHint.h>
#include <cage-engine/scene.h>
#include <cage-engine/sceneScreenSpaceEffects.h>
#include <cage-engine/window.h>
#include <cage-simple/engine.h>
#include <cage-simple/fpsCamera.h>
#include <cage-simple/statisticsGui.h>

using namespace cage;
constexpr uint32 AssetsName = HashString("cage-tests/translucent/translucent.pack");
constexpr uint32 lightsCount = 500;

struct MovingComponent
{
Vec3 velocity;
};

struct OrbitingComponent
{
Vec3 origin;
Real dist;
Rads pitch, yaw;
Rads yawSpeed;

Transform make(uint64 time) const
{
Transform t;
t.orientation = Quat(pitch, yaw + yawSpeed * time, {});
t.position = origin + t.orientation * Vec3(0, 0, dist);
t.scale = 0.1;
return t;
}
};

void update()
{
entitiesVisitor(
Expand All @@ -28,30 +46,47 @@ void update()
mv.velocity *= -1;
},
engineEntities(), false);

const uint64 time = engineControlTime();
entitiesVisitor([time](TransformComponent &t, const OrbitingComponent &orb) { t = orb.make(time); }, engineEntities(), false);
}

void initializeGui()
{
Holder<GuiBuilder> g = newGuiBuilder(engineGuiEntities());
auto _1 = g->alignment(Vec2());
auto _2 = g->panel();
auto _3 = g->row();
g->label().text("lights limit: ");
g->horizontalSliderBar(CameraComponent().maxLights, 0, 200)
.event(inputFilter(
[](input::GuiValue in)
{
Entity *e = engineEntities()->get(1);
e->value<CameraComponent>().maxLights = numeric_cast<uint32>(in.entity->value<GuiSliderBarComponent>().value);
}));
}

int main(int argc, char *args[])
{
try
{
// log to console
Holder<Logger> log1 = newLogger();
log1->format.bind<logFormatConsole>();
log1->output.bind<logOutputStdOut>();

initializeConsoleLogger();
engineInitialize(EngineCreateConfig());

// events
const auto updateListener = controlThread().update.listen(&update);
const auto updateListener = controlThread().update.listen(update);
const auto closeListener = engineWindow()->events.listen(inputFilter([](input::WindowClose) { engineStop(); }));

// window
engineWindow()->setMaximized();
engineWindow()->title("many lights");
initializeGui();

// entities
EntityManager *ents = engineEntities();
ents->defineComponent(MovingComponent());
ents->defineComponent(OrbitingComponent());
{ // camera
Entity *e = ents->create(1);
TransformComponent &t = e->value<TransformComponent>();
Expand All @@ -62,6 +97,8 @@ int main(int argc, char *args[])
c.far = 1000;
c.ambientColor = Vec3(1);
c.ambientIntensity = 0.2;
ScreenSpaceEffectsComponent &s = e->value<ScreenSpaceEffectsComponent>();
s.effects &= ~ScreenSpaceEffectsFlags::Bloom;
}
{ // sun
Entity *e = ents->create(2);
Expand All @@ -71,28 +108,64 @@ int main(int argc, char *args[])
LightComponent &l = e->value<LightComponent>();
l.lightType = LightTypeEnum::Directional;
l.color = Vec3(1, 1, 0.5);
l.intensity = 2;
l.intensity = 1;
ShadowmapComponent &s = e->value<ShadowmapComponent>();
s.resolution = 2048;
s.directionalWorldSize = 15;
s.resolution = 4096;
s.directionalWorldSize = 20;
}
{ // floor
{ // green floor
Entity *e = ents->create(5);
e->value<RenderComponent>().object = HashString("cage-tests/translucent/shapes.blend?Ground");
e->value<TransformComponent>().position = Vec3(0, -0.2, 0);
e->value<TransformComponent>().position = Vec3(0, 0, 0);
}
{ // gray floor
Entity *e = ents->create(6);
e->value<RenderComponent>().object = HashString("cage-tests/bottle/other.obj?plane");
e->value<TransformComponent>().position = Vec3(0, 0, 0);
}
// lights
for (uint32 i = 0; i < lightsCount; i++)
{ // bottle
Entity *e = ents->create(7);
e->value<RenderComponent>().object = HashString("cage-tests/bottle/bottle.obj");
e->value<TransformComponent>().position += Vec3(1, 0, 0);
e->value<TransformComponent>().scale = 0.3;
}
// spot lights
for (uint32 i = 0; i < 3; i++)
{
Entity *e = ents->createUnique();
e->value<TransformComponent>();
OrbitingComponent &orb = e->value<OrbitingComponent>();
orb.origin = (randomChance3() * 2 - 1) * (i * 3 + 1) * Vec3(1, 0, 1);
orb.dist = randomRange(2, 5);
orb.pitch = Degs(randomRange(-50, -40));
orb.yaw = randomAngle();
orb.yawSpeed = Degs(20 * 1e-6);
RenderComponent &r = e->value<RenderComponent>();
r.object = HashString("cage-tests/bottle/other.obj?arrow");
LightComponent &l = e->value<LightComponent>();
l.color = r.color = Vec3(i == 0, i == 1, i == 2);
l.lightType = LightTypeEnum::Spot;
l.spotAngle = Degs(60);
l.spotExponent = 10;
l.intensity = 3;
l.minDistance = 1;
l.maxDistance = 100;
ShadowmapComponent &s = e->value<ShadowmapComponent>();
s.resolution = 4096;
s.directionalWorldSize = 500;
}
// point lights
for (uint32 i = 0; i < 500; i++)
{
Entity *e = ents->createAnonymous();
e->value<TransformComponent>().position = (randomChance3() * 2 - 1) * 5 * Vec3(1, 0, 1);
e->value<TransformComponent>().position = randomDirection3() * 8 * Vec3(1, 0, 1) + Vec3(1, 0.2, 1);
e->value<TransformComponent>().scale = 0.03;
e->value<MovingComponent>().velocity = randomDirection3() * 0.02 * Vec3(1, 0, 1);
e->value<LightComponent>().color = randomChance3() * 0.5 + 0.5;
e->value<LightComponent>().intensity = randomRange(0.01, 3.0);
e->value<RenderComponent>().object = HashString("cage-tests/translucent/shapes.blend?Bulb");
e->value<RenderComponent>().color = e->value<LightComponent>().color;
e->value<RenderComponent>().intensity = e->value<LightComponent>().intensity;
RenderComponent &r = e->value<RenderComponent>();
r.object = HashString("cage-tests/translucent/shapes.blend?Bulb");
LightComponent &l = e->value<LightComponent>();
l.color = r.color = randomChance3() * 0.5 + 0.5;
l.intensity = r.intensity = randomRange(0.5, 2.0);
}

Holder<FpsCamera> cameraCtrl = newFpsCamera(ents->get(1));
Expand Down

0 comments on commit aaa1ff7

Please sign in to comment.