dwmkerr / sharpgl

Use OpenGL in .NET applications. SharpGL wraps all modern OpenGL features and offers a powerful scene graph to aid development.
MIT License
768 stars 300 forks source link

Nothing being rendered at all #212

Open burdoto opened 2 years ago

burdoto commented 2 years ago

Hello, I'm already sure I'm doing something stupidly wrong, but this is extremely frustrating since I cannot locate the issue.

Basically I'm trying to draw ANYTHING using this Draw() method:

public const float lim = 1000;
private void Draw(object sender, OpenGLRoutedEventArgs args)
        {
            var gl = args.OpenGL;

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            gl.LoadIdentity();
            gl.LineWidth(10.0f);
            gl.PointSize(2.0f);

            gl.Color(1, 1, 1);

            // y axis
            gl.Begin(BeginMode.Lines);
            gl.Vertex(-lim, 0);
            gl.Vertex(lim, 0);
            gl.End();

            // x axis
            gl.Begin(BeginMode.Lines);
            gl.Vertex(0, -lim);
            gl.Vertex(0, lim);
            gl.End();

            for (int i = 0; i < 500; i++)
            {
                gl.Begin(BeginMode.TriangleFan);
                var Segments = 50;
                var radius = 1000;
                var Position = new Vector2(i, i);
                for (var a = 0; a < Segments; a++)
                {
                    float theta = 2f * MathF.PI * a / Segments;
                    float x = radius * MathF.Sin(theta);
                    float y = radius * MathF.Cos(theta);
                    gl.Vertex(Position.X + x, Position.Y + y);
                }
                gl.End();   
            }

            gl.Flush();
        }

But I'm only ever getting a black screen (with the FPS counter working fine). I've tried comparing my code to this sample, but I really cannot find the problem.

As you can see from the code, I have already made desperate, frustrated attempts to draw large cirlces ANYWHERE, with no success whatsoever. What am I missing?

petals-of-white commented 2 weeks ago

I have the exact same problem. It looks like none of gl.Draw ( <- Elements, Arrays etc) functions have any effect. I've already checked the VAOs, VBOs and textures. Tested using WPF, TargetFramework: net8.0-windows

petals-of-white commented 2 weeks ago

I've also noticed that OpenGL generates InvalidOperation error before each draw call. Suppose this method is called each time OpenGLDraw event is raised for OpenGLControl.

    private void DicomGLViewer_OpenGLDraw(object sender, OpenGLRoutedEventArgs args)
    {
        if (GL is not null) OpenGLHelpers.ThrowIfGLError(GL);

        //while (gl.GetErrorCode() is not ErrorCode.NoError);

        GL?.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
        glState?.DrawVertices(CurrentDepth);
    }

And ThrowIfGLError looks like this:

public static class OpenGLHelpers
{
    ...
    public static void ThrowIfGLError(OpenGL gl)
    {
        var error = gl.GetErrorCode();
        switch (error)
        {
            case ErrorCode.NoError:

                break;

            case (var other):
                throw new Exception(gl.GetErrorDescription(Convert.ToUInt32(other)));
        }
    }
}

It throws with 'Invalid Operation' OpenGL error So this error occurs before doing anything.

My opengl initialization (after OpenGLInitialized event) doesn't produce any errors, so this is quite strange.