trying to write Syphon server code in c++ header

Home Forums Syphon Syphon Implementations – User trying to write Syphon server code in c++ header


This topic contains 1 reply, has 2 voices, and was last updated by  vade 1 week, 3 days ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #59314


    I am using the Syphon framework to serve video frames to a client. There is one line of code I need to use to serve the frames through the server, and I am stuck trying to write it into my c++ code.

    The Syphon documentation here gives this example:

    [myServer publishFrameTexture:myTex textureTarget:GL_TEXTURE_RECTANGLE_EXT imageRegion:NSMakeRect(0, 0, width, height) textureDimensions:NSMakeSize(width, height) flipped:NO];

    How do I incorporate this into my c++ header? I have set my xcode compiler flags so that “Other C Flags” are set to “-x objective-c” and “Other C++ Flags” to “-x objective-c++”.

    To give some context the rest of my code currently looks like this (and it builds):

        class OpencvMat2Syphon {
            // hack to call glut to create opengl context (one only so they don't wipe out each other)
            static bool haveGLContext;
            uint texId;
            CGLContextObj ctx;
            static SyphonServer *syphonServer;
            OpencvMat2Syphon() {
                if (!haveGLContext) {
                    char* str[1];  // glut hack for faking passing CLI parameters from main
                    int argc = 1;
                    str[0] = "app";
                    glutInit(&argc, str);
                    haveGLContext = true;
                ctx = CGLGetCurrentContext();
                std::cout << "cgl context: " << ctx << std::endl;
                SyphonServer *syphonServer = [[SyphonServer alloc] initWithName:@"imageGrabberOutput" context:ctx options:nil];
                glGenTextures(1, &texId);
                glBindTexture(GL_TEXTURE_2D, texId);
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
            ~OpencvMat2Syphon() {
                glDeleteTextures(1, &texId);
            uint getTexId() {
                return texId;
            void uploadTexture(cv::Mat mat) {
                glBindTexture(GL_TEXTURE_2D, texId);
                // can do swizzling/channel-flipping here? see
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, mat.cols, mat.rows, 0, GL_RGB, GL_UNSIGNED_BYTE,;
        // ??            [syphonServer publishFrameTexture:texId textureTarget:GL_TEXTURE_2D imageRegion:NSMakeRect(0, 0, 1280, 720) textureDimensions:NSMakeSize(1280, 720) flipped:NO];
        bool OpencvMat2Syphon::haveGLContext = false;


    What you need to do use leverage Obj-C++ and make a shim class that bridges your C++ and your Obj-C code, so you can safely call this C++ interface from your C++ app.

    Typically you do this by:

    Make an Obj-C++ wrapper which holds the pure Obj-C classes, state and methods you need.

    You make a pure C++ class which wraps those that Obj-C++ class and only in private interfaces (ie, not public facing in headers etc) the Obj-C code (this isolates your pure C++ land from Obj-C bullshit, memory management and headers)

    Then you call your C++ as normal.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.