コード例 #1
0
ファイル: DefaultShader.java プロジェクト: Sasun/libgdx
 @Override
 public boolean canRender(final Renderable renderable) {
   final Attributes attributes = combineAttributes(renderable);
   return (attributesMask == (attributes.getMask() | optionalAttributes))
       && (vertexMask == renderable.meshPart.mesh.getVertexAttributes().getMask())
       && (renderable.environment != null) == lighting;
 }
コード例 #2
0
ファイル: DefaultShader.java プロジェクト: Sasun/libgdx
 public static String createPrefix(final Renderable renderable, final Config config) {
   final Attributes attributes = combineAttributes(renderable);
   String prefix = "";
   final long attributesMask = attributes.getMask();
   final long vertexMask = renderable.meshPart.mesh.getVertexAttributes().getMask();
   if (and(vertexMask, Usage.Position)) prefix += "#define positionFlag\n";
   if (or(vertexMask, Usage.ColorUnpacked | Usage.ColorPacked)) prefix += "#define colorFlag\n";
   if (and(vertexMask, Usage.BiNormal)) prefix += "#define binormalFlag\n";
   if (and(vertexMask, Usage.Tangent)) prefix += "#define tangentFlag\n";
   if (and(vertexMask, Usage.Normal)) prefix += "#define normalFlag\n";
   if (and(vertexMask, Usage.Normal) || and(vertexMask, Usage.Tangent | Usage.BiNormal)) {
     if (renderable.environment != null) {
       prefix += "#define lightingFlag\n";
       prefix += "#define ambientCubemapFlag\n";
       prefix += "#define numDirectionalLights " + config.numDirectionalLights + "\n";
       prefix += "#define numPointLights " + config.numPointLights + "\n";
       prefix += "#define numSpotLights " + config.numSpotLights + "\n";
       if (attributes.has(ColorAttribute.Fog)) {
         prefix += "#define fogFlag\n";
       }
       if (renderable.environment.shadowMap != null) prefix += "#define shadowMapFlag\n";
       if (attributes.has(CubemapAttribute.EnvironmentMap))
         prefix += "#define environmentCubemapFlag\n";
     }
   }
   final int n = renderable.meshPart.mesh.getVertexAttributes().size();
   for (int i = 0; i < n; i++) {
     final VertexAttribute attr = renderable.meshPart.mesh.getVertexAttributes().get(i);
     if (attr.usage == Usage.BoneWeight) prefix += "#define boneWeight" + attr.unit + "Flag\n";
     else if (attr.usage == Usage.TextureCoordinates)
       prefix += "#define texCoord" + attr.unit + "Flag\n";
   }
   if ((attributesMask & BlendingAttribute.Type) == BlendingAttribute.Type)
     prefix += "#define " + BlendingAttribute.Alias + "Flag\n";
   if ((attributesMask & TextureAttribute.Diffuse) == TextureAttribute.Diffuse) {
     prefix += "#define " + TextureAttribute.DiffuseAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.DiffuseAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & TextureAttribute.Specular) == TextureAttribute.Specular) {
     prefix += "#define " + TextureAttribute.SpecularAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.SpecularAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & TextureAttribute.Normal) == TextureAttribute.Normal) {
     prefix += "#define " + TextureAttribute.NormalAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.NormalAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & TextureAttribute.Emissive) == TextureAttribute.Emissive) {
     prefix += "#define " + TextureAttribute.EmissiveAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.EmissiveAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & TextureAttribute.Reflection) == TextureAttribute.Reflection) {
     prefix += "#define " + TextureAttribute.ReflectionAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.ReflectionAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & TextureAttribute.Ambient) == TextureAttribute.Ambient) {
     prefix += "#define " + TextureAttribute.AmbientAlias + "Flag\n";
     prefix +=
         "#define "
             + TextureAttribute.AmbientAlias
             + "Coord texCoord0\n"; // FIXME implement UV mapping
   }
   if ((attributesMask & ColorAttribute.Diffuse) == ColorAttribute.Diffuse)
     prefix += "#define " + ColorAttribute.DiffuseAlias + "Flag\n";
   if ((attributesMask & ColorAttribute.Specular) == ColorAttribute.Specular)
     prefix += "#define " + ColorAttribute.SpecularAlias + "Flag\n";
   if ((attributesMask & ColorAttribute.Emissive) == ColorAttribute.Emissive)
     prefix += "#define " + ColorAttribute.EmissiveAlias + "Flag\n";
   if ((attributesMask & ColorAttribute.Reflection) == ColorAttribute.Reflection)
     prefix += "#define " + ColorAttribute.ReflectionAlias + "Flag\n";
   if ((attributesMask & FloatAttribute.Shininess) == FloatAttribute.Shininess)
     prefix += "#define " + FloatAttribute.ShininessAlias + "Flag\n";
   if ((attributesMask & FloatAttribute.AlphaTest) == FloatAttribute.AlphaTest)
     prefix += "#define " + FloatAttribute.AlphaTestAlias + "Flag\n";
   if (renderable.bones != null && config.numBones > 0)
     prefix += "#define numBones " + config.numBones + "\n";
   return prefix;
 }
