Files
stgsharp/源代码/StgSharpDebug/Test/MainRenderStream.cs
T

190 lines
6.1 KiB
C#

using StgSharp;
using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharpDebug
{
public sealed class MainRenderStream : glRenderStream
{
private Uniform<float, float, float, float> color;
private ElementBuffer ebo;
private Uniform<int> ourTexture;
private Uniform<int> ourTexture2;
private ShaderProgram shader;
private Texture texture;
private VertexArray vao;
private VertexBuffer vbo;
protected MainRenderStream(RenderStreamConstructArgs args) : base(args)
{
}
protected override void Deinit()
{
/**/
vao.Dispose();
vbo.Dispose();
ebo.Dispose();
/**/
}
protected override void Init()
{
SetSizeLimit(800, 800, 800, 800);
FrameSizeCallback =
(IntPtr id, uint width, uint height) =>
{
GL.Viewport(0, 0, width, height);
};
shader = CreateShaderProgram();
var vertex = CreateShaderSegment(ShaderType.Vertex, 1);
var fragment = CreateShaderSegment(ShaderType.Fragment, 1);
vertex.Compile(0, File.ReadAllText("Shader\\vertex.vert"));
vertex.AttachTo(0, shader);
fragment.Compile(0, File.ReadAllText("Shader\\fragment.frag"));
fragment.AttachTo(0, shader);
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.3f, 0.3f, 130.0f,
0.3f, 0.3f, 130.0f,
0.3f, -0.3f, 130.0f,
-0.3f, -0.3f, 130.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 = CreateVertexArray(1);
vbo = CreateVertexBuffer(2);
ebo = CreateElementBuffer(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, TypeCode.Single, false, 4 * sizeof(float), 0);
vbo.SetValueArray(1, texCoord, BufferUsage.STATIC_DRAW);
GL.SetVertexAttribute(2, 2, TypeCode.Single, false, 2 * sizeof(float), 0);
/**/
GL.BindBuffer(BufferType.ARRAY_BUFFER, glHandle.Zero);
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);
shader.Use();
/**/
color = shader.GetUniform<float, float, float, float>("color");
ourTexture = shader.GetUniform<int>("ourTexture");
GL.SetValue(ourTexture, 0);
ourTexture2 = shader.GetUniform<int>("ourTexture2");
GL.SetValue(ourTexture2, 1);
/**/
NativeCameraViewTarget(
new vec3d(0, 0, 200),
new vec3d(0, 0, 100),
new vec3d(0, 1, 0)
);
NativeCameraViewRange(new Degree(5),(0, 0), (50f, 50f));
NativeCameraUniform(shader, "NativeCamera");
}
protected override void Main()
{
GL.ClearColor(0.333f, 0.555f, 0.555f, 1.0f);
GL.Clear(MaskBufferBit.COLOR | MaskBufferBit.DEPTH);
/**/
vao.Bind(0);
shader.Use();
/**/
/**/
texture.Activate(TextureUnit.Unit0);
texture.Bind2D(0);
texture.Activate(TextureUnit.Unit1);
texture.Bind2D(1);
/**/
/**/
float time = /* CurrentFrameCount / /**/ 60.0f;
GL.SetValue(color,
0.5f + MathF.Cos(time),
0.5f + MathF.Sin(time * 0.66667f + (2 * Scaler.Pi / 3)),
0.5f + MathF.Sin(time * 0.33333f + (Scaler.Pi / 3)),
time
);
NativeCamera.SetAllUniforms();
/**/
GL.DrawElements(GepmetryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero);
//vao.BindNull();
/**/
}
protected override void ProcessInput()
{
float movement = 0.005f;
if (InternalIO.glfwGetKey(CanvasHandle, (int)KeyboardKey.ESCAPE)
== KeyStatus.PRESS)
{
InternalIO.glfwSetWindowShouldClose(CanvasHandle, 1);
}
if (InternalIO.glfwGetKey(this.CanvasHandle, GLconst.GLFW_KEY_UP) == KeyStatus.PRESS)
{
NativeCamera.Pitch(new Radius(movement));
}
else if (InternalIO.glfwGetKey(this.CanvasHandle, GLconst.GLFW_KEY_DOWN) == KeyStatus.PRESS)
{
NativeCamera.Pitch(new Radius(-movement));
}
if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_LEFT) == KeyStatus.PRESS)
{
NativeCamera.Yaw(new Radius(-movement));
}
else if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_RIGHT) == KeyStatus.PRESS)
{
NativeCamera.Yaw(new Radius(movement));
}
}
}
}