Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bahlquist

macrumors member
Original poster
Oct 6, 2010
41
0
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?
 
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).
 
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;
}
 
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.
 
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;
}

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)?
 
I implemented initWithCoder instead of initWithFrame. The method is called, but I still get only a white rectangle.
 
Try adding [[self openGLContext] flushBuffer]; at the end of -drawRect
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.