2019-04-10 01:43:24 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using OpenTK;
|
2019-04-10 21:36:10 +02:00
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
2019-04-10 01:43:24 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using GL_EditorFramework.GL_Core;
|
|
|
|
|
using GL_EditorFramework.Interfaces;
|
|
|
|
|
using GL_EditorFramework.EditorDrawables;
|
|
|
|
|
using GL_EditorFramework;
|
|
|
|
|
|
|
|
|
|
namespace Switch_Toolbox.Library.Rendering
|
|
|
|
|
{
|
|
|
|
|
public class ProbeLighting : AbstractGlDrawable
|
|
|
|
|
{
|
|
|
|
|
public List<Entry> Entries = new List<Entry>();
|
|
|
|
|
|
|
|
|
|
public class Entry
|
|
|
|
|
{
|
2019-04-10 21:36:10 +02:00
|
|
|
|
public bool IsVisable = true;
|
|
|
|
|
|
2019-04-10 01:43:24 +02:00
|
|
|
|
public string Name { get { return $"b_{Index}"; } }
|
|
|
|
|
|
|
|
|
|
public uint Index = 0;
|
|
|
|
|
public uint Type = 0;
|
|
|
|
|
|
|
|
|
|
public Grid Grid = new Grid();
|
|
|
|
|
|
|
|
|
|
//Index Buffer
|
|
|
|
|
public uint IndexType = 1;
|
|
|
|
|
public uint UsedIndexNum;
|
|
|
|
|
public uint MaxIndexNum;
|
|
|
|
|
public uint[] IndexBuffer = new uint[0];
|
|
|
|
|
|
2019-04-10 21:36:10 +02:00
|
|
|
|
|
|
|
|
|
public ushort[] GetUint16BufferIndices()
|
|
|
|
|
{
|
|
|
|
|
if (!IsUint16Buffer())
|
|
|
|
|
throw new Exception("Buffer not a ushort buffer!");
|
|
|
|
|
|
|
|
|
|
List<ushort> Indices = new List<ushort>();
|
|
|
|
|
|
|
|
|
|
//Read the buffer data back as ushort
|
|
|
|
|
for (int i = 0; i < IndexBuffer.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = BitConverter.GetBytes(IndexBuffer[i]);
|
|
|
|
|
Indices.Add(BitConverter.ToUInt16(bytes, 0));
|
|
|
|
|
Indices.Add(BitConverter.ToUInt16(bytes, 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Indices.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUint16Buffer()
|
|
|
|
|
{
|
|
|
|
|
if (IndexBuffer.Length / 2 == UsedIndexNum)
|
|
|
|
|
return true;
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUint32Buffer()
|
|
|
|
|
{
|
|
|
|
|
if (IndexBuffer.Length == UsedIndexNum)
|
|
|
|
|
return true;
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 01:43:24 +02:00
|
|
|
|
//Data Buffer
|
|
|
|
|
public uint DataType = 0;
|
|
|
|
|
public uint UsedShDataNum;
|
|
|
|
|
public uint MaxShDataNum;
|
|
|
|
|
public uint PerProbeNum = 27;
|
|
|
|
|
public float[] DataBuffer = new float[0];
|
2019-04-10 21:36:10 +02:00
|
|
|
|
|
|
|
|
|
public bool IsHalfFloatBuffer()
|
|
|
|
|
{
|
|
|
|
|
if ((DataBuffer.Length * PerProbeNum) / 2 == UsedShDataNum)
|
|
|
|
|
return true;
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsFloatBuffer()
|
|
|
|
|
{
|
|
|
|
|
if ((DataBuffer.Length * PerProbeNum) == UsedShDataNum)
|
|
|
|
|
return true;
|
|
|
|
|
else return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Color data in the data buffer
|
|
|
|
|
public class ShereHermonic
|
|
|
|
|
{
|
|
|
|
|
//Usually 27
|
|
|
|
|
public Vector3[] Coefficents;
|
|
|
|
|
|
|
|
|
|
public ShereHermonic(int PerProbeNum) {
|
|
|
|
|
Coefficents = new Vector3[PerProbeNum];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-10 01:43:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Grid
|
|
|
|
|
{
|
2019-04-10 21:36:10 +02:00
|
|
|
|
public Vector3 GridColor;
|
|
|
|
|
|
2019-04-10 01:43:24 +02:00
|
|
|
|
public Vector3 AABB_Max_Position;
|
|
|
|
|
public Vector3 AABB_Min_Position;
|
|
|
|
|
public Vector3 Voxel_Step_Position;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 21:36:10 +02:00
|
|
|
|
protected static ShaderProgram ProbeSHShaderProgram;
|
|
|
|
|
|
|
|
|
|
int vbo_position;
|
|
|
|
|
|
|
|
|
|
public void Destroy()
|
|
|
|
|
{
|
|
|
|
|
bool buffersWereInitialized = vbo_position != 0;
|
|
|
|
|
if (!buffersWereInitialized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
GL.DeleteBuffer(vbo_position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Vector3> GetProbeVertices(int index)
|
|
|
|
|
{
|
|
|
|
|
var entry = Entries[index];
|
|
|
|
|
|
|
|
|
|
var vertices = new List<Vector3>();
|
|
|
|
|
|
|
|
|
|
Vector3 Max = entry.Grid.AABB_Max_Position;
|
|
|
|
|
Vector3 Min = entry.Grid.AABB_Min_Position;
|
|
|
|
|
Vector3 Step = entry.Grid.AABB_Min_Position;
|
|
|
|
|
|
|
|
|
|
float GridWidth = Min.X + Max.X;
|
|
|
|
|
float GirdHeight = Min.Y + Max.Y;
|
|
|
|
|
|
|
|
|
|
/* //Draw Z plane
|
|
|
|
|
for (int x = 0; x < GridWidth; x++)
|
|
|
|
|
{
|
|
|
|
|
for (int y = 0; y < GirdHeight; y++)
|
|
|
|
|
{
|
|
|
|
|
Vector3 position = new Vector3(Min.X + (x / Step.X) * (Max.X - Min.X),
|
|
|
|
|
Min.Y + (y / Step.Y) * (Max.Y - Min.Y),
|
|
|
|
|
Max.Z);
|
|
|
|
|
|
|
|
|
|
vertices.Add(position);
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
return vertices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Vector3> GetBoundingVertices(int index)
|
|
|
|
|
{
|
|
|
|
|
var entry = Entries[index];
|
|
|
|
|
|
|
|
|
|
var vertices = new List<Vector3>();
|
|
|
|
|
|
|
|
|
|
Vector3 Max = entry.Grid.AABB_Max_Position;
|
|
|
|
|
Vector3 Min = entry.Grid.AABB_Min_Position;
|
|
|
|
|
|
|
|
|
|
vertices.Add(new Vector3(Min.X, Min.Y, Min.Z));
|
|
|
|
|
vertices.Add(new Vector3(Min.X, Min.Y, Max.Z));
|
|
|
|
|
vertices.Add(new Vector3(Min.X, Max.Y, Min.Z));
|
|
|
|
|
vertices.Add(new Vector3(Min.X, Max.Y, Max.Z));
|
|
|
|
|
vertices.Add(new Vector3(Max.X, Min.Y, Min.Z));
|
|
|
|
|
vertices.Add(new Vector3(Max.X, Min.Y, Max.Z));
|
|
|
|
|
vertices.Add(new Vector3(Max.X, Max.Y, Min.Z));
|
|
|
|
|
vertices.Add(new Vector3(Max.X, Max.Y, Max.Z));
|
|
|
|
|
|
|
|
|
|
return vertices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int CellAmount;
|
|
|
|
|
public static int CellSize;
|
|
|
|
|
|
|
|
|
|
int CurrentIndex = 0;
|
|
|
|
|
|
|
|
|
|
Vector3[] Vertices
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetProbeVertices(CurrentIndex).ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateVertexData()
|
|
|
|
|
{
|
|
|
|
|
Vector3[] vertices = Vertices;
|
|
|
|
|
|
|
|
|
|
GL.GenBuffers(1, out vbo_position);
|
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
|
|
|
|
|
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
|
|
|
|
|
new IntPtr(vertices.Length * Vector3.SizeInBytes),
|
|
|
|
|
vertices, BufferUsageHint.StaticDraw);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 01:43:24 +02:00
|
|
|
|
public override void Draw(GL_ControlModern control, Pass pass)
|
|
|
|
|
{
|
2019-04-10 21:36:10 +02:00
|
|
|
|
if (pass == Pass.TRANSPARENT)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool buffersWereInitialized = vbo_position != 0;
|
|
|
|
|
if (!buffersWereInitialized)
|
|
|
|
|
UpdateVertexData();
|
|
|
|
|
|
|
|
|
|
if (!Runtime.OpenTKInitialized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
control.CurrentShader = ProbeSHShaderProgram;
|
|
|
|
|
control.UpdateModelMatrix(Matrix4.Identity);
|
|
|
|
|
|
|
|
|
|
Matrix4 previewScale = Utils.TransformValues(Vector3.Zero, Vector3.Zero, Runtime.previewScale);
|
|
|
|
|
|
|
|
|
|
ProbeSHShaderProgram.SetMatrix4x4("previewScale", ref previewScale);
|
|
|
|
|
|
|
|
|
|
foreach (var entry in Entries)
|
|
|
|
|
{
|
|
|
|
|
if (!entry.IsVisable)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Vector3[] vertices = GetBoundingVertices((int)entry.Index).ToArray();
|
|
|
|
|
|
|
|
|
|
Vector3 Max = entry.Grid.AABB_Max_Position;
|
|
|
|
|
Vector3 Min = entry.Grid.AABB_Min_Position;
|
|
|
|
|
Vector3 Step = entry.Grid.Voxel_Step_Position;
|
|
|
|
|
|
|
|
|
|
float gridHeight = Min.Y + Max.Y;
|
|
|
|
|
float gridWidth = Min.X + Max.X;
|
|
|
|
|
float gridDepth = Min.Z + Max.Z;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Draw bounding box
|
|
|
|
|
GL.Color3(entry.Grid.GridColor);
|
|
|
|
|
|
|
|
|
|
GL.Begin(PrimitiveType.LineLoop);
|
|
|
|
|
GL.Vertex3(vertices[0]);
|
|
|
|
|
GL.Vertex3(vertices[1]);
|
|
|
|
|
GL.Vertex3(vertices[3]);
|
|
|
|
|
GL.Vertex3(vertices[2]);
|
|
|
|
|
GL.End();
|
|
|
|
|
|
|
|
|
|
GL.Begin(PrimitiveType.LineLoop);
|
|
|
|
|
GL.Vertex3(vertices[4]);
|
|
|
|
|
GL.Vertex3(vertices[5]);
|
|
|
|
|
GL.Vertex3(vertices[7]);
|
|
|
|
|
GL.Vertex3(vertices[6]);
|
|
|
|
|
GL.End();
|
|
|
|
|
|
|
|
|
|
GL.Begin(PrimitiveType.Lines);
|
|
|
|
|
GL.Vertex3(vertices[0]);
|
|
|
|
|
GL.Vertex3(vertices[4]);
|
|
|
|
|
GL.Vertex3(vertices[1]);
|
|
|
|
|
GL.Vertex3(vertices[5]);
|
|
|
|
|
GL.Vertex3(vertices[3]);
|
|
|
|
|
GL.Vertex3(vertices[7]);
|
|
|
|
|
GL.Vertex3(vertices[2]);
|
|
|
|
|
GL.Vertex3(vertices[6]);
|
|
|
|
|
GL.End();
|
|
|
|
|
|
|
|
|
|
ProbeSHShaderProgram.EnableVertexAttributes();
|
|
|
|
|
Draw(ProbeSHShaderProgram);
|
|
|
|
|
ProbeSHShaderProgram.DisableVertexAttributes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GL.UseProgram(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Attributes(ShaderProgram shader)
|
|
|
|
|
{
|
|
|
|
|
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
|
|
|
|
|
GL.VertexAttribPointer(shader.GetAttribute("vPosition"), 3, VertexAttribPointerType.Float, false, 12, 0);
|
|
|
|
|
}
|
|
|
|
|
private void Uniforms(ShaderProgram shader)
|
|
|
|
|
{
|
|
|
|
|
shader.SetVector3("gridColor", ColorUtility.ToVector3(System.Drawing.Color.Black));
|
|
|
|
|
}
|
|
|
|
|
private void Draw(ShaderProgram shader)
|
|
|
|
|
{
|
|
|
|
|
Uniforms(shader);
|
|
|
|
|
Attributes(shader);
|
2019-04-10 01:43:24 +02:00
|
|
|
|
|
2019-04-10 21:36:10 +02:00
|
|
|
|
GL.DrawArrays(PrimitiveType.Points, 0, Vertices.Length);
|
2019-04-10 01:43:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(GL_ControlLegacy control, Pass pass)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Prepare(GL_ControlModern control)
|
|
|
|
|
{
|
2019-04-10 21:36:10 +02:00
|
|
|
|
var Frag = new FragmentShader(
|
|
|
|
|
@"#version 330
|
|
|
|
|
uniform vec3 gridColor;
|
2019-04-22 23:52:21 +02:00
|
|
|
|
out vec4 FragColor;
|
|
|
|
|
|
2019-04-10 21:36:10 +02:00
|
|
|
|
void main(){
|
2019-04-22 23:52:21 +02:00
|
|
|
|
FragColor = vec4(gridColor, 1);
|
2019-04-10 21:36:10 +02:00
|
|
|
|
}");
|
|
|
|
|
var Vert = new VertexShader(
|
|
|
|
|
@"#version 330
|
|
|
|
|
in vec3 vPosition;
|
|
|
|
|
|
|
|
|
|
uniform mat4 mtxMdl;
|
|
|
|
|
uniform mat4 mtxCam;
|
|
|
|
|
|
|
|
|
|
void main(){
|
|
|
|
|
gl_Position = mtxMdl * mtxCam * vec4(vPosition.xyz, 1);
|
|
|
|
|
}");
|
2019-04-10 01:43:24 +02:00
|
|
|
|
|
2019-06-19 23:05:44 +02:00
|
|
|
|
ProbeSHShaderProgram = new ShaderProgram(Frag, Vert, control);
|
2019-04-10 01:43:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Prepare(GL_ControlLegacy control)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|