Skip to content

Commit 152d4e5

Browse files
committed
misc cleanup
1 parent 4908a57 commit 152d4e5

7 files changed

+45
-85
lines changed

terrain/shaders/TerrainRenderCS.glsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main()
2929
);
3030

3131
// compute final vertex attributes
32-
ClipSpaceAttribute attrib = TessellateClipSpaceTriangle(
32+
VertexAttribute attrib = TessellateTriangle(
3333
triangleTexCoords,
3434
i_VertexPos
3535
);
@@ -41,10 +41,10 @@ void main()
4141
screenPos.xy*= 1.0f * vec2(1.0f, 1920.0f / 720.0f);
4242
gl_Position = vec4(screenPos, 1.0);
4343
#else
44-
gl_Position = u_ViewProjectionMatrix * attrib.position;
44+
gl_Position = u_ModelViewProjectionMatrix * attrib.position;
4545
#endif
4646
o_TexCoord = attrib.texCoord;
47-
o_WorldPos = attrib.position.xyz;
47+
o_WorldPos = (u_ModelMatrix * attrib.position).xyz;
4848
}
4949
#endif
5050

terrain/shaders/TerrainRenderCS_Wire.glsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main()
3030
);
3131

3232
// compute final vertex attributes
33-
ClipSpaceAttribute attrib = TessellateClipSpaceTriangle(
33+
VertexAttribute attrib = TessellateTriangle(
3434
triangleTexCoords,
3535
i_VertexPos
3636
);
@@ -42,10 +42,10 @@ void main()
4242
screenPos.xy*= 1.0f * vec2(1.0f, 1920.0f / 720.0f);
4343
gl_Position = vec4(screenPos, 1.0);
4444
#else
45-
gl_Position = u_ViewProjectionMatrix * attrib.position;
45+
gl_Position = u_ModelViewProjectionMatrix * attrib.position;
4646
#endif
4747
o_TexCoord = attrib.texCoord;
48-
o_WorldPos = attrib.position.xyz;
48+
o_WorldPos = (u_ModelMatrix * attrib.position).xyz;
4949
}
5050
#endif
5151

terrain/shaders/TerrainRenderCommon.glsl

