I've been trying to do some simple directional lighting in OpenGL (directly) and I just can't get it to work. For some reason, although the shading changes on the two rectangles that I'm drawing, they are always the same, despite the shapes being at right angles to eachother. Am I overlooking anything simple, and are there any examples of this working? (I don't want to use the default processing lights() because I am hoping to try to get bump mapping and some other things that need GL directly working eventually).
Here's a stripped down version of my program that highlights the problem:
- import processing.opengl.*;
- import javax.media.opengl.*;
- PGraphicsOpenGL pgl;
- GL gl;
- void setup() {
- size(640,480,OPENGL);
- pgl = (PGraphicsOpenGL) g;
- gl = pgl.beginGL();
- }
- void draw() {
- background(0);
- camera(70.0, 35.0, 120.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- initLight(gl);
- gl = pgl.beginGL();
- gl.glPushMatrix();
- gl.glRotatef(float(frameCount),0,1,1);
- gl.glColor3f(1.0,1.0,1.0);
- gl.glBegin(GL.GL_QUADS);
- gl.glVertex3f(0,50,0);
- gl.glVertex3f(50,50,0);
- gl.glVertex3f(50,0,0);
- gl.glVertex3f(0,0,0);
- gl.glEnd();
- gl.glBegin(GL.GL_QUADS);
- gl.glVertex3f(0,50,0);
- gl.glVertex3f(0,50,50);
- gl.glVertex3f(0,0,50);
- gl.glVertex3f(0,0,0);
- gl.glEnd();
- gl.glPopMatrix();
- pgl.endGL();
- gl.glFlush();
- if(frameCount`==0) println(frameRate);
- }
- public void initLight( GL gl)
- {
- float mat_specular[] = {
- 1.0, 1.0, 1.0, 1.0
- };
- float mat_shininess[] = {
- 50.0
- };
- float light_position2[] = {
- 1.0, 120.0, 120.0, 0.0
- };
- gl.glClearColor(0.0, 0.0, 0.0, 0.0);
- //gl.glShadeModel(GL.GL_SMOOTH);
- gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular,0);
- gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess,0);
- gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position2,0);
- gl.glEnable(GL.GL_LIGHTING);
- gl.glEnable(GL.GL_LIGHT0);
- gl.glEnable(GL.GL_DEPTH_TEST);
- }
1