Low-level GL and lights?

edited October 2014 in GLSL / Shaders

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 setupmethod should do the trick ;) :

        public void initLights()
        {
            float[] lightAmbient =
            {
                0.1f, 0.1f, 0.1f, 1.0f
            };
            float[] lightDiffuse =
            {
                0.7f, 0.7f, 0.7f, 1.0f
            };
            float[] lightSpecular =
            {
                0.5f, 0.5f, 0.5f, 1.0f
            };
            float[] lightPosition =
            {
                //-1.5f, 1.0f, -4.0f, 1.0f
                800.0f, 4.0f, 700.0f, 1.0f
            };
    
            // Light model parameters:
            float lightModelAmbient[] =
            {
                0.0f, 0.0f, 0.0f, 0.0f
            };
            gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, FloatBuffer.wrap(lightModelAmbient));
    
            gl.glLightModelf(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
            gl.glLightModelf(GL2.GL_LIGHT_MODEL_TWO_SIDE, 0.0f);
    
      // SETUP LIGHT 1 (Directional ? Light )
            // Common properties : ambient, diffuse, specular, position (which is direction in this case I think).
            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightAmbient, 0);
            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, lightDiffuse, 0);
            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightSpecular, 0);
            gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPosition, 0);
            gl.glEnable(GL2.GL_LIGHTING);
            gl.glEnable(GL2.GL_LIGHT1);
    
            // Don't know what it does..
            gl.glEnable(GL2.GL_NORMALIZE);
    
      // SETUP LIGHT 0 ( Spot Light )
            // Spotlight Parameters..
            float spotDirection[] =
            {
                1.0f, -1.0f, -1.0f
            };
            int spotExponent = 30;
            int spotCutoff = 180;
    
            gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPOT_DIRECTION, FloatBuffer.wrap(spotDirection));  // Direction
            gl.glLighti(GL2.GL_LIGHT0, GL2.GL_SPOT_EXPONENT, spotExponent);                     // Exponent
            gl.glLighti(GL2.GL_LIGHT0, GL2.GL_SPOT_CUTOFF, spotCutoff);                       // CutOff
    
            float contantAttenuation = 1.0f;
            float linarAttenuation = 0.0f;
            float quadraticAttenuation = 0.0f;
    
            gl.glLightf(GL2.GL_LIGHT0, GL2.GL_CONSTANT_ATTENUATION, contantAttenuation);              // ContantAttenuation
            gl.glLightf(GL2.GL_LIGHT0, GL2.GL_LINEAR_ATTENUATION, linarAttenuation);                  // LinarAttenuation
            gl.glLightf(GL2.GL_LIGHT0, GL2.GL_QUADRATIC_ATTENUATION, quadraticAttenuation);           // QuadraticAttenuation
    
            // Common properties (ambient, diffuse, specular, position)..
            gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, FloatBuffer.wrap(lightPosition));
            gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, FloatBuffer.wrap(lightAmbient));
            gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, FloatBuffer.wrap(lightDiffuse));
            gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, FloatBuffer.wrap(lightSpecular));
    
            // ENABLE LIGHT0 AND LIGHT 1
            gl.glEnable(GL2.GL_LIGHTING);
    
            gl.glEnable(GL2.GL_LIGHT0);
            gl.glEnable(GL2.GL_LIGHT1);
        }
    

    }

    have fun.

    capturevision.

    Site : capturevision.wordpress.com/

    Flickr : www.flickr.com/photos/capturevision/sets

    Vimeo : vimeo.com/capturedvision/

Sign In or Register to comment.