Code Monkey home page Code Monkey logo

Comments (3)

NogginBops avatar NogginBops commented on August 23, 2024

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?

from learnopentk.

shlykovarseny avatar shlykovarseny commented on August 23, 2024

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.

from learnopentk.

NogginBops avatar NogginBops commented on August 23, 2024

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?

from learnopentk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.