NSOpenGLView displays all white inside view

Discussion in 'Mac Programming' started by bahlquist, Nov 4, 2010.

  1. macrumors member

    Joined:
    Oct 6, 2010
    #1
    I have managed to get an NSOpenGLView subclass to work and have played around with it a bit. When I try to do the same in a new project, it doesn't work. Here is what I have done. I have a subclass of NSOpenGLView called BACubes. In interface builder, I drag an NSOpenGLView object from the palette into my window. I set its class to BACubes. Here is the code for BACubes:

    BACubes.h:

    PHP:
    #import <Cocoa/Cocoa.h>

    @interface BACubes NSOpenGLView {}
    @
    end
    BACubes.m:

    Code:
    #import "BACubes.h"
    #include <OpenGL/gl.h>
    
    @implementation BACubes
    
    -(void)drawRect:(NSRect)rect{
    	NSLog(@"hey");
        glClearColor(0,0,0,0);
    	glClear(GL_COLOR_BUFFER_BIT);
    }
    	
    @end
    I expect to see a black view screen. The console shows that drawRect has been called. Can you tell me what's wrong?
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    See if you get an error after glClear
    You should probably set the 0's to 0.0's or 0.0f's
     
  3. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #3
    Thanks for your reply. This is not the problem. Whole numbers as arguments seem to be ok in general (I would be curious if it ever resulted in a problem).
     
  4. macrumors newbie

    Joined:
    Nov 4, 2010
    Location:
    Colorado
    #4
    Try adding this:


    Code:
    -(id) initWithFrame: (NSRect) frameRect
    {
            NSOpenGLPixelFormatAttribute attribs[] =
            {
    		kCGLPFAAccelerated,
    		kCGLPFANoRecovery,
    		kCGLPFADoubleBuffer,
    		kCGLPFAColorSize, 24,
    		kCGLPFADepthSize, 16,
    		0
    	};
    	NSOpenGLPixelFormat * pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
    
    	self = [super initWithFrame: frameRect pixelFormat: pf];
        return self;
    }
    
     
  5. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    I didn't expect it to be the problem. But if you know the type of the argument, you should type in the right type. It's just my defensive programmer sense.
     
  6. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #6
    I added this to my implementation, but it never gets called (I noticed this with my other projects and so I omitted any "init" method.)

    Isn't it true that Interface Builder takes care of the initialization (as far as "double buffer" and such goes)?
     
  7. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
  8. thread starter macrumors member

    Joined:
    Oct 6, 2010
    #8
    I implemented initWithCoder instead of initWithFrame. The method is called, but I still get only a white rectangle.
     
  9. ncl
    macrumors member

    Joined:
    Aug 16, 2008
    #9
    Try adding [[self openGLContext] flushBuffer]; at the end of -drawRect
     

Share This Page