daily update on 20240513 by nitload youmu, instancing render added
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using StgSharp.Geometries;
|
||||
using StgSharp.Graphics;
|
||||
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 ParticleStream : glRenderStream
|
||||
{
|
||||
public ParticleStream(RenderStreamConstructArgs args) : base(args)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override string[] InputPortsName => ["input_1"];
|
||||
|
||||
protected override string[] OutputPortsName => ["output_1"];
|
||||
|
||||
protected override void Deinit()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
ShaderProgram s;
|
||||
PlainInstancingBuffer<ParticleOnPlain> buffer;
|
||||
VertexBuffer vbo;
|
||||
VertexArray vao;
|
||||
ElementBuffer ebo;
|
||||
|
||||
|
||||
|
||||
protected override void Init()
|
||||
{
|
||||
buffer = new PlainInstancingBuffer<ParticleOnPlain>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
for (int t = 0; t < 10; t++)
|
||||
{
|
||||
ParticleOnPlain p = new ParticleOnPlain(buffer);
|
||||
((IInstancing<ParticleOnPlain>)p).Coord = new vec3d(
|
||||
(-5 + i) * 0.2f + 0.1f, (-5 + t) * 0.2f + 0.1f, 0);
|
||||
((IInstancing<ParticleOnPlain>)p).Scale = Scaler.MinOf(i,t) * 0.2f;
|
||||
((IInstancing<ParticleOnPlain>)p).Motion = GeometryMotion.DefaultMotion;
|
||||
}
|
||||
}
|
||||
|
||||
SetSizeLimit(800, 800, 800, 800);
|
||||
|
||||
FrameSizeCallback =
|
||||
(IntPtr id, uint width, uint height) => GL.Viewport(0, 0, width, height);
|
||||
|
||||
s = CreateShaderProgram();
|
||||
Shader pvs = CreateShaderSegment(ShaderType.Vertex ,1);
|
||||
Shader pfs = CreateShaderSegment(ShaderType.Fragment ,1);
|
||||
|
||||
pvs.Compile(0, Shader.FromCodeFile("Shader\\particle.vert"));
|
||||
pvs.AttachTo(0,s);
|
||||
pfs.Compile(0, Shader.FromCodeFile("Shader\\particle.frag"));
|
||||
pfs.AttachTo(0,s);
|
||||
s.Link();
|
||||
|
||||
vbo = CreateVertexBuffer(3);
|
||||
vao = CreateVertexArray(1);
|
||||
ebo = CreateElementBuffer(1);
|
||||
vbo.Bind(0);
|
||||
|
||||
Rectangle rec = new Rectangle
|
||||
(
|
||||
0.05f,0.05f,0.0f,
|
||||
0.05f,-0.05f,0.0f,
|
||||
-0.05f,-0.05f,0.0f,
|
||||
-0.05f,0.05f,0.0f
|
||||
);
|
||||
|
||||
vao.Bind(0);
|
||||
vbo.WriteMatrixData(0, rec.vertexMat, BufferUsage.STATIC_DRAW);
|
||||
ebo.SetValue(0, rec.Indices, BufferUsage.STATIC_DRAW);
|
||||
vao.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float),0);
|
||||
|
||||
vbo.WriteVectorData(1, buffer.CoordSpan,BufferUsage.STATIC_DRAW);
|
||||
vao.SetVertexAttribute(1, 3, TypeCode.Single, false, 4 * sizeof(float), 0);
|
||||
vao.SetVertexAttribute(2, 1, TypeCode.Single, false, 4 * sizeof(float), 3 * sizeof(float));
|
||||
|
||||
GL.VertexAttributeDivisor(1, 1);
|
||||
GL.VertexAttributeDivisor(2, 1);
|
||||
|
||||
vbo.WriteScalerData(2, buffer.ScalingSpan, BufferUsage.STATIC_DRAW);
|
||||
vao.SetVertexAttribute(3,1,TypeCode.Single,false,sizeof(float),0);
|
||||
|
||||
GL.VertexAttributeDivisor(3, 1);
|
||||
|
||||
vbo.Unbind();
|
||||
vao.BindNull();
|
||||
|
||||
NativeCameraViewTarget
|
||||
(
|
||||
(0,0,-100),
|
||||
(0,0,0),
|
||||
(0,1,0)
|
||||
);
|
||||
NativeCameraViewRange(new Degree(1.7f),Vec2d.Zero,(50,50));
|
||||
NativeCameraUniform(s,"Camera");
|
||||
}
|
||||
|
||||
protected override void Main(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output)
|
||||
{
|
||||
|
||||
GL.ClearColor(0.6f,0.1f,0.3f,1);
|
||||
GL.Clear(MaskBufferBit.COLOR | MaskBufferBit.DEPTH);
|
||||
|
||||
vao.Bind(0);
|
||||
s.Use();
|
||||
|
||||
NativeCamera.SetAllUniforms();
|
||||
|
||||
GL.DrawElementsInstanced(
|
||||
GeometryType.TRIANGLES, 6, TypeCode.UInt32,IntPtr.Zero,100
|
||||
);
|
||||
|
||||
vao.BindNull();
|
||||
|
||||
BlueprintPipeline.SkipAll(output);
|
||||
|
||||
SwapBuffers();
|
||||
PollEvents();
|
||||
|
||||
}
|
||||
|
||||
protected override void ProcessInput()
|
||||
{
|
||||
if (CheckKey(KeyboardKey.ESCAPE) == KeyStatus.PRESS)
|
||||
{
|
||||
InternalIO.glfwSetWindowShouldClose(CanvasHandle, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user