OpenGL对象在Mac OS X 10.6中有光泽/闪亮,但不是10.5。为什么?

时间:2012-01-29 08:02:57

标签: macos cocoa opengl lighting specular

我为Mac OS X 10.5及更高版本编写了一个OpenGL屏幕保护程序,它显示了使用行进立方体构建的旋转心脏。它在我的黑色13.3“开发Macbook上运行Snow Leopard(10.6)时效果很好,漂亮而有光泽。

但是当我在较新的Macbook Pro上试用它但运行豹子(10.5)时,心脏没有光泽。它看起来好像只用环境光/漫射光照亮,但没有镜面光。就好像我已经将灯光的镜面反射分量设置为0.较新的Macbook有一个Nvidia显卡。运行10.6的旧版本有一台Intel XMA3100。

这是我的OpenGL初始化代码。使用行进立方体创建心脏,我也在计算法线向量。

- (void)setUpOpenGL
{ 
    [[glView openGLContext] makeCurrentContext];

    // Synchronize buffer swaps with vertical refresh rate
    GLint swapInterval = 1;
    [[glView openGLContext] setValues:&swapInterval
                         forParameter:NSOpenGLCPSwapInterval];

    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0);
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);

    // Light
    GLfloat ambient_light[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat diffuse_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat specular_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat position_light[] = { 0.0f, 0.0f, -1.0f, 0.0f };

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light);
    glLightfv(GL_LIGHT0, GL_POSITION, position_light);

    // Material
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    GLfloat mat_ambient[] = {0.7, 0.0, 0.0, 1.0};
    GLfloat mat_diffuse[] = { 0.7, 0.0, 0.0, 1.0 };
    GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 0.0 };
    GLfloat mat_shininess[] = { 100.0 };

    glColor3f(1.0, 0.0, 0.0);

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

    // Copy isosurface vertices to graphics array
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, [marchingCubes vertices]);
    glNormalPointer(GL_FLOAT, 0, [marchingCubes normals]);
}

实际绘图代码如下所示:

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [[glView openGLContext] makeCurrentContext];

    glDrawBuffer(GL_BACK);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Adjust viewpoint to give heart beat effect
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, radius, 2*radius/3,
              0, 0, 0,
              0, 0, 1);

    // Adjust rotation to make heart spin
    glRotatef(rotation, 0.0f, 0.0f, 1.0f);

    // Draw heart
    glDrawArrays(GL_TRIANGLES, 0, [marchingCubes vertexCount]/3);

    glFlush();

    [[glView openGLContext] flushBuffer];

    // Rotate and scale a bit
    rotation += 1.0f;
    radius = 4.0 + 0.25*sin(rotation/4);
}

为什么镜面反射光差异?某些显卡根本不支持高光照明吗?任何指针都非常感激。

1 个答案:

答案 0 :(得分:1)

GL_SPECULAR(和其他颜色)的默认值为{ 0.0,0.0,0.0,1.0 }

GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 1.0 };

您也可以在设置功能结束时执行以下操作:1。优化绘图; 2.仔细检查在设置时是否实际将光照应用于正面。

glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);