opengl中的平滑着色

时间:2012-11-20 18:53:32

标签: c opengl

我一直在寻找如何做到这一点,并查看了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;
}

1 个答案:

答案 0 :(得分:1)

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


  

...当场景没有光线时,我怎么能分辨它是否平坦或平滑。

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

GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );