Continued with lighting

This commit is contained in:
Michel Fedde 2020-12-14 15:52:14 +01:00
parent 597a14743b
commit e88d972ecc
12 changed files with 95 additions and 6 deletions

View file

@ -11,7 +11,7 @@ out vec2 FragPos;
void main() {
vTexture = aTex;
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
gl_Position = MVP * vec4(aPos, 1);

View file

@ -4,13 +4,17 @@ layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
uniform mat4 MVP;
uniform mat4 ModelMatrix;
out vec2 vTexture;
out vec2 FragPos;
void vertex();
void main() {
vTexture = aTex;
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
gl_Position = MVP * vec4(aPos, 1);

View file

@ -52,6 +52,7 @@ namespace SM.Base.PostProcess
GL.BindVertexArray(Plate.Object);
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
Uniforms["ModelMatrix"].SetMatrix4(PostProcessEffect.Model);
Uniforms["renderedTexture"].SetTexture(color, 0);
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
@ -72,6 +73,7 @@ namespace SM.Base.PostProcess
GL.BindVertexArray(Plate.Object);
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
Uniforms["ModelMatrix"].SetMatrix4(PostProcessEffect.Model);
Uniforms["renderedTexture"].SetTexture(color, 0);
setUniformAction(Uniforms);

View file

@ -148,6 +148,8 @@ namespace SM.Base
MouseState = Mouse.GetState()
};
if (context.KeyboardState[Key.AltLeft] && context.KeyboardState[Key.F4]) Close();
Update(e, ref context);
}

View 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);
}
}
}

View file

@ -0,0 +1,7 @@
namespace SM2D.Light
{
public class PointLight : LightObject
{
internal override int Type { get; } = 0;
}
}

View file

@ -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]);
}
});
}

View file

@ -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>();
}
}

View file

@ -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);
}

View file

@ -45,6 +45,8 @@
<Compile Include="Drawing\DrawParticles.cs" />
<Compile Include="Drawing\DrawText.cs" />
<Compile Include="GLWindow2D.cs" />
<Compile Include="Light\LightObjects\LightObject.cs" />
<Compile Include="Light\LightObjects\PointLight.cs" />
<Compile Include="Light\LightPostEffect.cs" />
<Compile Include="Light\LightSceneExtension.cs" />
<Compile Include="Object\Polygon.cs" />
@ -80,5 +82,6 @@
<ItemGroup>
<EmbeddedResource Include="Light\light.frag" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=light_005Clightobjects/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View file

@ -14,6 +14,7 @@ using SM.Base.Time;
using SM.Utility;
using SM2D;
using SM2D.Drawing;
using SM2D.Light;
using SM2D.Object;
using SM2D.Scene;
using Font = SM.Base.Drawing.Text.Font;
@ -66,7 +67,10 @@ namespace SM_TEST
text.Transform.Position.Set(0, 500);
scene.HUD.Add(text);
scene.LightInformations.Ambient = Color4.Blue;
PointLight light = new PointLight();
scene.LightInformations.Lights.Add(light);
scene.LightInformations.Ambient = Color4.Black;
//particles.Trigger();
}