0

我正在恢复 10 多年前的一些 openGL/glutcode。看起来 freeglut 已经从 glut 接管了,而且有些东西不太一样。此代码用于为球体着色:

void DrawRider() {
    glDisable(GL_TEXTURE_2D);
    glMatrixMode(GL_MODELVIEW);
    glColor4f(1.0, 0.6, 0.4, 0.5);
    glutSolidSphere(0.5, 12, 6);
}

现在球体是黑色的,尽管它显示出一些发光的迹象。 在此处输入图像描述

这是设置代码:

void init (void) {
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glShadeModel(GL_SMOOTH);    
    glFrontFace(GL_CCW);

    glEnable(GL_TEXTURE_2D);
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);

    if (transparent) glEnable(GL_BLEND);

    glColor3f(1.0,1.0,1.0);

    gCameraReset();

    glPolygonOffset (1.0, 1.0);
    SetLighting(4);
    glEnable(GL_LIGHTING);
}

void SetLighting(unsigned int mode)
{
    GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat mat_shininess[] = {90.0};

    GLfloat position[4] = {7.0,-7.0,12.0,0.0};
    GLfloat ambient[4]  = {0.2,0.2,0.2,1.0};
    GLfloat diffuse[4]  = {1.0,1.0,1.0,1.0};
    GLfloat specular[4] = {1.0,1.0,1.0,1.0};

    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
    glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);

    switch (mode) {
    // OTHER CASES ELIDED FOR POSTING TO StackOverflow
        case 4:
            glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
            glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);
            break;
    }

    glLightfv(GL_LIGHT0,GL_POSITION,position);
    glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
    glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
    glEnable(GL_LIGHT0);
}

这是调用 DrawRider 的显示代码:

void maindisplay(void)
{
    if (textured) glEnable(GL_TEXTURE_2D);
    else glDisable(GL_TEXTURE_2D);

    if (transparent) glDepthMask(0);
    else glDepthMask(1);

    if (!riding) {
        // track ball rotation
        glRotatef (gTrackBallRotation[0], gTrackBallRotation[1], gTrackBallRotation[2], gTrackBallRotation[3]);
        glRotatef (gWorldRotation[0], gWorldRotation[1], gWorldRotation[2], gWorldRotation[3]);
    }

    glClearColor (0.2f, 0.2f, 0.2f, 1.0f);  // defines the "background" color
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // clear the surface

    if (!riding) DrawRider();   

    glutSwapBuffers();
}
4

0 回答 0