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.
Page Index Toggle Pages: 1
openGL lighting... (Read 2204 times)
openGL lighting...
Feb 23rd, 2008, 4:31pm
 
Hey all,

I'm trying to get some openGL stuffs working (trying to go around the inbuilt processing library in the hope of achieving Fancier Effects) but I seem to be stumbling all over the place. I've been trying to get some basic lighting into one of the openGL examples - it runs, but er... doesn't seem to lighting the spinning object. Any ideas?  

Also, sorry for not using "Copy for Discourse" - it was throwing up an error! My lucky day eh?

Thanks a lot,

s.

import javax.media.opengl.*;
import processing.opengl.*;
import java.nio.*;
import com.sun.opengl.util.*;

float a;

PGraphicsOpenGL pgl;
GL gl;

void setup() {
   
 size(800, 600, OPENGL);
 pgl = (PGraphicsOpenGL) g;  // g may change
 gl = pgl.gl;

 gl.glShadeModel(GL.GL_SMOOTH);
 gl.glClearColor(0.0, 0.0, 0.0, 0.0);
 
 
 FloatBuffer mat_specular = BufferUtil.newFloatBuffer(4);
 mat_specular.put(1.0);
 mat_specular.put(1.0);
 mat_specular.put(1.0);
 mat_specular.put(1.0);
 
 FloatBuffer mat_shininess = BufferUtil.newFloatBuffer(1);
 mat_shininess.put(50.0);
 
 FloatBuffer light_position = BufferUtil.newFloatBuffer(4);
 light_position.put(1.0);
 light_position.put(1.0);
 light_position.put(1.0);
 light_position.put(0.0);
 
 FloatBuffer white_light = BufferUtil.newFloatBuffer(4);
 white_light.put(1.0);
 white_light.put(1.0);
 white_light.put(1.0);
 white_light.put(1.0);
 
 FloatBuffer lmodel_ambient = BufferUtil.newFloatBuffer(4);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 
 gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular);
 gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, white_light);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, white_light);
 gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
 
 gl.glEnable(GL.GL_LIGHTING);
 gl.glEnable(GL.GL_LIGHT0);
 gl.glEnable(GL.GL_DEPTH_TEST);  
 
}

void draw() {

 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 gl = pgl.beginGL();  // always use the GL object returned by beginGL
 
 gl.glColor4f(0.7, 0.7, 0.7, 0.8);
 gl.glTranslatef(width/2, height/2, 0);
 gl.glRotatef(a, 1, 0, 0);
 gl.glRotatef(a*2, 0, 1, 0);
 gl.glRectf(-200, -200, 200, 200);
 gl.glRotatef(90, 1, 0, 0);
 gl.glRectf(-200, -200, 200, 200);
 
 pgl.endGL();
 
 gl.glFlush();
 
 a += 0.5;
}

Re: openGL lighting...
Reply #1 - Feb 23rd, 2008, 5:03pm
 
Hmmm, incidentally, if I move:

gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_DEPTH_TEST);  

to the draw method and set the glClearColor to white instead of black, the object is visible, but it's black and not lit - apparently lights are set to black be default... I don't really understand why it's using the default setting when I'm specifying parameters - I wonder if the FloatBuffers are actually the problem..

Hrmph,

s.
Re: openGL lighting...
Reply #2 - Feb 23rd, 2008, 5:10pm
 
