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();
      }
    }
  }
示例#2
0
  /**
   * Initialize texture renderers. Load water textures. Create shaders.
   *
   * @param r
   */
  private void initialize(final Renderer r) {
    if (cam == null || initialized) {
      return;
    }
    initialized = true;

    final ContextCapabilities caps = ContextManager.getCurrentContext().getCapabilities();

    if (useRefraction && useProjectedShader && caps.getNumberOfFragmentTextureUnits() < 6
        || useRefraction && caps.getNumberOfFragmentTextureUnits() < 5) {
      useRefraction = false;
      logger.info("Not enough textureunits, falling back to non refraction water");
    }

    if (!caps.isGLSLSupported()) {
      supported = false;
    }
    if (!(caps.isPbufferSupported() || caps.isFBOSupported())) {
      supported = false;
    }

    if (isSupported()) {
      tRenderer =
          TextureRendererFactory.INSTANCE.createTextureRenderer( //
              cam.getWidth() / renderScale, // width
              cam.getHeight() / renderScale, // height
              8, // Depth bits... TODO: Make configurable?
              0, // Samples... TODO: Make configurable?
              r,
              caps);

      // blurSampleDistance = 1f / ((float) cam.getHeight() / renderScale);

      tRenderer.setMultipleTargets(true);
      tRenderer.setBackgroundColor(new ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f));
      tRenderer
          .getCamera()
          .setFrustum(
              cam.getFrustumNear(),
              cam.getFrustumFar(),
              cam.getFrustumLeft(),
              cam.getFrustumRight(),
              cam.getFrustumTop(),
              cam.getFrustumBottom());

      textureState = new TextureState();
      textureState.setEnabled(true);

      setupTextures();

      fullScreenQuad = new Quad("FullScreenQuad", cam.getWidth() / 4, cam.getHeight() / 4);
      fullScreenQuad.setTranslation(cam.getWidth() / 2, cam.getHeight() / 2, 0);
      fullScreenQuad.getSceneHints().setRenderBucketType(RenderBucketType.Ortho);
      fullScreenQuad.getSceneHints().setCullHint(CullHint.Never);
      fullScreenQuad.getSceneHints().setTextureCombineMode(TextureCombineMode.Replace);
      fullScreenQuad.getSceneHints().setLightCombineMode(LightCombineMode.Off);
      final TextureState ts = new TextureState();
      ts.setTexture(textureReflect);
      fullScreenQuad.setRenderState(ts);
      fullScreenQuad.setRenderState(blurShaderVertical);
      fullScreenQuad.updateWorldRenderStates(false);
    }

    if (!isSupported()) {
      createFallbackData();
    } else {
      noFog = new FogState();
      noFog.setEnabled(false);
    }

    getSceneHints().setCullHint(CullHint.Never);

    setWaterEffectOnSpatial(this);
  }