2

我混合了 Cocoa、GLUT 和 OpenGL 框架来绘制一个茶壶。
我通常在纯 C 中执行此操作,但我需要将 Cocoa 按钮和东西与 OpenGL 混合,这是 Corrispettive 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 混合完成的那个中做错了。

4

1 回答 1

3

确保在创建 GL 上下文时请求深度缓冲区。默认情况下,您可能不会得到一个。

于 2012-12-03T15:56:30.443 回答