Files
stgsharp/源代码/StgSharp/Graphics/GL/glRenderStream.cs
T

113 lines
3.8 KiB
C#

using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
public abstract partial class glRenderStream: RenderStream
{
protected sealed override void NativeCameraViewTarget(vec3d position, vec3d direction, vec3d up)
{
NativeCamera.SetViewDirection(position, direction, up);
}
protected sealed override Uniform<Matrix44> NativeCameraUniform(ShaderProgram source, string name)
{
NativeCamera.GainAllUniforms(source, name);
return NativeCamera.convertedUniform;
}
protected IntPtr glContextHandle
{
get { return ContextHandle; }
set { ContextHandle = value; }
}
internal unsafe sealed override void CreateCanvas()
{
byte[] nameCode = Encoding.UTF8.GetBytes(name);
CanvasHandle = InternalIO.glfwCreateWindow(
Width,Height,nameCode,Monitor,IntPtr.Zero
);
NativeCamera = new Camera();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected sealed override void NativeCameraViewRange(Radius fieldOfRange,vec2d offset, (float frontDepth, float backDepth) viewDepth)
{
NativeCamera.SetViewRange(fieldOfRange,this.Size, offset, viewDepth);
}
/// <summary>
/// Init an OpenGl context if current form has not been binded to any context. and set the context binded to
/// current form as current OpenGL context.
/// </summary>
/// <exception cref="Exception">
/// Something goes wrong and StgSharp fails in creating a new OpenGL context.
/// </exception>
public unsafe void MakeAsCurrentContext()
{
if (CanvasHandle == default)
{
IntPtr windowPtr;
byte[] tittleBytes = Encoding.UTF8.GetBytes(name);
if (SharedCanvas == IntPtr.Zero)
{
windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, Monitor, IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfwCreateWindow(Width, Height, tittleBytes, Monitor, SharedCanvas);
}
if (((long)windowPtr == -1) || ((long)windowPtr == -2))
{
throw new Exception("Cannot create window instance!");
}
else
{
CanvasHandle = windowPtr;
}
ContextHandle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
GLcontext* contextID = (GLcontext*)ContextHandle;
*contextID = new GLcontext();
contextID->glGetString = (delegate*<uint, IntPtr>)IntPtr.Zero;
}
GL.api = (GLcontext*)ContextHandle;
InternalIO.glfwMakeContextCurrent(CanvasHandle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void SetSizeLimit(int minWidth, int minHeight, int maxWidth, int maxHeight)
{
InternalIO.glfwSetWindowSizeLimits(this.CanvasHandle, minWidth, minHeight, maxWidth, maxHeight);
}
internal sealed override void InternalInit()
{
CreateCanvas();
MakeAsCurrentContext();
this.LoadGLapi();
Init();
}
internal sealed override void InternalDraw()
{
MakeAsCurrentContext();
Main();
}
internal sealed override void InternalTerminate()
{
Deinit();
}
}
}