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

261 lines
8.9 KiB
C#

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="MainRenderStream.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp;
using StgSharp.Geometries;
using StgSharp.Graphics;
using StgSharp.Graphics.GL;
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp
{
public class MainRenderStream : glRenderStream
{
private ElementBuffer ebo;
private ShaderProgram shader;
private TextureGL texture;
private Uniform<float, float, float, float> color;
private Uniform<int> ourTexture;
private Uniform<int> ourTexture2;
private VertexArray vao;
private VertexBuffer vbo;
public MainRenderStream(ViewPort args) : base(args,(100,100,100))
{
}
protected override string[] InputPortsName => ["input_1"];
protected override string[] OutputPortsName => ["output_1"];
protected override void Deinit()
{
/**/
vao.Dispose();
vbo.Dispose();
ebo.Dispose();
/**/
}
protected override sealed void Init()
{
SetSizeLimit(800, 800, 800, 800);
FrameSizeCallback =
(IntPtr id, uint width, uint height) => glFunc.Viewport(0, 0, width, height);
shader = CreateShaderProgram();
Shader vertex = CreateShaderSegment(ShaderType.Vertex, 1);
Shader fragment = CreateShaderSegment(ShaderType.Fragment, 1);
vertex.Compile(0, Shader.FromCodeFile("Shader\\main.vert"));
vertex.AttachTo(0, shader);
fragment.Compile(0, Shader.FromCodeFile("Shader\\main.frag"));
fragment.AttachTo(0, shader);
shader.Link();
Triangle tri = new Triangle(
-0.5f, -0.3f,
0.5f, -0.5f,
0.0f, 0.5f
);
PlainCoordinate p = new PlainCoordinate(
PlainCoordinate.StandardPlainCoordination,
new Point(0,0,0),
new vec3d(1, 0, 130), new vec3d(0, 1, 130)
);
Rectangle rec = new Rectangle(
p,
-0.3f, 0.3f,
0.3f, 0.3f,
0.3f, -0.3f,
-0.3f, -0.3f
);
Polygon5 polygon = new Polygon5(
-0.4f, 0.5f,
0.5f, 0.5f,
0.6f, -0.5f,
0, -1,
-0.7f, -0.5f
);
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 };
vao.Bind(0);
ebo.SetValue(0, rec.VertexIndices, BufferUsage.StaticDraw);
vbo.WriteVectorData(0, rec.VertexStream, BufferUsage.StaticDraw);
VertexArray.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
vbo.WriteScalerData(1, texCoord, BufferUsage.StaticDraw);
VertexArray.SetVertexAttribute(2, 2, TypeCode.Single, false, 2 * sizeof(float), 0);
/**/
vbo.Unbind();
VertexArray.BindNull();
/**/
texture.Bind2D(0);
texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
texture.LoadTexture(0,Image.FromFile( "Shader\\Image1.png", null));
texture.Bind2D(1);
texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat);
texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest);
texture.LoadTexture(1, Image.FromFile( "Shader\\awesomeface.png", null));
shader.Use();
/**/
color = shader.GetUniform<float, float, float, float>("color");
ourTexture = shader.GetUniform<int>("ourTexture");
glFunc.SetUniformValue(ourTexture, 0);
ourTexture2 = shader.GetUniform<int>("ourTexture2");
glFunc.SetUniformValue(ourTexture2, 1);
/**/
NativeCameraViewTarget(
new vec3d(0, 0, 200),
new vec3d(0, 0, 100),
new vec3d(0, 1, 0)
);
NativeCameraViewRange(new Degree(10f), (0, 0), (50f, 50f));
NativeCameraUniform(shader, "camera");
TextureGL.Activate(TextureUnit.Unit0);
texture.Bind2D(0);
TextureGL.Activate(TextureUnit.Unit1);
texture.Bind2D(1);
}
protected override sealed void Main(
in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output
)
{
glFunc.ClearColor(0.333f, 0.555f, 0.555f, 1.0f);
//Console.WriteLine(1);
glFunc.Clear(MaskBufferBit.COLOR /**/| MaskBufferBit.DEPTH /**/ );
/**/
vao.Bind(0);
shader.Use();
/**/
/**/
/**/
/**/
float time = /* CurrentFrameCount, / /**/ 0.003f;
glFunc.SetUniformValue(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();
/**/
//Console.WriteLine(NativeCamera.Projection);
glFunc.DrawElements(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero);
VertexArray.BindNull();
/**/
BlueprintPipeline.SkipAll(output);
PollEvents();
SwapBuffers();
}
protected override void ProcessInput()
{
float movement = 0.005f;
if (InternalIO.glfwGetKey(CanvasHandle, (int)KeyboardKey.ESCAPE)
== KeyStatus.PRESS)
{
InternalIO.glfwSetWindowShouldClose(CanvasHandle, 1);
return;
}
if (InternalIO.glfwGetKey(CanvasHandle, (int)KeyboardKey.LeftShift)
== KeyStatus.PRESS)
{
movement = 0.002f;
}
if (InternalIO.glfwGetKey(this.CanvasHandle, (int)KeyboardKey.UP) == KeyStatus.PRESS)
{
//Console.WriteLine(8);
NativeCamera.Pitch(new Radius(movement));
}
else if (InternalIO.glfwGetKey(this.CanvasHandle, (int)KeyboardKey.DOWN) == KeyStatus.PRESS)
{
//Console.WriteLine(2);
NativeCamera.Pitch(new Radius(-movement));
}
if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_LEFT) == KeyStatus.PRESS)
{
//Console.WriteLine(4);
NativeCamera.Yaw(new Radius(-movement));
}
else if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_RIGHT) == KeyStatus.PRESS)
{
//Console.WriteLine(6);
NativeCamera.Yaw(new Radius(movement));
}
}
}
}