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

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
I am trying out OpenGL 3.2 using a Cocoa window and an NSOpenGLView. However I can't get the NSOpenGLView to draw a red color. I just get a white window. Here is my code (inside subclass of NSOpenGLView):

Code:
-(void)awakeFromNib{

NSOpenGLPixelFormatAttribute attrs[] =
{
    NSOpenGLPFADoubleBuffer,
    NSOpenGLPFADepthSize, 24,
    // Must specify the 3.2 Core Profile to use OpenGL 3.2
    NSOpenGLPFAOpenGLProfile,
    NSOpenGLProfileVersion3_2Core,
    0
};

NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];

if (!pf)
{
    NSLog(@"No OpenGL pixel format");
}


NSOpenGLContext *context = [[[NSOpenGLContext alloc]initWithFormat:pf shareContext:nil]autorelease];

[self setPixelFormat:pf];
[self setOpenGLContext:context];


 }


 - (id)initWithFrame:(NSRect)frame
 {
self = [super initWithFrame:frame];
if (self) {
    // Initialization code here.
}

return self;
 }

 - (void)drawRect:(NSRect)dirtyRect
 {

glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

 }

The code should simply clear the view to red. No errors or warnings -- just a white window.

Any insight on why this is happening? Thanks!
 
Last edited:
What tutorial are you following? I don't think you are initializing things correctly.

I have this as an 'example' OpenGL program:

Code:
//
//  oglView.m
//  OGL
//

#import "oglView.h"
#import <GLUT/glut.h>

@implementation oglView

- (id)initWithFrame:(NSRect)frameRect
        pixelFormat:(NSOpenGLPixelFormat *)format  {
    
    self = [super initWithFrame:frameRect pixelFormat:format];
    [self initOpenGL];
    return self;
}

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder: coder];
    [self initOpenGL];
    return self;
}

- (void)initOpenGL {
    NSOpenGLContext *glcontext;
    glcontext = [self openGLContext];
    [glcontext makeCurrentContext];
    
	glClearColor(0.1, 0.1, 0.1, 0.0);			// background color is dark gray
	glClear(GL_COLOR_BUFFER_BIT);				// clear the view
	glOrtho(0.0, 400, 0.0, 400, -1.0, 1.0);		// setup our 400x400 coords
}

- (void)drawRect:(NSRect)dirtyRect {
    NSLog(@"drawRect");

	glClear(GL_COLOR_BUFFER_BIT);				// erase the screen
   
	glMatrixMode(GL_MODELVIEW);	// use model matrix
	glLoadIdentity();			// reset matrix
	glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0);	// we reset, so setup coords again	    

	glTranslatef(200, 200, 0);		// move to where we want to draw the triangle
	
	glBegin(GL_TRIANGLES);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3i(0, -100, 0);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3i(100, 100, 0);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3i(-100, 100, 0);
	glEnd();
    
    glFinish();
}

@end

Code:
//
//  oglView.h
//  OGL
//

#import <AppKit/AppKit.h>

@interface oglView : NSOpenGLView

- (void)initOpenGL;

@end
Of course, subclass an NSOpenGLView object with what I presented and you should be all set.
 
I'm not following any tutorial, I'm just using the docs. The goal is to get a 3.2 context up and going and clear the screen to red. The code you provided is legacy code, so I don't believe the code will work considering ML only supports the 3.2 core profile.

Thanks!
 
Yes, but my app is set to use the 3.2 core profile. Your code is legacy, therefore it won't work in my app because of the way it is configured. ;)
 
Code:
-(void)awakeFromNib{

NSOpenGLPixelFormatAttribute attrs[] =
{
    NSOpenGLPFADoubleBuffer,



Any insight on why this is happening? Thanks!

Just a thought, you specified double buffering. You probably need to swap the buffers....

What you are viewing may not be the buffer you are drawing to (clearing)
 
Just a thought, you specified double buffering. You probably need to swap the buffers....

What you are viewing may not be the buffer you are drawing to (clearing)

That was the problem!

I added:

Code:
glSwapAPPLE();

and that did the trick. I read somewhere that there is no need to swap buffers in an NSOpenGLView because it's done automatically -- which clearly wasn't true. I tried glfw, but I didn't like having to write everything in pure C and C++ (loading shaders into c strings was a pain). So I'm glad you've saved me the misery ;)

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.