/*
 * Copyright 2014 Matthew Collins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.thinkofdeath.mapviewer.client.render;

import java.util.Comparator;

class ChunkSorter implements Comparator<ChunkRenderObject> {

    private final Camera camera;

    public ChunkSorter(Camera camera) {
        this.camera = camera;
    }

    @Override
    public int compare(ChunkRenderObject a, ChunkRenderObject b) {
        int ax = (a.x << 4) + 8 - (int) camera.getX();
        int ay = (a.y << 4) + 8 - (int) camera.getY();
        int az = (a.z << 4) + 8 - (int) camera.getZ();
        int bx = (b.x << 4) + 8 - (int) camera.getX();
        int by = (b.y << 4) + 8 - (int) camera.getY();
        int bz = (b.z << 4) + 8 - (int) camera.getZ();
        return (bx * bx + by * by + bz * bz) - (ax * ax + ay * ay + az * az);
    }
}

/*
 * Copyright 2014 Matthew Collins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.thinkofdeath.mapviewer.client.render;

import java.util.Comparator;

class ChunkSorter implements Comparator<ChunkRenderObject> {

    private final Camera camera;

    public ChunkSorter(Camera camera) {
        this.camera = camera;
    }

    @Override
    public int compare(ChunkRenderObject a, ChunkRenderObject b) {
        int ax = (a.x << 4) + 8 - (int) camera.getX();
        int ay = (a.y << 4) + 8 - (int) camera.getY();
        int az = (a.z << 4) + 8 - (int) camera.getZ();
        int bx = (b.x << 4) + 8 - (int) camera.getX();
        int by = (b.y << 4) + 8 - (int) camera.getY();
        int bz = (b.z << 4) + 8 - (int) camera.getZ();
        return (ax * ax + ay * ay + az * az) - (bx * bx + by * by + bz * bz);
    }
}

Swap the rendering order of normal chunks to allow the graphics card to optimize away fragments better