c-o
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// file="RenderStream.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.Graphics.GL;
|
||||
using StgSharp.Logic;
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public abstract class RenderStream : IConvertableToBlueprintNode
|
||||
{
|
||||
private ViewPort primeArgs;
|
||||
|
||||
private protected (int x, int y, int z) _coordStretch;
|
||||
|
||||
private Camera nativeCamera;
|
||||
|
||||
public ViewPort Port => primeArgs;
|
||||
|
||||
internal IntPtr CanvasHandle { get => primeArgs.ViewPortID; }
|
||||
|
||||
|
||||
internal IntPtr ContextHandle
|
||||
{
|
||||
get => primeArgs.GraphicHandle;
|
||||
private protected set => primeArgs.GraphicHandle = value;
|
||||
}
|
||||
|
||||
private IntPtr monitor;
|
||||
private IntPtr sharedCanvas;
|
||||
|
||||
internal TimeSpanProvider timeProvider;
|
||||
/*
|
||||
protected RenderStream(View v, bool isSharing)
|
||||
{
|
||||
if (v == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(v));
|
||||
}
|
||||
if (isSharing)
|
||||
{
|
||||
primeArgs = v;
|
||||
}
|
||||
else
|
||||
{
|
||||
View? newArgs = args.Clone() as View;
|
||||
primeArgs = newArgs;
|
||||
}
|
||||
|
||||
monitor = args.Monitor;
|
||||
_coordStretch = args.UnitCubeSize;
|
||||
NativeCamera = new Camera();
|
||||
}
|
||||
/**/
|
||||
|
||||
public RenderStream(ViewPort v, (int,int,int) unitCubeSize)
|
||||
{
|
||||
if (v == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(v));
|
||||
}
|
||||
primeArgs = v;
|
||||
|
||||
monitor = v!.Monitor;
|
||||
_coordStretch = unitCubeSize;
|
||||
NativeCamera = new Camera();
|
||||
}
|
||||
|
||||
|
||||
public FrameBufferSizeHandler FrameSizeCallback
|
||||
{
|
||||
set
|
||||
{
|
||||
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(value);
|
||||
InternalIO.glfwSetFramebufferSizeCallback(CanvasHandle, handlerPtr);
|
||||
}
|
||||
}
|
||||
|
||||
public vec2d GlobalCoordSize => ((Width * 1.0f) / _coordStretch.x, (Height * 1.0f) / _coordStretch.y);
|
||||
|
||||
public int Height
|
||||
{
|
||||
get => primeArgs.Height;
|
||||
internal set
|
||||
{
|
||||
primeArgs.Height = value;
|
||||
InternalIO.glfwSetWindowSize(this.CanvasHandle, Width, Height);
|
||||
}
|
||||
}
|
||||
|
||||
public IntPtr Monitor
|
||||
{
|
||||
get => monitor;
|
||||
internal set => monitor = value;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => primeArgs.Name;
|
||||
}
|
||||
|
||||
public IntPtr SharedCanvas
|
||||
{
|
||||
get => sharedCanvas;
|
||||
internal set => sharedCanvas = value;
|
||||
}
|
||||
|
||||
public (int width, int height) Size
|
||||
{
|
||||
get => (Width, Height);
|
||||
internal set
|
||||
{
|
||||
primeArgs.Height = value.width;
|
||||
primeArgs.Width = value.height;
|
||||
InternalIO.glfwSetWindowSize(this.CanvasHandle, Width, Height);
|
||||
}
|
||||
}
|
||||
|
||||
public int Width
|
||||
{
|
||||
get => primeArgs.Width;
|
||||
internal set
|
||||
{
|
||||
primeArgs.Width = value;
|
||||
InternalIO.glfwSetWindowSize(this.CanvasHandle, Width, Height);
|
||||
}
|
||||
}
|
||||
|
||||
protected internal Camera NativeCamera
|
||||
{
|
||||
get => nativeCamera;
|
||||
internal set => nativeCamera = value;
|
||||
}
|
||||
|
||||
protected int Frames
|
||||
{
|
||||
get => timeProvider.CurrentSpan;
|
||||
}
|
||||
|
||||
protected abstract string[] InputPortsName { get; }
|
||||
protected abstract string[] OutputPortsName { get; }
|
||||
|
||||
protected float TimePassed
|
||||
{
|
||||
get => timeProvider.CurrentSpan;
|
||||
}
|
||||
|
||||
protected TimeSpanProvider TimeProvider
|
||||
{
|
||||
get => timeProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the size limit of current form
|
||||
/// </summary>
|
||||
/// <param name="minWidth">Minimum width of current form</param>
|
||||
/// <param name="minHeight">Minimum height of current form</param>
|
||||
/// <param name="maxWidth">Maximum width of current form</param>
|
||||
/// <param name="maxHeight">Maximum height of current form</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract void SetSizeLimit(int minWidth, int minHeight, int maxWidth, int maxHeight);
|
||||
|
||||
public static TTarget ShareContextWith<TSource, TTarget>(TSource source)
|
||||
where TSource : RenderStream
|
||||
where TTarget : RenderStream
|
||||
{
|
||||
TTarget target = Activator.CreateInstance(typeof(TTarget), source.primeArgs, source._coordStretch) as TTarget;
|
||||
if (target == null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Failed in creating a new {typeof(TTarget).Name} instance when calling ShareContextWith."
|
||||
);
|
||||
}
|
||||
target.timeProvider = source.timeProvider;
|
||||
target.Init();
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
string[] IConvertableToBlueprintNode.InputInterfacesName => InputPortsName;
|
||||
|
||||
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution => InternalDraw;
|
||||
|
||||
string[] IConvertableToBlueprintNode.OutputInterfacesName => OutputPortsName;
|
||||
|
||||
#region native camera operation
|
||||
protected abstract void NativeCameraViewRange(Radius fieldOfRange, vec2d offset, (float frontDepth, float backDepth) viewDepth);
|
||||
protected abstract void NativeCameraViewTarget(vec3d position, vec3d direction, vec3d up);
|
||||
/// <summary>
|
||||
/// Get <see cref="Uniform{T}"/>(T is <see cref="Matrix44"/>) uniform from a shader.
|
||||
/// This uniform will be used as projection matrix of this camera.
|
||||
/// Additionally, for multi-shader rendering, each shader requires a uniform,
|
||||
/// so do not ignore return value of this method.
|
||||
/// </summary>
|
||||
/// <param name="source">Shader using this camera</param>
|
||||
/// <param name="name">ContextName of projection camera named in shader</param>
|
||||
/// <returns>
|
||||
/// <see cref="Uniform{T}"/>(T is <see cref="Matrix44"/>) representing
|
||||
/// projection matrix in shader program.
|
||||
/// </returns>
|
||||
protected abstract Uniform<Matrix44> NativeCameraUniform(ShaderProgram source, string name);
|
||||
#endregion
|
||||
|
||||
#region internal operation
|
||||
internal abstract void InternalDraw(
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output);
|
||||
internal abstract void InternalInit();
|
||||
internal abstract void InternalTerminate();
|
||||
#endregion
|
||||
|
||||
#region public operation
|
||||
protected abstract void Deinit();
|
||||
protected abstract void Init();
|
||||
protected abstract void Main(
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output);
|
||||
protected abstract void ProcessInput();
|
||||
|
||||
#endregion
|
||||
|
||||
#region public resource generator
|
||||
|
||||
protected abstract Shader CreateShaderSegment(ShaderType type, int count);
|
||||
protected abstract ShaderProgram CreateShaderProgram();
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user