#import "MyWindowController.h"
@interface MyWindowController (InternalMethods)
-(BOOL)drawGLScene;
-(BOOL)initGL;
-(void)resizeGLSceneWithWidth:(int)width andHeight:(int)height;
@end
@implementation MyWindowController
// ****************************************************************************************************
// Handle keyboard input here
// ****************************************************************************************************
-(void)keyDown:(NSEvent *)theEvent
{
;
}
// ****************************************************************************************************
// All setup for OpenGL goes here
// ****************************************************************************************************
-(BOOL)initGL
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
NSImage * texImg = [NSImage imageNamed:@"a.bmp"];
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
NSBitmapImageRep * bitmap = [NSBitmapImageRep imageRepWithData:[texImg TIFFRepresentation]];
glTexImage2D(GL_TEXTURE_2D, 0, 3, [texImg size].width, [texImg size].height, 0, GL_RGB, GL_UNSIGNED_BYTE, [bitmap bitmapData]) ;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
return YES; // Initialization Went OK
}
// ****************************************************************************************************
// Resize And Initialize The GL Window
// ****************************************************************************************************
-(void)resizeGLSceneWithWidth:(int)width andHeight:(int)height
{
// Prevent a divide by zero error
if (height==0) height=1;
// Cocoa-specific, call when resized
[myGLContext update];
glViewport(0,0,width,height);
// Select The Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset The Projection Matrix
glLoadIdentity();
// Calculate the aspect ratio of the window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
// Select The Modelview Matrix
glMatrixMode(GL_MODELVIEW);
// Reset The Modelview Matrix
glLoadIdentity();
}
// ****************************************************************************************************
// Here's where we do all the drawing
// ****************************************************************************************************
-(BOOL)drawGLScene
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Matrix
glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units
glBindTexture(GL_TEXTURE_2D, texture);
glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Cube On X, Y & Z
glBegin(GL_QUADS); // Start Drawing The Cube
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd(); // Done Drawing The Quad
// Draw the scene
[myGLContext flushBuffer]; // Only works with double-buffered
rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
return YES;
}
-(void)dealloc
{
// Clean up objects
[theTimer invalidate];
[theTimer release];
[myGLContext release];
[super dealloc];
}
// ****************************************************************************************************
// Essentially emulate the main() function for this lesson
// ****************************************************************************************************
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Get the content area of the window
theView = [[super window] contentView];
// Create a pixel format
NSOpenGLPixelFormatAttribute attr[] =
{
NSOpenGLPFACompliant,
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, 16,
NSOpenGLPFADepthSize, 16,
nil
};
NSOpenGLPixelFormat * format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
// Create an OpenGL context with the previous pixel format
myGLContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
// Make the context the current context so all OpenGL calls apply to it
[myGLContext makeCurrentContext];
[self initGL];
// Lets just set the context to windowed mode for now, point it to the window's content area
[myGLContext setView: theView];
// Finish setting up OpenGL
[self resizeGLSceneWithWidth:[theView frame].size.width andHeight:[theView frame].size.height];
// Setup a simple timer for redrawing display
theTimer = [[NSTimer scheduledTimerWithTimeInterval:0.005
target:self
selector:@selector(drawGLScene)
userInfo:nil
repeats:YES] retain];
}
@end