Reputation: 21966
I mixed Cocoa, GLUT and OpenGL frameworks to draw a teapot.
I usually do this in plain C but I need to mix up Cocoa buttons and stuffs with OpenGL, this is the corrispettive C code:
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import <math.h>
#include "utility.h"
GLuint width=640, height=480;
GLfloat angle=0.0;
void init()
{
glEnable(GL_DEPTH_TEST);
glViewport(-500, -500, 1000, 1000);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/(float)height, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);
// Luci
glShadeModel(GL_FLAT);
glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat[]) {0,0,0} );
glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat[]) {1,1,0} );
glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat[]) {0.5,0.5,0} );
glMaterialfv(GL_FRONT, GL_AMBIENT, (const GLfloat[]) {1,0,0} );
glMaterialfv(GL_FRONT, GL_DIFFUSE, (const GLfloat[]) {1,0.25,0} );
glMaterialfv(GL_FRONT, GL_SPECULAR, (const GLfloat[]) {1,0.75,0} );
glMaterialfv(GL_FRONT, GL_SHININESS, (const GLfloat[]) {1,1,0} );
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
void display()
{
glClearColor(BLACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glutSolidTeapot(10);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='+')
{
angle+=5.0;
}
else if(key=='-')
{
angle-=5.0;
}
else
{
return;
}
makeRound(&angle);
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}
And this is the result:
Then I'm subclassing a NSOpenGLView and do the same of this C code, just with different sizes, but the meterials and lights are the same:
@implementation MyView
@synthesize angle;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
- (void) prepareOpenGL
{
NSOpenGLContext* context= self.openGLContext;
GLfloat width= self.bounds.size.width;
GLfloat height= self.bounds.size.height;
[context makeCurrentContext];
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width/height, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);
// Lighting
glShadeModel(GL_FLAT);
glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat[]) {0,0,0} );
glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat[]) {1,1,0} );
glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat[]) {0.5,0.5,0} );
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (const GLfloat[]) {1,0,0} );
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat[]) {1,0.25,0} );
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (const GLfloat[]) {1,0.75,0} );
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, (const GLfloat[]) {1,1,0} );
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
- (void)drawRect:(NSRect)dirtyRect
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glutSolidTeapot(20);
glPopMatrix();
glFlush();
}
#pragma mark - Actions
- (IBAction) increaseAngle :(id)sender
{
angle+=5.0;
if(angle>360.0)
{
angle-=360.0;
}
[self setNeedsDisplay: YES];
}
- (IBAction) decreaseAngle:(id)sender
{
angle-=5.0;
if(angle<0.0)
{
angle+=360.0;
}
[self setNeedsDisplay: YES];
}
@end
And this is the result:
That's pretty ugly and considering that I used the same meterials and lights, I think that I'm doing something wrong in the one done with Cocoa and OpenGL mixed.
Upvotes: 2
Views: 735
Reputation: 52083
Make sure you request a depth buffer when you create your GL context. You may not get one by default.
Upvotes: 3