private static void clearEnabledAttributes(final ShaderObjectsStateRecord record, final GL gl) {
   // go through and disable any enabled attributes
   if (!record.enabledAttributes.isEmpty()) {
     for (int i = 0, maxI = record.enabledAttributes.size(); i < maxI; i++) {
       final ShaderVariable var = record.enabledAttributes.get(i);
       if (var.getSize() == 1) {
         if (gl.isGL2()) {
           gl.getGL2().glDisableVertexAttribArrayARB(var.variableID);
         } else {
           if (gl.isGL2ES2()) {
             gl.getGL2ES2().glDisableVertexAttribArray(var.variableID);
           }
         }
       } else {
         for (int j = 0, maxJ = var.getSize(); j < maxJ; j++) {
           if (gl.isGL2()) {
             gl.getGL2().glDisableVertexAttribArrayARB(var.variableID + j);
           } else {
             if (gl.isGL2ES2()) {
               gl.getGL2ES2().glDisableVertexAttribArray(var.variableID + j);
             }
           }
         }
       }
     }
     record.enabledAttributes.clear();
   }
 }
  /** Applies fixed function bindings from the context to OpenGL */
  private void applyFixedFuncBindings(boolean forLighting) {
    GL gl = GLContext.getCurrentGL();
    if (forLighting) {
      gl.getGL2().glMaterialf(GL.GL_FRONT_AND_BACK, GLLightingFunc.GL_SHININESS, context.shininess);
      setMaterialColor(GLLightingFunc.GL_AMBIENT, context.ambient, ColorRGBA.DarkGray);
      setMaterialColor(GLLightingFunc.GL_DIFFUSE, context.diffuse, ColorRGBA.White);
      setMaterialColor(GLLightingFunc.GL_SPECULAR, context.specular, ColorRGBA.Black);

      if (context.useVertexColor) {
        gl.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
      } else {
        gl.glDisable(GLLightingFunc.GL_COLOR_MATERIAL);
      }
    } else {
      // Ignore other values as they have no effect when
      // GL_LIGHTING is disabled.
      ColorRGBA color = context.color;
      if (color != null) {
        gl.getGL2().glColor4f(color.r, color.g, color.b, color.a);
      } else {
        gl.getGL2().glColor4f(1, 1, 1, 1);
      }
    }
    if (context.alphaTestFallOff > 0f) {
      gl.glEnable(GL2ES1.GL_ALPHA_TEST);
      gl.getGL2ES1().glAlphaFunc(GL.GL_GREATER, context.alphaTestFallOff);
    } else {
      gl.glDisable(GL2ES1.GL_ALPHA_TEST);
    }
  }
  public void drawGL2(GL gl) {
    gl.getGL2().glPointSize(width);
    gl.getGL2().glBegin(GL2.GL_POINTS);

    if (points != null) {
      synchronized (points) {
        for (LightPoint p : points) {
          gl.getGL2().glColor4f(p.rgb.r, p.rgb.g, p.rgb.b, p.rgb.a);
          GlVertexExecutor.Vertex(gl, new Coord3d(p.xyz.x, p.xyz.y, p.xyz.z), transformers);
        }
      }
    }
    gl.getGL2().glEnd();
  }
  private static void checkLinkError(final int programId) {
    final GL gl = GLContext.getCurrentGL();

    final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
    final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
    compiled.clear();
    if (gl.isGL2()) {
      gl.getGL2().glGetObjectParameterivARB(programId, GL2ES2.GL_LINK_STATUS, compiled);
    } else {
      if (gl.isGL2ES2()) {
        gl.getGL2ES2().glGetProgramiv(programId, GL2ES2.GL_LINK_STATUS, compiled);
      }
    }
    if (compiled.get(0) == GL.GL_FALSE) {
      if (gl.isGL2()) {
        gl.getGL2().glGetObjectParameterivARB(programId, GL2ES2.GL_INFO_LOG_LENGTH, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glGetProgramiv(programId, GL2ES2.GL_INFO_LOG_LENGTH, compiled);
        }
      }
      final int length = compiled.get(0);
      String out = null;
      if (length > 0) {
        final ByteBuffer infoLogBuf = context.getDirectNioBuffersSet().getInfoLogBuffer();
        final ByteBuffer infoLog;
        if (length <= infoLogBuf.capacity()) {
          infoLog = infoLogBuf;
          infoLogBuf.rewind().limit(length);
        } else {
          infoLog = BufferUtils.createByteBuffer(length);
        }
        if (gl.isGL2()) {
          gl.getGL2().glGetInfoLogARB(programId, infoLog.limit(), compiled, infoLog);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glGetProgramInfoLog(programId, infoLog.limit(), compiled, infoLog);
          }
        }

        final byte[] infoBytes = new byte[length];
        infoLog.get(infoBytes);
        out = new String(infoBytes);
      }

      logger.severe(out);

      // throw new Ardor3dException("Error linking GLSL shader: " + out);
    }
  }
 private void drawElements(int mode, int format, Buffer data) {
   GL gl = GLContext.getCurrentGL();
   switch (format) {
     case GL.GL_UNSIGNED_BYTE:
       gl.getGL2().glDrawElements(mode, data.limit(), format, (ByteBuffer) data);
       break;
     case GL.GL_UNSIGNED_SHORT:
       gl.getGL2().glDrawElements(mode, data.limit(), format, (ShortBuffer) data);
       break;
     case GL.GL_UNSIGNED_INT:
       gl.getGL2().glDrawElements(mode, data.limit(), format, (IntBuffer) data);
       break;
     default:
       throw new UnsupportedOperationException();
   }
 }
  /**
   * Check for program errors. If an error is detected, program exits.
   *
   * @param compilerState the compiler state for a given shader
   * @param id shader's id
   */
  private static void checkProgramError(
      final int compilerState, final int id, final String shaderName) {
    final GL gl = GLContext.getCurrentGL();

    if (compilerState == GL.GL_FALSE) {
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer iVal = context.getDirectNioBuffersSet().getSingleIntBuffer();
      iVal.clear();
      if (gl.isGL2()) {
        gl.getGL2().glGetObjectParameterivARB(id, GL2.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glGetProgramiv(id, GL2ES2.GL_INFO_LOG_LENGTH, iVal);
        }
      }
      final int length = iVal.get(0);
      String out = null;

      if (length > 0) {
        final ByteBuffer infoLogBuf = context.getDirectNioBuffersSet().getInfoLogBuffer();
        final ByteBuffer infoLog;
        if (length <= infoLogBuf.capacity()) {
          infoLog = infoLogBuf;
          infoLogBuf.rewind().limit(length);
        } else {
          infoLog = BufferUtils.createByteBuffer(length);
        }
        if (gl.isGL2()) {
          gl.getGL2().glGetInfoLogARB(id, infoLog.limit(), iVal, infoLog);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glGetProgramInfoLog(id, infoLog.limit(), iVal, infoLog);
          }
        }

        final byte[] infoBytes = new byte[length];
        infoLog.get(infoBytes);
        out = new String(infoBytes);
      }

      logger.severe(out);

      final String nameString = shaderName.equals("") ? "" : " [ " + shaderName + " ]";
      throw new Ardor3dException("Error compiling GLSL shader " + nameString + ": " + out);
    }
  }
 private void setMaterialColor(int type, ColorRGBA color, ColorRGBA defaultColor) {
   GL gl = GLContext.getCurrentGL();
   if (color != null) {
     fb16.put(color.r).put(color.g).put(color.b).put(color.a).flip();
   } else {
     fb16.put(defaultColor.r).put(defaultColor.g).put(defaultColor.b).put(defaultColor.a).flip();
   }
   gl.getGL2().glMaterialfv(GL.GL_FRONT_AND_BACK, type, fb16);
 }
