OpenGL question: material and scaleing

Hi all,

I’ve got the following problem: I draw a simple triangle and set a material for it. Than I scale the triangle with glScalef. If the scale factor is 1 everything is fine, but if I decrease the scale factor the triangle gets brighter up to white. If I increase the scale factor the triangle gets darker. I’ve noticed that the shininess of the material influences the color. But how can it be that scaleing influences the material or lighting? I’ve got the feeling that the answer is quite simple, but I don’t see what it is. Below is a code snippet. It’s plain C but I’ve also tried it with JOGL and the result is the same:

`
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void myinit (void)
{
glShadeModel (GL_SMOOTH); /* GL_SMOOTH is the default /
glEnable (GL_LIGHTING); /
GL_SMOOTH is the default */
glEnable(GL_LIGHT0);

float light_ambient[] = {0.0, 0.0, 0.0, 1.0}; 
float light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
float light_specular[] = {1.0, 1.0, 1.0, 1.0};
  
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); // specify the ambient component
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); // specify the diffuse component
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); // specify the specular component

float ambient[] = {0.20999968,0.0357954,0.00238636,0.0}; 
float diffuse[] = {0.88,0.15,0.01,1.0}; 
float emission[] = {0.0,0.0,0.0,0.0}; 
float specular[] = {0.19,0.03,0.03,0.0}; 

glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, ambient );
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse );
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emission );
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialf( GL_FRONT, GL_SHININESS, 10.24 );

}

void triangle(void)
{
glPushMatrix();
glScalef( 0.1, 0.1, 0.1 );
glBegin (GL_TRIANGLES);
glVertex2f (5.0, 5.0);
glVertex2f (25.0, 5.0);
glVertex2f (5.0, 25.0);
glEnd ();
glPopMatrix();
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
triangle ();
glutSwapBuffers();
}

void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 30.0, 0.0,
30.0 * (float) h/(float) w);
else
gluOrtho2D (0.0, 30.0 * (float) w/(float) h, 0.0,
30.0);
glMatrixMode(GL_MODELVIEW);
}

int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow(“colors”);

myinit();

glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMainLoop();

return 0; /* ANSI C requires main to return int. */
}

`

Thanks in advance,
gerd

I think that this has something to do with the fact that scaling the triangle actually affects its vertices world coordinates and thus the specular highlight computation (since the specular computation is affected by the current viewpoint, vertex position and light position).

Hope this makes some sense.

Edit: You could try to place the light at some distance from the triangle to the direction of the triangle normal. That way, the specular highlights should be equally strong with all scales of the triangle.

The modelview matrix affects both positions and normals. If the MV matrix contains a scale factor other than 1.0, 1.0, 1.0, you end up with unnormalized normals, which break the lighting calculations. Have a look at GL_RESCALE_NORMAL and GL_NORMALIZE. Both are controlled by glEnable/glDisable.

You need to specify the vertex normals if your going to use lighting.

Thanks you very much :slight_smile: GL_RESCALE_NORMAL made it.

gerd