1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#version 430

layout(quads, equal_spacing) in;

in TCS_OUT
{
	vec2 pos;
} vIn[];

out vec4 vertexColor;

uniform vec2 overflow;

patch in uint quadId;

uniform int R;

struct QuadData
{
	vec2 cornerTexCoords[4];
	vec2 texCoords[9];
	uint cPtr;
};

layout(std430, binding = 0) buffer quadData
{
    QuadData quads[];
};

layout(std430, binding = 1) buffer colorData
{
    vec4 c[];
};

vec4 colorDisplacement(uvec2 coord)
{
	vec4 ret;
	ret = c[quads[quadId].cPtr + coord.x + coord.y * (R + 1)];

	return ret;
}

void main(void)
{
	vec2 uv = gl_TessCoord.xy;
	
	uvec2 i = uvec2(round(R * uv));
	vec4 cd = colorDisplacement(i);

	vertexColor = vec4(cd.rgb, 1);	
	const float gamma = 2.2 * 1.2;
	vertexColor.rgb = pow(vertexColor.rgb, vec3(1.0 / gamma));

	vec2 center = quads[quadId].texCoords[4];

	int leftInterpol, rightInterpol;
	float leftWeight;
	if(i.x == 0)
	{
		leftInterpol = rightInterpol = 0;
		leftWeight = 0.5;
	}
	else if(i.x == R / 2)
	{
		leftInterpol = rightInterpol = 1;
		leftWeight = 0.5;
	}
	else if(i.x == R)
	{
		leftInterpol = rightInterpol = 2;
		leftWeight = 0.5;
	}
	else if(i.x < R/2)
	{
		leftInterpol = 0;
		rightInterpol = 1;
		leftWeight = 2 * (0.5 - uv.x);
	}
	else
	{
		leftInterpol = 1;
		rightInterpol = 2;
		leftWeight = 2 * (1 - uv.x);
	}

	int bottomInterpol, topInterpol;
	float bottomWeight;
	if(i.y == 0)
	{
		bottomInterpol = topInterpol = 0;
		bottomWeight = 0.5;
	}
	else if(i.y == R / 2)
	{
		bottomInterpol = topInterpol = 1;
		bottomWeight = 0.5;
	}
	else if(i.y == R)
	{
		bottomInterpol = topInterpol = 2;
		bottomWeight = 0.5;
	}
	else if(i.y < R/2)
	{
		bottomInterpol = 0;
		topInterpol = 1;
		bottomWeight = 2 * (0.5 - uv.y);
	}
	else
	{
		bottomInterpol = 1;
		topInterpol = 2;
		bottomWeight = 2 * (1 - uv.y);
	}

	//leftInterpol = 0;
	//rightInterpol = 2;
	//leftWeight = 1-uv.x;
	//bottomInterpol = 0;
	//topInterpol = 2;
	//bottomWeight = 1-uv.y;

	vec2 p;
	
	//vertexColor.r = leftWeight;
	//vertexColor.g = bottomWeight;

	QuadData quad = quads[quadId];
	p = (1 - uv.x) * ((1 - uv.y) * vIn[0].pos + uv.y * vIn[3].pos) + uv.x * ((1 - uv.y) * vIn[1].pos + uv.y * vIn[2].pos);
	p = leftWeight * (bottomWeight * quad.texCoords[leftInterpol + 3 * bottomInterpol] + (1 - bottomWeight) * quad.texCoords[leftInterpol + 3 * topInterpol])
		+ (1 - leftWeight) * (bottomWeight * quad.texCoords[rightInterpol + 3 * bottomInterpol] + (1 - bottomWeight) * quad.texCoords[rightInterpol + 3 * topInterpol]);

	if(overflow != vec2(0, 0))
		p = p + normalize(p - center) * overflow;

	gl_Position = vec4(2 * p - vec2(1, 1), 0, 1);
}