#version 120

/*
 Copyright (C) 2010-2017 Kristian Duske
 
 This file is part of TrenchBroom.
 
 TrenchBroom is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 TrenchBroom is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
 */

uniform float Brightness;
uniform bool ApplyTexture;
uniform sampler2D Texture;
uniform bool RenderGrid;
uniform vec2 GridSizes;
uniform vec4 GridColor;
uniform vec2 GridScales;
uniform mat4 GridMatrix;
uniform vec2 GridDivider;
uniform float CameraZoom;

varying vec4 modelCoordinates;
varying vec3 modelNormal;
varying vec4 faceColor;

// Returns a measure for the closeness of the given coord from the next grid line, where 1 means the coord is on a
// grid line and 0 means the coord is exactly between two adjacent grid lines.
float closeness(float coord, float gridSize) {
    return abs(2.0 * fract(coord / gridSize) - 1.0);
}

float getSoftStripes(float coord, float gridDivider, float gridSize, float stripeSize) {
    // size of the minor and major grids
    float minorGridSize = gridSize / gridDivider;
    float majorGridSize = gridSize;
    
    // How close is the coord to the next major and minor grid lines?
    float minorCloseness = closeness(coord, minorGridSize);
	float majorCloseness = closeness(coord, majorGridSize);
	float isMajor = step(1.0 - 1.0 / gridDivider, majorCloseness);

    float outIntensity = isMajor * 0.9 + 0.65; // tweak intensities here

    float edge = 2.0 * fwidth(coord) / minorGridSize;
    return smoothstep(stripeSize - edge, stripeSize + edge, minorCloseness) * outIntensity;
}

void gridLinesSoft(vec2 inCoords) {
    // the width of the grid lines, corrected by the texture scaling factors and the camera zoom level
    vec2 lineWidths = abs(1.5 / GridScales / CameraZoom);
    // the widths of each grid stripe
    vec2 stripeWidths = GridSizes / GridDivider;
    // ratio of the line widths and the stripe widths
	vec2 stripeRatios = lineWidths / stripeWidths;
	vec2 stripeSizes = 1.0 - stripeRatios;
    
	float alpha = max(getSoftStripes(inCoords.x, GridDivider.x, GridSizes.x, stripeSizes.x),
                      getSoftStripes(inCoords.y, GridDivider.y, GridSizes.y, stripeSizes.y));

	gl_FragColor.rgb = mix(gl_FragColor.rgb, GridColor.rgb, alpha * GridColor.a * 0.5);
}

void main() {
	if (ApplyTexture)
		gl_FragColor = texture2D(Texture, gl_TexCoord[0].st);
	else
		gl_FragColor = faceColor;

    gl_FragColor = vec4(vec3(Brightness / 2.0 * gl_FragColor), gl_FragColor.a);
    gl_FragColor = clamp(2.0 * gl_FragColor, 0.0, 1.0);

	if (RenderGrid) {
		// vec2 gridRatios = GridDivider / GridSizes;
        // vec2 gridThickness = abs(1.5 / GridScales / CameraZoom);

        vec4 texCoordinates = GridMatrix * modelCoordinates;
		gridLinesSoft(texCoordinates.xy);
        
        /*
		float normX = abs(modelNormal.x);
		float normY = abs(modelNormal.y);
		float normZ = abs(modelNormal.z);

		float gridThickness = GridSize < 4 ? 0.25 : 0.5;
		float gridRatio = 1.0 / GridSize;
		vec2 baseCoords; // coordinates used for overlay creation

		if (normX > normY) {
			if (normX > normZ)
				baseCoords = modelCoordinates.yz;
			else
				baseCoords = modelCoordinates.xy;
		} else if (normY > normZ) {
			baseCoords = modelCoordinates.xz;
		} else {
			baseCoords = modelCoordinates.xy;
		}

		gridLinesSoft(baseCoords, gridRatio, gridThickness);
        */
	}
}