Continued with lighting
This commit is contained in:
parent
597a14743b
commit
e88d972ecc
12 changed files with 95 additions and 6 deletions
|
|
@ -11,7 +11,7 @@ out vec2 FragPos;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vTexture = aTex;
|
vTexture = aTex;
|
||||||
|
|
||||||
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
|
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
|
||||||
|
|
||||||
gl_Position = MVP * vec4(aPos, 1);
|
gl_Position = MVP * vec4(aPos, 1);
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,17 @@ layout(location = 0) in vec3 aPos;
|
||||||
layout(location = 1) in vec2 aTex;
|
layout(location = 1) in vec2 aTex;
|
||||||
|
|
||||||
uniform mat4 MVP;
|
uniform mat4 MVP;
|
||||||
|
uniform mat4 ModelMatrix;
|
||||||
|
|
||||||
out vec2 vTexture;
|
out vec2 vTexture;
|
||||||
|
out vec2 FragPos;
|
||||||
|
|
||||||
void vertex();
|
void vertex();
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vTexture = aTex;
|
vTexture = aTex;
|
||||||
|
|
||||||
|
FragPos = vec2(ModelMatrix * vec4(aPos, 1));
|
||||||
|
|
||||||
gl_Position = MVP * vec4(aPos, 1);
|
gl_Position = MVP * vec4(aPos, 1);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ namespace SM.Base.PostProcess
|
||||||
GL.BindVertexArray(Plate.Object);
|
GL.BindVertexArray(Plate.Object);
|
||||||
|
|
||||||
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
|
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
|
||||||
|
Uniforms["ModelMatrix"].SetMatrix4(PostProcessEffect.Model);
|
||||||
Uniforms["renderedTexture"].SetTexture(color, 0);
|
Uniforms["renderedTexture"].SetTexture(color, 0);
|
||||||
|
|
||||||
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
|
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
|
||||||
|
|
@ -72,6 +73,7 @@ namespace SM.Base.PostProcess
|
||||||
GL.BindVertexArray(Plate.Object);
|
GL.BindVertexArray(Plate.Object);
|
||||||
|
|
||||||
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
|
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
|
||||||
|
Uniforms["ModelMatrix"].SetMatrix4(PostProcessEffect.Model);
|
||||||
Uniforms["renderedTexture"].SetTexture(color, 0);
|
Uniforms["renderedTexture"].SetTexture(color, 0);
|
||||||
|
|
||||||
setUniformAction(Uniforms);
|
setUniformAction(Uniforms);
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,8 @@ namespace SM.Base
|
||||||
MouseState = Mouse.GetState()
|
MouseState = Mouse.GetState()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (context.KeyboardState[Key.AltLeft] && context.KeyboardState[Key.F4]) Close();
|
||||||
|
|
||||||
Update(e, ref context);
|
Update(e, ref context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
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.PostProcess;
|
||||||
using SM.Base.Scene;
|
using SM.Base.Scene;
|
||||||
using SM.OGL.Framebuffer;
|
using SM.OGL.Framebuffer;
|
||||||
|
using SM.OGL.Shaders;
|
||||||
using SM.Utility;
|
using SM.Utility;
|
||||||
|
|
||||||
namespace SM2D.Light
|
namespace SM2D.Light
|
||||||
|
|
@ -18,6 +19,13 @@ namespace SM2D.Light
|
||||||
_shader.Draw(main.ColorAttachments["color"], collection =>
|
_shader.Draw(main.ColorAttachments["color"], collection =>
|
||||||
{
|
{
|
||||||
collection["Ambient"].SetUniform4(sceneExtension.Ambient);
|
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
|
namespace SM2D.Light
|
||||||
{
|
{
|
||||||
|
|
@ -6,6 +7,6 @@ namespace SM2D.Light
|
||||||
{
|
{
|
||||||
public Color4 Ambient = Color4.White;
|
public Color4 Ambient = Color4.White;
|
||||||
|
|
||||||
|
public List<LightObject> Lights = new List<LightObject>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,47 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
#define PI 3.14159265359
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
int Type;
|
||||||
|
vec2 Position;
|
||||||
|
vec4 Color;
|
||||||
|
};
|
||||||
|
|
||||||
in vec2 vTexture;
|
in vec2 vTexture;
|
||||||
|
in vec2 FragPos;
|
||||||
vec4 GetRenderColor();
|
|
||||||
|
|
||||||
uniform vec4 Ambient = vec4(1);
|
uniform vec4 Ambient = vec4(1);
|
||||||
|
uniform Light[24] Lights;
|
||||||
|
uniform int LightCount;
|
||||||
|
|
||||||
layout(location = 0) out vec4 color;
|
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() {
|
void main() {
|
||||||
color = GetRenderColor() * Ambient;
|
color = GetRenderColor() * Ambient;
|
||||||
|
color += vec4(calcLight(), 1);
|
||||||
}
|
}
|
||||||
|
|
@ -45,6 +45,8 @@
|
||||||
<Compile Include="Drawing\DrawParticles.cs" />
|
<Compile Include="Drawing\DrawParticles.cs" />
|
||||||
<Compile Include="Drawing\DrawText.cs" />
|
<Compile Include="Drawing\DrawText.cs" />
|
||||||
<Compile Include="GLWindow2D.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\LightPostEffect.cs" />
|
||||||
<Compile Include="Light\LightSceneExtension.cs" />
|
<Compile Include="Light\LightSceneExtension.cs" />
|
||||||
<Compile Include="Object\Polygon.cs" />
|
<Compile Include="Object\Polygon.cs" />
|
||||||
|
|
@ -80,5 +82,6 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="Light\light.frag" />
|
<EmbeddedResource Include="Light\light.frag" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
||||||
2
SMCode/SM2D/SM2D.csproj.DotSettings
Normal file
2
SMCode/SM2D/SM2D.csproj.DotSettings
Normal 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>
|
||||||
|
|
@ -14,6 +14,7 @@ using SM.Base.Time;
|
||||||
using SM.Utility;
|
using SM.Utility;
|
||||||
using SM2D;
|
using SM2D;
|
||||||
using SM2D.Drawing;
|
using SM2D.Drawing;
|
||||||
|
using SM2D.Light;
|
||||||
using SM2D.Object;
|
using SM2D.Object;
|
||||||
using SM2D.Scene;
|
using SM2D.Scene;
|
||||||
using Font = SM.Base.Drawing.Text.Font;
|
using Font = SM.Base.Drawing.Text.Font;
|
||||||
|
|
@ -66,7 +67,10 @@ namespace SM_TEST
|
||||||
text.Transform.Position.Set(0, 500);
|
text.Transform.Position.Set(0, 500);
|
||||||
scene.HUD.Add(text);
|
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();
|
//particles.Trigger();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue