PDA

View Full Version : NSOpenGLView displays all white inside view




bahlquist
Nov 4, 2010, 01:29 PM
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:

#import <Cocoa/Cocoa.h>

@interface BACubes : NSOpenGLView {}
@end

BACubes.m:

#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?



jared_kipe
Nov 4, 2010, 02:51 PM
See if you get an error after glClear
You should probably set the 0's to 0.0's or 0.0f's

bahlquist
Nov 4, 2010, 04:07 PM
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).

jvaneat
Nov 4, 2010, 04:25 PM
Try adding this:



-(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;
}

jared_kipe
Nov 4, 2010, 04:59 PM
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).

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.

bahlquist
Nov 4, 2010, 05:46 PM
Try adding this:



-(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;
}


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)?

jared_kipe
Nov 5, 2010, 02:48 PM
-initWithCoder:

bahlquist
Nov 6, 2010, 08:32 PM
I implemented initWithCoder instead of initWithFrame. The method is called, but I still get only a white rectangle.

ncl
Nov 7, 2010, 06:01 AM
Try adding [[self openGLContext] flushBuffer]; at the end of -drawRect