mr_filth
Ex Member
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; }