예제 #8
0
  public void update(GL glGeneric) {
    if (!init) return;

    GL2 gl = glGeneric.getGL2();
    contentManager.update(gl);
    ui.update(gl, elapsedMS);
    world.update(gl, elapsedMS, ui);
    drawings.update();
  }
 public void clearVertexAttribs() {
   for (int i = 0; i < 16; i++) {
     VertexBuffer vb = context.boundAttribs[i];
     if (vb != null) {
       int arrayType = convertArrayType(vb.getBufferType());
       GL gl = GLContext.getCurrentGL();
       gl.getGL2().glDisableClientState(arrayType);
       context.boundAttribs[vb.getBufferType().ordinal()] = null;
     }
   }
 }
  public void renderMesh(Mesh mesh, int lod, int count, VertexBuffer[] instanceData) {
    if (mesh.getVertexCount() == 0) {
      return;
    }
    GL gl = GLContext.getCurrentGL();
    if (context.pointSize != mesh.getPointSize()) {
      gl.getGL2().glPointSize(mesh.getPointSize());
      context.pointSize = mesh.getPointSize();
    }
    if (context.lineWidth != mesh.getLineWidth()) {
      gl.getGL2().glLineWidth(mesh.getLineWidth());
      context.lineWidth = mesh.getLineWidth();
    }

    boolean dynamic = false;
    if (mesh.getBuffer(Type.InterleavedData) != null) {
      throw new UnsupportedOperationException("Interleaved meshes are not supported");
    }

    if (mesh.getNumLodLevels() == 0) {
      for (VertexBuffer vb : mesh.getBufferList().getArray()) {
        if (vb.getUsage() != VertexBuffer.Usage.Static) {
          dynamic = true;
          break;
        }
      }
    } else {
      dynamic = true;
    }

    statistics.onMeshDrawn(mesh, lod);

    //        if (!dynamic) {
    // dealing with a static object, generate display list
    //            renderMeshDisplayList(mesh);
    //        } else {
    renderMeshDefault(mesh, lod, count);
    //        }

  }
  /** Removes the tessellation evaluation shader */
  private static void removeTessEvalShader(final GLSLShaderObjectsState state) {
    final GL gl = GLContext.getCurrentGL();

    if (state._tessellationEvaluationShaderID != -1) {
      if (gl.isGL2()) {
        gl.getGL2().glDetachObjectARB(state._programID, state._tessellationEvaluationShaderID);
        gl.getGL2().glDeleteObjectARB(state._tessellationEvaluationShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glDetachShader(state._programID, state._tessellationEvaluationShaderID);
          gl.getGL2ES2().glDeleteShader(state._tessellationEvaluationShaderID);
        }
      }
    }
  }
