Commit d78dd254 authored by Dario Seyb's avatar Dario Seyb

fixed lighting

parent 7ce14883
Pipeline #626 skipped
......@@ -8,7 +8,7 @@ uniform float uSampleWeights[SAMPLE_COUNT];
in vec2 vTexCoord;
out vec4 oColor;
out vec3 oColor;
void main()
{
......@@ -19,5 +19,5 @@ void main()
color += texture(uSamplerColor, vTexCoord + uSampleOffsets[i]).rgb * uSampleWeights[i];
}
oColor = vec4(color,1);
oColor = color;
}
......@@ -17,7 +17,7 @@ uniform mat4 uPrevViewProjectionMatrix;
out vec4 oColor;
out vec4 oEmissive;
out vec4 oNormal;
out vec3 oNormal;
out vec3 oMotion;
float rand(vec2 co){
......@@ -32,7 +32,6 @@ void main()
{
vec4 color = color();
float sampl = rand(gl_FragCoord.xy + vec2(mod(uTime,1)));
if(sampl > color.a) discard;
......@@ -47,5 +46,5 @@ void main()
oMotion.z = thisPosition.z;
oColor = color;
oEmissive = emissive();
oNormal = vec4(normalize(normal()) * 0.5 + 0.5, 1.0);
oNormal = normalize(normal());
}
#version 330 core
#version 410 core
uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerEmissive;
......@@ -13,6 +13,8 @@ uniform vec2 uOneOverShadowTexSize;
uniform vec3 uLightPosition;
uniform vec4 uLightColor;
uniform mat4 uLightProjMatrix;
uniform bool uLightIsDirectional;
uniform vec3 uLightDir;
uniform float uNear;
uniform float uFar;
......@@ -24,7 +26,7 @@ in vec2 vTexCoord;
out vec4 oColor;
vec3 unpackNormal(vec4 norm) {
return (norm.xyz * 2) - vec3(1, 1, 1);
return norm.xyz; //(norm.xyz * 2) - vec3(1, 1, 1);
}
float linearizeDepth(float depth) {
......@@ -76,20 +78,28 @@ void main()
float depth = unpackDepth(texture(uSamplerDepth, vTexCoord).r);
if(depth == 1) discard;
vec3 worldPosition = unpackWorldPosition(depth);
vec3 lightDir = uLightPosition - worldPosition;
float lightDistance = length(lightDir);
lightDir /= lightDistance;
vec3 lightDir = vec3(0);
float lightDistance = 1;
if(uLightIsDirectional) {
lightDir = uLightDir;
} else {
lightDir = uLightPosition - worldPosition;
lightDistance = length(lightDir);
lightDir /= lightDistance;
}
float diffuseFactor = dot(lightDir, normal);
if(diffuseFactor < 0) diffuseFactor = 0;
vec3 diffuseColor = diffuseFactor * uLightColor.rgb * color.rgb;
vec3 viewDir = normalize(cameraPosition - worldPosition);
float specularFactor = dot( reflect(lightDir, -normal), -viewDir);
if(specularFactor < 0) specularFactor = 0;
......
......@@ -19,7 +19,6 @@ vec4 color() {
color = amplify(d1, 80, -0.5) * amplify(d2, 120, -0.5) * color;
color.w = 1.0;
return color;
//return texture(uTexture, teTexCoord).rgba * uTintColor;
}
vec4 emissive() {
......
......@@ -203,18 +203,15 @@ vec3 interpolate3D(vec3 v0, vec3 v1, vec3 v2){
void main(){
tNormal = interpolate3D(tcNormal[0], tcNormal[1], tcNormal[2]);
tTexCoord = interpolate2D(tcTexCoord[0], tcTexCoord[1], tcTexCoord[2]);
tPatchDistance = gl_TessCoord;
tPosition = interpolate3D(tcPosition[0], tcPosition[1], tcPosition[2]);
//vec4 camWorld = uModelMatrix * vec4(tPosition, 1.0);
tPosition += tNormal * fBM( tPosition , 8, 32, 0.007 ) ;
tNormal = inverse(transpose(mat3(uModelMatrix))) * normalize(tNormal);
tPosition = (uModelMatrix * vec4(tPosition, 1)).xyz;
......
#include <engine/scene/Entity.hpp>
#include <ACGL/Math/Math.hh>
enum class LightType {
POINT,
DIRECTIONAL
};
struct Light : Component<Light> {
Light(glm::vec4 color, glm::vec3 dir, bool castShadow) : color(color), dir(dir), castShadow(castShadow) {}
Light(glm::vec4 color, glm::vec3 dir, bool castShadow, LightType type = LightType::POINT) : color(color), dir(dir), castShadow(castShadow), type(type) {}
glm::vec4 color;
glm::vec3 dir;
bool castShadow;
LightType type;
};
\ No newline at end of file
......@@ -17,6 +17,8 @@
struct LightData {
glm::vec4 color;
bool castShadow;
glm::vec3 dir;
LightType type;
TransformData lastSimulateTransform;
TransformData thisSimulateTransform;
......@@ -114,11 +116,6 @@ public:
}
ACGL::OpenGL::SharedTexture2D createScreenspaceTexture(ScreenSpaceSize size, GLenum internalFormat) {
static const float SIZE_LOOKUP[3] = {
1, 0.5, 0.25
};
auto windowSize = m_window->getSize();
auto tex = SharedTexture2D(new Texture2D({ windowSize.x * 1.0f / (1 << (int)size), windowSize.y * 1.0f / (1 << (int)size) }, internalFormat));
......
......@@ -51,6 +51,10 @@ public:
cameraTransform->position = p;
}
glm::vec3 getWorldPosition() {
return cameraTransform->lastGlobalTransform.pos;
}
glm::vec3 getPosition() {
return cameraTransform->position;
}
......
#pragma once
#include <engine/scene/Scene.hpp>
#include <engine/scene/OrbitalSimulationSystem.hpp>
#include <engine/scene/PlayerSystem.hpp>
#include <engine/audio/SoundSource.hpp>
class AtmosphereTestScene : public Scene {
private:
OrbitalSimulationSystem* m_orbitals;
PlayerSystem* m_player;
Light::Handle sunLight;
Entity sun;
Entity earth;
public:
CONSTRUCT_SCENE(AtmosphereTestScene) { };
bool startup() override;
void shutdown() override;
};
\ No newline at end of file
......@@ -11,7 +11,9 @@ using namespace ACGL::Utils;
float computeGaussian(float n) {
float theta = 4;
return (float)((1.0 / sqrtf(2 * M_PI * theta)) * exp(-(n * n) / (2 * theta * theta)));
float val = ((float)((1.0 / sqrtf(2 * M_PI * theta)) * exp(-(n * n) / (2 * theta * theta))) - 0.001f);
if (val < 0) val = 0;
return val;
}
void BloomPostFX::startup() {
......@@ -46,7 +48,7 @@ void BloomPostFX::startup() {
m_gaussianWeights[i * 2 + 2] = weight;
totalWeights += weight * 2;
float sampleOffset = i * 2 + 1.5f;
float sampleOffset = i + .5f;
m_sampleOffsets[i * 2 + 1] = sampleOffset;
m_sampleOffsets[i * 2 + 2] = -sampleOffset;
}
......
......@@ -48,7 +48,7 @@ bool RendererSystem::startup() {
m_colorBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA32F);
m_emissiveBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA32F);
m_normalBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGBA8);
m_normalBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGB32F);
m_motionBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_RGB32F);
m_depthBuffer = createScreenspaceTexture(ScreenSpaceSize::FULL, GL_DEPTH24_STENCIL8);
......@@ -309,7 +309,12 @@ void RendererSystem::frame(double interp, double totalTime) {
bool hasShadow = light.shadowFbo != nullptr;
m_deferredCombineProgram->setUniform("uLightHasShadow", hasShadow);
m_deferredCombineProgram->setUniform("uLightIsDirectional", light.type == LightType::DIRECTIONAL);
if (light.type == LightType::DIRECTIONAL) {
m_deferredCombineProgram->setUniform("uLightDir", light.dir);
}
if (hasShadow) {
m_deferredCombineProgram->setUniform("uLightProjMatrix",
biasMatrix * light.projMatrix);
......
......@@ -52,7 +52,7 @@ bool OrbitalSimulationSystem::startup() {
.attributeLocations(vaoSphere->getAttributeLocations())
.fragmentDataLocations(m_renderer->getGBufferLocations()));
defaultMat = { glm::vec4(1, 1, 1, 1), glm::vec4(0.0, 0.0, 0.0, 1), texture, dispMappingShader };
defaultMat = { glm::vec4(1, 1, 1, 1), glm::vec4(0, 0, 0, 1), texture, dispMappingShader };
defaultMat2 = { glm::vec4(1, 1, 1, 1), glm::vec4(1, 0, 0, 1), texture, testShader };
......@@ -146,7 +146,7 @@ Entity OrbitalSimulationSystem::addPlanet(Transform::Handle sun, std::string n,
glm::vec3 pos = applyKepler(planetComponent, 0);
pos = glm::vec3(AUToWorld(pos.x), AUToWorld(pos.y), AUToWorld(pos.z));
planetTransform->scale = glm::vec3(solarToWorld(r), solarToWorld(r), solarToWorld(r));
planetTransform->scale = glm::vec3(solarToWorld(r), solarToWorld(r), solarToWorld(r));
planetTransform->position = pos;
......
......@@ -100,7 +100,7 @@ void SceneGraphSystem::prepareDraw(double interp) {
auto viewMatrix = glm::lookAt<float>(-light->dir, { 0,0,0 }, glm::vec3(0.0f, 1.0f, 0.0f));
auto lightVP = glm::ortho<float>(-1500.0f, +1500.0f, 1500.0f, -1500.0f, 0.0f, -1000.0f) * viewMatrix;
m_renderer->submit({light->color, light->castShadow, transform->lastGlobalTransform, transform->thisGlobalTransform, lightVP, nullptr, nullptr});
m_renderer->submit({light->color, light->castShadow,light->dir, light->type, transform->lastGlobalTransform, transform->thisGlobalTransform, lightVP, nullptr, nullptr});
}
}
......
#include <engine/scene/scenes/AtmosphereTestScene.hpp>
#include <engine/ui/UISystem.hpp>
#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Objects.hh>
#include <ACGL/Base/Settings.hh>
#include <ACGL/Math/Math.hh>
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <ACGL/OpenGL/Managers.hh>
#include <ACGL/OpenGL/Creator/ShaderProgramCreator.hh>
#include <ACGL/OpenGL/Creator/VertexArrayObjectCreator.hh>
#include <ACGL/OpenGL/Creator/Texture2DCreator.hh>
#include <engine/scene/Transform.hpp>
#include <engine/scene/Drawable.hpp>
#include <engine/scene/Planet.hpp>
#include <engine/events/MouseEvent.hpp>
#include <engine/events/KeyboardEvent.hpp>
#include <engine/graphics/BloomPostFX.hpp>
using namespace ACGL::OpenGL;
using namespace ACGL::Base;
using namespace ACGL::Utils;
bool AtmosphereTestScene::startup() {
if (!Scene::startup()) {
return false;
}
RESOLVE_DEPENDENCY(m_player);
RESOLVE_DEPENDENCY(m_orbitals);
m_renderer->addEffect<BloomPostFX>();
auto vaoSun = VertexArrayObjectCreator("uvsphere.obj").create();
// load a texture:
auto checkboardTexture = Texture2DFileManager::the()->get(Texture2DCreator("checkerboard.png"));
// look up all shader files starting with 'PBR' and build a ShaderProgram from it:
auto pbrShader = ShaderProgramCreator("PBR")
.attributeLocations(vaoSun->getAttributeLocations())
.fragmentDataLocations(m_renderer->getGBufferLocations()).create();
// Create geometry objects that point to the previously initialized vaos
Geometry geom1 = { vaoSun };
geom1.vao->setMode(GL_PATCHES);
// Create a material that uses the loaded shader program
Material sunMaterial = { glm::vec4{ 1, 1, 1, 1 }, glm::vec4{ 2, 2, 1, 1 }, checkboardTexture, pbrShader };
// Let's create a placeholder sun
sun = m_sceneGraph->create();
// Add a transform component to it so we are able to position it in space
auto sunTransform = sun.assign<Transform>();
// Add a Drawable component to it so the renderer has something to draw
sun.assign<Drawable>(geom1, sunMaterial);
float scale = m_orbitals->solarRadius * m_orbitals->scaleFactor * 0.1;
sunTransform->scale = glm::vec3(scale);
sunLight = sun.assign<Light>(glm::vec4(1, 1, 1, 1.5), glm::vec3(1, 0, 0), false, LightType::DIRECTIONAL);
// create a planet!
// Mass in solar masses, Radius in solar radii, Eccentricity, Semimajor axis, Inclination, Ascending Node, Arg. of Periapsis, time at perihelion
// mass radius e a i N w
earth = m_orbitals->addPlanet(sunTransform, "Earth", 0.000002988, 0.009153735632184, 0.016713, 1.000000, 0.0000000, 0.00000, 4.93533, 0);
m_events->subscribe<SimulateEvent>([this](const SimulateEvent& e) {
sunLight->dir = -glm::normalize(m_player->getWorldPosition());
});
// Subscribe to the UI drawing event.
// This is called once per frame
m_events->subscribe<"DrawUI"_sh>([this]() {
ImGui::Begin("Camera Control", 0,
ImGuiWindowFlags_::ImGuiWindowFlags_AlwaysAutoResize);
float r;
if (ImGui::Button("Sun/Global", glm::vec2(100, 20))) {
// Reset the camera to sun coords
m_player->attachToParent(sun);
r = sun.component<Transform>()->scale.x;
m_player->setPosition(glm::vec3(0, 0, r * 1.2));
m_player->setRotation(glm::mat4());
}
if (ImGui::Button("Earth", glm::vec2(100, 20))) {
m_player->attachToParent(earth);
r = earth.component<Transform>()->scale.x;
m_player->setPosition(glm::vec3(0, 0, r * 1.2));
m_player->setRotation(glm::mat4());
}
ImGui::End();
});
return true;
}
void AtmosphereTestScene::shutdown() {
}
......@@ -35,7 +35,7 @@ bool OrbitsScene::startup() {
RESOLVE_DEPENDENCY(m_orbitals);
RESOLVE_DEPENDENCY(m_player);
m_renderer->addEffect<BloomPostFX>();
//m_renderer->addEffect<BloomPostFX>();
// load the geometry of the stanford bunny and build a VAO:
auto vaoTeapot = VertexArrayObjectCreator("teapot.obj").create();
......@@ -86,7 +86,7 @@ bool OrbitsScene::startup() {
// Add a Drawable component to it so the renderer has something to draw
sun.assign<Drawable>(geom1, sunMaterial);
sunTransform->scale = glm::vec3(scale);
sun.assign<Light>(glm::vec4(1, 1, 1, 1200000000000), glm::vec3(1, 0, 0), false);
sun.assign<Light>(glm::vec4(1, 1, 1, 2), glm::vec3(1, 0, 0), false, LightType::DIRECTIONAL);
// create a planet!
// Mass in solar masses, Radius in solar radii, Eccentricity, Semimajor axis, Inclination, Ascending Node, Arg. of Periapsis, time at perihelion
......
......@@ -14,11 +14,14 @@
// Scenes we know about
#include <engine/scene/scenes/OrbitsScene.hpp>
#include <engine/scene/scenes/AtmosphereTestScene.hpp>
static std::unique_ptr<Scene> createScene(std::string sceneType, std::string sceneIni, Context* context) {
if (sceneType == "Orbits") {
return std::make_unique<OrbitsScene>(context, sceneIni);
} else if (sceneType == "AtmosphereTest") {
return std::make_unique<AtmosphereTestScene>(context, sceneIni);
}
// Add your scenes here
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment