Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
CGGLContextCreate color strangeness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CGGLContextCreate color strangeness



Hi all,

I have a strange problem that I can't nail down. I'm trying to set up an OpenGL context which is shared by Quartz, mainly to be able to use Quartz font drawing with my OpenGL program, but also to maybe draw Quartz Composer backgrounds.

The problem is that as soon as I create a CGGLContext, the OpenGL colors are all wrong. I don't even have to actually draw anything using Quartz, the mere CGGLContextCreate() call is sufficient. I'm attaching an example that draws a red Quad; if compiled without the - DDEMO flag, it works perfectly, if compiled with -DDEMO, the Quad is gone. Experimentation shows that it's not *not drawn*, but just not drawn in the correct color.

In case this is a driver-specific problem: I'm on a 15" AlBook, Radeon 9600.

Has anyone encountered this problem before and can give me some pointers?

Thanks very much in advance,

Björn


[snip] #import <AppKit/AppKit.h> #import <OpenGL/gl.h>

@interface MyOpenGLView: NSOpenGLView
{
  NSOpenGLContext* glCtx;
  CGContextRef cgCtx;
}
- (id) initWithFrame: (NSRect) aRect;
- (void) drawRect: (NSRect) aRect;
@end

@implementation MyOpenGLView: NSOpenGLView
- (id) initWithFrame: (NSRect) aRect
{
    NSOpenGLPixelFormatAttribute winattrs[] = {
    NSOpenGLPFANoRecovery,
    NSOpenGLPFAColorSize, 24,
    NSOpenGLPFADepthSize, 16,
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAAccelerated,
    0
  };
  NSOpenGLPixelFormat *winFormat = [[[NSOpenGLPixelFormat alloc]
                      initWithAttributes: winattrs]
                     autorelease];

  [super initWithFrame: aRect pixelFormat: winFormat];

  CGSize size = {aRect.size.width, aRect.size.height};
  glCtx = [self openGLContext];

#ifdef DEMO
  // this is what makes the colors go wrong!
  cgCtx = CGGLContextCreate([glCtx CGLContextObj], size, NULL);
#endif

  return self;
}

- (void) drawRect: (NSRect) aRect
{
  [glCtx makeCurrentContext];
  NSLog(@"GL context: %@", glCtx);

  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
      GL_ACCUM_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

  NSRect r = [self frame];
  glViewport(0, 0, r.size.width, r.size.height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, r.size.width, 0, r.size.height, -1, 1);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glColor3f(1.0f, 0.0f, 0.0f);

  glBegin(GL_QUADS);
  glVertex2f(10.0f, 10.0f);
  glVertex2f(r.size.width - 10.0f, 10.0f);
  glVertex2f(r.size.width - 10.0f, r.size.height - 10.0f);
  glVertex2f(10.0f, r.size.height - 10.0f);
  glEnd();

  [glCtx flushBuffer];
}
@end

@interface MyController: NSObject
- (void) applicationDidFinishLaunching: (NSNotification*) aNote;
@end;

@implementation MyController: NSObject
- (void) applicationDidFinishLaunching: (NSNotification*) aNote
{
  NSRect r = NSMakeRect(100, 100, 400, 300);
  NSRect f = NSMakeRect(0, 0, 400, 300);
  NSWindow* win = [[NSWindow alloc] initWithContentRect: r
                    styleMask: (NSClosableWindowMask |
                           NSMiniaturizableWindowMask |
                           NSTitledWindowMask)
                    backing: NSBackingStoreBuffered
                    defer: NO];
  [win setContentView: [[MyOpenGLView alloc] initWithFrame: f]];
  [win makeKeyAndOrderFront: self];
}
@end;

int main(int argc, const char* argv[])
{
  NSAutoreleasePool *pool = [NSAutoreleasePool new];
  [NSApplication sharedApplication];
  [NSApp setDelegate: [MyController new]];
  int retval = NSApplicationMain(argc, argv);
  [pool release];
  return retval;
}
[snip] _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.