예제 #12
0
 private long getBufferSizeImpl(int target, int buffer, GL caller) {
   // See whether we know the size of this buffer object; at this
   // point we almost certainly should if the application is
   // written correctly
   long sz = bufferSizeMap.get(buffer);
   if (0 > sz) {
     // For robustness, try to query this value from the GL as we used to
     // FIXME: both functions return 'int' types, which is not suitable,
     // since buffer lenght is 64bit ?
     int[] tmp = new int[1];
     if (0 == target) {
       // DirectState ..
       if (caller.isFunctionAvailable("glGetNamedBufferParameterivEXT")) {
         caller.getGL2().glGetNamedBufferParameterivEXT(buffer, GL.GL_BUFFER_SIZE, tmp, 0);
       } else {
         throw new GLException(
             "Error: getDirectStateBufferSize called with unknown state and GL function 'glGetNamedBufferParameterivEXT' n/a to query size");
       }
     } else {
       caller.glGetBufferParameteriv(target, GL.GL_BUFFER_SIZE, tmp, 0);
     }
     if (tmp[0] == 0) {
       // Assume something is wrong rather than silently going along
       throw new GLException(
           "Error: buffer size returned by "
               + ((0 == target) ? "glGetNamedBufferParameterivEXT" : "glGetBufferParameteriv")
               + " was zero; probably application error");
     }
     // Assume we just don't know what's happening
     sz = (long) tmp[0];
     bufferSizeMap.put(buffer, sz);
     if (DEBUG) {
       System.err.println(
           "GLBufferSizeTracker.getBufferSize(): made slow query to cache size "
               + sz
               + " for buffer "
               + buffer);
     }
   }
   return sz;
 }
  public void initialize() {
    GL gl = GLContext.getCurrentGL();
    if (gl.isExtensionAvailable("GL_VERSION_1_2")) {
      gl12 = true;
    }

    // workaround, always assume we support GLSL100
    // some cards just don't report this correctly
    caps.add(Caps.GLSL100);

    // Default values for certain GL state.
    gl.getGL2ES1().glShadeModel(GLLightingFunc.GL_SMOOTH);
    gl.getGL2().glColorMaterial(GL.GL_FRONT_AND_BACK, GLLightingFunc.GL_DIFFUSE);
    gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

    // Enable rescaling/normaling of normal vectors.
    // Fixes lighting issues with scaled models.
    if (gl12) {
      gl.glEnable(GL2ES1.GL_RESCALE_NORMAL);
    } else {
      gl.glEnable(GLLightingFunc.GL_NORMALIZE);
    }

    if (gl.isExtensionAvailable("GL_ARB_texture_non_power_of_two")) {
      caps.add(Caps.NonPowerOfTwoTextures);
    } else {
      logger.log(
          Level.WARNING,
          "Your graphics card does not "
              + "support non-power-of-2 textures. "
              + "Some features might not work.");
    }

    gl.glGetIntegerv(GL2ES1.GL_MAX_LIGHTS, ib1);
    maxLights = ib1.get(0);

    gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, ib1);
    maxTexSize = ib1.get(0);
  }
예제 #14
0
  @Override
  protected void render(GL g) {
    GL2 gl = g.getGL2();

    gl.glColor3f(.5f, .5f, .5f);

    // =======================================\\
    // Retained Mode \\
    // =======================================\\
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexBuffer);

    if (normal) {
      gl.glEnableClientState(GL2.GL_NORMAL_ARRAY);
      gl.glNormalPointer(GL2.GL_FLOAT, 0, normalBuffer);
    }

    if (texture) {
      gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
      gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureBuffer);
    } else gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);

    for (IntBuffer face : faces) {
      if (face.capacity() == 3)
        gl.glDrawElements(GL2.GL_TRIANGLES, face.capacity(), GL2.GL_UNSIGNED_INT, face);
      else if (face.capacity() == 4)
        gl.glDrawElements(GL2.GL_QUADS, face.capacity(), GL2.GL_UNSIGNED_INT, face);
      else gl.glDrawElements(GL2.GL_POLYGON, face.capacity(), GL2.GL_UNSIGNED_INT, face);
    }

    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);

    if (normal) gl.glDisableClientState(GL2.GL_NORMAL_ARRAY);

    if (texture) gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);

    if (drawBox) bbox.draw(gl);
  }
예제 #15
0
 @Override
 public void setGLContext(GL context) {
   gl = context.getGL2();
 }