+7-33
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ bool FrustumCullingTest(in const vec4[3] patchVertices)
200200
{
201201
vec3 bmin = min(min(patchVertices[0].xyz, patchVertices[1].xyz), patchVertices[2].xyz);
202202
vec3 bmax = max(max(patchVertices[0].xyz, patchVertices[1].xyz), patchVertices[2].xyz);
203-
# if FLAG_DISPLACE
204-
bmin.z = 0.0;
205-
bmax.z = u_DmapFactor;
206-
# endif
207203

208204
return FrustumCullingTest(u_FrustumPlanes, bmin, bmax);
209205
}
@@ -255,45 +251,23 @@ vec4 BarycentricInterpolation(in vec4 v[3], in vec2 u)
255251
* GenerateVertex -- Computes the final vertex position
256252
*
257253
*/
258-
struct ClipSpaceAttribute {
254+
struct VertexAttribute {
259255
vec4 position;
260256
vec2 texCoord;
261257
};
262258

263-
ClipSpaceAttribute TessellateClipSpaceTriangle(
264-
in const vec4 vertexPositions[3],
265-
in const vec2 vertexTexCoords[3],
266-
in vec2 tessellationCoordinate
267-
) {
268-
// compute final attributes
269-
vec4 position = BarycentricInterpolation(vertexPositions, tessellationCoordinate);
270-
vec2 texCoord = BarycentricInterpolation(vertexTexCoords, tessellationCoordinate);
271-
272-
#if FLAG_DISPLACE
273-
// displace the surface in clip space
274-
vec4 upDir = u_ModelViewProjectionMatrix[2];
275-
float z = u_DmapFactor * textureLod(u_DmapSampler, texCoord, 0.0).r;
276-
277-
position+= upDir * z;
278-
#endif
279-
280-
return ClipSpaceAttribute(position, texCoord);
281-
}
282-
283-
ClipSpaceAttribute TessellateClipSpaceTriangle(
284-
in const vec2 vertexTexCoords[3],
285-
in vec2 tessellationCoordinate
259+
VertexAttribute TessellateTriangle(
260+
in const vec2 texCoords[3],
261+
in vec2 tessCoord
286262
) {
287-
// compute final attributes
288-
vec2 texCoord = BarycentricInterpolation(vertexTexCoords, tessellationCoordinate);
263+
vec2 texCoord = BarycentricInterpolation(texCoords, tessCoord);
289264
vec4 position = vec4(texCoord, 0, 1);
290265

291266
#if FLAG_DISPLACE
292-
float z = u_DmapFactor * textureLod(u_DmapSampler, texCoord, 0.0).r;
293-
position.z = z;
267+
position.z = u_DmapFactor * textureLod(u_DmapSampler, texCoord, 0.0).r;
294268
#endif
295269

296-
return ClipSpaceAttribute(u_ModelMatrix * position, texCoord);
270+
return VertexAttribute(position, texCoord);
297271
}
298272

299273

terrain/shaders/TerrainRenderGS.glsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ GenerateVertex(
5353
in vec2 tessCoord
5454
) {
5555
// compute final vertex attributes
56-
ClipSpaceAttribute attrib = TessellateClipSpaceTriangle(
56+
VertexAttribute attrib = TessellateTriangle(
5757
triangleTexCoords,
5858
tessCoord
5959
);
6060

6161
// set varyings and emit vertex
62-
o_WorldPos = attrib.position.xyz;
62+
o_WorldPos = (u_ModelMatrix * attrib.position).xyz;
6363
o_TexCoord = attrib.texCoord;
64-
gl_Position = u_ViewProjectionMatrix * attrib.position;
64+
gl_Position = u_ModelViewProjectionMatrix * attrib.position;
6565
EmitVertex();
6666
}
6767

terrain/shaders/TerrainRenderGS_Wire.glsl

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ vec2[3] DecodeTriangleTexCoords(in const cbt_Node node)
5151
}
5252

5353
void EmitTriangle(
54-
inout ClipSpaceAttribute v0,
55-
inout ClipSpaceAttribute v1,
56-
inout ClipSpaceAttribute v2
54+
inout VertexAttribute v0,
55+
inout VertexAttribute v1,
56+
inout VertexAttribute v2
5757
) {
58-
vec4 c0 = u_ViewProjectionMatrix * v0.position;
59-
vec4 c1 = u_ViewProjectionMatrix * v1.position;
60-
vec4 c2 = u_ViewProjectionMatrix * v2.position;
58+
vec4 c0 = u_ModelViewProjectionMatrix * v0.position;
59+
vec4 c1 = u_ModelViewProjectionMatrix * v1.position;
60+
vec4 c2 = u_ModelViewProjectionMatrix * v2.position;
6161
vec2 p0 = u_ScreenResolution * c0.xy / c0.w;
6262
vec2 p1 = u_ScreenResolution * c1.xy / c1.w;
6363
vec2 p2 = u_ScreenResolution * c2.xy / c2.w;
@@ -68,19 +68,19 @@ void EmitTriangle(
6868

6969
gl_Position = c0;
7070
o_TexCoord = v0.texCoord;
71-
o_WorldPos = v0.position.xyz;
71+
o_WorldPos = (u_ModelMatrix * v0.position).xyz;
7272
o_Distance = vec3(area / length(u0), 0, 0);
7373
EmitVertex();
7474

7575
gl_Position = c1;
7676
o_TexCoord = v1.texCoord;
77-
o_WorldPos = v1.position.xyz;
77+
o_WorldPos = (u_ModelMatrix * v1.position).xyz;
7878
o_Distance = vec3(0, area / length(u1), 0);
7979
EmitVertex();
8080

8181
gl_Position = c2;
8282
o_TexCoord = v2.texCoord;
83-
o_WorldPos = v2.position.xyz;
83+
o_WorldPos = (u_ModelMatrix * v2.position).xyz;
8484
o_Distance = vec3(0, 0, area / length(u2));
8585
EmitVertex();
8686

@@ -138,15 +138,15 @@ void main()
138138
only one triangle.
139139
*/
140140
#if TERRAIN_PATCH_SUBD_LEVEL == 0
141-
ClipSpaceAttribute v0 = TessellateClipSpaceTriangle(
141+
VertexAttribute v0 = TessellateTriangle(
142142
triangleTexCoords,
143143
vec2(0, 1)
144144
);
145-
ClipSpaceAttribute v1 = TessellateClipSpaceTriangle(
145+
VertexAttribute v1 = TessellateTriangle(
146146
triangleTexCoords,
147147
vec2(0, 0)
148148
);
149-
ClipSpaceAttribute v2 = TessellateClipSpaceTriangle(
149+
VertexAttribute v2 = TessellateTriangle(
150150
triangleTexCoords,
151151
vec2(1, 0)
152152
);
@@ -159,19 +159,19 @@ void main()
159159
for (uint nodeID = minNodeID; nodeID < maxNodeID; ++nodeID) {
160160
cbt_Node node = cbt_CreateNode(nodeID, nodeDepth);
161161
vec2 tessCoords[3] = DecodeTriangleTexCoords(node);
162-
ClipSpaceAttribute v0 = TessellateClipSpaceTriangle(
162+
VertexAttribute v0 = TessellateTriangle(
163163
triangleTexCoords,
164164
tessCoords[2]
165165
);
166-
ClipSpaceAttribute v1 = TessellateClipSpaceTriangle(
166+
VertexAttribute v1 = TessellateTriangle(
167167
triangleTexCoords,
168168
tessCoords[1]
169169
);
170-
ClipSpaceAttribute v2 = TessellateClipSpaceTriangle(
170+
VertexAttribute v2 = TessellateTriangle(
171171
triangleTexCoords,
172172
(tessCoords[0] + tessCoords[2]) / 2.0f
173173
);
174-
ClipSpaceAttribute v3 = TessellateClipSpaceTriangle(
174+
VertexAttribute v3 = TessellateTriangle(
175175
triangleTexCoords,
176176
tessCoords[0]
177177
);

terrain/shaders/TerrainRenderTS.glsl

+10-24
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main()
2525
#ifdef TESS_CONTROL_SHADER
2626
layout (vertices = 1) out;
2727
out PatchData {
28-
vec4 packedData[4];
28+
vec2 texCoords[3];
2929
} o_Patch[];
3030

3131

@@ -67,11 +67,10 @@ void main()
6767
if (true) {
6868
#endif
6969
// set output data
70-
o_Patch[gl_InvocationID].packedData = vec4[4](
71-
u_ModelViewProjectionMatrix * vec4(triangleVertices[0].xy, 0.0f, 1.0f),
72-
u_ModelViewProjectionMatrix * vec4(triangleVertices[1].xy, 0.0f, 1.0f),
73-
u_ModelViewProjectionMatrix * vec4(triangleVertices[2].xy, 0.0f, 1.0f),
74-
vec4(triangleVertices[0].xy, triangleVertices[1].xy)
70+
o_Patch[gl_InvocationID].texCoords = vec2[3](
71+
triangleVertices[0].xy,
72+
triangleVertices[1].xy,
73+
triangleVertices[2].xy
7574
);
7675

7776
// set tess levels
@@ -98,37 +97,24 @@ void main()
9897
#ifdef TESS_EVALUATION_SHADER
9998
layout (triangles, ccw, equal_spacing) in;
10099
in PatchData {
101-
vec4 packedData[4];
100+
vec2 texCoords[3];
102101
} i_Patch[];
103102

104103
layout(location = 0) out vec2 o_TexCoord;
105104
layout(location = 1) out vec3 o_WorldPos;
106105

107106
void main()
108107
{
109-
// unpack triangle attributes
110-
vec4 trianglePositions[3] = vec4[3](
111-
i_Patch[0].packedData[0],
112-
i_Patch[0].packedData[1],
113-
i_Patch[0].packedData[2]
114-
);
115-
vec2 tmp = i_Patch[0].packedData[3].xy - i_Patch[0].packedData[3].zw;
116-
vec2 triangleTexCoords[3] = vec2[3](
117-
i_Patch[0].packedData[3].xy,
118-
i_Patch[0].packedData[3].zw,
119-
i_Patch[0].packedData[3].zw + vec2(tmp.y, -tmp.x)
120-
);
121-
122108
// compute final vertex attributes
123-
ClipSpaceAttribute attrib = TessellateClipSpaceTriangle(
124-
triangleTexCoords,
109+
VertexAttribute attrib = TessellateTriangle(
110+
i_Patch[0].texCoords,
125111
gl_TessCoord.xy
126112
);
127113

128114
// set varyings
129-
gl_Position = u_ViewProjectionMatrix * attrib.position;
115+
gl_Position = u_ModelViewProjectionMatrix * attrib.position;
130116
o_TexCoord = attrib.texCoord;
131-
o_WorldPos = attrib.position.xyz;
117+
o_WorldPos = (u_ModelMatrix * attrib.position).xyz;
132118
}
133119
#endif
134120

terrain/shaders/TerrainRenderTS_Wire.glsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ void main()
121121
);
122122

123123
// compute final vertex attributes
124-
ClipSpaceAttribute attrib = TessellateClipSpaceTriangle(
124+
VertexAttribute attrib = TessellateTriangle(
125125
triangleTexCoords,
126126
gl_TessCoord.xy
127127
);
128128

129129
// set varyings
130-
gl_Position = u_ViewProjectionMatrix * attrib.position;
130+
gl_Position = u_ModelViewProjectionMatrix * attrib.position;
131131
o_TexCoord = attrib.texCoord;
132-
o_WorldPos = attrib.position.xyz;
132+
o_WorldPos = (u_ModelMatrix * attrib.position).xyz;
133133
}
134134
#endif
135135

0 commit comments

Comments
 (0)