1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.g3d.attributes; 18 19 import com.badlogic.gdx.graphics.Texture; 20 import com.badlogic.gdx.graphics.g2d.TextureRegion; 21 import com.badlogic.gdx.graphics.g3d.Attribute; 22 import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor; 23 import com.badlogic.gdx.math.MathUtils; 24 import com.badlogic.gdx.utils.GdxRuntimeException; 25 import com.badlogic.gdx.utils.NumberUtils; 26 27 public class TextureAttribute extends Attribute { 28 public final static String DiffuseAlias = "diffuseTexture"; 29 public final static long Diffuse = register(DiffuseAlias); 30 public final static String SpecularAlias = "specularTexture"; 31 public final static long Specular = register(SpecularAlias); 32 public final static String BumpAlias = "bumpTexture"; 33 public final static long Bump = register(BumpAlias); 34 public final static String NormalAlias = "normalTexture"; 35 public final static long Normal = register(NormalAlias); 36 public final static String AmbientAlias = "ambientTexture"; 37 public final static long Ambient = register(AmbientAlias); 38 public final static String EmissiveAlias = "emissiveTexture"; 39 public final static long Emissive = register(EmissiveAlias); 40 public final static String ReflectionAlias = "reflectionTexture"; 41 public final static long Reflection = register(ReflectionAlias); 42 43 protected static long Mask = Diffuse | Specular | Bump | Normal | Ambient | Emissive | Reflection; 44 is(final long mask)45 public final static boolean is (final long mask) { 46 return (mask & Mask) != 0; 47 } 48 createDiffuse(final Texture texture)49 public static TextureAttribute createDiffuse (final Texture texture) { 50 return new TextureAttribute(Diffuse, texture); 51 } 52 createDiffuse(final TextureRegion region)53 public static TextureAttribute createDiffuse (final TextureRegion region) { 54 return new TextureAttribute(Diffuse, region); 55 } 56 createSpecular(final Texture texture)57 public static TextureAttribute createSpecular (final Texture texture) { 58 return new TextureAttribute(Specular, texture); 59 } 60 createSpecular(final TextureRegion region)61 public static TextureAttribute createSpecular (final TextureRegion region) { 62 return new TextureAttribute(Specular, region); 63 } 64 createNormal(final Texture texture)65 public static TextureAttribute createNormal (final Texture texture) { 66 return new TextureAttribute(Normal, texture); 67 } 68 createNormal(final TextureRegion region)69 public static TextureAttribute createNormal (final TextureRegion region) { 70 return new TextureAttribute(Normal, region); 71 } 72 createBump(final Texture texture)73 public static TextureAttribute createBump (final Texture texture) { 74 return new TextureAttribute(Bump, texture); 75 } 76 createBump(final TextureRegion region)77 public static TextureAttribute createBump (final TextureRegion region) { 78 return new TextureAttribute(Bump, region); 79 } 80 createAmbient(final Texture texture)81 public static TextureAttribute createAmbient (final Texture texture) { 82 return new TextureAttribute(Ambient, texture); 83 } 84 createAmbient(final TextureRegion region)85 public static TextureAttribute createAmbient (final TextureRegion region) { 86 return new TextureAttribute(Ambient, region); 87 } 88 createEmissive(final Texture texture)89 public static TextureAttribute createEmissive (final Texture texture) { 90 return new TextureAttribute(Emissive, texture); 91 } 92 createEmissive(final TextureRegion region)93 public static TextureAttribute createEmissive (final TextureRegion region) { 94 return new TextureAttribute(Emissive, region); 95 } 96 createReflection(final Texture texture)97 public static TextureAttribute createReflection (final Texture texture) { 98 return new TextureAttribute(Reflection, texture); 99 } 100 createReflection(final TextureRegion region)101 public static TextureAttribute createReflection (final TextureRegion region) { 102 return new TextureAttribute(Reflection, region); 103 } 104 105 public final TextureDescriptor<Texture> textureDescription; 106 public float offsetU = 0; 107 public float offsetV = 0; 108 public float scaleU = 1; 109 public float scaleV = 1; 110 /** The index of the texture coordinate vertex attribute to use for this TextureAttribute. Whether this value is used, depends 111 * on the shader and {@link Attribute#type} value. For basic (model specific) types (e.g. {@link #Diffuse}, {@link #Normal}, 112 * etc.), this value is usually ignored and the first texture coordinate vertex attribute is used. */ 113 public int uvIndex = 0; 114 TextureAttribute(final long type)115 public TextureAttribute (final long type) { 116 super(type); 117 if (!is(type)) throw new GdxRuntimeException("Invalid type specified"); 118 textureDescription = new TextureDescriptor<Texture>(); 119 } 120 TextureAttribute(final long type, final TextureDescriptor<T> textureDescription)121 public <T extends Texture> TextureAttribute (final long type, final TextureDescriptor<T> textureDescription) { 122 this(type); 123 this.textureDescription.set(textureDescription); 124 } 125 TextureAttribute(final long type, final TextureDescriptor<T> textureDescription, float offsetU, float offsetV, float scaleU, float scaleV, int uvIndex)126 public <T extends Texture> TextureAttribute (final long type, final TextureDescriptor<T> textureDescription, float offsetU, 127 float offsetV, float scaleU, float scaleV, int uvIndex) { 128 this(type, textureDescription); 129 this.offsetU = offsetU; 130 this.offsetV = offsetV; 131 this.scaleU = scaleU; 132 this.scaleV = scaleV; 133 this.uvIndex = uvIndex; 134 } 135 TextureAttribute(final long type, final TextureDescriptor<T> textureDescription, float offsetU, float offsetV, float scaleU, float scaleV)136 public <T extends Texture> TextureAttribute (final long type, final TextureDescriptor<T> textureDescription, float offsetU, 137 float offsetV, float scaleU, float scaleV) { 138 this(type, textureDescription, offsetU, offsetV, scaleU, scaleV, 0); 139 } 140 TextureAttribute(final long type, final Texture texture)141 public TextureAttribute (final long type, final Texture texture) { 142 this(type); 143 textureDescription.texture = texture; 144 } 145 TextureAttribute(final long type, final TextureRegion region)146 public TextureAttribute (final long type, final TextureRegion region) { 147 this(type); 148 set(region); 149 } 150 TextureAttribute(final TextureAttribute copyFrom)151 public TextureAttribute (final TextureAttribute copyFrom) { 152 this(copyFrom.type, copyFrom.textureDescription, copyFrom.offsetU, copyFrom.offsetV, copyFrom.scaleU, copyFrom.scaleV, 153 copyFrom.uvIndex); 154 } 155 set(final TextureRegion region)156 public void set (final TextureRegion region) { 157 textureDescription.texture = region.getTexture(); 158 offsetU = region.getU(); 159 offsetV = region.getV(); 160 scaleU = region.getU2() - offsetU; 161 scaleV = region.getV2() - offsetV; 162 } 163 164 @Override copy()165 public Attribute copy () { 166 return new TextureAttribute(this); 167 } 168 169 @Override hashCode()170 public int hashCode () { 171 int result = super.hashCode(); 172 result = 991 * result + textureDescription.hashCode(); 173 result = 991 * result + NumberUtils.floatToRawIntBits(offsetU); 174 result = 991 * result + NumberUtils.floatToRawIntBits(offsetV); 175 result = 991 * result + NumberUtils.floatToRawIntBits(scaleU); 176 result = 991 * result + NumberUtils.floatToRawIntBits(scaleV); 177 result = 991 * result + uvIndex; 178 return result; 179 } 180 181 @Override compareTo(Attribute o)182 public int compareTo (Attribute o) { 183 if (type != o.type) return type < o.type ? -1 : 1; 184 TextureAttribute other = (TextureAttribute)o; 185 final int c = textureDescription.compareTo(other.textureDescription); 186 if (c != 0) return c; 187 if (uvIndex != other.uvIndex) return uvIndex - other.uvIndex; 188 if (!MathUtils.isEqual(scaleU, other.scaleU)) return scaleU > other.scaleU ? 1 : -1; 189 if (!MathUtils.isEqual(scaleV, other.scaleV)) return scaleV > other.scaleV ? 1 : -1; 190 if (!MathUtils.isEqual(offsetU, other.offsetU)) return offsetU > other.offsetU ? 1 : -1; 191 if (!MathUtils.isEqual(offsetV, other.offsetV)) return offsetV > other.offsetV ? 1 : -1; 192 return 0; 193 } 194 } 195