Package com.badlogic.gdx.graphics.g3d.shaders

Source Code of com.badlogic.gdx.graphics.g3d.shaders.DepthShader$Config

/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.graphics.g3d.shaders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Renderable;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;

public class DepthShader extends DefaultShader {
  public static class Config extends DefaultShader.Config {
    public boolean depthBufferOnly = false;
    public float defaultAlphaTest = 0.5f;

    public Config () {
      super();
      defaultCullFace = GL20.GL_FRONT;
    }

    public Config (String vertexShader, String fragmentShader) {
      super(vertexShader, fragmentShader);
    }
  }

  private static String defaultVertexShader = null;

  public final static String getDefaultVertexShader () {
    if (defaultVertexShader == null)
      defaultVertexShader = Gdx.files.classpath("com/badlogic/gdx/graphics/g3d/shaders/depth.vertex.glsl").readString();
    return defaultVertexShader;
  }

  private static String defaultFragmentShader = null;

  public final static String getDefaultFragmentShader () {
    if (defaultFragmentShader == null)
      defaultFragmentShader = Gdx.files.classpath("com/badlogic/gdx/graphics/g3d/shaders/depth.fragment.glsl").readString();
    return defaultFragmentShader;
  }

  public static String createPrefix (final Renderable renderable, final Config config) {
    String prefix = DefaultShader.createPrefix(renderable, config);
    if (!config.depthBufferOnly) prefix += "#define PackedDepthFlag\n";
    return prefix;
  }

  public final int numBones;
  public final int weights;
  private final FloatAttribute alphaTestAttribute;

  public DepthShader (final Renderable renderable) {
    this(renderable, new Config());
  }

  public DepthShader (final Renderable renderable, final Config config) {
    this(renderable, config, createPrefix(renderable, config));
  }

  public DepthShader (final Renderable renderable, final Config config, final String prefix) {
    this(renderable, config, prefix, config.vertexShader != null ? config.vertexShader : getDefaultVertexShader(),
      config.fragmentShader != null ? config.fragmentShader : getDefaultFragmentShader());
  }

  public DepthShader (final Renderable renderable, final Config config, final String prefix, final String vertexShader,
    final String fragmentShader) {
    this(renderable, config, new ShaderProgram(prefix + vertexShader, prefix + fragmentShader));
  }

  public DepthShader (final Renderable renderable, final Config config, final ShaderProgram shaderProgram) {
    super(renderable, config, shaderProgram);
    this.numBones = renderable.bones == null ? 0 : config.numBones;
    int w = 0;
    final int n = renderable.mesh.getVertexAttributes().size();
    for (int i = 0; i < n; i++) {
      final VertexAttribute attr = renderable.mesh.getVertexAttributes().get(i);
      if (attr.usage == Usage.BoneWeight) w |= (1 << attr.unit);
    }
    weights = w;
    alphaTestAttribute = new FloatAttribute(FloatAttribute.AlphaTest, config.defaultAlphaTest);
  }

  @Override
  public void begin (Camera camera, RenderContext context) {
    super.begin(camera, context);
    // Gdx.gl20.glEnable(GL20.GL_POLYGON_OFFSET_FILL);
    // Gdx.gl20.glPolygonOffset(2.f, 100.f);
  }

  @Override
  public void end () {
    super.end();
    // Gdx.gl20.glDisable(GL20.GL_POLYGON_OFFSET_FILL);
  }

  @Override
  public boolean canRender (Renderable renderable) {
    if (renderable.material.has(BlendingAttribute.Type)) {
      if ((materialMask & BlendingAttribute.Type) != BlendingAttribute.Type)
        return false;
      if (renderable.material.has(TextureAttribute.Diffuse) != ((materialMask & TextureAttribute.Diffuse) == TextureAttribute.Diffuse))
        return false;
    }
    final boolean skinned = ((renderable.mesh.getVertexAttributes().getMask() & Usage.BoneWeight) == Usage.BoneWeight);
    if (skinned != (numBones > 0)) return false;
    if (!skinned) return true;
    int w = 0;
    final int n = renderable.mesh.getVertexAttributes().size();
    for (int i = 0; i < n; i++) {
      final VertexAttribute attr = renderable.mesh.getVertexAttributes().get(i);
      if (attr.usage == Usage.BoneWeight) w |= (1 << attr.unit);
    }
    return w == weights;
  }
 
  @Override
  public void render (final Renderable renderable) {
    if (renderable.material.has(BlendingAttribute.Type)) {
      final BlendingAttribute blending = (BlendingAttribute)renderable.material.get(BlendingAttribute.Type);
      renderable.material.remove(BlendingAttribute.Type);
      final boolean hasAlphaTest = renderable.material.has(FloatAttribute.AlphaTest);
      if (!hasAlphaTest)
        renderable.material.set(alphaTestAttribute);
      if (blending.opacity >= ((FloatAttribute)renderable.material.get(FloatAttribute.AlphaTest)).value)
        super.render(renderable);
      if (!hasAlphaTest)
        renderable.material.remove(FloatAttribute.AlphaTest);
      renderable.material.set(blending);
    } else
      super.render(renderable);
  }
}
TOP

Related Classes of com.badlogic.gdx.graphics.g3d.shaders.DepthShader$Config

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.