We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › OPENGL lighting GL_LIGHT0 Issues
Page Index Toggle Pages: 1
OPENGL lighting GL_LIGHT0 Issues (Read 1725 times)
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();
}
Re: OPENGL lighting GL_LIGHT0 Issues
Reply #1 - Mar 24th, 2008, 11:27pm
 
I seem to have found the problem,if i rename  GL_LIGHT0 to GL_LIGHT1 it works, anyone know why?
Re: OPENGL lighting GL_LIGHT0 Issues
Reply #2 - Mar 24th, 2008, 11:34pm
 
I'm not sure why, but it seems that any value over 0 is giving full brigtness on that channel. So if your light is purely red and ambient is 0, the quad is red.

Also FWIW, you can dump a lot of those GL calls since processing does them itself internally, so the following works just as well as your original code:
Code:
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;

//////////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.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0); // THIS LIGHT DOESN'T WORK
float[] ambientlight =new float[]{
0,0,0,1 };
float[] diffuselight = new float[]{
0.0, 0.01, 0.0001, 1.0 }; ///red light so i know when it works
float[] lightpos = new float[]{
-50.f, 50.0f, 100.0f, 1.0f };
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);

////////////// QUAD //////////////
pgl.beginGL();
gl.glRotatef(frameCount, 1,1,1);
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();
}
Re: OPENGL lighting GL_LIGHT0 Issues
Reply #3 - Mar 25th, 2008, 12:26am
 
so basically don't use channel 0.  As far as the other GL calls i take it processing sets these states internally, is there any sort of list of opengl states that processing uses by default?.  gl.glMaterialfv and gl.glColorMaterial commands don't seem to do anything. for example i set

gl.glMaterialfv(GL.GL_FRONT_AND_BACK,GL.GL_AMBIENT_AND_DIFFUSE, new float[]{0.0f,0.0f,1.0f,1.0f},0);

and it doesn't do anything, the poly is still only being affected by lights on one side and the color doesn't change. gl.glColor3f seems to work fine though.  I suspect that processing has internal states that over ride everything else.  I'm starting to wonder if i shouldn't be doing this stuff in c++, but i cringe at the thought.
Re: OPENGL lighting GL_LIGHT0 Issues
Reply #4 - Mar 25th, 2008, 12:55am
 
Before every draw call, the following ones are done:
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
if (hints[DISABLE_DEPTH_TEST]) {
gl.glDisable(GL.GL_DEPTH_TEST);
} else {
gl.glEnable(GL.GL_DEPTH_TEST);
}
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glFrontFace(GL.GL_CW);
gl.glEnable(GL.GL_COLOR_MATERIAL);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR);
Re: OPENGL lighting GL_LIGHT0 Issues
Reply #5 - Mar 25th, 2008, 1:22am
 
Thanks john you've been extremely helpful. I need to sleep and let my brain digest all this info before  i make any more stupid mistakes.

peace.
Page Index Toggle Pages: 1