Resolved glClear() NSOpenGLView

Discussion in 'Mac Programming' started by Blakeasd, Jul 22, 2013.

  1. Blakeasd, Jul 22, 2013
    Last edited: Jul 23, 2013

    macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    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!
     
  2. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    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.
     
  3. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    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!
     
  4. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
    It works on 10.8.4, I tested it before I posted it.
     
  5. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    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. ;)
     
  6. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #6
  7. macrumors newbie

    Joined:
    Jan 31, 2011
    Location:
    U.K.
    #7
    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)
     
  8. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #8
    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!
     
  9. macrumors newbie

    Joined:
    Jan 31, 2011
    Location:
    U.K.
    #9
    No problem :) I'm glad it turned out to be simple. I like those....
     

Share This Page