0

我正在尝试用平滑的阴影创建自己的圆环。然而,正常的似乎是错误的。这是我的代码。

GLfloat NoMat[]             =   {   0.0f,   0.0f,   0.0f,   1.0f    };
GLfloat MatAmbient[]        =   {   0.7f,   0.7f,   0.7f,   1.0f    };
GLfloat MatAmbientColor[]   =   {   0.21f,  0.13f,  0.05f,  1.0f    };
GLfloat MatDiffuse[]        =   {   0.71f,  0.43f,  0.18f,  1.0f    };
GLfloat Shine               =   100.0f; 
GLfloat NoShine             =   0.0f;

glMaterialfv(GL_FRONT, GL_AMBIENT, MatAmbientColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, MatDiffuse);
glEnable(GL_NORMALIZE);

glMaterialfv(GL_FRONT, GL_SPECULAR, MatAmbient);
glMaterialf(GL_FRONT, GL_SHININESS, Shine);

int i, j, k;
double s, t, x, y, z;

for (i = 0; i < nsides; i++) {
    glBegin(GL_QUAD_STRIP);
    for (j = 0; j <= rings + 1; j++) {
        for (k = 1; k >= 0; k--) { //for both negative and positive
            s = (i + k) % nsides + 0.5;
            t = j % rings;

            x = (totalRadius + centerRadius * cos(s*2*M_PI/nsides)) * cos(t*2*M_PI/rings);
            z = (totalRadius + centerRadius * cos(s*2*M_PI/nsides)) * sin(t*2*M_PI/rings);
            y = centerRadius * sin(s*2*M_PI/nsides);
            glVertex3f(x, y, z);
            glNormal3f(x, y, z);
        }
    }
    glEnd();
}

但是,照明无法正常工作,因为它的行为类似于平面着色而不是平滑着色。我做了一些谷歌搜索,但显然我需要使用差异。但是,我不太确定如何做到这一点。有人有什么想法吗?

4

1 回答 1

4

您正在使用顶点位置作为法线?这仅(且仅)适用于单位半径球体

您需要计算正常值。那是

δ{x,y,z}/δs × δ{x,y,z}/δt

圆环的好处是,您可以在纸上轻松评估它,为您提供精确的公式,而不是使用数值近似。

于 2011-10-15T09:44:29.403 回答