Files
stgsharp/源代码/StgSharpDebug/Program.cs
T

220 lines
6.0 KiB
C#

using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.Math;
using StgSharp.Math.Linear;
using System;
using System.Diagnostics;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp
{
public class StgSharpDemoForm : Form
{
Uniform<float, float, float> color;
ElementBuffer ebo;
Uniform<int> ourTexture;
Uniform<int> ourTexture2;
Shader shader;
Texture texture;
VertexArray vao;
VertexBuffer vbo;
public static unsafe void ProcessInput(IntPtr windowPtr)
{
if (InternalIO.glfw.GetKey(windowPtr, (int)Operation.Key.ESCAPE)
== KeyStatus.PRESS)
{
InternalIO.glfw.SetWindowShouldClose(windowPtr, 1);
}
}
public override void Deinit(object[] args, IglFunc GL)
{
/**/
vao.Dispose();
vbo.Dispose();
ebo.Dispose();
/**/
InternalIO.glfw.Terminate();
}
public unsafe void FormSizeCallback(IntPtr window, int width, int height)
{
GL.Viewport(0, 0, width, height);
}
public override void Init(object[] args, IglFunc GL)
{
InternalIO.InternalWriteLog("\n\n\n", "Program start", LogType.Info);
Platform.InitGL(4, 6);
InitForm(800, 600, "Hello, STG#!", IntPtr.Zero, null);
MakeAsCurrentContext();
SetSizeLimit(800, 600, 800, 600);
GL.SetFrameBufferSizeCallback(windowID, FormSizeCallback);
this.LoadGLapi(null);
shader = CreateShader();
shader.CompileVertexShader(
File.ReadAllText("Shader\\vertex.vert")
);
shader.CompileFragmentShader(
File.ReadAllText("Shader\\fragment.frag")
);
shader.Link();
Triangle tri = new Triangle(
-0.5f, -0.3f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
);
Rectangle rec = new Rectangle(
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
);
Polygon5 polygon = new Polygon5(
-0.4f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.6f, -0.5f, 0.0f,
0, -1, 0,
-0.7f, -0.5f, 0.0f
);
vao = new VertexArray(1);
vbo = new VertexBuffer(2);
ebo = new ElementBuffer(1);
texture = CreateTexture(2);
float[] texCoord = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f };
GL.BindVertexArray(vao[0]);
ebo.SetValue(0, rec.Indices, BufferUsage.STATIC_DRAW);
vbo.SetValue(0, rec.VertexBuffer, BufferUsage.STATIC_DRAW);
GL.SetVertexAttribute(0, 3, typeof(float), false, 4 * sizeof(float), 0);
vbo.SetValueArray(1, texCoord, BufferUsage.STATIC_DRAW);
GL.SetVertexAttribute(2, 2, typeof(float), false, 2 * sizeof(float), 0);
/**/
GL.BindBuffer(BufferType.ARRAY_BUFFER, 0);
vao.BindNull();
/**/
texture.Bind2D(0);
texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
texture.LoadTexture(0, "Shader\\Image1.png", null);
texture.Bind2D(1);
texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
texture.LoadTexture(1, "Shader\\awesomeface.png", null);
//color = GetUniform<float, float, float>(shader, "color");
shader.Use();
/**/
//ourTexture = GetUniform<int>(shader, "ourTexture");
GL.SetValue(ourTexture, 0);
//ourTexture2 = GetUniform<int>(shader, "ourTexture2");
GL.SetValue(ourTexture2, 1);
/**/
}
public override void OnRenderFrame(object[] args, IglFunc GL)
{
}
public override void OnUpdateFrame(object[] args, IglFunc GL)
{
ProcessInput(windowID);
GL.ClearColor(0.333f, 0.555f, 0.555f, 1.0f);
//GL.api.Clear(GLconst.COLOR_BUFFER_BIT);
GL.Clear(MaskBufferBit.COLOR);
vao.Bind(0);
shader.Use();
/**/
texture.Activate(TextureUnit.Unit0);
texture.Bind2D(0);
texture.Activate(TextureUnit.Unit1);
texture.Bind2D(1);
/**/
float time = (float)InternalIO.glfw.GetTime();
GL.SetValue(color,
0.5f + MathF.Sin(time),
0.5f + MathF.Sin(time/* + 0.667f * 3.14f/**/),
0.5f + MathF.Sin(time/* - 0.667f * 3.14f/**/)
);
vao.Bind(0);
AlignFrame(this, () =>
{
GL.DrawElements(GepmetryType.TRIANGLES, 6, typeof(uint), IntPtr.Zero);
vao.BindNull();
InternalIO.glfw.SwapBuffers(windowID);
InternalIO.glfw.PollEvents();
});
}
}
internal static class Program
{
internal unsafe static void Main(string[] args)
{
try
{
StgSharpDemoForm demoForm = new StgSharpDemoForm();
demoForm.Show(null);
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(
ex.Message + "\n" + ex.StackTrace,
LogType.Error);
}
}
[DllImport(InternalIO.SSC_libname, EntryPoint = "printShaderCode",
CallingConvention = CallingConvention.Cdecl)]
internal extern unsafe static void PrintShader(byte** shadercode);
}
}