sgsrules
Full Member
Offline
Posts: 108
Texas
OPENGL lighting GL_LIGHT0 Issues
Mar 24th , 2008, 10:55pm
Hi everyone, I've been having issues with some of the native opengl lights. I can get the ambient light to work fine but as soon as i enable GL_LIGHT0 all my polys go completely white or nothing happens at all. At first i though i wasn't calculating my normals correctly on a model but i tried lighting a single quad with normals assigned and i get the same results. The OPENGL tuts from the book i'm reading says that i should place my gl.glEnable calls in the setup so that they run at startup but they only seem to work if i place them in my draw window, i take it it's because the book was written for c++, could someone please clarify this? I created quick sketch with a single quad in order to demonstrate. I also created a function at the bottom of the sketch that uses some of toxi's awesome vec3d library so that i could calculate normals easily, i'm not sure if my math is right or if this is the best way to do this. Any help would be greatly apprecited. import toxi.geom.*; import processing.opengl.*; import javax.media.opengl.*; float a; PGraphicsOpenGL pgl; GL gl; void setup() { size(400, 400, OPENGL); hint( ENABLE_OPENGL_4X_SMOOTH ); ////////////////GL STUFF////////////// pgl = (PGraphicsOpenGL) g; gl = pgl.gl; gl.glEnable(GL.GL_DEPTH_TEST); gl.glShadeModel(GL.GL_SMOOTH); gl.glClearColor(0, 0, 0, 1.0); ///////////LIGHTS////////////// float[] ambientlight =new float[]{ 0.3f, 0.3f, 0.6f, 1.0f }; float[] diffuselight = new float[]{ 1.0f, 0.0f, 0.0f, 1.0f }; ///red light so i know when it works float[] lightpos = new float[]{ -50.f, 50.0f, 100.0f, 1.0f }; gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT,ambientlight, 0); //THIS LIGHT WORKS gl.glLightfv(GL.GL_LIGHT0,GL.GL_AMBIENT,ambientlight, 0); gl.glLightfv(GL.GL_LIGHT0,GL.GL_DIFFUSE,diffuselight, 0); gl.glLightfv(GL.GL_LIGHT0,GL.GL_POSITION,lightpos, 0); //////////CAMERA//////////////// perspective(50.0, 1.0, 0.5, 1000.0); camera(0, 0, 300.0,0, 0, 0.0, 0.0, 1.0, 0.0); } void draw() { background(0); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glEnable(GL.GL_DEPTH_TEST); gl.glEnable(GL.GL_LIGHTING); gl.glEnable(GL.GL_LIGHT0); // THIS LIGHT DOESN'T WORK gl.glEnable(GL.GL_COLOR_MATERIAL); gl.glColorMaterial(GL.GL_FRONT_AND_BACK,GL.GL_AMBIENT_AND_DIFFUSE); ////////////// QUAD ////////////// pgl.beginGL(); gl.glRotatef(frameCount, 1,1,1); gl.glColor3f(1.0f, 1.0f, 1.0f); gl.glBegin(gl.GL_QUADS); gl.glNormal3f(0, 0, 1.0f); gl.glVertex3f (10, 10, 0); gl.glVertex3f (-10, 10, 0); gl.glVertex3f (-10, -10, 0); gl.glVertex3f (10, -10, 0); gl.glEnd(); pgl.endGL(); } ////////// calculate normal ///////////////// Vec3D getNormal( Vec3D p1, Vec3D p2, Vec3D p3){ Vec3D Normal,nEdge1,nEdge2; nEdge1=p2.sub(p1); nEdge2=p3.sub(p1); Normal=nEdge1.cross(nEdge2); return Normal.normalize(); }