コード例 #3
0
ファイル: DefaultShader.java プロジェクト: Sasun/libgdx
  public DefaultShader(
      final Renderable renderable, final Config config, final ShaderProgram shaderProgram) {
    final Attributes attributes = combineAttributes(renderable);
    this.config = config;
    this.program = shaderProgram;
    this.lighting = renderable.environment != null;
    this.environmentCubemap =
        attributes.has(CubemapAttribute.EnvironmentMap)
            || (lighting && attributes.has(CubemapAttribute.EnvironmentMap));
    this.shadowMap = lighting && renderable.environment.shadowMap != null;
    this.renderable = renderable;
    attributesMask = attributes.getMask() | optionalAttributes;
    vertexMask = renderable.meshPart.mesh.getVertexAttributes().getMask();

    this.directionalLights =
        new DirectionalLight
            [lighting && config.numDirectionalLights > 0 ? config.numDirectionalLights : 0];
    for (int i = 0; i < directionalLights.length; i++)
      directionalLights[i] = new DirectionalLight();
    this.pointLights =
        new PointLight[lighting && config.numPointLights > 0 ? config.numPointLights : 0];
    for (int i = 0; i < pointLights.length; i++) pointLights[i] = new PointLight();
    this.spotLights =
        new SpotLight[lighting && config.numSpotLights > 0 ? config.numSpotLights : 0];
    for (int i = 0; i < spotLights.length; i++) spotLights[i] = new SpotLight();

    if (!config.ignoreUnimplemented && (implementedFlags & attributesMask) != attributesMask)
      throw new GdxRuntimeException("Some attributes not implemented yet (" + attributesMask + ")");

    // Global uniforms
    u_projTrans = register(Inputs.projTrans, Setters.projTrans);
    u_viewTrans = register(Inputs.viewTrans, Setters.viewTrans);
    u_projViewTrans = register(Inputs.projViewTrans, Setters.projViewTrans);
    u_cameraPosition = register(Inputs.cameraPosition, Setters.cameraPosition);
    u_cameraDirection = register(Inputs.cameraDirection, Setters.cameraDirection);
    u_cameraUp = register(Inputs.cameraUp, Setters.cameraUp);
    u_time = register(new Uniform("u_time"));
    // Object uniforms
    u_worldTrans = register(Inputs.worldTrans, Setters.worldTrans);
    u_viewWorldTrans = register(Inputs.viewWorldTrans, Setters.viewWorldTrans);
    u_projViewWorldTrans = register(Inputs.projViewWorldTrans, Setters.projViewWorldTrans);
    u_normalMatrix = register(Inputs.normalMatrix, Setters.normalMatrix);
    u_bones =
        (renderable.bones != null && config.numBones > 0)
            ? register(Inputs.bones, new Setters.Bones(config.numBones))
            : -1;

    u_shininess = register(Inputs.shininess, Setters.shininess);
    u_opacity = register(Inputs.opacity);
    u_diffuseColor = register(Inputs.diffuseColor, Setters.diffuseColor);
    u_diffuseTexture = register(Inputs.diffuseTexture, Setters.diffuseTexture);
    u_diffuseUVTransform = register(Inputs.diffuseUVTransform, Setters.diffuseUVTransform);
    u_specularColor = register(Inputs.specularColor, Setters.specularColor);
    u_specularTexture = register(Inputs.specularTexture, Setters.specularTexture);
    u_specularUVTransform = register(Inputs.specularUVTransform, Setters.specularUVTransform);
    u_emissiveColor = register(Inputs.emissiveColor, Setters.emissiveColor);
    u_emissiveTexture = register(Inputs.emissiveTexture, Setters.emissiveTexture);
    u_emissiveUVTransform = register(Inputs.emissiveUVTransform, Setters.emissiveUVTransform);
    u_reflectionColor = register(Inputs.reflectionColor, Setters.reflectionColor);
    u_reflectionTexture = register(Inputs.reflectionTexture, Setters.reflectionTexture);
    u_reflectionUVTransform = register(Inputs.reflectionUVTransform, Setters.reflectionUVTransform);
    u_normalTexture = register(Inputs.normalTexture, Setters.normalTexture);
    u_normalUVTransform = register(Inputs.normalUVTransform, Setters.normalUVTransform);
    u_ambientTexture = register(Inputs.ambientTexture, Setters.ambientTexture);
    u_ambientUVTransform = register(Inputs.ambientUVTransform, Setters.ambientUVTransform);
    u_alphaTest = register(Inputs.alphaTest);

    u_ambientCubemap =
        lighting
            ? register(
                Inputs.ambientCube,
                new Setters.ACubemap(config.numDirectionalLights, config.numPointLights))
            : -1;
    u_environmentCubemap =
        environmentCubemap ? register(Inputs.environmentCubemap, Setters.environmentCubemap) : -1;
  }