207 lines
6.4 KiB
C#
207 lines
6.4 KiB
C#
using StgSharp.Geometries;
|
|
using StgSharp.Graphics;
|
|
using StgSharp.Math;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Numerics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security;
|
|
using System.Text;
|
|
|
|
namespace StgSharp
|
|
{
|
|
|
|
internal static class Program
|
|
{
|
|
|
|
internal unsafe static void Main(string[] args)
|
|
{
|
|
|
|
Graphic.InitGL(3, 3);
|
|
|
|
Form f = Graphic.GLcreateWindow(
|
|
800, 600, "Test STG#!", IntPtr.Zero, null
|
|
);
|
|
Graphic.GLmakeContexCurrent(f);
|
|
internalIO.glfwSetFramebufferSizeCallback(f.windowID, FormSizeCallback);
|
|
|
|
GetApiRep();
|
|
Graphic.InitGLAD(Graphic.GetProcAddress);
|
|
|
|
string vertstr = File.ReadAllText("Shader\\vertex.vert", Encoding.ASCII);
|
|
byte[] vert = Encoding.ASCII.GetBytes(vertstr + '\0');
|
|
uint vertexShader;
|
|
fixed (byte* shaderSource = vert)
|
|
{
|
|
vertexShader = internalIO.InternalLoadShaderCode(shaderSource, GLFW.GL_VERTEX_SHADER);
|
|
if (vertexShader == 0)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
string fragstr = File.ReadAllText("Shader\\fragment.frag", Encoding.ASCII);
|
|
byte[] frag = Encoding.ASCII.GetBytes(fragstr + '\0');
|
|
|
|
uint fragmentShader;
|
|
fixed (byte* shaderSource = frag)
|
|
{
|
|
fragmentShader = internalIO.InternalLoadShaderCode(shaderSource, GLFW.GL_FRAGMENT_SHADER);
|
|
if (fragmentShader == 0)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
uint shaderProgram = Graphic.glCreateProgram();
|
|
|
|
Graphic.glAttachShader(shaderProgram, vertexShader);
|
|
Graphic.glAttachShader(shaderProgram, fragmentShader);
|
|
|
|
if (internalIO.InternalLinkShaderProgram(shaderProgram) == 0)
|
|
{
|
|
IntPtr log = internalIO.InternalReadLog();
|
|
Console.WriteLine(Marshal.PtrToStringAnsi(log));
|
|
}
|
|
|
|
Graphic.glDeleteShader(vertexShader);
|
|
Graphic.glDeleteShader(fragmentShader);
|
|
|
|
|
|
|
|
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
|
|
);
|
|
|
|
uint VAO;
|
|
Graphic.glGenVertexArrays(1, &VAO);
|
|
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, VAO);
|
|
Graphic.glBindVertexArray(VAO);
|
|
|
|
/*
|
|
// for triangle rendering
|
|
uint VBO;
|
|
Graphic.glGenBuffers(1, &VBO);
|
|
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, VBO);
|
|
fixed (void* data = &tri.vertexMap)
|
|
{
|
|
Graphic.glBufferData(GLFW.GL_ARRAY_BUFFER, 12 * sizeof(float), data, GLFW.GL_STATIC_DRAW);
|
|
}
|
|
|
|
Graphic.glVertexAttribPointer(0, 3, GLFW.GL_FLOAT, false, 4 * sizeof(float), (void*)0);
|
|
Graphic.glEnableVertexAttribArray(0);
|
|
|
|
/**/
|
|
|
|
/**/
|
|
// for rectangle rendering
|
|
|
|
uint VBO;
|
|
Graphic.glGenBuffers(1, &VBO);
|
|
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, VBO);
|
|
fixed (void* data = &rec.vertexMat)
|
|
{
|
|
Graphic.glBufferData(GLFW.GL_ARRAY_BUFFER, 16 * sizeof(float), data, GLFW.GL_STATIC_DRAW);
|
|
}
|
|
|
|
uint[] indices = new uint[6]
|
|
{
|
|
0,1,3,
|
|
1,2,3
|
|
};
|
|
|
|
uint EBO;
|
|
Graphic.glGenBuffers(1, &EBO);
|
|
Graphic.glBindBuffer(GLFW.GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
fixed (void* data = &indices[0])
|
|
{
|
|
Graphic.glBufferData(GLFW.GL_ELEMENT_ARRAY_BUFFER, indices.Length * sizeof(uint), data, GLFW.GL_STATIC_DRAW);
|
|
}
|
|
|
|
Graphic.glVertexAttribPointer(0, 3, GLFW.GL_FLOAT, false, 4 * sizeof(float), (void*)0);
|
|
Graphic.glEnableVertexAttribArray(0);
|
|
|
|
/**/
|
|
|
|
Graphic.glBindBuffer(GLFW.GL_ARRAY_BUFFER, 0);
|
|
Graphic.glBindVertexArray(0);
|
|
|
|
|
|
while (internalIO.glfwWindowShouldClose(f.windowID) == 0)
|
|
{
|
|
ProcessInput(f.windowID);
|
|
|
|
Graphic.glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
Graphic.glClear(GLFW.GL_COLOR_BUFFER_BIT);
|
|
|
|
Graphic.glUseProgram(shaderProgram);
|
|
Graphic.glBindVertexArray(VAO);
|
|
|
|
//Graphic.glDrawArrays(GLFW.GL_TRIANGLES, 0, 3);
|
|
Graphic.glDrawElements(GLFW.GL_TRIANGLES, 6, GLFW.GL_UNSIGNED_INT, (void*)0);
|
|
|
|
Graphic.glBindVertexArray(0);
|
|
|
|
internalIO.glfwSwapBuffers(f.windowID);
|
|
internalIO.glfwPollEvents();
|
|
}
|
|
|
|
Graphic.glDeleteVertexArrays(1, &VAO);
|
|
Graphic.glDeleteBuffers(1, &VBO);
|
|
Graphic.glDeleteBuffers(1, &EBO);
|
|
internalIO.glfwTerminate();
|
|
|
|
}
|
|
|
|
public static void FormSizeCallback(IntPtr window, int width, int height)
|
|
{
|
|
Graphic.glViewport(0, 0, width, height);
|
|
}
|
|
|
|
public static void ProcessInput(IntPtr windowPtr)
|
|
{
|
|
if (internalIO.glfwGetKey(windowPtr, (int)Control.Key.ESCAPE)
|
|
== KeyStatus.PRESS)
|
|
{
|
|
internalIO.glfwSetWindowShouldClose(windowPtr, 1);
|
|
}
|
|
}
|
|
|
|
[DllImport(internalIO.SSGC_libname, EntryPoint = "getApiReporter",
|
|
CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
|
internal extern static void internal_getApiReporter(IntPtr reporter);
|
|
|
|
internal static void GetApiRep()
|
|
{
|
|
showApiHandler handle = new showApiHandler(ShowApiAddress);
|
|
internal_getApiReporter(Marshal.GetFunctionPointerForDelegate(handle));
|
|
}
|
|
internal delegate void showApiHandler(ulong add);
|
|
internal static void ShowApiAddress(ulong address)
|
|
{
|
|
Console.WriteLine(address);
|
|
}
|
|
|
|
[DllImport(internalIO.SSGC_libname, EntryPoint = "printShaderCode",
|
|
CallingConvention = CallingConvention.Cdecl)]
|
|
internal extern unsafe static void PrintShader(byte** shadercode);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|