reve/src/renderer/shaders/blinn/geometry.glsl 4.7 KiB raw
1
#version 330 core
2
3
const uint InteractionModeObjects  = 0u;
4
const uint InteractionModeEdges    = 1u;
5
const uint InteractionModePoints   = 2u;
6
const uint InteractionModePolygons = 3u;
7
8
const uint VertexStyleDefault  = 1u << 0;
9
const uint VertexStyleConstant = 1u << 1;
10
11
layout(triangles)                         in;
12
layout(triangle_strip, max_vertices = 15) out;
13
14
smooth        in  vec3  vertViewDirWorld[];
15
smooth        in  vec3  vertPosWorld[];
16
smooth        in  vec3  vertNormalSmooth[];
17
flat          in  vec3  vertNormalFlat[];
18
flat          in  float vertSelected[];
19
flat          in  float vertHovered[];
20
flat          in  int   vertID[];
21
flat          in  uint  vertDecorate[];
22
smooth        in  float vertExcludeEdge[];
23
smooth        in  vec4  vertColor[];
24
smooth        in  float vertOpacity[];
25
smooth        in  vec2  vertTextureCoord[];
26
27
smooth        out vec3  fragPosWorld;
28
smooth        out vec2  textureCoord;
29
smooth        out vec3  viewDirWorld;
30
smooth        out vec4  vertexColor;
31
smooth        out float vertexOpacity;
32
smooth        out vec3  vertexNormalSmooth;
33
flat          out int   vertexObjectId;
34
flat          out uint  vertexStyle;
35
flat          out float vertexSelected;
36
flat          out float vertexHovered;
37
flat          out vec3  vertexNormalFlat;
38
noperspective out vec3  vertexDist;
39
40
uniform mat4 model;
41
uniform mat4 view;
42
uniform mat4 proj;
43
uniform vec2 windowSize; // Window sizea for screen-space calculations.
44
uniform uint renderMode;
45
uniform uint interactionMode;
46
uniform uint objectId;
47
48
int vertexId(int i) {
49
	if (interactionMode == InteractionModeObjects) {
50
		return int(objectId);
51
	}
52
	return vertID[i];
53
}
54
55
float isVertexSelected() {
56
	for (int i = 0; i < gl_in.length(); ++i) {
57
		if (vertSelected[i] < 1.0) {
58
			return 0.0;
59
		}
60
	}
61
	return 1.0;
62
}
63
64
float isVertexHovered() {
65
	if (interactionMode == InteractionModePolygons) {
66
		for (int i = 0; i < gl_in.length(); ++i) {
67
			if (vertHovered[i] < 1.0) {
68
				return 0.0;
69
			}
70
		}
71
		return 1.0;
72
	}
73
	return 0.0;
74
}
75
76
void emitVertex(int i, vec3 dist) {
77
    fragPosWorld = vertPosWorld[i];
78
    viewDirWorld = vertViewDirWorld[i];
79
    textureCoord = vertTextureCoord[i];
80
    vertexNormalSmooth = vertNormalSmooth[i];
81
    vertexNormalFlat = vertNormalFlat[i];
82
    vertexColor = vertColor[i];
83
    vertexOpacity = vertOpacity[i];
84
	vertexStyle = VertexStyleDefault;
85
	vertexSelected = isVertexSelected();
86
	vertexHovered = isVertexHovered();
87
	vertexObjectId = vertexId(i);
88
	vertexDist = dist;
89
    gl_Position = gl_in[i].gl_Position;
90
91
    EmitVertex();
92
}
93
94
// Emit vertices for a screen-aligned quad.
95
void emitVertexDecoration(int i) {
96
    // Perform perspective division to get to NDC.
97
	vec4 clipSpacePosition = gl_in[i].gl_Position;
98
    vec3 ndc = clipSpacePosition.xyz / clipSpacePosition.w;
99
100
	float squareNDCSize = 10.0;
101
102
    // Calculate the size of the billboard in NDC
103
    vec2 halfSize = vec2(
104
		squareNDCSize / windowSize.x,
105
		squareNDCSize / windowSize.y
106
	);
107
	float depthOffset = 0.0001 * gl_in[i].gl_Position.w;
108
109
	vec2 offsets[4] = vec2[4](
110
		vec2(-halfSize.x, -halfSize.y),
111
		vec2( halfSize.x, -halfSize.y),
112
		vec2(-halfSize.x,  halfSize.y),
113
		vec2( halfSize.x,  halfSize.y)
114
	);
115
	for (int j = 0; j < 4; j++) {
116
		vertexColor = vec4(0.0, 0.0, 1.0, 1.0);
117
		vertexStyle = VertexStyleConstant;
118
		vertexObjectId = vertID[i];
119
		vertexSelected = vertSelected[i];
120
		vertexHovered = -1;
121
		gl_Position = vec4(ndc.xy + offsets[j], ndc.z - depthOffset, 1.0);
122
		EmitVertex();
123
	}
124
    EndPrimitive();
125
}
126
127
vec2 screenSpaceCoord(int ix) {
128
    return windowSize * gl_in[ix].gl_Position.xy / gl_in[ix].gl_Position.w;
129
}
130
131
void main() {
132
	const float maxEdgeWidth = 100.0;
133
134
	// Screen-space coordinates of vertices.
135
    vec2 p0 = screenSpaceCoord(0);
136
    vec2 p1 = screenSpaceCoord(1);
137
    vec2 p2 = screenSpaceCoord(2);
138
139
	// Screen-space edge vectors.
140
    vec2 v0 = p2 - p1;
141
    vec2 v1 = p2 - p0;
142
    vec2 v2 = p1 - p0;
143
    float area = abs(v1.x * v2.y - v1.y * v2.x);
144
145
	vec3 distances[3] = vec3[3](
146
		vec3(
147
			area / length(v0),
148
			vertExcludeEdge[1] * maxEdgeWidth,
149
			vertExcludeEdge[2] * maxEdgeWidth
150
		),
151
		vec3(
152
			vertExcludeEdge[0] * maxEdgeWidth,
153
			area / length(v1),
154
			vertExcludeEdge[2] * maxEdgeWidth
155
		),
156
		vec3(
157
			vertExcludeEdge[0] * maxEdgeWidth,
158
			vertExcludeEdge[1] * maxEdgeWidth,
159
			area / length(v2)
160
		)
161
	);
162
163
	// Emit the input vertices with additional distance information, for
164
	// rendering wireframes in the fragment shader.
165
    for (int i = 0; i < gl_in.length(); ++i) {
166
		emitVertex(i, distances[i]);
167
    }
168
    EndPrimitive();
169
170
	// Emit squares at each vertex of the triangle, in screen space.
171
	if (interactionMode == InteractionModePoints) {
172
		for (int i = 0; i < gl_in.length(); ++i) {
173
			// Don't render decorations for vertices that aren't facing the camera.
174
			if (dot(vertNormalSmooth[i], vertViewDirWorld[i]) > 0) {
175
				emitVertexDecoration(i);
176
			}
177
		}
178
	}
179
}
180