Pregunta

Tengo un programa OpenGL ejecutándose y muestra geometría, pero es todo "plano", un tono gris, sin sombras difusas ni reflejos especulares:

Tori with no shading

En la foto aparecen tres tori, cada uno hecho de tiras cuádruples.Deberíamos ver sombras, pero no es así.¿Qué estoy haciendo mal?

Aquí está el código donde configuro los vértices y las normales (draw_torus() se llama para construir una lista de visualización):

/* WrapTorus, adapted from
    http://www.math.ucsd.edu/~sbuss/MathCG/OpenGLsoft/WrapTorus/WrapTorus.html
    by Sam Buss */

/*
 * Issue vertex command for segment number j of wrap number i.
 * Normal added by Lars Huttar.
 * slices1 = numWraps; slices2 = numPerWrap.
 */
void putVert(float i, float j, float slices1, float slices2, float majR, float minR) {
    float wrapFrac = j / slices2;
    /* phi is rotation about the circle of revolution */
    float phi = PI2 * wrapFrac;
    /* theta is rotation about the origin, in the xz plane. */
    float theta = PI2 * (i + wrapFrac) / slices1;
    float y = minR * (float)sin(phi);
    float r = majR + minR * (float)cos(phi);
    float x = (float)sin(theta) * r;
    float z = (float)cos(theta) * r;
    /* normal vector points to (x,y,z) from: */
    float xb = (float)sin(theta) * majR;
    float zb = (float)cos(theta) * majR;
    glNormal3f(x - xb, y, z - zb);
    glVertex3f(x, y, z);
}

static void draw_torus(int numPerWrap, int numWraps, float majR, float minR) {
    int i, j;
    glBegin( GL_QUAD_STRIP );
    for (i=0; i < numWraps; i++ ) {
        for (j=0; j < numPerWrap; j++) {
            putVert((float)i, (float)j, (float)numWraps, (float)numPerWrap, majR, minR);
            putVert((float)(i + 1), (float)j, (float)numWraps, (float)numPerWrap, majR, minR);
        }
    }
    putVert(0.0, 0.0, (float)numWraps, (float)numPerWrap, majR, minR);
    putVert(1.0, 0.0, (float)numWraps, (float)numPerWrap, majR, minR);
    glEnd();
}

¿Hay algún problema con el orden de los vértices?

Aquí está la parte de la función init donde se construye la lista de visualización:

GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat color[4] = { 0.5, 0.6, 0.7, 1.0 };
    ...

glShadeModel(GL_SMOOTH);

torusDL = glGenLists (1);
glNewList(torusDL, GL_COMPILE);
setMaterial(color, white, 100);
draw_torus(8, 45, 1.0, 0.05);
glEndList();

donde setMaterial() simplemente hace:

static void setMaterial(const GLfloat color[3], const GLfloat hlite[3], int shininess) {
    glColor3fv(color);
    glMaterialfv(GL_FRONT, GL_SPECULAR, hlite);
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
    glMateriali(GL_FRONT, GL_SHININESS, shininess); /* [0,128] */
}

Aquí se muestra la iluminación que también se realiza durante la inicialización:

  GLfloat pos[4] = {0.4, 0.2, 0.4, 0.0};
  GLfloat amb[4] = {0.2, 0.2, 0.2, 1.0};
  GLfloat dif[4] = {1.0, 1.0, 1.0, 1.0};
  GLfloat spc[4] = {1.0, 1.0, 1.0, 1.0};
  GLfloat color[4] = {0.20, 0.20, 0.20, 1.00};
  GLfloat spec[4]  = {0.30, 0.30, 0.30, 1.00};
  GLfloat shiny    = 8.0;

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);

  glLightfv(GL_LIGHT0, GL_POSITION, pos);
  glLightfv(GL_LIGHT0, GL_AMBIENT,  amb);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,  dif);
  glLightfv(GL_LIGHT0, GL_SPECULAR, spc);

  glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
  glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec);
  glMaterialf  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny);

Aquí es donde se llama a la lista de visualización, en la función de dibujo:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();       

glScalef(3.5, 3.5, 3.5);

for (i = 0; i < ac->nrings; i++) {
    glScalef(0.8, 0.8, 0.8);
    glRotatef(...);
    glCallList(torusDL);
}

glFlush();
glPopMatrix();
glXSwapBuffers(dpy, window);

El archivo fuente .c completo para este "truco glx" es aquí.En caso de que haga una diferencia, este código está en el contexto de salvapantallas.

¿Fue útil?

Solución

Lars, como has visto, glEnable(GL_NORMALIZE) normaliza (sorpresa) los vectores normales previos a las transformaciones utilizadas para los cálculos de iluminación (en tuberías de función fija).Estos cálculos se basan en las normales de longitud unitaria para obtener resultados correctos.

Vale la pena señalar que las transformaciones aplicadas a vectores normales son no Lo mismo que las transformaciones aplicadas a la geometría de vértices.La especificación OpenGL 2.1 describe la transformación, al igual que muchos otro recursos.Como vector, una normal tiene la representación homogénea: [nx, ny, nz, 0] - un punto en el 'infinito' y una forma matemáticamente elegante de unificar operaciones matriciales y de 4 vectores en el proceso GL.

Por supuesto, puede realizar esta normalización usted mismo y puede ser más eficiente hacerlo, ya que su geometría de toro solo se genera una vez para una lista de visualización precompilada:

nx = x - b, ny = y, nz = z - zb;
nl = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
glNormal3f(nx * nl, ny * nl, nz * nl);

Asegúrese de verificar (nl) para la división por cero (o algún épsilon), si es posible.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top