我将Cocoa,GLUT和OpenGL框架混合在一起绘制茶壶 我通常在普通的C中做这个,但是我需要把Cocoa按钮和东西混合在一起,这是腐蚀性的C代码:
#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;
}
这就是结果:
然后我将NSOpenGLView子类化并执行相同的C代码,只是具有不同的大小,但是材料和灯光是相同的:
@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
这就是结果:
这很丑陋,考虑到我使用相同的材料和灯光,我认为我在使用Cocoa和OpenGL混合完成时做错了。
答案 0 :(得分:3)
确保在创建GL上下文时请求深度缓冲区。你可能默认不会得到一个。