content of NSView disappear

Home Forums Syphon Syphon Development – Developer content of NSView disappear

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #58713
    m3can0
    Participant

    Hi,
    I finally did it. I have been able to send the content of an NSView by creating the texture, but now when I publish the frame texture, the content of my NSView disappear. I’ve been trying to bypass the problem by copying my customGLView (fail), create an offscreen OpenGLview with the same drawing code (fail) and to create a context inside my custom GLview, but nothing work. Is anybody had this problem before?

    Thanks,

    My code
    AppDelegate.m

    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
        
        debug = YES;
        [offscreenView update];
        
        //GLuint *texName = 0;
        //[self textureFromImage: image textureName: texName];
        offscreenView = [[CustomGLView alloc] initWithFrame:NSMakeRect(0, 0, view.frame.size.width, view.frame.size.height) ];
        //offscreenView =  [view copy];
        
        //Initialisation du context
        context = [[view openGLContext] CGLContextObj];
        //offscreenContext = *([offscreenView getViewContext]);
    
        //Initialisation du server Syphon
        aSyphonServer = [[SyphonServer alloc] initWithName:nil context:context options:nil];
        //aSyphonServer = [[SyphonServer alloc] initWithName:nil context:offscreenContext options:nil];
        
    }
    
    - (void) applicationWillTerminate:(NSNotification *)notification
    {
    	//Arret du server syphon
        [aSyphonServer stop];
    }
    
    - (IBAction)sendToSyphon:(id)sender {
        if ([aSyphonServer hasClients]){
            if(debug)
                NSLog(@"[aSyphonServer hasClients] == YES");
            GLuint texName = 0;
            
            //Code avec view
            [view getTextureWithTexName:&texName];
            CGLLockContext(aSyphonServer.context);
            [aSyphonServer publishFrameTexture:texName textureTarget:GL_TEXTURE_RECTANGLE imageRegion:NSMakeRect(0, 0, offscreenView.frame.size.width, offscreenView.frame.size.height) textureDimensions:NSSizeFromCGSize(offscreenView.frame.size) flipped:YES];
            CGLUnlockContext(aSyphonServer.context);
            [view update];
            
        }
        else{
            if(debug)
                NSLog(@"[aSyphonServer hasClients] == NO");
        }
    }

    CustomGLView:

    
    @implementation CustomGLView
    
    - (id)initWithFrame:(NSRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
           /* NSOpenGLPixelFormatAttribute attrs[] = {
                NSOpenGLPFAFullScreen,
                NSOpenGLPFADoubleBuffer,
                NSOpenGLPFADepthSize, 32,
                0
            };
            
            contextView = nil;
            NSOpenGLPixelFormat *pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
            
            if (pixFmt !=nil) {
                contextView = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:NO];
            }*/
            
            [self update];
        }
        return self;
    }
    
    /*- (NSOpenGLContext*)getViewContext{
        return contextView;
    }*/
    
    - (void)update{
        [self setNeedsDisplay:YES];
    }
    
    - (void)drawRect:(NSRect)dirtyRect
    {
    	[super drawRect:dirtyRect];
    	
        // Drawing code here.
        
        //Load the image
        NSImage *image = [[NSImage alloc] initWithContentsOfFile:@"/Users/phileas_fog/Desktop/Tetris_xcode/exemples_des_guides/SyphonServerExample/SyphonServerExample/bloc00.png"];
     
        NSSize imageSize = [image size];
        
        //draw the image
        //[image drawAtPoint:imageRect.origin fromRect:imageRect operation:NSCompositeCopy fraction:1.0];
        [image drawAtPoint:NSMakePoint(0.0, 0.0) fromRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) operation:NSCompositeSourceOver fraction:1.0];
        
        
        //creation of a new rectangle with the color blue
        NSRect squareRect;
        squareRect.origin = NSMakePoint(350.0, 10.0);
        squareRect.size.width = 50.0;
        squareRect.size.height = 50.0;
        //the blue color
        NSColor *aColor = [NSColor blueColor];
        [aColor set];
        [NSBezierPath fillRect:squareRect];
    }
    
    - (void)getTextureWithTexName:(GLuint*) texName{
        NSBitmapImageRep *bitmap = [NSBitmapImageRep alloc];
        int samplesPerPixel = 0;
        
        //on lock sur la vue et on cree une representation bitmap
        [self lockFocus];
        [bitmap initWithFocusedViewRect:NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height)];
        [self unlockFocus];
        
        //Creation d'une texture openGL
        glPixelStorei(GL_UNPACK_ROW_LENGTH, (int)[bitmap pixelsWide]);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        
        //identification de la texture
        if (*texName == 0) {
            glGenTextures(1, texName);
        }
        glBindTexture(GL_TEXTURE_RECTANGLE, *texName);
        glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        
        //Gestion des cas particulies
        samplesPerPixel = [bitmap samplesPerPixel];
        
        if(![bitmap isPlanar] && (samplesPerPixel == 3 || samplesPerPixel == 4)){
            glTexImage2D(GL_TEXTURE_RECTANGLE, 0, samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8, [bitmap pixelsWide], [bitmap pixelsHigh], 0, samplesPerPixel == 4 ? GL_RGBA : GL_RGB , GL_UNSIGNED_BYTE, [bitmap bitmapData]);
        }
        
        //[self setNeedsDisplay:YES];
        //return texName;
    }
    
    #58719
    bangnoise
    Keymaster

    Hi

    There are a few problems with your approach – more than I am going to detail, as you should start with a clear method in mind: what is the final intention of your app?

    If, as this code tries to do, your intention is to display an image from a file on disk and also output that image via Syphon, then you could consider minimising the amount of rendering to pixel buffers and texture uploads which occur. A good strategy would be to load the image to a buffer and then load that buffer to a texture once, and use that texture for drawing in your view and publishing to Syphon (assuming you are comfortable enough with OpenGL).

    Alternatively, something like OpenFrameworks might simplify the task for you considerably.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.