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();
   }
 }
  public static void apply(final JoglRenderer renderer, final GLSLShaderObjectsState state) {
    final GL gl = GLU.getCurrentGL();
    final RenderContext context = ContextManager.getCurrentContext();
    final ContextCapabilities caps = context.getCapabilities();

    if (caps.isGLSLSupported()) {
      // Ask for the current state record
      final ShaderObjectsStateRecord record =
          (ShaderObjectsStateRecord) context.getStateRecord(StateType.GLSLShader);
      context.setCurrentState(StateType.GLSLShader, state);

      if (state.isEnabled()) {
        if (state._needSendShader) {
          sendToGL(state);
        }

        if (state._shaderDataLogic != null) {
          state._shaderDataLogic.applyData(state, state._mesh, renderer);
        }
      }

      if (!record.isValid() || record.getReference() != state || state.needsRefresh()) {
        record.setReference(state);
        if (state.isEnabled()) {
          if (state._programID != -1) {
            gl.glUseProgramObjectARB(state._programID);

            final List<ShaderVariable> attribs = state.getShaderAttributes();
            for (int i = attribs.size(); --i >= 0; ) {
              final ShaderVariable shaderVariable = attribs.get(i);
              if (shaderVariable.needsRefresh) {
                JoglShaderUtil.updateAttributeLocation(shaderVariable, state._programID);
                shaderVariable.needsRefresh = false;
              }
              JoglShaderUtil.updateShaderAttribute(shaderVariable);
            }

            final List<ShaderVariable> uniforms = state.getShaderUniforms();
            for (int i = uniforms.size(); --i >= 0; ) {
              final ShaderVariable shaderVariable = uniforms.get(i);
              if (shaderVariable.needsRefresh) {
                JoglShaderUtil.updateUniformLocation(shaderVariable, state._programID);
                JoglShaderUtil.updateShaderUniform(shaderVariable);
                shaderVariable.needsRefresh = false;
              }
            }
          }
        } else {
          gl.glUseProgramObjectARB(0);
        }
      }

      if (!record.isValid()) {
        record.validate();
      }
    }
  }
Example #3
0
  /**
   * Update variableID for uniform shadervariable if needed.
   *
   * @param variable shadervaribale to update ID on
   * @param programID shader program context ID
   */
  public static void updateUniformLocation(final ShaderVariable variable, final int programID) {
    final GL gl = GLU.getCurrentGL();

    if (variable.variableID == -1) {
      variable.variableID =
          gl.glGetUniformLocationARB(programID, variable.name); // TODO Check variable.name

      if (variable.variableID == -1) {
        logger.severe("Shader uniform [" + variable.name + "] could not be located in shader");
      }
    }
  }