opentk / LearnOpenTK

A port of learnopengl.com's tutorials to OpenTK and C#.
Creative Commons Attribution 4.0 International
465 stars 115 forks source link

GL.GenBuffer() gets 0 #86

Open shlykovarseny opened 11 months ago

shlykovarseny commented 11 months ago

Hi there. If I define buffers inside the OpenTkInit() method like in tutorials, it work OK. But if I do same in some other method after the OpenTK is already initialized, GL.GenBuffer() gets zero. How to solve it? I need to add buffered data during the runtime. For example, add some surface from the file or remove it.

NogginBops commented 11 months ago

What is the OpenTkInit() function and where in the code is it? What version of OpenTK are you using? Are you running code on different threads?

shlykovarseny commented 11 months ago

First of all, I'm using OpenTK in the Avalonia project. Avalonia's version is 0.10.14 and OpenTK's version is 4.7.7

All my code is single thread for now.

I took this code as reference OpenTKAvalonia.

Here is the "original" OpentTkInit method:

protected override void OpenTkInit()
        {
            //Compile shaders
            _shader = new("Shaders/shader.vert", "Shaders/shader.frag");

            //Load textures
            _brickTexture = new();
            _brickTexture.Use();
            _brickTexture.LoadFromFile("Textures/wall.jpg");

            //Set textures in shaders
            _shader.Use();
            _shader.SetInt("texture0", 2);

            //Create vertex and buffer objects
            _vertexArrayObject = GL.GenVertexArray();
            _vertexBufferObject = GL.GenBuffer();

            //Set bg colour to a dark forest green
            GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);

            //Bind to the VAO
            GL.BindVertexArray(_vertexArrayObject);

            //Set up the buffer for the triangle
            GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);

            //Copy triangle vertices to the buffer
            GL.BufferData(BufferTarget.ArrayBuffer, _vertices.Length * sizeof(float), _vertices, BufferUsageHint.StaticDraw);

            //Configure structure of the vertices
            //                    (position parameter in vertex shader, 3 points, data is stored as floats, non-normalized, 5 floats/point, first point at offset 0 in data array)
            GL.VertexAttribPointer(_shader.GetAttribLocation("aPosition"), 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
            GL.EnableVertexAttribArray(_shader.GetAttribLocation("aPosition"));

            //Configure texture coordinate structure
            var texCoordLocation = _shader.GetAttribLocation("aTexCoord");
            GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
            GL.EnableVertexAttribArray(texCoordLocation);

            //Set up the EBO
            _elementBufferObject = GL.GenBuffer();

            //Set up its buffer
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, _elementBufferObject);

            //Copy data to the buffer
            GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint), _indices, BufferUsageHint.StaticDraw);
        }

From the base class BaseTkOpenGlControl inherited from the Avalonia.OpenGL.Controls.OpenGlControlBase:

protected sealed override void OnOpenGlInit(GlInterface gl, int fb)
    {
        //Initialize the OpenTK<->Avalonia Bridge
        _avaloniaTkContext = new(gl);
        GL.LoadBindings(_avaloniaTkContext);

        //Invoke the subclass' init function
        OpenTkInit();
    }

    //Simply call the subclass' teardown function
    protected sealed override void OnOpenGlDeinit(GlInterface gl, int fb)
    {
        OpenTkTeardown();

        //Workaround an avalonia issue where the depth buffer ID is not cleared
        //which causes depth problems (see issue #1 on this repo)
        _depthBufferField.SetValue(this, 0);
    }

That works ok. My issue is about moving all job with buffers to the other method whith runs in the app's runtime. Absolutely same code but in the other place.

NogginBops commented 7 months ago

You need to make sure that you have an opengl context current on the thread. Is it possible that the "runtime" function doesn't have a context current?