Home | History | Annotate | Download | only in attributes
      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.GL20;
     20 import com.badlogic.gdx.graphics.g3d.Attribute;
     21 import com.badlogic.gdx.math.MathUtils;
     22 import com.badlogic.gdx.utils.NumberUtils;
     23 
     24 public class BlendingAttribute extends Attribute {
     25 	public final static String Alias = "blended";
     26 	public final static long Type = register(Alias);
     27 
     28 	public final static boolean is (final long mask) {
     29 		return (mask & Type) == mask;
     30 	}
     31 
     32 	/** Whether this material should be considered blended (default: true). This is used for sorting (back to front instead of front
     33 	 * to back). */
     34 	public boolean blended;
     35 	/** Specifies how the (incoming) red, green, blue, and alpha source blending factors are computed (default: GL_SRC_ALPHA) */
     36 	public int sourceFunction;
     37 	/** Specifies how the (existing) red, green, blue, and alpha destination blending factors are computed (default:
     38 	 * GL_ONE_MINUS_SRC_ALPHA) */
     39 	public int destFunction;
     40 	/** The opacity used as source alpha value, ranging from 0 (fully transparent) to 1 (fully opaque), (default: 1). */
     41 	public float opacity = 1.f;
     42 
     43 	public BlendingAttribute () {
     44 		this(null);
     45 	}
     46 
     47 	public BlendingAttribute (final boolean blended, final int sourceFunc, final int destFunc, final float opacity) {
     48 		super(Type);
     49 		this.blended = blended;
     50 		this.sourceFunction = sourceFunc;
     51 		this.destFunction = destFunc;
     52 		this.opacity = opacity;
     53 	}
     54 
     55 	public BlendingAttribute (final int sourceFunc, final int destFunc, final float opacity) {
     56 		this(true, sourceFunc, destFunc, opacity);
     57 	}
     58 
     59 	public BlendingAttribute (final int sourceFunc, final int destFunc) {
     60 		this(sourceFunc, destFunc, 1.f);
     61 	}
     62 
     63 	public BlendingAttribute (final boolean blended, final float opacity) {
     64 		this(blended, GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, opacity);
     65 	}
     66 
     67 	public BlendingAttribute (final float opacity) {
     68 		this(true, opacity);
     69 	}
     70 
     71 	public BlendingAttribute (final BlendingAttribute copyFrom) {
     72 		this(copyFrom == null ? true : copyFrom.blended, copyFrom == null ? GL20.GL_SRC_ALPHA : copyFrom.sourceFunction,
     73 			copyFrom == null ? GL20.GL_ONE_MINUS_SRC_ALPHA : copyFrom.destFunction, copyFrom == null ? 1.f : copyFrom.opacity);
     74 	}
     75 
     76 	@Override
     77 	public BlendingAttribute copy () {
     78 		return new BlendingAttribute(this);
     79 	}
     80 
     81 	@Override
     82 	public int hashCode () {
     83 		int result = super.hashCode();
     84 		result = 947 * result + (blended ? 1 : 0);
     85 		result = 947 * result + sourceFunction;
     86 		result = 947 * result + destFunction;
     87 		result = 947 * result + NumberUtils.floatToRawIntBits(opacity);
     88 		return result;
     89 	}
     90 
     91 	@Override
     92 	public int compareTo (Attribute o) {
     93 		if (type != o.type) return (int)(type - o.type);
     94 		BlendingAttribute other = (BlendingAttribute)o;
     95 		if (blended != other.blended) return blended ? 1 : -1;
     96 		if (sourceFunction != other.sourceFunction) return sourceFunction - other.sourceFunction;
     97 		if (destFunction != other.destFunction) return destFunction - other.destFunction;
     98 		return (MathUtils.isEqual(opacity, other.opacity)) ? 0 : (opacity < other.opacity ? 1 : -1);
     99 	}
    100 }
    101