예제 #16
0
  /** ******************************************************************* */
  public void draw(GL gl, GLU glu, Camera cam) {
    doTransform(gl, glu, cam);

    applyMaterial(gl); // TODO: shall we avoid calling this @ each draw?
    Coord3d norm = Normal.compute(points.get(0).xyz, points.get(1).xyz, points.get(2).xyz);

    // Draw content of polygon

    if (gl.isGL2()) {

      if (facestatus) {
        gl.getGL2().glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
        if (wfstatus) {
          gl.getGL2().glEnable(GL2.GL_POLYGON_OFFSET_FILL);
          gl.getGL2().glPolygonOffset(1.0f, 1.0f);
        }

        gl.getGL2().glBegin(GL2.GL_POLYGON);
        for (Point p : points) {
          if (mapper != null) {
            Color c = mapper.getColor(p.xyz); // TODO: should store
            // result in the
            // point color
            gl.getGL2().glColor4f(c.r, c.g, c.b, c.a);
          } else gl.getGL2().glColor4f(p.rgb.r, p.rgb.g, p.rgb.b, p.rgb.a);
          gl.getGL2().glVertex3f(p.xyz.x, p.xyz.y, p.xyz.z);
          gl.getGL2().glNormal3f(norm.x, norm.y, norm.z);
        }
        gl.getGL2().glEnd();
        if (wfstatus) gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
      }

      // Draw edge of polygon
      if (wfstatus) {
        gl.getGL2().glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);

        gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
        gl.glPolygonOffset(1.0f, 1.0f);

        gl.getGL2().glColor4f(wfcolor.r, wfcolor.g, wfcolor.b, 1); // wfcolor.a);
        gl.glLineWidth(wfwidth);

        gl.getGL2().glBegin(GL2.GL_POLYGON);
        for (Point p : points) {
          gl.getGL2().glVertex3f(p.xyz.x, p.xyz.y, p.xyz.z);
          gl.getGL2().glNormal3f(norm.x, norm.y, norm.z);
        }
        gl.getGL2().glEnd();
        gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
      }
    } else {

      if (facestatus) {
        GLES2CompatUtils.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
        if (wfstatus) {
          gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
          gl.glPolygonOffset(1.0f, 1.0f);
        }

        GLES2CompatUtils.glBegin(GL2.GL_POLYGON);
        for (Point p : points) {
          if (mapper != null) {
            Color c = mapper.getColor(p.xyz); // TODO: should store
            // result in the
            // point color
            GLES2CompatUtils.glColor4f(c.r, c.g, c.b, c.a);
          } else GLES2CompatUtils.glColor4f(p.rgb.r, p.rgb.g, p.rgb.b, p.rgb.a);
          GLES2CompatUtils.glVertex3f(p.xyz.x, p.xyz.y, p.xyz.z);
          GLES2CompatUtils.glNormal3f(norm.x, norm.y, norm.z);
        }
        GLES2CompatUtils.glEnd();
        if (wfstatus) gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
      }

      // Draw edge of polygon
      if (wfstatus) {
        GLES2CompatUtils.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);

        gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
        gl.glPolygonOffset(1.0f, 1.0f);

        GLES2CompatUtils.glColor4f(wfcolor.r, wfcolor.g, wfcolor.b, 1); // wfcolor.a);
        gl.glLineWidth(wfwidth);

        GLES2CompatUtils.glBegin(GL2.GL_POLYGON);
        for (Point p : points) {
          GLES2CompatUtils.glVertex3f(p.xyz.x, p.xyz.y, p.xyz.z);
          GLES2CompatUtils.glNormal3f(norm.x, norm.y, norm.z);
        }
        GLES2CompatUtils.glEnd();
        gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
      }
    }

    /*
     * // Drawbarycenter Point b = new Point(getBarycentre(), Color.BLUE);
     * b.setWidth(5); b.draw(gl,glu,cam);
     */

  }
  public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
    if (vb.getBufferType() == VertexBuffer.Type.Color && !context.useVertexColor) {
      // Ignore vertex color buffer if vertex color is disabled.
      return;
    }

    int arrayType = convertArrayType(vb.getBufferType());
    if (arrayType == -1) {
      return; // unsupported
    }
    GL gl = GLContext.getCurrentGL();
    gl.getGL2GL3().glEnableClientState(arrayType);
    context.boundAttribs[vb.getBufferType().ordinal()] = vb;

    if (vb.getBufferType() == Type.Normal) {
      // normalize if requested
      if (vb.isNormalized() && !context.normalizeEnabled) {
        gl.glEnable(GLLightingFunc.GL_NORMALIZE);
        context.normalizeEnabled = true;
      } else if (!vb.isNormalized() && context.normalizeEnabled) {
        gl.glDisable(GLLightingFunc.GL_NORMALIZE);
        context.normalizeEnabled = false;
      }
    }

    // NOTE: Use data from interleaved buffer if specified
    Buffer data = idb != null ? idb.getData() : vb.getData();
    int comps = vb.getNumComponents();
    int type = convertVertexFormat(vb.getFormat());

    data.rewind();

    switch (vb.getBufferType()) {
      case Position:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glVertexPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        break;
      case Normal:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glNormalPointer(type, vb.getStride(), (FloatBuffer) data);
        break;
      case Color:
        if (data instanceof FloatBuffer) {
          gl.getGL2().glColorPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        } else if (data instanceof ByteBuffer) {
          gl.getGL2().glColorPointer(comps, type, vb.getStride(), (ByteBuffer) data);
        } else {
          throw new UnsupportedOperationException();
        }
        break;
      case TexCoord:
        if (!(data instanceof FloatBuffer)) {
          throw new UnsupportedOperationException();
        }

        gl.getGL2().glTexCoordPointer(comps, type, vb.getStride(), (FloatBuffer) data);
        break;
      default:
        // Ignore, this is an unsupported attribute for OpenGL1.
        break;
    }
  }
  protected static void sendToGL(
      final GLSLShaderObjectsState state, final ContextCapabilities caps) {
    final GL gl = GLContext.getCurrentGL();

    if (state.getVertexShader() == null && state.getFragmentShader() == null) {
      logger.warning("Could not find shader resources!" + "(both inputbuffers are null)");
      state._needSendShader = false;
      return;
    }

    if (state._programID == -1) {
      if (gl.isGL2()) {
        state._programID = gl.getGL2().glCreateProgramObjectARB();
      } else {
        if (gl.isGL2ES2()) {
          state._programID = gl.getGL2ES2().glCreateProgram();
        }
      }
    }

    if (state.getVertexShader() != null) {
      if (state._vertexShaderID != -1) {
        removeVertShader(state);
      }
      if (gl.isGL2()) {
        state._vertexShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_VERTEX_SHADER);
      } else {
        if (gl.isGL2ES2()) {
          state._vertexShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_VERTEX_SHADER);
        }
      }

      // Create the sources
      final byte array[] = new byte[state.getVertexShader().limit()];
      state.getVertexShader().rewind();
      state.getVertexShader().get(array);
      if (gl.isGL2()) {
        gl.getGL2()
            .glShaderSourceARB(
                state._vertexShaderID,
                1,
                new String[] {new String(array)},
                new int[] {array.length},
                0);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2()
              .glShaderSource(
                  state._vertexShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        }
      }

      // Compile the vertex shader
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
      compiled.clear();
      if (gl.isGL2()) {
        gl.getGL2().glCompileShaderARB(state._vertexShaderID);
        gl.getGL2()
            .glGetObjectParameterivARB(
                state._vertexShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glCompileShader(state._vertexShaderID);
          gl.getGL2ES2().glGetShaderiv(state._vertexShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
        }
      }
      checkProgramError(compiled.get(0), state._vertexShaderID, state._vertexShaderName);

      // Attach the program
      if (gl.isGL2()) {
        gl.getGL2().glAttachObjectARB(state._programID, state._vertexShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glAttachShader(state._programID, state._vertexShaderID);
        }
      }
    } else if (state._vertexShaderID != -1) {
      removeVertShader(state);
      state._vertexShaderID = -1;
    }

    if (state.getFragmentShader() != null) {
      if (state._fragmentShaderID != -1) {
        removeFragShader(state);
      }

      if (gl.isGL2()) {
        state._fragmentShaderID = gl.getGL2().glCreateShaderObjectARB(GL2ES2.GL_FRAGMENT_SHADER);
      } else {
        if (gl.isGL2ES2()) {
          state._fragmentShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_FRAGMENT_SHADER);
        }
      }

      // Create the sources
      final byte array[] = new byte[state.getFragmentShader().limit()];
      state.getFragmentShader().rewind();
      state.getFragmentShader().get(array);
      if (gl.isGL2()) {
        gl.getGL2()
            .glShaderSourceARB(
                state._fragmentShaderID,
                1,
                new String[] {new String(array)},
                new int[] {array.length},
                0);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2()
              .glShaderSource(
                  state._fragmentShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        }
      }

      // Compile the fragment shader
      final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
      final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
      compiled.clear();
      if (gl.isGL2()) {
        gl.getGL2().glCompileShaderARB(state._fragmentShaderID);
        gl.getGL2()
            .glGetObjectParameterivARB(
                state._fragmentShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glCompileShader(state._fragmentShaderID);
          gl.getGL2ES2().glGetShaderiv(state._fragmentShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
        }
      }
      checkProgramError(compiled.get(0), state._fragmentShaderID, state._vertexShaderName);

      // Attach the program
      if (gl.isGL2()) {
        gl.getGL2().glAttachObjectARB(state._programID, state._fragmentShaderID);
      } else {
        if (gl.isGL2ES2()) {
          gl.getGL2ES2().glAttachShader(state._programID, state._fragmentShaderID);
        }
      }
    } else if (state._fragmentShaderID != -1) {
      removeFragShader(state);
      state._fragmentShaderID = -1;
    }

    if (caps.isGeometryShader4Supported()) {
      if (state.getGeometryShader() != null) {
        if (state._geometryShaderID != -1) {
          removeGeomShader(state);
        }

        if (gl.isGL2()) {
          state._geometryShaderID = gl.getGL2().glCreateShaderObjectARB(GL3.GL_GEOMETRY_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._geometryShaderID = gl.getGL2ES2().glCreateShader(GL3.GL_GEOMETRY_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getGeometryShader().limit()];
        state.getGeometryShader().rewind();
        state.getGeometryShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._geometryShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._geometryShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the geometry shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._geometryShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._geometryShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._geometryShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(state._geometryShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(compiled.get(0), state._geometryShaderID, state._geometryShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._geometryShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._geometryShaderID);
          }
        }
      } else if (state._geometryShaderID != -1) {
        removeGeomShader(state);
        state._geometryShaderID = -1;
      }
    }

    if (caps.isTessellationShadersSupported()) {
      if (state.getTessellationControlShader() != null) {
        if (state._tessellationControlShaderID != -1) {
          removeTessControlShader(state);
        }

        if (gl.isGL2()) {
          state._tessellationControlShaderID =
              gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._tessellationControlShaderID =
                gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getTessellationControlShader().limit()];
        state.getTessellationControlShader().rewind();
        state.getTessellationControlShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._tessellationControlShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._tessellationControlShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the tessellation control shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._tessellationControlShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._tessellationControlShaderID, GL2.GL_OBJECT_COMPILE_STATUS_ARB, compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._tessellationControlShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(
                    state._tessellationControlShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(
            compiled.get(0),
            state._tessellationControlShaderID,
            state._tessellationControlShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._tessellationControlShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._tessellationControlShaderID);
          }
        }
      } else if (state._tessellationControlShaderID != -1) {
        removeTessControlShader(state);
        state._tessellationControlShaderID = -1;
      }
      if (state.getTessellationEvaluationShader() != null) {
        if (state._tessellationEvaluationShaderID != -1) {
          removeTessEvalShader(state);
        }

        if (gl.isGL2()) {
          state._tessellationEvaluationShaderID =
              gl.getGL2().glCreateShaderObjectARB(GL4.GL_TESS_CONTROL_SHADER);
        } else {
          if (gl.isGL2ES2()) {
            state._tessellationEvaluationShaderID =
                gl.getGL2ES2().glCreateShader(GL4.GL_TESS_CONTROL_SHADER);
          }
        }

        // Create the sources
        final byte array[] = new byte[state.getTessellationEvaluationShader().limit()];
        state.getTessellationEvaluationShader().rewind();
        state.getTessellationEvaluationShader().get(array);
        if (gl.isGL2()) {
          gl.getGL2()
              .glShaderSourceARB(
                  state._tessellationEvaluationShaderID,
                  1,
                  new String[] {new String(array)},
                  new int[] {array.length},
                  0);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2()
                .glShaderSource(
                    state._tessellationEvaluationShaderID,
                    1,
                    new String[] {new String(array)},
                    new int[] {array.length},
                    0);
          }
        }

        // Compile the tessellation control shader
        final JoglRenderContext context = (JoglRenderContext) ContextManager.getCurrentContext();
        final IntBuffer compiled = context.getDirectNioBuffersSet().getSingleIntBuffer();
        compiled.clear();
        if (gl.isGL2()) {
          gl.getGL2().glCompileShaderARB(state._tessellationEvaluationShaderID);
          gl.getGL2()
              .glGetObjectParameterivARB(
                  state._tessellationEvaluationShaderID,
                  GL2.GL_OBJECT_COMPILE_STATUS_ARB,
                  compiled);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glCompileShader(state._tessellationEvaluationShaderID);
            gl.getGL2ES2()
                .glGetShaderiv(
                    state._tessellationEvaluationShaderID, GL2ES2.GL_COMPILE_STATUS, compiled);
          }
        }
        checkProgramError(
            compiled.get(0),
            state._tessellationEvaluationShaderID,
            state._tessellationEvaluationShaderName);

        // Attach the program
        if (gl.isGL2()) {
          gl.getGL2().glAttachObjectARB(state._programID, state._tessellationEvaluationShaderID);
        } else {
          if (gl.isGL2ES2()) {
            gl.getGL2ES2().glAttachShader(state._programID, state._tessellationEvaluationShaderID);
          }
        }
      } else if (state._tessellationEvaluationShaderID != -1) {
        removeTessEvalShader(state);
        state._tessellationEvaluationShaderID = -1;
      }
    }

    if (gl.isGL2()) {
      gl.getGL2().glLinkProgramARB(state._programID);
    } else {
      if (gl.isGL2ES2()) {
        gl.getGL2ES2().glLinkProgram(state._programID);
      }
    }
    checkLinkError(state._programID);
    state.setNeedsRefresh(true);
    state._needSendShader = false;
  }
  public void setLighting(LightList list) {
    GL gl = GLContext.getCurrentGL();
    // XXX: This is abuse of setLighting() to
    // apply fixed function bindings
    // and do other book keeping.
    if (list == null || list.size() == 0) {
      gl.glDisable(GLLightingFunc.GL_LIGHTING);
      applyFixedFuncBindings(false);
      setModelView(worldMatrix, viewMatrix);
      return;
    }

    // Number of lights set previously
    int numLightsSetPrev = lightList.size();

    // If more than maxLights are defined, they will be ignored.
    // The GL1 renderer is not permitted to crash due to a
    // GL1 limitation. It must render anything that the GL2 renderer
    // can render (even incorrectly).
    lightList.clear();
    materialAmbientColor.set(0, 0, 0, 0);

    for (int i = 0; i < list.size(); i++) {
      Light l = list.get(i);
      if (l.getType() == Light.Type.Ambient) {
        // Gather
        materialAmbientColor.addLocal(l.getColor());
      } else {
        // Add to list
        lightList.add(l);

        // Once maximum lights reached, exit loop.
        if (lightList.size() >= maxLights) {
          break;
        }
      }
    }

    applyFixedFuncBindings(true);

    gl.glEnable(GLLightingFunc.GL_LIGHTING);

    fb16.clear();
    fb16.put(materialAmbientColor.r)
        .put(materialAmbientColor.g)
        .put(materialAmbientColor.b)
        .put(1)
        .flip();

    gl.getGL2ES1().glLightModelfv(GL2ES1.GL_LIGHT_MODEL_AMBIENT, fb16);

    if (context.matrixMode != GLMatrixFunc.GL_MODELVIEW) {
      gl.getGL2ES1().glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
      context.matrixMode = GLMatrixFunc.GL_MODELVIEW;
    }
    // Lights are already in world space, so just convert
    // them to view space.
    gl.getGL2ES1().glLoadMatrixf(storeMatrix(viewMatrix, fb16));

    for (int i = 0; i < lightList.size(); i++) {
      int glLightIndex = GLLightingFunc.GL_LIGHT0 + i;
      Light light = lightList.get(i);
      Light.Type lightType = light.getType();
      ColorRGBA col = light.getColor();
      Vector3f pos;

      // Enable the light
      gl.glEnable(glLightIndex);

      // OGL spec states default value for light ambient is black
      switch (lightType) {
        case Directional:
          DirectionalLight dLight = (DirectionalLight) light;

          fb16.clear();
          fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_DIFFUSE, fb16);
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_SPECULAR, fb16);

          pos = tempVec.set(dLight.getDirection()).negateLocal().normalizeLocal();
          fb16.clear();
          fb16.put(pos.x).put(pos.y).put(pos.z).put(0.0f).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_POSITION, fb16);
          gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_SPOT_CUTOFF, 180);
          break;
        case Point:
          PointLight pLight = (PointLight) light;

          fb16.clear();
          fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_DIFFUSE, fb16);
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_SPECULAR, fb16);

          pos = pLight.getPosition();
          fb16.clear();
          fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_POSITION, fb16);
          gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_SPOT_CUTOFF, 180);

          if (pLight.getRadius() > 0) {
            // Note: this doesn't follow the same attenuation model
            // as the one used in the lighting shader.
            gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_CONSTANT_ATTENUATION, 1);
            gl.getGL2ES1()
                .glLightf(
                    glLightIndex, GLLightingFunc.GL_LINEAR_ATTENUATION, pLight.getInvRadius() * 2);
            gl.getGL2ES1()
                .glLightf(
                    glLightIndex,
                    GLLightingFunc.GL_QUADRATIC_ATTENUATION,
                    pLight.getInvRadius() * pLight.getInvRadius());
          } else {
            gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_CONSTANT_ATTENUATION, 1);
            gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_LINEAR_ATTENUATION, 0);
            gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_QUADRATIC_ATTENUATION, 0);
          }

          break;
        case Spot:
          SpotLight sLight = (SpotLight) light;

          fb16.clear();
          fb16.put(col.r).put(col.g).put(col.b).put(col.a).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_DIFFUSE, fb16);
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_SPECULAR, fb16);

          pos = sLight.getPosition();
          fb16.clear();
          fb16.put(pos.x).put(pos.y).put(pos.z).put(1.0f).flip();
          gl.getGL2().glLightfv(glLightIndex, GLLightingFunc.GL_POSITION, fb16);

          Vector3f dir = sLight.getDirection();
          fb16.clear();
          fb16.put(dir.x).put(dir.y).put(dir.z).put(1.0f).flip();
          gl.getGL2ES1().glLightfv(glLightIndex, GLLightingFunc.GL_SPOT_DIRECTION, fb16);

          float outerAngleRad = sLight.getSpotOuterAngle();
          float innerAngleRad = sLight.getSpotInnerAngle();
          float spotCut = outerAngleRad * FastMath.RAD_TO_DEG;
          float spotExpo = 0.0f;
          if (outerAngleRad > 0) {
            spotExpo = (1.0f - (innerAngleRad / outerAngleRad)) * 128.0f;
          }

          gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_SPOT_CUTOFF, spotCut);
          gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_SPOT_EXPONENT, spotExpo);

          if (sLight.getSpotRange() > 0) {
            gl.getGL2ES1()
                .glLightf(
                    glLightIndex, GLLightingFunc.GL_LINEAR_ATTENUATION, sLight.getInvSpotRange());
          } else {
            gl.getGL2ES1().glLightf(glLightIndex, GLLightingFunc.GL_LINEAR_ATTENUATION, 0);
          }

          break;
        default:
          throw new UnsupportedOperationException("Unrecognized light type: " + lightType);
      }
    }

    // Disable lights after the index
    for (int i = lightList.size(); i < numLightsSetPrev; i++) {
      gl.glDisable(GLLightingFunc.GL_LIGHT0 + i);
    }

    // This will set view matrix as well.
    setModelView(worldMatrix, viewMatrix);
  }