Newbie question: White in Cocoa subclass
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:
MyOpenGLView.m:
BTW, I know 'rotation++' does nothing. I'll put it in later.
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;
@endMyOpenGLView.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");
}
@endBTW, I know 'rotation++' does nothing. I'll put it in later.
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)
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Cocoa Animation Question | DesertPenguin | 10 | 6,519 |
Feb 12, 2008 02:58 PM Last Post: OneSadCookie |
|
| Newbie question regarding Resources folder | antester | 2 | 2,303 |
Dec 5, 2007 09:21 PM Last Post: antester |
|
| bug in OpenGL setup for NSOpenGL subclass (I think) | backslash | 2 | 3,270 |
Jul 18, 2007 01:10 PM Last Post: backslash |
|
| glColor4f and glBlendFunc for fading to white? | dave05 | 3 | 4,475 |
Jun 24, 2005 05:31 PM Last Post: Skorche |
|
| Cocoa loaded textures have white edges | Jake | 6 | 3,712 |
May 6, 2005 09:17 AM Last Post: Jake |
|

