Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GL.GenBuffer() gets 0 #86

Open
shlykovarseny opened this issue Nov 30, 2023 · 3 comments
Open

GL.GenBuffer() gets 0 #86

shlykovarseny opened this issue Nov 30, 2023 · 3 comments

Comments

@shlykovarseny
Copy link

shlykovarseny commented Nov 30, 2023

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
Copy link
Member

NogginBops commented Dec 2, 2023

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
Copy link
Author

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
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants