OpenGL测试
This commit is contained in:
parent
4862d41d13
commit
ae42978590
@ -807,6 +807,7 @@ namespace CPF.Windows
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WindowsMessage.WM_DESTROY:
|
case WindowsMessage.WM_DESTROY:
|
||||||
|
Closed?.Invoke();
|
||||||
if (handle != IntPtr.Zero)
|
if (handle != IntPtr.Zero)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -857,7 +858,6 @@ namespace CPF.Windows
|
|||||||
RenderBitmap = null;
|
RenderBitmap = null;
|
||||||
}
|
}
|
||||||
handle = IntPtr.Zero;
|
handle = IntPtr.Zero;
|
||||||
Closed?.Invoke();
|
|
||||||
foreach (var item in invokeQueue)
|
foreach (var item in invokeQueue)
|
||||||
{
|
{
|
||||||
item.SendOrPostCallback(item.Data);
|
item.SendOrPostCallback(item.Data);
|
||||||
|
@ -12,7 +12,7 @@ namespace CPF.OpenGL
|
|||||||
/// 用来获取和保存Skia创建的GRContext
|
/// 用来获取和保存Skia创建的GRContext
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IDisposable GRContext { get; set; }
|
IDisposable GRContext { get; set; }
|
||||||
//void MakeCurrent();
|
void MakeCurrent();
|
||||||
//void SwapBuffers();
|
//void SwapBuffers();
|
||||||
//void Dispose();
|
//void Dispose();
|
||||||
//public abstract GRGlTextureInfo CreateTexture(SKSizeI textureSize);
|
//public abstract GRGlTextureInfo CreateTexture(SKSizeI textureSize);
|
||||||
@ -35,7 +35,7 @@ namespace CPF.OpenGL
|
|||||||
public static class OpenglEx
|
public static class OpenglEx
|
||||||
{
|
{
|
||||||
static bool loaded;
|
static bool loaded;
|
||||||
private static void Load(IGlContext context)
|
public static void Load(IGlContext context)
|
||||||
{
|
{
|
||||||
if (!loaded)
|
if (!loaded)
|
||||||
{
|
{
|
||||||
|
@ -3046,7 +3046,7 @@ namespace CPF
|
|||||||
/// 子级,一般自定义组件的时候使用
|
/// 子级,一般自定义组件的时候使用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[NotCpfProperty]
|
[NotCpfProperty]
|
||||||
internal protected virtual UIElementCollection Children
|
internal protected UIElementCollection Children
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netcoreapp3.0;net40;net5;net6;net8</TargetFrameworks>
|
<TargetFrameworks>netcoreapp3.0;net40;net5;net6</TargetFrameworks>
|
||||||
<ApplicationIcon />
|
<ApplicationIcon />
|
||||||
<StartupObject />
|
<StartupObject />
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
@ -102,11 +102,12 @@
|
|||||||
<ProjectReference Include="..\CPF.Linux\CPF.Linux.csproj" />
|
<ProjectReference Include="..\CPF.Linux\CPF.Linux.csproj" />
|
||||||
<ProjectReference Include="..\CPF.Mac\CPF.Mac.csproj" />
|
<ProjectReference Include="..\CPF.Mac\CPF.Mac.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<!--<ItemGroup Condition="'$(TargetFramework)'!='net40'">
|
<ItemGroup Condition="'$(TargetFramework)'!='net40'">
|
||||||
<PackageReference Include="System.Reactive">
|
<!--<PackageReference Include="System.Reactive">
|
||||||
<Version>5.0.0</Version>
|
<Version>5.0.0</Version>
|
||||||
</PackageReference>
|
</PackageReference>-->
|
||||||
</ItemGroup>-->
|
<PackageReference Condition="'$(TargetFramework)'!='netcoreapp3.0'" Include="Silk.NET.OpenGLES" Version="2.19.0" />
|
||||||
|
</ItemGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<DefineConstants Condition="'$(TargetFramework)'=='net40'">Net4</DefineConstants>
|
<DefineConstants Condition="'$(TargetFramework)'=='net40'">Net4</DefineConstants>
|
||||||
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
|
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
|
||||||
|
174
ConsoleApp1/GLView.cs
Normal file
174
ConsoleApp1/GLView.cs
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
#if !NET40&&!NETCOREAPP3_0
|
||||||
|
using CPF;
|
||||||
|
using CPF.Animation;
|
||||||
|
using CPF.Charts;
|
||||||
|
using CPF.Controls;
|
||||||
|
using CPF.Drawing;
|
||||||
|
using CPF.Shapes;
|
||||||
|
using CPF.Skia;
|
||||||
|
using CPF.Styling;
|
||||||
|
using CPF.Svg;
|
||||||
|
using CPF.OpenGL;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using SkiaSharp;
|
||||||
|
using Silk.NET.OpenGLES;
|
||||||
|
using static System.Net.Mime.MediaTypeNames;
|
||||||
|
|
||||||
|
namespace ConsoleApp1
|
||||||
|
{
|
||||||
|
[CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
|
||||||
|
public class GLView : UIElement
|
||||||
|
{
|
||||||
|
GL _gl;
|
||||||
|
public uint Id { get; set; }
|
||||||
|
|
||||||
|
public uint ColorBuffer { get; set; }
|
||||||
|
|
||||||
|
public uint DepthRenderBuffer { get; set; }
|
||||||
|
Size oldSize;
|
||||||
|
SKImage image;
|
||||||
|
|
||||||
|
uint vao;
|
||||||
|
uint shaderProgram;
|
||||||
|
//IGlContext context;
|
||||||
|
protected unsafe override void OnRender(DrawingContext dc)
|
||||||
|
{
|
||||||
|
var size1 = ActualSize;
|
||||||
|
if (size1.Width <= 0 || size1.Height <= 0 || DesignMode)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var size = new PixelSize((int)Math.Round(size1.Width * Root.RenderScaling), (int)Math.Round(size1.Height * Root.RenderScaling));
|
||||||
|
var skia = dc as SkiaDrawingContext;
|
||||||
|
var gl = skia.GlContext;
|
||||||
|
//context = gl;
|
||||||
|
OpenglEx.Load(gl);
|
||||||
|
if (_gl == null)
|
||||||
|
{
|
||||||
|
_gl = GL.GetApi(gl.GetProcAddress);
|
||||||
|
Id = _gl.GenFramebuffer();
|
||||||
|
ColorBuffer = _gl.GenTexture();
|
||||||
|
DepthRenderBuffer = _gl.GenRenderbuffer();
|
||||||
|
|
||||||
|
_gl.BindTexture(GLEnum.Texture2D, ColorBuffer);
|
||||||
|
|
||||||
|
_gl.TexParameter(GLEnum.Texture2D, GLEnum.TextureMinFilter, (int)GLEnum.Linear);
|
||||||
|
_gl.TexParameter(GLEnum.Texture2D, GLEnum.TextureMagFilter, (int)GLEnum.Linear);
|
||||||
|
|
||||||
|
_gl.BindTexture(GLEnum.Texture2D, 0);
|
||||||
|
|
||||||
|
|
||||||
|
_gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
float[] vertices = {
|
||||||
|
-0.5f, -0.5f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.0f,
|
||||||
|
0.0f, 0.5f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
_gl.GenBuffers(1, out uint vbo);
|
||||||
|
_gl.BindBuffer(GLEnum.ArrayBuffer, vbo);
|
||||||
|
_gl.BufferData<float>(GLEnum.ArrayBuffer, (nuint)vertices.Length * sizeof(float), vertices, GLEnum.StaticDraw);
|
||||||
|
|
||||||
|
_gl.GenVertexArrays(1, out vao);
|
||||||
|
_gl.BindVertexArray(vao);
|
||||||
|
_gl.VertexAttribPointer(0, 3, GLEnum.Float, false, 3 * sizeof(float), null);
|
||||||
|
_gl.EnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
string vertexShaderSource = @"#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
string fragmentShaderSource = @"#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
uint vertexShader = _gl.CreateShader(GLEnum.VertexShader);
|
||||||
|
_gl.ShaderSource(vertexShader, vertexShaderSource);
|
||||||
|
_gl.CompileShader(vertexShader);
|
||||||
|
|
||||||
|
uint fragmentShader = _gl.CreateShader(GLEnum.FragmentShader);
|
||||||
|
_gl.ShaderSource(fragmentShader, fragmentShaderSource);
|
||||||
|
_gl.CompileShader(fragmentShader);
|
||||||
|
|
||||||
|
shaderProgram = _gl.CreateProgram();
|
||||||
|
_gl.AttachShader(shaderProgram, vertexShader);
|
||||||
|
_gl.AttachShader(shaderProgram, fragmentShader);
|
||||||
|
_gl.LinkProgram(shaderProgram);
|
||||||
|
|
||||||
|
_gl.DeleteShader(vertexShader);
|
||||||
|
_gl.DeleteShader(fragmentShader);
|
||||||
|
}
|
||||||
|
if (size1 != oldSize)
|
||||||
|
{
|
||||||
|
oldSize = size1;
|
||||||
|
|
||||||
|
_gl.BindTexture(GLEnum.Texture2D, ColorBuffer);
|
||||||
|
_gl.TexImage2D(GLEnum.Texture2D, 0, (int)GLEnum.Rgba, (uint)size.Width, (uint)size.Height, 0, GLEnum.Rgb, GLEnum.UnsignedByte, null);
|
||||||
|
_gl.BindTexture(GLEnum.Texture2D, 0);
|
||||||
|
|
||||||
|
_gl.BindRenderbuffer(GLEnum.Renderbuffer, DepthRenderBuffer);
|
||||||
|
_gl.RenderbufferStorage(GLEnum.Renderbuffer, GLEnum.Depth32fStencil8, (uint)size.Width, (uint)size.Height);
|
||||||
|
|
||||||
|
_gl.BindRenderbuffer(GLEnum.Renderbuffer, 0);
|
||||||
|
|
||||||
|
_gl.BindFramebuffer(GLEnum.Framebuffer, Id);
|
||||||
|
|
||||||
|
_gl.FramebufferTexture2D(GLEnum.Framebuffer, GLEnum.ColorAttachment0, GLEnum.Texture2D, ColorBuffer, 0);
|
||||||
|
|
||||||
|
_gl.FramebufferRenderbuffer(GLEnum.Framebuffer, GLEnum.DepthStencilAttachment, GLEnum.Renderbuffer, DepthRenderBuffer);
|
||||||
|
_gl.BindFramebuffer(GLEnum.Framebuffer, 0);
|
||||||
|
|
||||||
|
|
||||||
|
GRBackendTexture backendTexture = new GRBackendTexture((int)(size.Width / Root.RenderScaling), (int)(size.Height / Root.RenderScaling), false, new GRGlTextureInfo(0x0DE1, (uint)ColorBuffer, SKColorType.Rgba8888.ToGlSizedFormat()));
|
||||||
|
|
||||||
|
image = SKImage.FromTexture((GRContext)skia.GlContext.GRContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
|
||||||
|
}
|
||||||
|
|
||||||
|
_gl.BindFramebuffer(FramebufferTarget.Framebuffer, Id);
|
||||||
|
var vp = new float[4];
|
||||||
|
gl.GetFloatv(GlConsts.GL_VIEWPORT, vp);
|
||||||
|
_gl.Viewport(0, 0, (uint)size.Width, (uint)size.Height);
|
||||||
|
OnGlRender(gl, size);
|
||||||
|
_gl.Viewport((int)vp[0], (int)vp[1], (uint)vp[2], (uint)vp[3]);
|
||||||
|
skia.SKCanvas.DrawImage(image, 0, 0);
|
||||||
|
base.OnRender(dc);
|
||||||
|
}
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
protected void OnGlRender(IGlContext gl, PixelSize viewPort)
|
||||||
|
{
|
||||||
|
//_gl.ClearColor(0.5f, 0, 0, 0.5f);
|
||||||
|
//_gl.Clear(ClearBufferMask.ColorBufferBit);
|
||||||
|
_gl.UseProgram(shaderProgram);
|
||||||
|
_gl.BindVertexArray(vao);
|
||||||
|
_gl.DrawArrays(GLEnum.Triangles, 0, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (_gl != null)
|
||||||
|
{
|
||||||
|
OpenglEx.DeleteFramebuffers(null, 1, new int[] { (int)Id });
|
||||||
|
OpenglEx.DeleteTextures(null, 1, new int[] { (int)ColorBuffer });
|
||||||
|
OpenglEx.DeleteRenderbuffers(null, 1, new int[] { (int)DepthRenderBuffer });
|
||||||
|
//_gl.DeleteFramebuffer(Id);
|
||||||
|
//_gl.DeleteTexture(ColorBuffer);
|
||||||
|
//_gl.DeleteRenderbuffer(DepthRenderBuffer);
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
@ -38,7 +38,7 @@ namespace ConsoleApp1
|
|||||||
#if Net4
|
#if Net4
|
||||||
(OperatingSystemType.Windows, new WindowsPlatform(), new CPF.GDIPlus.GDIPlusDrawingFactory { ClearType = true })
|
(OperatingSystemType.Windows, new WindowsPlatform(), new CPF.GDIPlus.GDIPlusDrawingFactory { ClearType = true })
|
||||||
#else
|
#else
|
||||||
(OperatingSystemType.Windows, new WindowsPlatform(false), new SkiaDrawingFactory { })
|
(OperatingSystemType.Windows, new WindowsPlatform(false), new SkiaDrawingFactory { UseGPU=true })
|
||||||
, (OperatingSystemType.OSX, new CPF.Mac.MacPlatform(), new SkiaDrawingFactory { UseGPU = false })
|
, (OperatingSystemType.OSX, new CPF.Mac.MacPlatform(), new SkiaDrawingFactory { UseGPU = false })
|
||||||
, (OperatingSystemType.Linux, new CPF.Linux.LinuxPlatform(), new SkiaDrawingFactory { UseGPU = false })
|
, (OperatingSystemType.Linux, new CPF.Linux.LinuxPlatform(), new SkiaDrawingFactory { UseGPU = false })
|
||||||
#endif
|
#endif
|
||||||
@ -88,8 +88,20 @@ namespace ConsoleApp1
|
|||||||
////Application.AllowDeveloperTool = false;
|
////Application.AllowDeveloperTool = false;
|
||||||
////Application.DisablePopupClose = true;
|
////Application.DisablePopupClose = true;
|
||||||
|
|
||||||
//Console.SetOut(new tr());
|
|
||||||
//Console.WriteLine("123");
|
//data aa = new data();
|
||||||
|
//aa.test.test.test.Name = "11111";
|
||||||
|
//model.Test1.test = aa;
|
||||||
|
|
||||||
|
//var test1 = new TextBlock
|
||||||
|
//{
|
||||||
|
// [nameof(TextBlock.Text)] = new CPF.Obx<MainModel>(a => a.Test1.test.test.test.test.Name),
|
||||||
|
//};
|
||||||
|
//test1.DataContext=model;
|
||||||
|
|
||||||
|
//aa = new data();
|
||||||
|
//aa.test.test.test.Name = "666666";
|
||||||
|
//model.Test1.test = aa;
|
||||||
Application.Run(new Window2 { DataContext = model, CommandContext = model });
|
Application.Run(new Window2 { DataContext = model, CommandContext = model });
|
||||||
|
|
||||||
//Application.Run(new Window
|
//Application.Run(new Window
|
||||||
@ -139,7 +151,7 @@ namespace ConsoleApp1
|
|||||||
}
|
}
|
||||||
class tr : TextWriter
|
class tr : TextWriter
|
||||||
{
|
{
|
||||||
public override Encoding Encoding => Encoding.Unicode;
|
public override Encoding Encoding => Encoding.Unicode;
|
||||||
|
|
||||||
public override void Write(string value)
|
public override void Write(string value)
|
||||||
{
|
{
|
||||||
|
@ -738,15 +738,17 @@ namespace ConsoleApp1
|
|||||||
MarginTop = 450,
|
MarginTop = 450,
|
||||||
Height = 58,
|
Height = 58,
|
||||||
Width = 121,
|
Width = 121,
|
||||||
},//#if !Net4
|
},
|
||||||
//new CPF.Skia.GlView
|
#if !Net4&&!NETCOREAPP3_0
|
||||||
//{
|
new GLView
|
||||||
// MarginRight = 56,
|
{
|
||||||
// MarginTop = 44,
|
MarginRight = 56,
|
||||||
// Height = 132,
|
MarginTop = 44,
|
||||||
// Width = 151,
|
Height = 132,
|
||||||
//},
|
Width = 151,
|
||||||
//#endif
|
[nameof(GLView.DoubleClick)]=new CommandDescribe((s,e)=>{ s.Dispose(); }),
|
||||||
|
},
|
||||||
|
#endif
|
||||||
new Button
|
new Button
|
||||||
{
|
{
|
||||||
Commands =
|
Commands =
|
||||||
@ -2444,14 +2446,14 @@ new TabItemTemplate{
|
|||||||
BindingMode.OneWay),
|
BindingMode.OneWay),
|
||||||
Name = "hmbb"
|
Name = "hmbb"
|
||||||
},
|
},
|
||||||
new TextBox
|
//new TextBox
|
||||||
{
|
//{
|
||||||
Width = 130,
|
// Width = 130,
|
||||||
Height= 60,
|
// Height= 60,
|
||||||
Background =Color.Gray,
|
// Background =Color.Gray,
|
||||||
[nameof(TextBox.Text)]= new Obx<MainModel>(a => a.Test1.test.test.test.test.Name,
|
// [nameof(TextBox.Text)]= new Obx<MainModel>(a => a.Test1.test.test.test.test.Name,
|
||||||
BindingMode.OneWayToSource),
|
// BindingMode.OneWayToSource),
|
||||||
},
|
//},
|
||||||
new Button
|
new Button
|
||||||
{
|
{
|
||||||
Content="创建对象",
|
Content="创建对象",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user