daily update on 20231016 by nitload youmu, vao,vbo,ebo struct added
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal unsafe struct ElementBuffer : IDisposable
|
||||
{
|
||||
uint _bufferID;
|
||||
|
||||
public ElementBuffer(int n)
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glGenBuffers(n, id);
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
Graphic.glBindBuffer(GLFW.GL_ELEMENT_ARRAY_BUFFER, _bufferID);
|
||||
}
|
||||
|
||||
public void SetData<T>(T bufferData, BufferReadType readType) where T : IMat
|
||||
{
|
||||
int size = Marshal.SizeOf(bufferData);
|
||||
Graphic.glBufferData(
|
||||
(int)BufferType.ELEMENT_ARRAY_BUFFER,
|
||||
size,
|
||||
&bufferData,
|
||||
(int)readType);
|
||||
}
|
||||
|
||||
public void SetData<T>(T[] bufferArray, BufferReadType readType) where T : struct, IConvertible
|
||||
{
|
||||
|
||||
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
|
||||
|
||||
fixed (void* bufferPtr = &bufferArray[0])
|
||||
Graphic.glBufferData(
|
||||
(int)BufferType.ELEMENT_ARRAY_BUFFER,
|
||||
size,
|
||||
bufferPtr,
|
||||
(int)readType);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glDeleteBuffers(1, id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace StgSharp.Graphics
|
||||
|
||||
using _glfw = _GLFWlibrary;
|
||||
|
||||
public static unsafe partial class GLFW
|
||||
internal static unsafe class GLFW
|
||||
{
|
||||
internal static _glfw glfw = new _glfw();
|
||||
|
||||
@@ -2045,4 +2045,31 @@ namespace StgSharp.Graphics
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
public enum ShaderType : int
|
||||
{
|
||||
FRAGMENT = 0x8B30,
|
||||
VERTEX = 0x8B31
|
||||
}
|
||||
|
||||
public enum BufferType : int
|
||||
{
|
||||
ARRAY_BUFFER = 0x8892,
|
||||
ELEMENT_ARRAY_BUFFER = 0x8893,
|
||||
ARRAY_BUFFER_BINDING = 0x8894
|
||||
}
|
||||
|
||||
public enum BufferReadType : int
|
||||
{
|
||||
STREAM_DRAW = 0x88E0,
|
||||
STREAM_READ = 0x88E1,
|
||||
STREAM_COPY = 0x88E2,
|
||||
STATIC_DRAW = 0x88E4,
|
||||
STATIC_READ = 0x88E5,
|
||||
STATIC_COPY = 0x88E6,
|
||||
DYNAMIC_DRAW = 0x88E8,
|
||||
DYNAMIC_READ = 0x88E9,
|
||||
DYNAMIC_COPY = 0x88EA
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Graphics.GL
|
||||
{
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct shaderObject
|
||||
{
|
||||
uint VBO;
|
||||
uint VAO;
|
||||
uint shaderProgram;
|
||||
}
|
||||
}
|
||||
+134
-62
@@ -1,16 +1,95 @@
|
||||
using StgSharp.Controlling;
|
||||
using StgSharp.Graphics;
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public static unsafe partial class Graphic
|
||||
{
|
||||
/// <summary>
|
||||
/// sets the framebuffer resize callback of the specified <see cref="Form"/>,
|
||||
///which is called when the framebuffer of the specified <see cref="Form"/> is resized.
|
||||
/// </summary>
|
||||
/// <param name="form">The Form instance to be controlled</param>
|
||||
/// <param name="handler">Method hadler </param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void SetFrameBufferSizeCallback(Form form, FrameBufferSizeHadler handler)
|
||||
{
|
||||
internalIO.glfwSetFramebufferSizeCallback(form.windowID, handler);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Call glClearColor in OpenGL
|
||||
/// specifies the red, green, blue, and alpha values used by glClear to clear the color buffers.
|
||||
/// Values specified by glClearColor are clamped to the range [0,1]
|
||||
/// </summary>
|
||||
/// <param name="R">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="G">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="B">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="A">Specify the red values used when the color buffers are cleared.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ClearColor(float R, float G, float B, float A)
|
||||
{
|
||||
glClearColor(R, G, B, A);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Conpile the provided shader code in string form.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">A string represent the shader's code</param>
|
||||
/// <param name="shaderType">An <see cref="ShaderType"/> enum represent the type of the shader to
|
||||
/// conpile, including vertex shader and fragment shader.</param>
|
||||
/// <returns>An uint value as the only mark of conpiled shader</returns>
|
||||
public static unsafe uint CompileShader(string shaderCode, ShaderType shaderType)
|
||||
{
|
||||
uint shaderID;
|
||||
shaderCode += '\0';
|
||||
byte[] buffer = Encoding.ASCII.GetBytes(shaderCode);
|
||||
fixed (byte* data = &buffer[0])
|
||||
{
|
||||
shaderID = internalIO.InternalLoadShaderCode(data, (int)shaderType);
|
||||
}
|
||||
if (shaderID == 0)
|
||||
{
|
||||
IntPtr logPtr = internalIO.InternalReadLog();
|
||||
string log;
|
||||
try
|
||||
{
|
||||
byte[] logByte = new byte[512];
|
||||
Marshal.Copy(logPtr, logByte, 0, 512);
|
||||
log = Encoding.UTF8.GetString(logByte);
|
||||
log = log.Replace("\0", "");
|
||||
|
||||
bool isUseful = false;
|
||||
foreach (char item in log)
|
||||
{
|
||||
if (!char.IsControl(item))
|
||||
{
|
||||
isUseful = true; break;
|
||||
}
|
||||
}
|
||||
if (!isUseful)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
|
||||
|
||||
}
|
||||
|
||||
internalIO.InternalWriteLog(log, LogType.Error);
|
||||
|
||||
}
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a form and its associated context.
|
||||
@@ -22,7 +101,7 @@ namespace StgSharp
|
||||
/// <param name="share"></param>
|
||||
/// <returns>The instance of created form</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static unsafe Form GLcreateWindow(
|
||||
public static unsafe Form CreateWindow(
|
||||
int width, int height, string tittle, IntPtr monitor, Form share)
|
||||
{
|
||||
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
|
||||
@@ -30,7 +109,7 @@ namespace StgSharp
|
||||
if (share == null)
|
||||
{
|
||||
windowPtr = internalIO.glfwCreateWindow(
|
||||
800, 600, tittleBytes, monitor, IntPtr.Zero);
|
||||
width, height, tittleBytes, monitor, IntPtr.Zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -52,71 +131,64 @@ namespace StgSharp
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a window and its associated context.
|
||||
/// </summary>
|
||||
/// <param name="width">Required width</param>
|
||||
/// <param name="height">Required width</param>
|
||||
/// <param name="tittle">Tittle of the window </param>
|
||||
/// <param name="monitor"></param>
|
||||
/// <param name="share"></param>
|
||||
/// <returns>The handle of created window</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static unsafe Form GLcreateWindow(int width, int height, string tittle, IntPtr monitor, IntPtr share)
|
||||
|
||||
public static unsafe uint CreatGraphicProgram()
|
||||
{
|
||||
byte[] tittleCharArray = System.Text.Encoding.UTF8.GetBytes(tittle);
|
||||
IntPtr windowPtr = internalIO.glfwCreateWindow
|
||||
(width, height, tittleCharArray, monitor, share);
|
||||
|
||||
if ((int)windowPtr == -1 || (int)windowPtr == -2)
|
||||
{
|
||||
throw new Exception("Cannot create the form!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Form form = new Form();
|
||||
form.Size = new Vec2d(width, height);
|
||||
form.windowID = windowPtr;
|
||||
return form;
|
||||
}
|
||||
return glCreateProgram();
|
||||
}
|
||||
|
||||
|
||||
public static unsafe void GLmakeContexCurrent(Form form)
|
||||
{
|
||||
internalIO.glfwMakeContextCurrent(form.windowID);
|
||||
}
|
||||
|
||||
|
||||
public static unsafe bool GLwindowShouldClose(Form form)
|
||||
{
|
||||
int a = internalIO.glfwWindowShouldClose(form.windowID);
|
||||
return a != 0;
|
||||
}
|
||||
|
||||
|
||||
public static unsafe void GLswapBuffers(Form form)
|
||||
{
|
||||
internalIO.glfwSwapBuffers(form.windowID);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static IntPtr GetProcAddress(string name)
|
||||
{
|
||||
IntPtr ret = internalIO.glfwGetProcAddress(name);
|
||||
if (ret == IntPtr.Zero)
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine(name);
|
||||
#endif
|
||||
return internalIO.glfwGetProcAddress(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static unsafe bool GetWindowShouldClose(Form form)
|
||||
{
|
||||
return internalIO.glfwWindowShouldClose(form.windowID) != 0;
|
||||
}
|
||||
|
||||
public static unsafe void GLswapBuffers(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
internalIO.glfwSwapBuffers(form.windowID);
|
||||
}
|
||||
|
||||
public static unsafe void MakeContexCurrent(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
internalIO.glfwMakeContextCurrent(form.windowID);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetBufferData<T>(BufferType bufferType, T bufferData, BufferReadType readType) where T : IMat
|
||||
{
|
||||
int size = Marshal.SizeOf(bufferData);
|
||||
glBufferData(
|
||||
(int)bufferType,
|
||||
size,
|
||||
&bufferData,
|
||||
(int)readType);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferReadType readType) where T : struct, IConvertible
|
||||
{
|
||||
|
||||
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
|
||||
|
||||
fixed (void* bufferPtr = &bufferArray[0])
|
||||
glBufferData(
|
||||
(int)bufferType,
|
||||
size,
|
||||
bufferPtr,
|
||||
(int)readType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+731
-708
File diff suppressed because it is too large
Load Diff
@@ -1,9 +1,8 @@
|
||||
using StgSharp.Controlling;
|
||||
using StgSharp.Graphics;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public static unsafe partial class Graphic
|
||||
{
|
||||
@@ -39,7 +38,7 @@ namespace StgSharp
|
||||
/// and before other graphic method.
|
||||
/// </summary>
|
||||
/// <exception cref="Exception">The accident that program cannot attach GLFW to GLAD</exception>
|
||||
public static unsafe void InitGLAD(glLoader loader)
|
||||
public static unsafe void InitGLManager(glLoader loader)
|
||||
{
|
||||
|
||||
if (!isGLADset)
|
||||
@@ -63,13 +62,6 @@ namespace StgSharp
|
||||
throw new Exception("Do not init GLAD twice.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics.SSG
|
||||
{
|
||||
internal class Buffer
|
||||
{
|
||||
BufferType type;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal class Shader
|
||||
{
|
||||
public readonly uint programID;
|
||||
internal uint vertexShaderID;
|
||||
internal uint fragmentShaderID;
|
||||
|
||||
public Shader()
|
||||
{
|
||||
programID = Graphic.glCreateProgram();
|
||||
}
|
||||
|
||||
public void CompileVertexShader(string shaderCode)
|
||||
{
|
||||
vertexShaderID = Graphic.CompileShader(shaderCode, ShaderType.VERTEX);
|
||||
Graphic.glAttachShader(programID, vertexShaderID);
|
||||
}
|
||||
|
||||
public void CompileFragmentShader(string shaderCode)
|
||||
{
|
||||
fragmentShaderID = Graphic.CompileShader(shaderCode, ShaderType.FRAGMENT);
|
||||
Graphic.glAttachShader(programID, fragmentShaderID);
|
||||
}
|
||||
|
||||
public void Link()
|
||||
{
|
||||
if (internalIO.InternalLinkShaderProgram(programID) == 0)
|
||||
{
|
||||
IntPtr logPtr = internalIO.InternalReadLog();
|
||||
try
|
||||
{
|
||||
byte[] logByte = new byte[512];
|
||||
Marshal.Copy(logPtr, logByte, 0, 512);
|
||||
string log = Encoding.UTF8.GetString(logByte);
|
||||
log = log.Replace("\0", "");
|
||||
internalIO.InternalWriteLog(log, LogType.Error);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Use()
|
||||
{
|
||||
Graphic.glUseProgram(programID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal unsafe struct VertexArray : IDisposable
|
||||
{
|
||||
internal uint _bufferID;
|
||||
|
||||
public VertexArray()
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glGenVertexArrays(1, id);
|
||||
}
|
||||
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, _bufferID);
|
||||
}
|
||||
|
||||
public VertexArray(int n)
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glGenVertexArrays(n, id);
|
||||
}
|
||||
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, _bufferID);
|
||||
}
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
Graphic.glBindVertexArray(_bufferID);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glDeleteVertexArrays(1, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public unsafe struct VertexBuffer : IDisposable
|
||||
{
|
||||
internal uint _bufferID;
|
||||
|
||||
public VertexBuffer(int n)
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glGenBuffers(n, id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public uint BufferID
|
||||
{
|
||||
get { return _bufferID; }
|
||||
}
|
||||
public void Bind()
|
||||
{
|
||||
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, _bufferID);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = &_bufferID)
|
||||
{
|
||||
Graphic.glDeleteBuffers(1, id);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetData<T>(T bufferData, BufferReadType readType) where T : IMat
|
||||
{
|
||||
int size = Marshal.SizeOf(bufferData);
|
||||
Graphic.glBufferData(
|
||||
(int)BufferType.ARRAY_BUFFER,
|
||||
size,
|
||||
&bufferData,
|
||||
(int)readType);
|
||||
}
|
||||
|
||||
public void SetData<T>(T[] bufferArray, BufferReadType readType) where T : struct, IConvertible
|
||||
{
|
||||
|
||||
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
|
||||
|
||||
fixed (void* bufferPtr = &bufferArray[0])
|
||||
Graphic.glBufferData(
|
||||
(int)BufferType.ARRAY_BUFFER,
|
||||
size,
|
||||
bufferPtr,
|
||||
(int)readType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user