SharpGL WPF 不使用 gl.DrawArrays() 和 gl.VertexPointer() 绘制
SharpGL WPF Not drawing with gl.DrawArrays() and gl.VertexPointer()
我正在尝试使用 SharpGL 在使用 gl.VertexPointer() 和 gl.DrawArrays() 的 WPF 应用程序中呈现。但我无法让它渲染一个正方形。背景变为绿色,我可以在 WPF 面板的左下方看到 FPS 绘图。添加正方形代码时,FPS 文本也消失了,我只有一个空白屏幕。
我正在做与我在 C++ 项目中所做的完全相同的事情,它工作得很好。我不知道我遗漏了什么或做错了什么。
XAML
<Window x:Class="NodePlusPlus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NodePlusPlus"
xmlns:gl="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="900" Width="1600" Background="#FF202020">
<Grid x:Name="MainGrid">
<gl:OpenGLControl x:Name="glPanel" DrawFPS="True" />
</Grid>
</Window>
C#
public MainWindow()
{
InitializeComponent();
glPanel.OpenGLInitialized += glPanel_OpenGLInitialized;
glPanel.OpenGLDraw += glPanel_OpenGLDraw;
glPanel.Resized += glPanel_Resized;
}
private void glPanel_Resized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Ortho2D(0, Width, 0, Height);
}
private void glPanel_OpenGLInitialized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_FASTEST);
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.Enable(OpenGL.GL_CULL_FACE);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Enable(OpenGL.GL_BLEND);
gl.Enable(OpenGL.GL_DEPTH_TEST);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
gl.ClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
private void glPanel_OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
// Clear the color and depth buffers
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
// Build a rectangle
float Width = 100.0f;
float Height = 100.0f;
float[] vertices = new float[18];
vertices[0] = 0; vertices[1] = 0; vertices[2] = 0.0f;
vertices[3] = 0; vertices[4] = Height; vertices[5] = 0.0f;
vertices[6] = Width; vertices[7] = Height; vertices[8] = 0.0f;
vertices[9] = 0; vertices[10] = 0; vertices[11] = 0.0f;
vertices[12] = Width; vertices[13] = Height; vertices[14] = 0.0f;
vertices[15] = Width; vertices[16] = 0; vertices[17] = 0.0f;
// If I remove this whole Begin()-End() section, it will render the Open GL FPS. With this draw code here, it does not render the FPS.
gl.Begin(OpenGL.GL_TRIANGLES);
gl.Translate(0.0f, 0.0f, 0.0f);
gl.Color(0.0f, 0.0f, 0.0f, 1.0f);
gl.VertexPointer(3, 0, vertices);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);
gl.End();
gl.Flush();
}
有Draw代码...
没有绘制代码...
如@BDL所述,在这种情况下不使用glBegin()
和glEnd()
。
如@Rabbid76 所述,glEnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY)
应该删除,因为我没有使用纹理坐标。
感谢大家的帮助!
在具体示例中还需要删除 gl.Enable(OpenGL.GL_CULL_FACE)
以绘制我的正方形。正如@Rabbid76 提到的,默认的绕组顺序是逆时针的。我的顶点顺时针旋转。
最终结果
private void glPanel_OpenGLInitialized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_FASTEST);
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Enable(OpenGL.GL_BLEND);
gl.Enable(OpenGL.GL_DEPTH_TEST);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.ClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
private void glPanel_OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
// Clear the color and depth buffers
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
// Build a rectangle
float Width = 100.0f;
float Height = 100.0f;
float[] vertices = new float[18];
vertices[0] = 0; vertices[1] = 0; vertices[2] = 0.0f;
vertices[3] = 0; vertices[4] = Height; vertices[5] = 0.0f;
vertices[6] = Width; vertices[7] = Height; vertices[8] = 0.0f;
vertices[9] = 0; vertices[10] = 0; vertices[11] = 0.0f;
vertices[12] = Width; vertices[13] = Height; vertices[14] = 0.0f;
vertices[15] = Width; vertices[16] = 0; vertices[17] = 0.0f;
gl.Translate(0.0f, 0.0f, 0.0f);
gl.Color(0.0f, 0.0f, 0.0f, 1.0f);
gl.VertexPointer(3, 0, vertices);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);
gl.Flush();
}
我正在尝试使用 SharpGL 在使用 gl.VertexPointer() 和 gl.DrawArrays() 的 WPF 应用程序中呈现。但我无法让它渲染一个正方形。背景变为绿色,我可以在 WPF 面板的左下方看到 FPS 绘图。添加正方形代码时,FPS 文本也消失了,我只有一个空白屏幕。
我正在做与我在 C++ 项目中所做的完全相同的事情,它工作得很好。我不知道我遗漏了什么或做错了什么。
XAML
<Window x:Class="NodePlusPlus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NodePlusPlus"
xmlns:gl="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="900" Width="1600" Background="#FF202020">
<Grid x:Name="MainGrid">
<gl:OpenGLControl x:Name="glPanel" DrawFPS="True" />
</Grid>
</Window>
C#
public MainWindow()
{
InitializeComponent();
glPanel.OpenGLInitialized += glPanel_OpenGLInitialized;
glPanel.OpenGLDraw += glPanel_OpenGLDraw;
glPanel.Resized += glPanel_Resized;
}
private void glPanel_Resized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Ortho2D(0, Width, 0, Height);
}
private void glPanel_OpenGLInitialized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_FASTEST);
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.Enable(OpenGL.GL_CULL_FACE);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Enable(OpenGL.GL_BLEND);
gl.Enable(OpenGL.GL_DEPTH_TEST);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);
gl.ClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
private void glPanel_OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
// Clear the color and depth buffers
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
// Build a rectangle
float Width = 100.0f;
float Height = 100.0f;
float[] vertices = new float[18];
vertices[0] = 0; vertices[1] = 0; vertices[2] = 0.0f;
vertices[3] = 0; vertices[4] = Height; vertices[5] = 0.0f;
vertices[6] = Width; vertices[7] = Height; vertices[8] = 0.0f;
vertices[9] = 0; vertices[10] = 0; vertices[11] = 0.0f;
vertices[12] = Width; vertices[13] = Height; vertices[14] = 0.0f;
vertices[15] = Width; vertices[16] = 0; vertices[17] = 0.0f;
// If I remove this whole Begin()-End() section, it will render the Open GL FPS. With this draw code here, it does not render the FPS.
gl.Begin(OpenGL.GL_TRIANGLES);
gl.Translate(0.0f, 0.0f, 0.0f);
gl.Color(0.0f, 0.0f, 0.0f, 1.0f);
gl.VertexPointer(3, 0, vertices);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);
gl.End();
gl.Flush();
}
有Draw代码...
没有绘制代码...
如@BDL所述,在这种情况下不使用glBegin()
和glEnd()
。
如@Rabbid76 所述,glEnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY)
应该删除,因为我没有使用纹理坐标。
感谢大家的帮助!
在具体示例中还需要删除 gl.Enable(OpenGL.GL_CULL_FACE)
以绘制我的正方形。正如@Rabbid76 提到的,默认的绕组顺序是逆时针的。我的顶点顺时针旋转。
最终结果
private void glPanel_OpenGLInitialized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_FASTEST);
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Enable(OpenGL.GL_BLEND);
gl.Enable(OpenGL.GL_DEPTH_TEST);
gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
gl.ClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
private void glPanel_OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
OpenGL gl = args.OpenGL;
// Clear the color and depth buffers
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
// Build a rectangle
float Width = 100.0f;
float Height = 100.0f;
float[] vertices = new float[18];
vertices[0] = 0; vertices[1] = 0; vertices[2] = 0.0f;
vertices[3] = 0; vertices[4] = Height; vertices[5] = 0.0f;
vertices[6] = Width; vertices[7] = Height; vertices[8] = 0.0f;
vertices[9] = 0; vertices[10] = 0; vertices[11] = 0.0f;
vertices[12] = Width; vertices[13] = Height; vertices[14] = 0.0f;
vertices[15] = Width; vertices[16] = 0; vertices[17] = 0.0f;
gl.Translate(0.0f, 0.0f, 0.0f);
gl.Color(0.0f, 0.0f, 0.0f, 1.0f);
gl.VertexPointer(3, 0, vertices);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6);
gl.Flush();
}