1

我正在尝试将照明与 3D 图形结合起来。球体的深度为 100,半径为 100。我使用 translatef 使其远离眼睛位置。
但是屏幕上什么也没有显示:

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>

GLfloat width=500, height=500;
GLfloat angle=0.0;

void makeRound (GLfloat* angle)
{
    if(*angle>360.0)
    {
        *angle-=360.0;
    }
    else if(*angle<0.0)
    {
        *angle+=360.0;
    }
}

void init(void)
{
    GLfloat mat_specular[]   = { 1.0, 0.0, 0.0, 0.0 };
    GLfloat mat_diffuse[]    = { 0.9, 0.0, 0.0, 0.0 };
    GLfloat mat_shininess[]  = { 50.0 };
    GLfloat light_position[] = { 5.0, 5.0, 5.0, 1.0 };
    GLfloat white_light[]    = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat lmodel_ambient[] = { 0.1, 0.5, 0.1, 1.0 };

    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 0, 0, 0, 100, 0, 1, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, 1, 1, 1000);

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);

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

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
    glLightfv(GL_LIGHT0, GL_SPECULAR,white_light);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glRotatef(angle, 0, 1, 0);
    glTranslatef(0, 0, 100);
    glutSolidSphere (100.0, 80, 64);
    glPopMatrix();

    glutSwapBuffers();
}


void keyboard(unsigned char key, int x, int y)
{
    switch (key)
    {
        case '+':
            angle+=5.0;
            makeRound(&angle);
            glutPostRedisplay();
            break;
        case '-':
            angle-=5.0;
            makeRound(&angle);
            glutPostRedisplay();
            break;
        default:
            break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (width, height);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    init ();
    glutMainLoop();
    return 0;
}

更新

这就是我看到的:

glTranslatef(0,0,-200);

在此处输入图像描述

4

1 回答 1

2

由于您的翻译,您实际上位于球体的边缘。这意味着多边形实际上会通过“你”。由于靠近剪切平面,您将看不到任何东西,并且由于背面剔除,您看不到球体另一侧的 tris。

设置:

glCullFace( GL_FRONT );

或者设置变换以将球体推得更远:

glTranslatef(0, 0, -200);
于 2012-11-27T18:59:54.957 回答