We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I changed an older OpenGL example to draw triangles instead of drawing points (below). Can somebody tell me how I incorporate lights into this scene? I don't need anything fancy; something that replicates processing's usual lights() function would be perfect.
Thanks for your suggestions! Fred
ps. On another note, can PGL also draw quads instead of triangles?
import javax.media.opengl.GL2;
import java.nio.*;
int nvert = 1000;
int SIZEOF_INT = Integer.SIZE / 8;
int SIZEOF_FLOAT = Float.SIZE / 8;
PGL pgl;
IntBuffer vboName;
FloatBuffer vertData;
void setup() {
size(640, 360, P3D);
createGeometry();
initVBO();
}
void draw() {
background(0);
translate(320,180);
rotate(frameCount * 0.01, width, height, 0);
pgl = beginPGL();
GL2 gl2 = ((PJOGL)pgl).gl.getGL2();
pgl.bindBuffer(PGL.ARRAY_BUFFER, vboName.get(0));
gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl2.glVertexPointer(3, PGL.FLOAT, 7 * SIZEOF_FLOAT, 0);
gl2.glColorPointer(4, PGL.FLOAT, 7 * SIZEOF_FLOAT, 3 * SIZEOF_FLOAT);
pgl.drawArrays(PGL.TRIANGLES, 0, nvert/3);
gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl2.glDisableClientState(GL2.GL_COLOR_ARRAY);
pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
endPGL();
if (frameCount % 60 == 0) println("fps: " + frameRate);
}
void createGeometry() {
float[] temp = new float[nvert * 7];
for (int n = 0; n < nvert; n++) {
// position
temp[n * 7 + 0] = random(-100, +100);
temp[n * 7 + 1] = random(-100, +100);
temp[n * 7 + 2] = random(-100, +100);
// color
temp[n * 7 + 3] = 1;
temp[n * 7 + 4] = 1;
temp[n * 7 + 5] = 1;
temp[n * 7 + 6] = 1;
}
vertData = allocateDirectFloatBuffer(nvert * 7);
vertData.rewind();
vertData.put(temp);
vertData.position(0);
}
void initVBO() {
vboName = allocateDirectIntBuffer(1);
pgl = beginPGL();
pgl.genBuffers(1, vboName);
pgl.bindBuffer(PGL.ARRAY_BUFFER, vboName.get(0));
pgl.bufferData(PGL.ARRAY_BUFFER, nvert * 7 * SIZEOF_FLOAT, vertData, PGL.STATIC_DRAW);
pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
endPGL();
}
IntBuffer allocateDirectIntBuffer(int n) {
return ByteBuffer.allocateDirect(n * SIZEOF_INT).order(ByteOrder.nativeOrder()).asIntBuffer();
}
FloatBuffer allocateDirectFloatBuffer(int n) {
return ByteBuffer.allocateDirect(n * SIZEOF_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
}
Answers
Hello, the following method creates two OpenGL lights (for further knowledge, see ambient, diffuse, spot and pointLight reference). So.. putting a call to this in your
setup
method should do the trick ;) :}
have fun.
capturevision.
Site : capturevision.wordpress.com/
Flickr : www.flickr.com/photos/capturevision/sets
Vimeo : vimeo.com/capturedvision/