1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
17 package com.badlogic.gdx.graphics.g3d.utils;
19 import java.util.Comparator;
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.Vector3;
25 import com.badlogic.gdx.utils.Array;
27 public class DefaultRenderableSorter implements RenderableSorter, Comparator<Renderable> {
28 private Camera camera;
29 private final Vector3 tmpV1 = new Vector3();
30 private final Vector3 tmpV2 = new Vector3();
33 public void sort (final Camera camera, final Array<Renderable> renderables) {
35 renderables.sort(this);
39 public int compare (final Renderable o1, final Renderable o2) {
40 final boolean b1 = o1.material.has(BlendingAttribute.Type) ?
41 ((BlendingAttribute)o1.material.get(BlendingAttribute.Type)).blended : false;
42 final boolean b2 = o2.material.has(BlendingAttribute.Type) ?
43 ((BlendingAttribute)o2.material.get(BlendingAttribute.Type)).blended : false;
46 // FIXME implement better sorting algorithm
47 // final boolean same = o1.shader == o2.shader && o1.mesh == o2.mesh && (o1.lights == null) == (o2.lights == null) &&
48 // o1.material.equals(o2.material);
49 o1.worldTransform.getTranslation(tmpV1);
50 o2.worldTransform.getTranslation(tmpV2);
51 final float dst = camera.position.dst2(tmpV1) - camera.position.dst2(tmpV2);
52 final int result = dst < 0f ? -1 : (dst > 0f ? 1 : 0);
53 return b1 ? -result : result;