GithubHelp home page GithubHelp logo

Comments (2)

bangnoise avatar bangnoise commented on September 17, 2024

Due to how Apple's IOSurface/OpenGL intergration works, calls to glGetTexImage() won't work - if you need access to the pixel data, draw the texture to an FBO and then download from the FBO's colour attachment (ideally into a PBO so you can defer the download).

Obviously drawing to the window via this method is a terrible idea (just draw the original texture directly) - but presumably this is just an example and you plan to do further processing via OpenCV.

from syphon-framework.

Kautenja avatar Kautenja commented on September 17, 2024

Ah, I see. Thanks for the feedback. For future reference, rendering to a frame buffer did address the issue. This was the client callback block that solved my issue based on your suggestion. Indeed the goal is to process the frame, rendering with OpenCV is just a sanity check.

                    SyphonOpenGLImage *frame = [client newFrameImage];

                    GLuint fbo;
                    glGenFramebuffers(1, &fbo);
                    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
                    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, frame.textureName, 0);
                    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
                    if (status != GL_FRAMEBUFFER_COMPLETE) {
                        std::cout << "frame buffer error " << status << std::endl;
                    }

                    glBindTexture(GL_TEXTURE_RECTANGLE, frame.textureName);
                    int width, height;
                    glGetTexLevelParameteriv(GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_WIDTH, &width);
                    glGetTexLevelParameteriv(GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_HEIGHT, &height);

                    uint8_t* pixelBuffer = new uint8_t[width * height * 4];
                    std::memset(pixelBuffer, 0, width * height * 4);

                    glViewport(0, 0, width, height);
                    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                    glClear(GL_COLOR_BUFFER_BIT);

                    glEnable(GL_TEXTURE_RECTANGLE);
                    glDisable(GL_DEPTH_TEST);

                    glBindTexture(GL_TEXTURE_RECTANGLE, frame.textureName);

                    glBegin(GL_QUADS);
                    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
                    glTexCoord2f(width, 0.0f); glVertex2f(1.0f, -1.0f);
                    glTexCoord2f(width, height); glVertex2f(1.0f, 1.0f);
                    glTexCoord2f(0.0f, height); glVertex2f(-1.0f, 1.0f);
                    glEnd();

                    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer);

                    glBindTexture(GL_TEXTURE_RECTANGLE, 0);
                    glDeleteFramebuffers(1, &fbo);

                    torch::Tensor tensor = torch::from_blob(pixelBuffer, {height, width, 4}, torch::kUInt8).flip(0).slice(2, 0, 3, 1).contiguous();
                    cv::imshow(serverName, tensor2mat(tensor));
                    char c = (char) cv::waitKey(10);

                    delete[] pixelBuffer;
                    [frame release];

from syphon-framework.

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.