I'm trying to use SharpGL to render in a WPF application using gl.VertexPointer() and gl.DrawArrays(). But I cannot get it to render a square. The background clears to green and I can see the FPS drawing at the bottom left of the WPF panel. When adding the code for the square, the FPS text disappears as well and I just have a blank screen.
I'm doing the same exact thing that I have in a C project which works just fine. I don't know what I'm missing or doing incorrectly.
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();
}
CodePudding user response:
As mentioned by @BDL, glBegin()
and glEnd()
are not used in this case.
As mentioned by @Rabbid76, glEnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY)
should be removed since I am not using texture coordinates.
Thanks for the help guys!
Also removing gl.Enable(OpenGL.GL_CULL_FACE)
was necessary in the specific example to draw my square. As @Rabbid76 mentioned, the default winding order is counter-clockwise. My vertices are winding clockwise.
End result
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();
}