Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_shader_edges_front_back_persp_legacy_vert.glsl « shaders « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 30b3bdb890d024627da256556e42e6b4dd08445b (plain)
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

// Draw "fancy" wireframe, displaying front-facing, back-facing and
// silhouette lines differently.
// Mike Erwin, April 2015

// After working with this shader a while, convinced we should make
// separate shaders for perpective & ortho. (Oct 2016)

// This shader is an imperfect stepping stone until all platforms are
// ready for geometry shaders.

// Due to perspective, the line segment's endpoints might disagree on
// whether the adjacent faces are front facing. Need to use a geometry
// shader or pass in an extra position attribute (the other endpoint)
// to do this properly.

uniform bool drawFront = true;
uniform bool drawBack = true;
uniform bool drawSilhouette = true;

uniform vec4 frontColor;
uniform vec4 backColor;
uniform vec4 silhouetteColor;

uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjectionMatrix;
uniform mat3 NormalMatrix;

in vec3 pos;

// normals of faces this edge joins (object coords)
in vec3 N1;
in vec3 N2;

flat out vec4 finalColor;

// TODO: in float angle; // [-pi .. +pi], + peak, 0 flat, - valley

// to discard an entire line, set its color to invisible
// (must have GL_BLEND enabled, or discard in fragment shader)
const vec4 invisible = vec4(0.0);

bool front(vec3 N)
{
	vec4 xformed = ModelViewMatrix * vec4(pos, 1.0);
	return dot(NormalMatrix * N, normalize(-xformed.xyz)) > 0.0;
}

void main()
{
	bool face_1_front = front(N1);
	bool face_2_front = front(N2);

	gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);

	if (face_1_front && face_2_front) {
		// front-facing edge
		finalColor = drawFront ? frontColor : invisible;
	}
	else if (face_1_front || face_2_front) {
		// exactly one face is front-facing, silhouette edge
		finalColor = drawSilhouette ? silhouetteColor : invisible;
	}
	else {
		// back-facing edge
		finalColor = drawBack ? backColor : invisible;
	}
}