PDA

View Full Version : Newbie question: White in Cocoa subclass


sinclair44
2002.08.12, 07:30 PM
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:
#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:
- (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.

furballphat
2002.08.12, 07:45 PM
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)

sinclair44
2002.08.13, 05:57 AM
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.