Newbie question: White in Cocoa subclass

Post: #1
When the following class is put into a window in a nib file, all I get is a white box with nothing in it. I have a very basic understanding of OpenGL and know what most of the code does, but a lot of it is just copy and paste.

#import <Cocoa/Cocoa.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>

@interface MyOpenGLView : NSOpenGLView
    BOOL haveInitedGl;
    float rotation;

- (void)awakeFromNib;
- (void)initGL;
- (void)drawRect:(NSRect)rect;
- (void)rotate:(NSTimer*)timer;
- (void)reshape;


- (void)awakeFromNib
    haveInitedGl = FALSE;

- (void) initGL

    [[self openGLContext] makeCurrentContext];

    [self reshape];

    glShadeModel( GL_SMOOTH );                // Enable smooth shading
    glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );   // Black background
    glClearDepth( 1.0f );                     // Depth buffer setup
    glEnable( GL_DEPTH_TEST );                // Enable depth testing
    glDepthFunc( GL_LEQUAL );                 // Type of depth test to do
                                              // Really nice perspective calculations

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotate:) userInfo:nil repeats:YES];

    haveInitedGl = TRUE;

- (void)drawRect:(NSRect)rect

    if (!haveInitedGl)
        [self initGL];

    // Clear the screen and depth buffer
    glLoadIdentity();   // Reset the current modelview matrix

    glTranslatef( -1.5f, 0.0f, -6.0f );   // Left 1.5 units, into screen 6.0

    glBegin( GL_TRIANGLES );         // Draw a triangle
    glColor3f(1.0f,0.0f,0.0f);         // Red
    glVertex3f(  0.0f,  1.0f, 0.0f );    // Top
    glVertex3f( -1.0f, -1.0f, 0.0f );    // Bottom left
    glVertex3f(  1.0f, -1.0f, 0.0f );    // Bottom right
    glEnd();                             // Done with the triangle

    glTranslatef( 3.0f, 0.0f, 0.0f );    // Move right 3 units

    glBegin( GL_QUADS );                // Draw a quad
    glVertex3f( -1.0f,  1.0f, 0.0f );   // Top left
    glVertex3f(  1.0f,  1.0f, 0.0f );   // Top right
    glVertex3f(  1.0f, -1.0f, 0.0f );   // Bottom right
    glVertex3f( -1.0f, -1.0f, 0.0f );   // Bottom left
    glEnd();                            // Quad is complete

    [[self openGLContext] flushBuffer];

- (void)rotate:(NSTimer*)timer


    [[self window] display];

- (void)reshape
    NSRect sceneBounds;

    [[self openGLContext] update];
    sceneBounds = [self bounds];
    // Reset current viewport
    glViewport( 0, 0, sceneBounds.size.width, sceneBounds.size.height );
    glMatrixMode( GL_PROJECTION );   // Select the projection matrix
    glLoadIdentity();                // and reset it
                                     // Calculate the aspect ratio of the view
    gluPerspective( 45.0f, sceneBounds.size.width / sceneBounds.size.height,
                    0.1f, 100.0f );
    glMatrixMode( GL_MODELVIEW );    // Select the modelview matrix
    glLoadIdentity();                // and reset it



BTW, I know 'rotation++' does nothing. I'll put it in later.
Quote this message in a reply
Post: #2
I think you might need to do some stuff with NSPixelFormat. Have a look in the GLCubes code (
Quote this message in a reply
Post: #3
Thanks. I was looking for a project like that anyway.

BTW, I had to get it from the iDisk because I got a file not found from iDevGames.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocoa Animation Question DesertPenguin 10 6,909 Feb 12, 2008 02:58 PM
Last Post: OneSadCookie
  Newbie question regarding Resources folder antester 2 2,488 Dec 5, 2007 09:21 PM
Last Post: antester
  bug in OpenGL setup for NSOpenGL subclass (I think) backslash 2 3,578 Jul 18, 2007 01:10 PM
Last Post: backslash
  glColor4f and glBlendFunc for fading to white? dave05 3 5,053 Jun 24, 2005 05:31 PM
Last Post: Skorche
  Cocoa loaded textures have white edges Jake 6 4,003 May 6, 2005 09:17 AM
Last Post: Jake