PDA

View Full Version : [Resolved] glClear() NSOpenGLView




Blakeasd
Jul 22, 2013, 05:03 PM
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):


-(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!



robvas
Jul 22, 2013, 07:56 PM
What tutorial are you following? I don't think you are initializing things correctly.

I have this as an 'example' OpenGL program:


//
// 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


//
// 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.

Blakeasd
Jul 22, 2013, 08:49 PM
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!

robvas
Jul 23, 2013, 08:02 AM
It works on 10.8.4, I tested it before I posted it.

Blakeasd
Jul 23, 2013, 09:54 AM
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. ;)

robvas
Jul 23, 2013, 10:30 AM
Right. Have you checked out GLFW? I would throw together a sample app but I'm not at home.

http://www.gabriell.es/2013/02/osx-and-opengl-32.html

rthpjm
Jul 23, 2013, 12:36 PM
-(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)

Blakeasd
Jul 23, 2013, 03:50 PM
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:


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!

rthpjm
Jul 24, 2013, 12:34 PM
Thanks!

No problem :) I'm glad it turned out to be simple. I like those....