TerrainTransformFeedback.tcsh 2.48 KB
Newer Older
David Gilbert's avatar
David Gilbert committed
1 2
#version 410 core

3 4 5 6
#pragma import "noise/noise3d.glsl"
#pragma import "Utils.glsl"
#pragma import "planets/earth/earth_texture.glsl"

David Gilbert's avatar
David Gilbert committed
7 8 9 10 11 12
layout(vertices = 3) out;

in vec3 vPosition[];
in vec2 vTexCoord[];
in vec3 vNormal[];

David Gilbert's avatar
David Gilbert committed
13 14 15
out vec3 tcNormal[];
out vec2 tcTexCoord[];
out vec3 tcPosition[];
David Gilbert's avatar
David Gilbert committed
16 17 18

uniform mat4 uModelMatrix;
uniform vec3 cameraPosition;
19 20
uniform float recursionDepth;
uniform vec2 viewport;
David Gilbert's avatar
David Gilbert committed
21 22 23 24 25 26

#define ID gl_InvocationID


float getTessLevel(float d0, float d1){
    float avg = (d0 + d1) / 2.0;
27
    float factor = 1.0/(recursionDepth*recursionDepth);
David Gilbert's avatar
David Gilbert committed
28 29
   
    if(avg >= 2000 * factor){
David Gilbert's avatar
David Gilbert committed
30 31
   		return 1;
   	}
Dario Seyb's avatar
Dario Seyb committed
32
   	if(avg >= 1800 * factor){
David Gilbert's avatar
David Gilbert committed
33 34
   		return 2;
   	}
Dario Seyb's avatar
Dario Seyb committed
35
   	if(avg >= 1000 * factor){
David Gilbert's avatar
David Gilbert committed
36 37
   		return 4;
   	}
David Gilbert's avatar
David Gilbert committed
38
   	if(avg >= 400 * factor){
David Gilbert's avatar
David Gilbert committed
39 40
   		return 8;
   	}
41
   	if(avg >= 200 * factor){
David Gilbert's avatar
David Gilbert committed
42 43
   		return 16;
   	}
44
   	if(avg >= 100 * factor){
David Gilbert's avatar
David Gilbert committed
45 46 47 48 49
   		return 32;
   	}
   	if(avg >= 0){
   		return 64;
   	}
David Gilbert's avatar
David Gilbert committed
50
   	else return 1;
David Gilbert's avatar
David Gilbert committed
51 52 53 54 55 56 57 58 59 60 61
}


void main(){

	tcNormal[ID] = vNormal[ID];
	tcTexCoord[ID] = vTexCoord[ID];
	tcPosition[ID] = vPosition[ID];

	if (ID == 0) {

62 63 64
		vec4 p0 = vec4(texture_getDisplacedPosition_modelspace(normalize(tcPosition[0]), normalize(tcNormal[0])), 1);
		vec4 p1 = vec4(texture_getDisplacedPosition_modelspace(normalize(tcPosition[1]), normalize(tcNormal[1])), 1);
		vec4 p2 = vec4(texture_getDisplacedPosition_modelspace(normalize(tcPosition[2]), normalize(tcNormal[2])), 1);
David Gilbert's avatar
David Gilbert committed
65

66 67 68
		float d0 = distance(cameraPosition, (uModelMatrix * p0).xyz );
	    float d1 = distance(cameraPosition, (uModelMatrix * p1).xyz );
	    float d2 = distance(cameraPosition, (uModelMatrix * p2).xyz );
David Gilbert's avatar
David Gilbert committed
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

		gl_TessLevelOuter[0] = getTessLevel(d1, d2);
	    gl_TessLevelOuter[1] = getTessLevel(d2, d0);
	    gl_TessLevelOuter[2] = getTessLevel(d0, d1);
	    gl_TessLevelInner[0] = 0.5 * gl_TessLevelOuter[2];

	    /*

	    float avgDist = (d0 + d1 + d2)/3.0;

	    vec3 midPoint;
	    float fDistance;

	    midPoint = (tcPosition[2] + tcPosition[0] / 2.0);
	    fDistance = distance(midPoint, cameraPosition) * 1;
	    gl_TessLevelOuter[0] = getTessLevel(fDistance, fDistance);


	   	midPoint = (tcPosition[2] + tcPosition[0] / 2.0);
	    fDistance = distance(midPoint, cameraPosition) * 1;
	    gl_TessLevelOuter[1] = getTessLevel(fDistance, fDistance);


	    midPoint = (tcPosition[2] + tcPosition[0] / 2.0);
	    fDistance = distance(midPoint, cameraPosition) * 1;
	    gl_TessLevelOuter[2] = getTessLevel(fDistance, fDistance);

	    gl_TessLevelInner[0] = (gl_TessLevelOuter[0] + gl_TessLevelOuter[1] +gl_TessLevelOuter[2]) /3;
	    */
	}
}