Er... yeah, just got rid of the float buffers and used a float array followed by a int offset (which I set to 0 as I don't really understand what it does...) and that er... fixed it. I feel quite embarrassed now. But anyway, here's the working code! Hope it at least saves someone else a headache!

s.


import javax.media.opengl.*;
import processing.opengl.*;
import java.nio.*;
import com.sun.opengl.util.*;

float a;

PGraphicsOpenGL pgl;
GL gl;
GLUT glut = new GLUT();;

FloatBuffer mat_specular, mat_shininess, light_position, white_light, lmodel_ambient;

void setup() {
   
 size(800, 600, OPENGL);
 pgl = (PGraphicsOpenGL) g;  // g may change
 gl = pgl.gl;

 gl.glShadeModel(GL.GL_SMOOTH);
 gl.glClearColor(1.0, 1.0, 1.0, 1.0);
 
 mat_specular = BufferUtil.newFloatBuffer(4);
 mat_specular.put(1.0);
 mat_specular.put(0.0);
 mat_specular.put(0.0);
 mat_specular.put(1.0);
 
 mat_shininess = BufferUtil.newFloatBuffer(1);
 mat_shininess.put(500.0);
 
 light_position = BufferUtil.newFloatBuffer(4);
 light_position.put(1.0);
 light_position.put(1.0);
 light_position.put(1.0);
 light_position.put(1.0);
 
 white_light = BufferUtil.newFloatBuffer(4);
 white_light.put(1.0);
 white_light.put(1.0);
 white_light.put(1.0);
 white_light.put(1.0);
 
 lmodel_ambient = BufferUtil.newFloatBuffer(4);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 lmodel_ambient.put(1.0);
 
 gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, new float[]{1.0, 0.0, 0.0, 1.0}, 0);
 gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, new float[]{0.0, 0.0, 1.0, 1.0}, 0);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{1.0, 1.0, 1.0, 1.0}, 0);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, new float[]{1.0, 1.0, 1.0, 1.0}, 0);
 gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, new float[]{1.0, 1.0, 1.0, 1.0}, 0);
 gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, new float[]{1.0, 0.0, 0.0, 1.0}, 0);
 
}

void draw() {

 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
   
 gl.glEnable(GL.GL_LIGHTING);
 gl.glEnable(GL.GL_LIGHT0);
 gl.glEnable(GL.GL_DEPTH_TEST);  

 
 gl = pgl.beginGL();  // always use the GL object returned by beginGL
   
 gl.glColor4f(0.7, 0.7, 0.7, 0.8);
 gl.glTranslatef(width/2, height/2, 0);
 gl.glRotatef(a, 1, 0, 0);
 gl.glRotatef(a*2, 0, 1, 0);
 gl.glRectf(-200, -200, 200, 200);
 gl.glRotatef(90, 1, 0, 0);
 gl.glRectf(-200, -200, 200, 200);
 
 pgl.endGL();
 
 gl.glFlush();
 
 a += 0.5;
}

Re: openGL lighting...
Reply #3 - Feb 23rd, 2008, 5:26pm
 
You need to .rewind() FloatBuffers before using them I believe.

It should also be noted, that you don't need most of that code. Processing does almost all of those GL functions for you behind the scenes.
Re: openGL lighting...
Reply #4 - Feb 23rd, 2008, 7:36pm
 
Ah, cheers for the .rewind tip, I'll give it a whirl.

Yes, I know processing can easily do everything above, but there are many things that it can't easily do that I plan to use in my forthcoming patches - accumulation buffer effects such as motion blur and full GLSL support being the 2 most important. Why use Processing at all then right? Well, i prefer Java to C++ (not to mention to lovely environment)! And Processing's built in graphics API makes it quick and easy to prototype/code logic. Also, I get to bin Jitter.

Cheers!

s.
Re: openGL lighting...
Reply #5 - Feb 23rd, 2008, 7:48pm
 
You can mix-and-match procesing's methods and pure GL. e.g. you can use background()/specular()/diffuse() etc and then go into pure GL mode to do fancy things.

To do accumulation buffers you'll have to build a custom version of processing.. The PGraphicsOpenGL renderer doesn't have that capability turned on.

As for GLSL: http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1199877349;start=2#2
Re: openGL lighting...
Reply #6 - Feb 23rd, 2008, 8:10pm
 
Hi,

Thanks for the heads up on the accumulation buffer - if processing doesn't support it then I guess I just won't be able to use it. The idea of learning C++ makes me quite ill. Maybe it's possible to capture the screen as a texture and feed it back through a GL shader to achieve a similar effect (much as you do in Jitter). Also, thanks for the link, that'll keep me busy for a while ;)

Cheers,

s.
Page Index Toggle Pages: 1