#version 330 compatibility
#include "includes.glsl"

// This is used to clear variables that need to be cleared at the start
// Will soon be used to move data rather than clear

uniform vec3 cameraPosition;
uniform vec3 previousCameraPosition;
uniform sampler2D colortex8; //textures
uniform sampler2D colortex9; //Speculars
const bool colortex8Clear = false;
const bool colortex9Clear = false;

in vec2 texcoord;

/* DRAWBUFFERS:89 */
layout(location = 0) out vec4 Color8;
layout(location = 1) out vec4 Color9;

ivec2 imgSize = ivec2(VoxelBufferSize, VoxelBufferSize);

void main() {
    ivec3 Offset = ivec3(floor(cameraPosition)-floor(previousCameraPosition));
    Color8 = texelFetch(colortex8, ivec2(texcoord*imgSize), 0);
    Color9 = texelFetch(colortex9, ivec2(texcoord*imgSize), 0);
    if (Offset != ivec3(0,0,0)){
        ivec2 ImgLocation = ivec2(texcoord*vec2(imgSize));
        int ID = ImgLocation.x + (ImgLocation.y*imgSize.x);
        ID += Offset.x + (Offset.y*RealVoxDist) + (Offset.z*RealVoxDist*RealVoxDist);
        ImgLocation = ivec2(ID%imgSize.x, ID/imgSize.x);

        vec4 color8 = texelFetch(colortex8, ImgLocation, 0);
        vec4 color9 = texelFetch(colortex9, ImgLocation, 0);

        if (ID < 0 || ID >= RealVoxDist*RealVoxDist*RealVoxDist || true){
            Color8 = vec4(0,0,0,1);
            Color9 = vec4(0,0,0,1);
        }
    }
}