1

我一直在到处寻找如何做到这一点,并查看了 opengl 的红皮书,它只讨论了对 2d 多边形进行平滑着色。我不确定您将如何为 glutSolidSphere 进行平滑着色。我知道你必须做 glShadeModel。当场景没有光线时,我如何区分它是平坦的还是光滑的。

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
    glClearColor(0,0,0,1);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,1,100);
    //glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,10,0);
    glVertex3f(10,10,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0,10,0);
    glVertex3f(0,10,10);
    glVertex3f(10,10,10);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glVertex3f(10,10,0);
    glVertex3f(10,10,10);
    glVertex3f(10,0,10);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    //draw cone
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    glutSolidCone(cone,5,10,2);
    //draw sphere
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    glutSolidSphere(sphere,500,500);

}
void move(unsigned char key, int x, int y){
    switch(key){
        case 'y': 
            viewy++;
            glutPostRedisplay();
            break;
        case 'x':
            viewx++;
            glutPostRedisplay();
            break;
        case 'z':
            viewz++;
            glutPostRedisplay();
            break;
        //moves head
        case 'd':
            headup--;
            headright--;
            glutPostRedisplay();
            break;
        case 'a':
            headup++;
            headright++;
            glutPostRedisplay();
            break;
    }
}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    drawRoom();
    drawObjects();
    glutSwapBuffers();
    glFlush();
}
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Room");
    myinit();
    glutDisplayFunc(display);
    glutKeyboardFunc(move);
    glutMotionFunc(moveHead);
    glutMainLoop();
    return 0;
}
4

1 回答 1

1

您需要启用照明,启用至少一个灯光,并为您的几何体提供不同的每个顶点法线以查看GL_SMOOTH.


...当场景没有光线时,我如何区分它是平坦的还是光滑的。

您可以通过以下方式检索当前的着色模型glGetIntegerv()

GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );
于 2012-11-20T19:09:23.357 回答