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

162 lines
4.3 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
{
internal static class Program
{
internal unsafe static void DebugMain(string[] args)
{
InternalIO.InternalWriteLog("\n\n\n", "Program start", LogType.Info);
Graphic.InitGL(4, 6);
//GL.glfw->WindowHint(GLFW.GLFW_DECORATED, GLFW.GL_FALSE);
//Form f = Graphic.CreateWindow(800, 600, "Test STG#!", IntPtr.Zero, null);
Form f = new Form(800, 600, "Hello,STG#!", IntPtr.Zero, null);
f.MakeAsCurrentContext();
f.SetSizeLimit(800, 600, 800, 600);
GL.SetFrameBufferSizeCallback(f, FormSizeCallback);
f.LoadGlapi();
Shader shader = new Shader();
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.4f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.6f, -0.5f, 0.0f,
-0.7f, -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
);
VertexArray vao = new VertexArray(1);
VertexBuffer vbo = new VertexBuffer(1);
ElementBuffer ebo = new ElementBuffer(1);
GL.gl->BindVertexArray(vao[0]);
vbo.SetData(0, polygon.VertexBuffer, BufferUsage.STATIC_DRAW);
ebo.SetData(0, polygon.Indices, BufferUsage.STATIC_DRAW);
GL.gl->VertexAttribPointer(0, 3, GLconst.FLOAT, false, 4 * sizeof(float), (void*)0);
GL.gl->EnableVertexAttribArray(0);
/**/
GL.gl->BindBuffer(GLconst.ARRAY_BUFFER, 0);
GL.gl->BindVertexArray(0);
/**/
while (InternalIO.glfw.WindowShouldClose(f.windowID) == 0)
{
ProcessInput(f.windowID);
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
GL.gl->Clear(GLconst.COLOR_BUFFER_BIT);
shader.Use();
GL.gl->BindVertexArray(vao._bufferID[0]);
GL.DrawElements(GepmetryType.TRIANGLES, 9, typeof(uint), IntPtr.Zero);
GL.gl->BindVertexArray(0);
InternalIO.glfw.SwapBuffers(f.windowID);
InternalIO.glfw.PollEvents();
InternalIO.glfw.PollEvents();
}
/**/
vao.Dispose();
vbo.Dispose();
ebo.Dispose();
/**/
/*
Graphic.glDeleteVertexArrays(1, &vao);
Graphic.glDeleteBuffers(1, &vao);
Graphic.glDeleteBuffers(1, &vao);
/**/
InternalIO.glfw.Terminate();
}
internal unsafe static void Main(string[] args)
{
try
{
DebugMain(args);
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(
ex.Message + "\n" + ex.StackTrace,
LogType.Error);
}
}
public static unsafe void FormSizeCallback(IntPtr window, int width, int height)
{
GL.gl->Viewport(0, 0, width, height);
}
public static unsafe void ProcessInput(IntPtr windowPtr)
{
if (InternalIO.glfw.GetKey(windowPtr, (int)Control.Key.ESCAPE)
== KeyStatus.PRESS)
{
InternalIO.glfw.SetWindowShouldClose(windowPtr, 1);
}
}
[DllImport(InternalIO.SSC_libname, EntryPoint = "printShaderCode",
CallingConvention = CallingConvention.Cdecl)]
internal extern unsafe static void PrintShader(byte** shadercode);
}
}