【问题标题】:Smooth Shading in openglopengl中的平滑阴影
【发布时间】:2019-05-09 22:22:51
【问题描述】:

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

【问题讨论】:

  • 不要使用固定功能管道(1992 年发布!),使用 Shaders、Vertex-Buffers 和其他新东西。 OpenGL 2.0 已于 2004 年发布,每款显卡都支持着色器。 A.e.我帮不了你,因为我从不使用固定功能管道。
  • “平滑着色”是什么意思?

标签: c opengl


【解决方案1】:

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


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

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

GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2015-02-23
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多