Continued with lighting
This commit is contained in:
parent
597a14743b
commit
e88d972ecc
12 changed files with 95 additions and 6 deletions
22
SMCode/SM2D/Light/LightObjects/LightObject.cs
Normal file
22
SMCode/SM2D/Light/LightObjects/LightObject.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics;
|
||||
using SM.Base.Types;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM2D.Light
|
||||
{
|
||||
public abstract class LightObject
|
||||
{
|
||||
internal abstract int Type { get; }
|
||||
|
||||
public CVector2 Position = new CVector2(0);
|
||||
public Color4 Color = Color4.White;
|
||||
|
||||
internal virtual void SetUniforms(Dictionary<string, Uniform> uniforms)
|
||||
{
|
||||
uniforms["Type"].SetUniform1(Type);
|
||||
uniforms["Position"].SetUniform2(Position);
|
||||
uniforms["Color"].SetUniform4(Color);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
SMCode/SM2D/Light/LightObjects/PointLight.cs
Normal file
7
SMCode/SM2D/Light/LightObjects/PointLight.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace SM2D.Light
|
||||
{
|
||||
public class PointLight : LightObject
|
||||
{
|
||||
internal override int Type { get; } = 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
using SM.Base.PostProcess;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL.Framebuffer;
|
||||
using SM.OGL.Shaders;
|
||||
using SM.Utility;
|
||||
|
||||
namespace SM2D.Light
|
||||
|
|
@ -18,6 +19,13 @@ namespace SM2D.Light
|
|||
_shader.Draw(main.ColorAttachments["color"], collection =>
|
||||
{
|
||||
collection["Ambient"].SetUniform4(sceneExtension.Ambient);
|
||||
collection["LightCount"].SetUniform1(sceneExtension.Lights.Count);
|
||||
UniformArray array = collection.GetArray("Lights");
|
||||
|
||||
for (int i = 0; i < sceneExtension.Lights.Count; i++)
|
||||
{
|
||||
sceneExtension.Lights[i].SetUniforms(array[i]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using OpenTK.Graphics;
|
||||
using System.Collections.Generic;
|
||||
using OpenTK.Graphics;
|
||||
|
||||
namespace SM2D.Light
|
||||
{
|
||||
|
|
@ -6,6 +7,6 @@ namespace SM2D.Light
|
|||
{
|
||||
public Color4 Ambient = Color4.White;
|
||||
|
||||
|
||||
public List<LightObject> Lights = new List<LightObject>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,47 @@
|
|||
#version 330
|
||||
#define PI 3.14159265359
|
||||
|
||||
struct Light {
|
||||
int Type;
|
||||
vec2 Position;
|
||||
vec4 Color;
|
||||
};
|
||||
|
||||
in vec2 vTexture;
|
||||
|
||||
vec4 GetRenderColor();
|
||||
in vec2 FragPos;
|
||||
|
||||
uniform vec4 Ambient = vec4(1);
|
||||
uniform Light[24] Lights;
|
||||
uniform int LightCount;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
vec4 GetRenderColor();
|
||||
|
||||
vec3 calcPointLight(Light light) {
|
||||
float dis = distance(FragPos, light.Position);
|
||||
float intensity = 4 / 4 * PI * pow(dis, 2);
|
||||
|
||||
return vec3(light.Color * intensity);
|
||||
}
|
||||
|
||||
vec3 calcLight() {
|
||||
vec3 addedLight = vec3(0);
|
||||
|
||||
for(int i = 0; i < LightCount; i++) {
|
||||
Light light = Lights[i];
|
||||
|
||||
switch(light.Type) {
|
||||
case 0:
|
||||
addedLight += calcPointLight(light);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return addedLight;
|
||||
}
|
||||
|
||||
void main() {
|
||||
color = GetRenderColor() * Ambient;
|
||||
color += vec4(calcLight(), 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue