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.utils; 18 19 import java.util.Comparator; 20 21 import com.badlogic.gdx.graphics.Camera; 22 import com.badlogic.gdx.graphics.g3d.Renderable; 23 import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute; 24 import com.badlogic.gdx.math.MathUtils; 25 import com.badlogic.gdx.math.Vector3; 26 import com.badlogic.gdx.utils.Array; 27 28 public class DefaultRenderableSorter implements RenderableSorter, Comparator<Renderable> { 29 private Camera camera; 30 private final Vector3 tmpV1 = new Vector3(); 31 private final Vector3 tmpV2 = new Vector3(); 32 33 @Override sort(final Camera camera, final Array<Renderable> renderables)34 public void sort (final Camera camera, final Array<Renderable> renderables) { 35 this.camera = camera; 36 renderables.sort(this); 37 } 38 39 @Override compare(final Renderable o1, final Renderable o2)40 public int compare (final Renderable o1, final Renderable o2) { 41 final boolean b1 = o1.material.has(BlendingAttribute.Type) && ((BlendingAttribute)o1.material.get(BlendingAttribute.Type)).blended; 42 final boolean b2 = o2.material.has(BlendingAttribute.Type) && ((BlendingAttribute)o2.material.get(BlendingAttribute.Type)).blended; 43 if (b1 != b2) return b1 ? 1 : -1; 44 // FIXME implement better sorting algorithm 45 // final boolean same = o1.shader == o2.shader && o1.mesh == o2.mesh && (o1.lights == null) == (o2.lights == null) && 46 // o1.material.equals(o2.material); 47 o1.worldTransform.getTranslation(tmpV1); 48 o2.worldTransform.getTranslation(tmpV2); 49 final float dst = (int)(1000f * camera.position.dst2(tmpV1)) - (int)(1000f * camera.position.dst2(tmpV2)); 50 final int result = dst < 0 ? -1 : (dst > 0 ? 1 : 0); 51 return b1 ? -result : result; 52 } 53 } 54