X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fdeferred-shading%2Fdeferred-shading.cpp;h=83bb6111c9d164d35d0c963370d7ede1ac42371c;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=72ca6c5f5b08e89c5c1dbdc59e9918eee6ade721;hpb=5600d3f69cb9a57d55b2fad694d2a9f53ba700d6;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/deferred-shading/deferred-shading.cpp b/examples/deferred-shading/deferred-shading.cpp index 72ca6c5..83bb611 100644 --- a/examples/deferred-shading/deferred-shading.cpp +++ b/examples/deferred-shading/deferred-shading.cpp @@ -14,12 +14,12 @@ * limitations under the License. * */ +#include +#include +#include #include "dali/dali.h" #include "dali/public-api/actors/actor.h" #include "dali/public-api/rendering/renderer.h" -#include -#include -#include using namespace Dali; @@ -40,6 +40,8 @@ namespace #define DEFINE(x) "#define " DALI_COMPOSE_SHADER(x) DALI_COMPOSE_SHADER(\n) +// clang-format off + //============================================================================= // PRE-PASS //============================================================================= @@ -64,7 +66,7 @@ in vec3 aNormal; out vec4 vPosition; out vec3 vNormal; -vec4 Map(vec4 v) // projection space -> texture +vec4 Map(vec4 v) // projection space -> texture { return vec4(v.xyz / (2.f * v.w) + vec3(.5f), (v.w - NEAR) * INV_DEPTH); } @@ -149,7 +151,7 @@ uniform vec3 uDepth_InvDepth_Near;\n) // Light source uniforms struct Light { - vec3 position; // view space + vec3 position; // view space float radius; vec3 color; }; @@ -160,7 +162,7 @@ in vec2 vUv; out vec4 oColor; -vec4 Unmap(vec4 m) // texture -> projection +vec4 Unmap(vec4 m) // texture -> projection { m.w = m.w * DEPTH + NEAR; m.xyz = (m.xyz - vec3(.5)) * (2.f * m.w); @@ -180,10 +182,10 @@ vec3 CalculateLighting(vec3 pos, vec3 normal) rel /= distance; float a = uLights[i].radius / (kAttenuationConst + kAttenuationLinear * distance + - kAttenuationQuadratic * distance * distance); // attenuation + kAttenuationQuadratic * distance * distance); // attenuation - float l = max(0.f, dot(normal, rel)); // lambertian - float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f); // specular + float l = max(0.f, dot(normal, rel)); // lambertian + float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f); // specular light += (uLights[i].color * (l + s)) * a; } @@ -209,19 +211,21 @@ void main() oColor = vec4(finalColor, 1.f); }); +// clang-format on //============================================================================= // PRNG for floats. struct FloatRand { - std::random_device mDevice; - std::mt19937 mMersenneTwister; + std::random_device mDevice; + std::mt19937 mMersenneTwister; std::uniform_real_distribution mDistribution; FloatRand() : mMersenneTwister(mDevice()), mDistribution(0., 1.) - {} + { + } float operator()() { @@ -239,7 +243,7 @@ float FastFloor(float x) Vector3 FromHueSaturationLightness(Vector3 hsl) { Vector3 rgb; - if (hsl.y * hsl.y > 0.f) + if(hsl.y * hsl.y > 0.f) { if(hsl.x >= 360.f) { @@ -247,50 +251,50 @@ Vector3 FromHueSaturationLightness(Vector3 hsl) } hsl.x /= 60.f; - int i = FastFloor(hsl.x); + int i = FastFloor(hsl.x); float ff = hsl.x - i; - float p = hsl.z * (1.0 - hsl.y); - float q = hsl.z * (1.0 - (hsl.y * ff)); - float t = hsl.z * (1.0 - (hsl.y * (1.f - ff))); + float p = hsl.z * (1.0 - hsl.y); + float q = hsl.z * (1.0 - (hsl.y * ff)); + float t = hsl.z * (1.0 - (hsl.y * (1.f - ff))); - switch (i) + switch(i) { - case 0: - rgb.r = hsl.z; - rgb.g = t; - rgb.b = p; - break; - - case 1: - rgb.r = q; - rgb.g = hsl.z; - rgb.b = p; - break; - - case 2: - rgb.r = p; - rgb.g = hsl.z; - rgb.b = t; - break; - - case 3: - rgb.r = p; - rgb.g = q; - rgb.b = hsl.z; - break; - - case 4: - rgb.r = t; - rgb.g = p; - rgb.b = hsl.z; - break; - - case 5: - default: - rgb.r = hsl.z; - rgb.g = p; - rgb.b = q; - break; + case 0: + rgb.r = hsl.z; + rgb.g = t; + rgb.b = p; + break; + + case 1: + rgb.r = q; + rgb.g = hsl.z; + rgb.b = p; + break; + + case 2: + rgb.r = p; + rgb.g = hsl.z; + rgb.b = t; + break; + + case 3: + rgb.r = p; + rgb.g = q; + rgb.b = hsl.z; + break; + + case 4: + rgb.r = t; + rgb.g = p; + rgb.b = hsl.z; + break; + + case 5: + default: + rgb.r = hsl.z; + rgb.g = p; + rgb.b = q; + break; } } else @@ -312,26 +316,26 @@ Geometry CreateTexturedQuadGeometry(bool flipV) }; Vertex vertexData[] = { - { Vector3(-.5f, .5f, .0f), Vector2(.0f, 1.0f) }, - { Vector3(.5f, .5f, .0f), Vector2(1.0f, 1.0f) }, - { Vector3(-.5f, -.5f, .0f), Vector2(.0f, .0f) }, - { Vector3(.5f, -.5f, .0f), Vector2(1.0f, .0f) }, + {Vector3(-.5f, .5f, .0f), Vector2(.0f, 1.0f)}, + {Vector3(.5f, .5f, .0f), Vector2(1.0f, 1.0f)}, + {Vector3(-.5f, -.5f, .0f), Vector2(.0f, .0f)}, + {Vector3(.5f, -.5f, .0f), Vector2(1.0f, .0f)}, }; - if (flipV) + if(flipV) { std::swap(vertexData[0].aTexCoord, vertexData[2].aTexCoord); std::swap(vertexData[1].aTexCoord, vertexData[3].aTexCoord); } - PropertyBuffer vertexBuffer = PropertyBuffer::New( Property::Map() - .Add( "aPosition", Property::VECTOR3 ) - .Add( "aTexCoord", Property::VECTOR2 ) ); - vertexBuffer.SetData( vertexData, std::extent::value ); + VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map() + .Add("aPosition", Property::VECTOR3) + .Add("aTexCoord", Property::VECTOR2)); + vertexBuffer.SetData(vertexData, std::extent::value); Geometry geometry = Geometry::New(); - geometry.AddVertexBuffer( vertexBuffer ); - geometry.SetType( Geometry::TRIANGLE_STRIP ); + geometry.AddVertexBuffer(vertexBuffer); + geometry.SetType(Geometry::TRIANGLE_STRIP); return geometry; } @@ -339,12 +343,12 @@ Geometry CreateTexturedQuadGeometry(bool flipV) Geometry CreateOctahedron(bool invertNormals) { Vector3 positions[] = { - Vector3{ -1.f, 0.f, 0.f }, - Vector3{ 1.f, 0.f, 0.f }, - Vector3{ 0.f, -1.f, 0.f }, - Vector3{ 0.f, 1.f, 0.f }, - Vector3{ 0.f, 0.f, -1.f }, - Vector3{ 0.f, 0.f, 1.f }, + Vector3{-1.f, 0.f, 0.f}, + Vector3{1.f, 0.f, 0.f}, + Vector3{0.f, -1.f, 0.f}, + Vector3{0.f, 1.f, 0.f}, + Vector3{0.f, 0.f, -1.f}, + Vector3{0.f, 0.f, 1.f}, }; struct Vertex @@ -353,72 +357,71 @@ Geometry CreateOctahedron(bool invertNormals) Vector3 normal; }; Vertex vertexData[] = { - { positions[0] }, - { positions[3] }, - { positions[5] }, + {positions[0]}, + {positions[3]}, + {positions[5]}, - { positions[5] }, - { positions[3] }, - { positions[1] }, + {positions[5]}, + {positions[3]}, + {positions[1]}, - { positions[1] }, - { positions[3] }, - { positions[4] }, + {positions[1]}, + {positions[3]}, + {positions[4]}, - { positions[4] }, - { positions[3] }, - { positions[0] }, + {positions[4]}, + {positions[3]}, + {positions[0]}, - { positions[0] }, - { positions[5] }, - { positions[2] }, + {positions[0]}, + {positions[5]}, + {positions[2]}, - { positions[5] }, - { positions[1] }, - { positions[2] }, + {positions[5]}, + {positions[1]}, + {positions[2]}, - { positions[1] }, - { positions[4] }, - { positions[2] }, + {positions[1]}, + {positions[4]}, + {positions[2]}, - { positions[4] }, - { positions[0] }, - { positions[2] }, + {positions[4]}, + {positions[0]}, + {positions[2]}, }; // Calculate normals - for (uint32_t i = 0; i < std::extent::value / 3; ++i) + for(uint32_t i = 0; i < std::extent::value / 3; ++i) { uint32_t idx = i * 3; - Vector3 normal = (vertexData[idx + 2].position - vertexData[idx].position). - Cross(vertexData[idx + 1].position - vertexData[idx].position); + Vector3 normal = (vertexData[idx + 2].position - vertexData[idx].position).Cross(vertexData[idx + 1].position - vertexData[idx].position); normal.Normalize(); normal *= invertNormals * 2.f - 1.f; vertexData[idx++].normal = normal; vertexData[idx++].normal = normal; - vertexData[idx].normal = normal; + vertexData[idx].normal = normal; } // Configure property buffers and create geometry. - PropertyBuffer vertexBuffer = PropertyBuffer::New(Property::Map() - .Add("aPosition", Property::VECTOR3) - .Add("aNormal", Property::VECTOR3)); + VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map() + .Add("aPosition", Property::VECTOR3) + .Add("aNormal", Property::VECTOR3)); vertexBuffer.SetData(vertexData, std::extent::value); Geometry geometry = Geometry::New(); - geometry.AddVertexBuffer( vertexBuffer ); - geometry.SetType( Geometry::TRIANGLES ); + geometry.AddVertexBuffer(vertexBuffer); + geometry.SetType(Geometry::TRIANGLES); return geometry; } //============================================================================= enum RendererOptions { - OPTION_NONE = 0x0, - OPTION_BLEND = 0x01, - OPTION_DEPTH_TEST = 0x02, + OPTION_NONE = 0x0, + OPTION_BLEND = 0x01, + OPTION_DEPTH_TEST = 0x02, OPTION_DEPTH_WRITE = 0x04 }; @@ -426,14 +429,14 @@ Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, u { Renderer renderer = Renderer::New(geometry, shader); renderer.SetProperty(Renderer::Property::BLEND_MODE, - (options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF); + (options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF); renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, - (options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF); + (options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF); renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE, - (options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF); + (options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF); renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK); - if (!textures) + if(!textures) { textures = TextureSet::New(); } @@ -445,8 +448,8 @@ Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, u //============================================================================= void CenterActor(Actor actor) { - actor.SetAnchorPoint( AnchorPoint::CENTER ); - actor.SetParentOrigin( ParentOrigin::CENTER ); + actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); + actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); } //============================================================================= @@ -455,14 +458,14 @@ void RegisterDepthProperties(float depth, float near, Handle& h) h.RegisterProperty("uDepth_InvDepth_Near", Vector3(depth, 1.f / depth, near)); } -} +} // namespace //============================================================================= /// Create a String whose size can be evaluated at compile time struct ConstantString { - const char * const string; - const uint16_t size; + const char* const string; + const uint16_t size; template constexpr ConstantString(const char (&input)[inputSize]) @@ -475,7 +478,7 @@ struct ConstantString constexpr ConstantString POSITION_STRING("position"); constexpr ConstantString RADIUS_STRING("radius"); constexpr ConstantString COLOR_STRING("color"); -constexpr uint16_t LIGHT_SOURCE_BUFFER_SIZE(128u); +constexpr uint16_t LIGHT_SOURCE_BUFFER_SIZE(128u); //============================================================================= class DeferredShadingExample : public ConnectionTracker @@ -485,7 +488,7 @@ public: { enum { - NONE = 0x0, + NONE = 0x0, SHOW_LIGHTS = 0x1, }; }; @@ -494,34 +497,32 @@ public: : mApp(app), mOptions(options) { - app.InitSignal().Connect( this, &DeferredShadingExample::Create ); - app.TerminateSignal().Connect( this, &DeferredShadingExample::Destroy ); + app.InitSignal().Connect(this, &DeferredShadingExample::Create); + app.TerminateSignal().Connect(this, &DeferredShadingExample::Destroy); } private: void Create(Application& app) { - // Grab stage, configure layer - Stage stage = Stage::GetCurrent(); - auto rootLayer = stage.GetRootLayer(); - rootLayer.SetBehavior(Layer::LAYER_3D); + // Grab window, configure layer + Window window = app.GetWindow(); + auto rootLayer = window.GetRootLayer(); + rootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D); - auto stageSize = stage.GetSize(); - auto stageHalfSize = stageSize * .5f; - auto invStageHalfSize = Vector2::ONE / stageHalfSize; + Vector2 windowSize = window.GetSize(); - float unit = stageSize.y / 24.f; + float unit = windowSize.y / 24.f; // Get camera - we'll be re-using the same old camera in the two passes. - RenderTaskList tasks = stage.GetRenderTaskList(); - CameraActor camera = tasks.GetTask(0).GetCameraActor(); + RenderTaskList tasks = window.GetRenderTaskList(); + CameraActor camera = tasks.GetTask(0).GetCameraActor(); auto zCameraPos = camera.GetProperty(Actor::Property::POSITION_Z).Get(); - camera.SetFarClippingPlane(zCameraPos + stageSize.y * .5f); - camera.SetNearClippingPlane(zCameraPos - stageSize.y * .5f); + camera.SetFarClippingPlane(zCameraPos + windowSize.y * .5f); + camera.SetNearClippingPlane(zCameraPos - windowSize.y * .5f); const float zNear = camera.GetNearClippingPlane(); - const float zFar = camera.GetFarClippingPlane(); + const float zFar = camera.GetFarClippingPlane(); const float depth = zFar - zNear; // Create root of scene that shall be rendered off-screen. @@ -529,7 +530,7 @@ private: CenterActor(sceneRoot); mSceneRoot = sceneRoot; - stage.Add(sceneRoot); + window.Add(sceneRoot); // Create an axis to spin our actors around. auto axis = Actor::New(); @@ -541,76 +542,68 @@ private: Geometry mesh = CreateOctahedron(false); // Create main actors - Shader preShader = Shader::New(PREPASS_VSH, PREPASS_FSH); + Shader preShader = Shader::New(PREPASS_VSH, PREPASS_FSH); TextureSet noTexturesThanks = TextureSet::New(); - Renderer meshRenderer = CreateRenderer(noTexturesThanks, mesh, preShader, - OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); + Renderer meshRenderer = CreateRenderer(noTexturesThanks, mesh, preShader, OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); meshRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK); - meshRenderer.RegisterProperty("uInvStageHalfSize", invStageHalfSize); RegisterDepthProperties(depth, zNear, meshRenderer); float c = 1.f; - for (auto v: { - Vector3{ -c, -c, -c }, - Vector3{ c, -c, -c }, - Vector3{ -c, c, -c }, - Vector3{ c, c, -c }, - Vector3{ -c, -c, c }, - Vector3{ c, -c, c }, - Vector3{ -c, c, c }, - Vector3{ c, c, c }, - - Vector3{ 0.f, -c, -c }, - Vector3{ 0.f, c, -c }, - Vector3{ 0.f, -c, c }, - Vector3{ 0.f, c, c }, - - Vector3{ -c, 0.f, -c }, - Vector3{ c, 0.f, -c }, - Vector3{ -c, 0.f, c }, - Vector3{ c, 0.f, c }, - - Vector3{ -c, -c, 0.f }, - Vector3{ c, -c, 0.f }, - Vector3{ -c, c, 0.f }, - Vector3{ c, c, 0.f }, - }) + for(auto v : { + Vector3{-c, -c, -c}, + Vector3{c, -c, -c}, + Vector3{-c, c, -c}, + Vector3{c, c, -c}, + Vector3{-c, -c, c}, + Vector3{c, -c, c}, + Vector3{-c, c, c}, + Vector3{c, c, c}, + + Vector3{0.f, -c, -c}, + Vector3{0.f, c, -c}, + Vector3{0.f, -c, c}, + Vector3{0.f, c, c}, + + Vector3{-c, 0.f, -c}, + Vector3{c, 0.f, -c}, + Vector3{-c, 0.f, c}, + Vector3{c, 0.f, c}, + + Vector3{-c, -c, 0.f}, + Vector3{c, -c, 0.f}, + Vector3{-c, c, 0.f}, + Vector3{c, c, 0.f}, + }) { Actor a = Actor::New(); CenterActor(a); - Vector3 position{ v * unit * 5.f }; - a.SetPosition(position); + Vector3 position{v * unit * 5.f}; + a.SetProperty(Actor::Property::POSITION, position); - float scale = (c + ((v.x + v.y + v.z) + c * 3.f) * .5f) / (c * 4.f); - Vector3 size{ Vector3::ONE * scale * unit * 2.f }; - a.SetSize(size); + float scale = (c + ((v.x + v.y + v.z) + c * 3.f) * .5f) / (c * 4.f); + Vector3 size{Vector3::ONE * scale * unit * 2.f}; + a.SetProperty(Actor::Property::SIZE, size); - a.SetColor(Color::WHITE * .25f + - (Color::RED * (v.x + c) / (c * 2.f) + - Color::GREEN * (v.y + c) / (c * 2.f) + - Color::BLUE * (v.z + c) / (c * 2.f)) * .015625f); + a.SetProperty(Actor::Property::COLOR, Color::WHITE * .25f + (Color::RED * (v.x + c) / (c * 2.f) + Color::GREEN * (v.y + c) / (c * 2.f) + Color::BLUE * (v.z + c) / (c * 2.f)) * .015625f); a.AddRenderer(meshRenderer); axis.Add(a); } // Create off-screen textures, fbo and render task. - uint32_t width = static_cast(stageSize.x); - uint32_t height = static_cast(stageSize.y); - - Texture rttNormal = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGB888, - width, height); - Texture rttPosition = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, - width, height); - Texture rttColor = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGB888, - width, height); - FrameBuffer fbo = FrameBuffer::New(width, height, FrameBuffer::Attachment::DEPTH); + uint32_t width = static_cast(windowSize.x); + uint32_t height = static_cast(windowSize.y); + + Texture rttNormal = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGB888, width, height); + Texture rttPosition = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, width, height); + Texture rttColor = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGB888, width, height); + FrameBuffer fbo = FrameBuffer::New(width, height, FrameBuffer::Attachment::DEPTH); fbo.AttachColorTexture(rttNormal); fbo.AttachColorTexture(rttPosition); fbo.AttachColorTexture(rttColor); RenderTask sceneRender = tasks.CreateTask(); - sceneRender.SetViewportSize(stageSize); + sceneRender.SetViewportSize(windowSize); sceneRender.SetFrameBuffer(fbo); sceneRender.SetCameraActor(camera); sceneRender.SetSourceActor(sceneRoot); @@ -625,7 +618,7 @@ private: // Create final image for deferred shading auto finalImage = Actor::New(); CenterActor(finalImage); - finalImage.SetSize(stageSize); + finalImage.SetProperty(Actor::Property::SIZE, windowSize); TextureSet finalImageTextures = TextureSet::New(); finalImageTextures.SetTexture(0, rttNormal); @@ -638,18 +631,16 @@ private: finalImageTextures.SetSampler(1, sampler); finalImageTextures.SetSampler(2, sampler); - Shader shdMain = Shader::New(MAINPASS_VSH, MAINPASS_FSH); - Geometry finalImageGeom = CreateTexturedQuadGeometry(true); + Shader shdMain = Shader::New(MAINPASS_VSH, MAINPASS_FSH); + Geometry finalImageGeom = CreateTexturedQuadGeometry(true); Renderer finalImageRenderer = CreateRenderer(finalImageTextures, finalImageGeom, shdMain); - finalImageRenderer.RegisterProperty("uStageHalfSize", stageHalfSize); RegisterDepthProperties(depth, zNear, finalImageRenderer); - auto propInvProjection = finalImageRenderer.RegisterProperty("uInvProjection", Matrix::IDENTITY); - Constraint cnstrInvProjection = Constraint::New(finalImageRenderer, propInvProjection, - [zCameraPos, zNear, depth](Matrix& output, const PropertyInputContainer& input) { - output = input[0]->GetMatrix(); - DALI_ASSERT_ALWAYS(output.Invert() && "Failed to invert projection matrix."); - }); + auto propInvProjection = finalImageRenderer.RegisterProperty("uInvProjection", Matrix::IDENTITY); + Constraint cnstrInvProjection = Constraint::New(finalImageRenderer, propInvProjection, [zCameraPos, zNear, depth](Matrix& output, const PropertyInputContainer& input) { + output = input[0]->GetMatrix(); + DALI_ASSERT_ALWAYS(output.Invert() && "Failed to invert projection matrix."); + }); cnstrInvProjection.AddSource(Source(camera, CameraActor::Property::PROJECTION_MATRIX)); cnstrInvProjection.AddSource(Source(camera, CameraActor::Property::VIEW_MATRIX)); cnstrInvProjection.Apply(); @@ -657,7 +648,7 @@ private: finalImage.AddRenderer(finalImageRenderer); mFinalImage = finalImage; - stage.Add(finalImage); + window.Add(finalImage); // Create a node for our lights auto lights = Actor::New(); @@ -666,29 +657,28 @@ private: // Create Lights const bool showLights = mOptions & Options::SHOW_LIGHTS; - Renderer lightRenderer; - if (showLights) + Renderer lightRenderer; + if(showLights) { Geometry lightMesh = CreateOctahedron(true); - lightRenderer = CreateRenderer(noTexturesThanks, lightMesh, preShader, - OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); + lightRenderer = CreateRenderer(noTexturesThanks, lightMesh, preShader, OPTION_DEPTH_TEST | OPTION_DEPTH_WRITE); lightRenderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::FRONT); } - Vector3 lightPos{ unit * 12.f, 0.f, 0.f }; - float theta = M_PI * 2.f / MAX_LIGHTS; - float cosTheta = std::cos(theta); - float sinTheta = std::sin(theta); - for (int i = 0; i < MAX_LIGHTS; ++i) + Vector3 lightPos{unit * 12.f, 0.f, 0.f}; + float theta = M_PI * 2.f / MAX_LIGHTS; + float cosTheta = std::cos(theta); + float sinTheta = std::sin(theta); + for(int i = 0; i < MAX_LIGHTS; ++i) { Vector3 color = FromHueSaturationLightness(Vector3((360.f * i) / MAX_LIGHTS, .5f, 1.f)); Actor light = CreateLight(lightPos * (1 + (i % 8)) / 8.f, unit * 16.f, color, camera, finalImageRenderer); - float z = (((i & 1) << 1) - 1) * unit * 8.f; + float z = (((i & 1) << 1) - 1) * unit * 8.f; lightPos = Vector3(cosTheta * lightPos.x - sinTheta * lightPos.y, sinTheta * lightPos.x + cosTheta * lightPos.y, z); - if (showLights) + if(showLights) { light.SetProperty(Actor::Property::SIZE, Vector3::ONE * unit / 8.f); light.AddRenderer(lightRenderer); @@ -704,16 +694,16 @@ private: animLights.Play(); // Event handling - stage.KeyEventSignal().Connect(this, &DeferredShadingExample::OnKeyEvent); + window.KeyEventSignal().Connect(this, &DeferredShadingExample::OnKeyEvent); mPanDetector = PanGestureDetector::New(); mPanDetector.DetectedSignal().Connect(this, &DeferredShadingExample::OnPan); - mPanDetector.Attach(stage.GetRootLayer()); + mPanDetector.Attach(window.GetRootLayer()); } void Destroy(Application& app) { - Stage::GetCurrent().GetRenderTaskList().RemoveTask(mSceneRender); + app.GetWindow().GetRenderTaskList().RemoveTask(mSceneRender); mSceneRender.Reset(); UnparentAndReset(mSceneRoot); @@ -727,11 +717,11 @@ private: light.SetProperty(Actor::Property::COLOR, Color::WHITE); light.SetProperty(Actor::Property::POSITION, position); - auto iPropRadius = light.RegisterProperty("radius", radius); + auto iPropRadius = light.RegisterProperty("radius", radius); auto iPropLightColor = light.RegisterProperty("lightcolor", color); // Create light source uniforms on lighting shader. - char buffer[LIGHT_SOURCE_BUFFER_SIZE]; + char buffer[LIGHT_SOURCE_BUFFER_SIZE]; char* writep = buffer + snprintf(buffer, LIGHT_SOURCE_BUFFER_SIZE, "uLights[%d].", mNumLights); ++mNumLights; @@ -746,25 +736,22 @@ private: // Constrain the light position, radius and color to lighting shader uniforms. // Convert light position to view space; - Constraint cLightPos = Constraint::New(renderer, oPropLightPos, [](Vector3& output, const PropertyInputContainer& input) - { + Constraint cLightPos = Constraint::New(renderer, oPropLightPos, [](Vector3& output, const PropertyInputContainer& input) { Vector4 worldPos(input[0]->GetVector3()); worldPos.w = 1.f; worldPos = input[1]->GetMatrix() * worldPos; - output = Vector3(worldPos); + output = Vector3(worldPos); }); cLightPos.AddSource(Source(light, Actor::Property::WORLD_POSITION)); cLightPos.AddSource(Source(camera, CameraActor::Property::VIEW_MATRIX)); cLightPos.Apply(); - Constraint cLightRadius = Constraint::New(renderer, oPropLightRadius, - EqualToConstraint()); + Constraint cLightRadius = Constraint::New(renderer, oPropLightRadius, EqualToConstraint()); cLightRadius.AddSource(Source(light, iPropRadius)); cLightRadius.Apply(); - Constraint cLightColor = Constraint::New(renderer, oPropLightColor, - EqualToConstraint()); + Constraint cLightColor = Constraint::New(renderer, oPropLightColor, EqualToConstraint()); cLightColor.AddSource(Source(light, iPropLightColor)); cLightColor.Apply(); @@ -773,17 +760,18 @@ private: void OnPan(Actor, PanGesture const& gesture) { - Quaternion q = mAxis.GetProperty(Actor::Property::ORIENTATION).Get(); - Quaternion qx(Radian(Degree(gesture.screenDisplacement.y) * -.5f), Vector3::XAXIS); - Quaternion qy(Radian(Degree(gesture.screenDisplacement.x) * .5f), Vector3::YAXIS); + Quaternion q = mAxis.GetProperty(Actor::Property::ORIENTATION).Get(); + const Vector2& displacement = gesture.GetScreenDisplacement(); + Quaternion qx(Radian(Degree(displacement.y) * -.5f), Vector3::XAXIS); + Quaternion qy(Radian(Degree(displacement.x) * .5f), Vector3::YAXIS); mAxis.SetProperty(Actor::Property::ORIENTATION, qy * qx * q); } void OnKeyEvent(const KeyEvent& event) { - if(event.state == KeyEvent::Down) + if(event.GetState() == KeyEvent::DOWN) { - if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) ) + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) { mApp.Quit(); } @@ -791,32 +779,29 @@ private: } Application& mApp; - uint32_t mOptions; + uint32_t mOptions; Actor mSceneRoot; Actor mAxis; RenderTask mSceneRender; - Actor mFinalImage; + Actor mFinalImage; int mNumLights = 0; PanGestureDetector mPanDetector; }; - int main(int argc, char** argv) { - const bool showLights = [](int argc, char** argv) - { + const bool showLights = [](int argc, char** argv) { auto endArgs = argv + argc; - return std::find_if(argv, endArgs, [](const char* arg) - { - return strcmp(arg, "--show-lights") == 0; - }) != endArgs; + return std::find_if(argv, endArgs, [](const char* arg) { + return strcmp(arg, "--show-lights") == 0; + }) != endArgs; }(argc, argv); - Application app = Application::New(&argc, &argv); + Application app = Application::New(&argc, &argv); DeferredShadingExample example(app, (showLights ? DeferredShadingExample::Options::SHOW_LIGHTS : 0)); app.MainLoop(); return 0;