shaderIsoLines.geom 1.84 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
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
// ShaderIsoLines::geometryShaderText
uniform mat4 NormalMatrix;
uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjectionMatrix;
uniform vec4 colorMin;
uniform vec4 colorMax;
uniform float vmin;
uniform float vmax;
uniform int vnb;

VARYING_IN float  attribData[3];
VARYING_OUT vec4 ColorFS;


float bary(float x, float xmin, float xmax)
{
	return (x-xmin)/(xmax-xmin);
}


/*
* warning works only with triangles
*/
void isoLine(float x)
{
	ColorFS = mix(colorMin,colorMax,(x-vmin)/(vmax-vmin));

	float b01 = bary(x,attribData[0],attribData[1]);
	float b02 = bary(x,attribData[0],attribData[2]);
	float b12 = bary(x,attribData[1],attribData[2]);
	
	bool in01 = (b01>=0.0) && (b01<=1.0);
	bool in02 = (b02>=0.0) && (b02<=1.0);
	bool in12 = (b12>=0.0) && (b12<=1.0);
	
	if (in01)
	{
		if (in02)
		{
		// line 01 - 02
			vec4 pos =  mix(POSITION_IN(0),POSITION_IN(1),b01);
			gl_Position = ModelViewProjectionMatrix *  pos;
			EmitVertex();
			pos =  mix(POSITION_IN(0),POSITION_IN(2),b02);
			gl_Position = ModelViewProjectionMatrix *  pos;
			EmitVertex();
			EndPrimitive();
		}
		if (in12)
		{
		// line 01 - 12
			vec4 pos =  mix(POSITION_IN(0),POSITION_IN(1),b01);
			gl_Position = ModelViewProjectionMatrix *  pos;
			EmitVertex();
			pos =  mix(POSITION_IN(1),POSITION_IN(2),b12);
			gl_Position = ModelViewProjectionMatrix *  pos;
			EmitVertex();
			EndPrimitive();
		}
	}
	if (in02 && in12)
	{
	// line 12 - 02
		vec4 pos =  mix(POSITION_IN(0),POSITION_IN(2),b02);
		gl_Position = ModelViewProjectionMatrix *  pos;
		EmitVertex();
		pos =  mix(POSITION_IN(1),POSITION_IN(2),b12);
		gl_Position = ModelViewProjectionMatrix *  pos;
		EmitVertex();
		EndPrimitive();
	}
}


void main(void)
{
	
	float inc = (vmax-vmin)/float(vnb);
	for (int i=0; i<vnb; ++i)
	{
		float v = vmin + float(i)*inc;
		isoLine(v);
	}

//	isoLine(0.5);
//	isoLine(0.5);
//	isoLine(0.75);
	
}