Handmade Hero»Forums»Code
Ameen Sayegh
51 posts
Specular light in OpenGL
Hey everyone,

I'm trying to learn about lighting using OpenGL.
I'm using the fixed APIs (no shaders yet) and already stumbling.
What I'm actually doing is making sure that I understand how light is being simulated, in OpenGL implementation (meaning the 'hacky' light equation Page 62).

Emission light, ambient light and diffuse light are relatively easy to understand and the results I've got is what I expected.
My problem is with Specular light.
My understanding of specular light is a shiny spot on the reflecting surface when the angle of the incoming light is equal to the angle that the eye vector make with the surface at the reflection point.
So I setup a test using: rectangle facing the camera, a light in the middle of the screen facing the rectangle, and I set all the other light factors to zero. so it is clear.
But it is not working.
Also, the parameter GL_SHININESS is an exponent so when I increase it, it should increase the shininess instead it reduces the shininess here.
I might be just not using the APIs correctly.
Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	glViewport(0, 0, Width, Height);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(50.0f, (float)Width/Height, 1.0f, 100.0f);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, V3(0,0,-1).e);
	glLightfv(GL_LIGHT0, GL_POSITION, V3(0.0f, 0.0f, -1.1f).e);

	glLightfv(GL_LIGHT0, GL_AMBIENT,  V4(0.0f, 0.0f, 0.0f, 1.0f).e);
	glLightfv(GL_LIGHT0, GL_DIFFUSE,  V4(0.0f, 0.0f, 0.0f, 1.0f).e);
	glLightfv(GL_LIGHT0, GL_SPECULAR, V4(0.0f, 1.0f, 0.0f, 1.0f).e);

	glMaterialfv(GL_FRONT, GL_EMISSION,  V4(0.0f, 0.0f, 0.0f, 1.0f).e);
	glMaterialfv(GL_FRONT, GL_AMBIENT,   V4(0.0f, 0.0f, 0.0f, 1.0f).e);
	glMaterialfv(GL_FRONT, GL_DIFFUSE,   V4(0.0f, 0.0f, 0.0f, 1.0f).e);
	glMaterialfv(GL_FRONT, GL_SPECULAR,  V4(1.0f, 1.0f, 1.0f, 1.0f).e);
	glMaterialf (GL_FRONT, GL_SHININESS, 10.0f);

	glBegin(GL_QUADS);

	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f(-0.5f, -0.5f, -1.9f);
	glVertex3f(0.5f, -0.5f, -1.9f);
	glVertex3f(0.5f, 0.5f, -1.9f);
	glVertex3f(-0.5f, 0.5f, -1.9f);

	glEnd();

	SwapBuffers(DC);
511 posts
Specular light in OpenGL
This is something a lot of people would just say to learn shaders and the actual math involved.

Knowing which ancient magic knob in an antiquated API to turn to make stuff happen isn't worth the trouble over learning over the actual math that happens per pixel.

Shaders are much easier to translate across apis than the opengl 1.2 FFP calls.

That said specular light in opengl is I believe Phong shading. The multiplier is derived from pow(dot(eye, reflect(normal, light)), exponent). aka the cosine between the ideal reflect and the eye vector raised to a power. The higher the power the faster it drops to 0 and tightens the relfection highlight.