dwmkerr / sharpgl

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

There is an OpenGL instance with its 'RenderContextProvider' unbinded in 'SceneControl' #105

Open bitzhuwei opened 9 years ago

bitzhuwei commented 9 years ago

It's about the 'SharpGL.SceneControl' class. In its 'SharpGL.SceneControl.Scene' property there is an OpenGL instance 'gl' created like this: private OpenGL gl = new OpenGL();. (let's call this 'gl' instance 'glA') Here comes a problem when using the 'SharpGL.SceneControl'. If you use var c = this.sceneControl.Scene.OpenGL.RenderContextProvider;, the c will be null. But actually there is another OpenGL instance(let's call it 'glB') that holds a valid RenderContextProvider. And that OpenGL instance is the one we should have gotten. This 'glB' is instantiated in 'SharpGL.SceneControl''s parent class(SharpGL.OpenGLControl) like this:private readonly OpenGL gl = new OpenGL();. And it has binded to everything that should be. So I think we can force 'glA' pointing to 'glB' by updating the InitializeComponent() method in SharpGL.SceneControl class like this:


        private void InitializeComponent()
        {
            // 
            // OpenGLCtrl
            // 
            this.Name = "OpenGLCtrl";
            this.scene.OpenGL = this.OpenGL;// force 'glA' pointing to 'glB'
        }

And also the 'glA''s instatiation should be removed. Sorry for my english.

JPTIZ commented 8 years ago

bump

Got a problem with SharpGL OpenGLControl's RenderContextProvider too. Trying to add it to WindowsForms control's "Controls" property is okay, but font creation fails as RenderContextProvider is null.

To be more specific...the callstack:

    SharpGL.dll!SharpGL.FontBitmaps.CreateFontBitmapEntry(SharpGL.OpenGL gl, string faceName, int height) Line 62   C#
    SharpGL.dll!SharpGL.FontBitmaps.DrawText(SharpGL.OpenGL gl, int x, int y, float r, float g, float b, string faceName, float fontSize, string text) Line 127 C#
    SharpGL.WinForms.dll!SharpGL.OpenGLControl.Render(System.Drawing.Graphics graphics) Line 117    C#
    SharpGL.WinForms.dll!SharpGL.OpenGLControl.OnPaint(System.Windows.Forms.PaintEventArgs e) Line 151  C#
    System.Windows.Forms.dll!System.Windows.Forms.Control.PaintWithErrorHandling(System.Windows.Forms.PaintEventArgs e, short layer)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WmPaint(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m)  Unknown
    System.Windows.Forms.dll!System.Windows.Forms.UserControl.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m)   Unknown
    System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam)  Unknown
    [Native to Managed Transition]  
    [Managed to Native Transition]  
    System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData)  Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context)    Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Unknown
    System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm)   Unknown
>   SimpleTexture3DCylinder.exe!Labmetro.Program.Main() Line 17 C#

My "Renderer" class:

public class Renderer : System.Windows.Forms.ContainerControl
{
    public Renderer()
    {
        Width = 800;
        Height = 600;
        Controls.Add(openGLControl = new OpenGLControl
        {
            Width = Width,
            Height = Height,
            DrawFPS = true,
            RenderContextType = RenderContextType.FBO,
        });
    }

    private OpenGLControl openGLControl = null;
}

Am I forgetting to set any property?

As you said, there's a valid instance of the ContextProvider somewhere, and if I need to set my control's proviter to that, how exactly do I do that?

bitzhuwei commented 8 years ago

It's complex...I've forgotten how to do that. Also I don't feel it a very good design to instantiate an OpenGL object. It may be more convenient to use static OpenGL commands.

JPTIZ commented 8 years ago

Just checked again and saw you had problem with SceneControl actually. Mine is with OpenGLControl, which worked when using WPF but does not work (NullPointerException with ContextProvider when generating font bitmap) with Windows.Forms.

Going to look forward abound using OpenGL calls manually.