Newbie question: White in Cocoa subclass

sinclair44
Unregistered
 
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.

MyOpenGLView.h:
Code:
#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;

@end

MyOpenGLView.m:
Code:
- (void)awakeFromNib
{
    NSLog(@"awakeFromNib");
    haveInitedGl = FALSE;
}

- (void) initGL
{
    NSLog(@"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
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

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

    haveInitedGl = TRUE;
}

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

    if (!haveInitedGl)
        [self initGL];

    // Clear the screen and depth buffer
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    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
{
    NSLog(@"rotate");

    rotation++;

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

    NSLog(@"reshape");
}

@end

BTW, I know 'rotation++' does nothing. I'll put it in later.
Quote this message in a reply
furballphat
Unregistered
 
Post: #2
I think you might need to do some stuff with NSPixelFormat. Have a look in the GLCubes code (http://www.idevgames.com/fileshow.php3?showid=232)
Quote this message in a reply
sinclair44
Unregistered
 
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 7,076 Feb 12, 2008 02:58 PM
Last Post: OneSadCookie
  Newbie question regarding Resources folder antester 2 2,586 Dec 5, 2007 09:21 PM
Last Post: antester
  bug in OpenGL setup for NSOpenGL subclass (I think) backslash 2 3,715 Jul 18, 2007 01:10 PM
Last Post: backslash
  glColor4f and glBlendFunc for fading to white? dave05 3 5,226 Jun 24, 2005 05:31 PM
Last Post: Skorche
  Cocoa loaded textures have white edges Jake 6 4,161 May 6, 2005 09:17 AM
Last Post: Jake