forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmokeShaders.cpp
304 lines (262 loc) · 14 KB
/
SmokeShaders.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// GLSL shaders for particle rendering
#define STRINGIFY(A) #A
// particle vertex shader
const char *particleVS = STRINGIFY(
uniform float pointRadius; // point size in world space \n
uniform float pointScale; // scale to calculate size in pixels \n
uniform vec4 eyePos; \n
void main() \n
{
\n
vec4 wpos = vec4(gl_Vertex.xyz, 1.0); \n
gl_Position = gl_ModelViewProjectionMatrix *wpos; \n
// calculate window-space point size \n
vec4 eyeSpacePos = gl_ModelViewMatrix *wpos; \n
float dist = length(eyeSpacePos.xyz); \n
gl_PointSize = pointRadius * (pointScale / dist); \n
gl_TexCoord[0] = gl_MultiTexCoord0; // sprite texcoord \n
gl_TexCoord[1] = eyeSpacePos; \n
gl_FrontColor = gl_Color; \n
} \n
);
// motion blur shaders
const char *mblurVS = STRINGIFY(
uniform float timestep; \n
void main() \n
{
\n
vec3 pos = gl_Vertex.xyz; \n
vec3 vel = gl_MultiTexCoord0.xyz; \n
vec3 pos2 = (pos - vel*timestep).xyz; // previous position \n
gl_Position = gl_ModelViewMatrix * vec4(pos, 1.0); \n // eye space
gl_TexCoord[0] = gl_ModelViewMatrix * vec4(pos2, 1.0); \n
// aging \n
float lifetime = gl_MultiTexCoord0.w; \n
float age = gl_Vertex.w; \n
float phase = (lifetime > 0.0) ? (age / lifetime) : 1.0; \n // [0, 1]
gl_TexCoord[1].x = phase; \n
float fade = 1.0 - phase; \n
// float fade = 1.0; \n
// gl_FrontColor = gl_Color; \n
gl_FrontColor = vec4(gl_Color.xyz, gl_Color.w*fade); \n
} \n
);
// motion blur geometry shader
// - outputs stretched quad between previous and current positions
const char *mblurGS =
"#version 120\n"
"#extension GL_EXT_geometry_shader4 : enable\n"
STRINGIFY(
uniform float pointRadius; // point size in world space \n
void main() \n
{
\n
// aging \n
float phase = gl_TexCoordIn[0][1].x; \n
float radius = pointRadius; \n
// eye space \n
vec3 pos = gl_PositionIn[0].xyz; \n
vec3 pos2 = gl_TexCoordIn[0][0].xyz; \n
vec3 motion = pos - pos2; \n
vec3 dir = normalize(motion); \n
float len = length(motion); \n
vec3 x = dir *radius; \n
vec3 view = normalize(-pos); \n
vec3 y = normalize(cross(dir, view)) * radius; \n
float facing = dot(view, dir); \n
// check for very small motion to avoid jitter \n
float threshold = 0.01; \n
if ((len < threshold) || (facing > 0.95) || (facing < -0.95))
{
\n
pos2 = pos;
\n
x = vec3(radius, 0.0, 0.0);
\n
y = vec3(0.0, -radius, 0.0);
\n
} \n
// output quad \n
gl_FrontColor = gl_FrontColorIn[0]; \n
gl_TexCoord[0] = vec4(0, 0, 0, phase); \n
gl_TexCoord[1] = gl_PositionIn[0]; \n
gl_Position = gl_ProjectionMatrix * vec4(pos + x + y, 1); \n
EmitVertex(); \n
gl_TexCoord[0] = vec4(0, 1, 0, phase); \n
gl_TexCoord[1] = gl_PositionIn[0]; \n
gl_Position = gl_ProjectionMatrix * vec4(pos + x - y, 1); \n
EmitVertex(); \n
gl_TexCoord[0] = vec4(1, 0, 0, phase); \n
gl_TexCoord[1] = gl_PositionIn[0]; \n
gl_Position = gl_ProjectionMatrix * vec4(pos2 - x + y, 1); \n
EmitVertex(); \n
gl_TexCoord[0] = vec4(1, 1, 0, phase); \n
gl_TexCoord[1] = gl_PositionIn[0]; \n
gl_Position = gl_ProjectionMatrix * vec4(pos2 - x - y, 1); \n
EmitVertex(); \n
} \n
);
const char *simplePS = STRINGIFY(
void main() \n
{
\n
gl_FragColor = gl_Color; \n
} \n
);
// render particle without shadows
const char *particlePS = STRINGIFY(
uniform float pointRadius; \n
void main() \n
{
\n
// calculate eye-space sphere normal from texture coordinates \n
vec3 N; \n
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0); \n
float r2 = dot(N.xy, N.xy); \n
if (r2 > 1.0) discard; // kill pixels outside circle \n
N.z = sqrt(1.0-r2); \n
// float alpha = saturate(1.0 - r2); \n
float alpha = clamp((1.0 - r2), 0.0, 1.0); \n
alpha *= gl_Color.w; \n
gl_FragColor = vec4(gl_Color.xyz * alpha, alpha); \n
} \n
);
// render particle including shadows
const char *particleShadowPS = STRINGIFY(
uniform float pointRadius; \n
uniform sampler2D shadowTex; \n
uniform sampler2D depthTex; \n
void main() \n
{
\n
// calculate eye-space sphere normal from texture coordinates \n
vec3 N; \n
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0); \n
float r2 = dot(N.xy, N.xy); \n
if (r2 > 1.0) discard; \n // kill pixels outside circle
N.z = sqrt(1.0-r2); \n
vec4 eyeSpacePos = gl_TexCoord[1]; \n
vec4 eyeSpaceSpherePos = vec4(eyeSpacePos.xyz + N*pointRadius, 1.0); \n // point on sphere
vec4 shadowPos = gl_TextureMatrix[0] * eyeSpaceSpherePos; \n
vec3 shadow = vec3(1.0) - texture2DProj(shadowTex, shadowPos.xyw).xyz; \n
// float alpha = saturate(1.0 - r2); \n
float alpha = clamp((1.0 - r2), 0.0, 1.0); \n
alpha *= gl_Color.w; \n
gl_FragColor = vec4(gl_Color.xyz *shadow * alpha, alpha); \n // premul alpha
}
);
// render particle as lit sphere
const char *particleSpherePS = STRINGIFY(
uniform float pointRadius; \n
uniform vec3 lightDir = vec3(0.577, 0.577, 0.577); \n
void main() \n
{
\n
// calculate eye-space sphere normal from texture coordinates \n
vec3 N; \n
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0); \n
float r2 = dot(N.xy, N.xy); \n
if (r2 > 1.0) discard; // kill pixels outside circle \n
N.z = sqrt(1.0-r2); \n
// calculate depth \n
vec4 eyeSpacePos = vec4(gl_TexCoord[1].xyz + N*pointRadius, 1.0); // position of this pixel on sphere in eye space \n
vec4 clipSpacePos = gl_ProjectionMatrix *eyeSpacePos; \n
gl_FragDepth = (clipSpacePos.z / clipSpacePos.w)*0.5+0.5; \n
float diffuse = max(0.0, dot(N, lightDir)); \n
gl_FragColor = diffuse *gl_Color; \n
} \n
);
const char *passThruVS = STRINGIFY(
void main() \n
{
\n
gl_Position = gl_Vertex; \n
gl_TexCoord[0] = gl_MultiTexCoord0; \n
gl_FrontColor = gl_Color; \n
} \n
);
const char *texture2DPS = STRINGIFY(
uniform sampler2D tex; \n
void main() \n
{
\n
gl_FragColor = texture2D(tex, gl_TexCoord[0].xy); \n
} \n
);
// 4 tap 3x3 gaussian blur
const char *blurPS = STRINGIFY(
uniform sampler2D tex; \n
uniform vec2 texelSize; \n
uniform float blurRadius; \n
void main() \n
{
\n
vec4 c; \n
c = texture2D(tex, gl_TexCoord[0].xy + vec2(-0.5, -0.5)*texelSize*blurRadius); \n
c += texture2D(tex, gl_TexCoord[0].xy + vec2(0.5, -0.5)*texelSize*blurRadius); \n
c += texture2D(tex, gl_TexCoord[0].xy + vec2(0.5, 0.5)*texelSize*blurRadius); \n
c += texture2D(tex, gl_TexCoord[0].xy + vec2(-0.5, 0.5)*texelSize*blurRadius); \n
c *= 0.25; \n
gl_FragColor = c; \n
} \n
);
// floor shader
const char *floorVS = STRINGIFY(
varying vec4 vertexPosEye; // vertex position in eye space \n
varying vec3 normalEye; \n
void main() \n
{
\n
gl_Position = gl_ModelViewProjectionMatrix *gl_Vertex; \n
gl_TexCoord[0] = gl_MultiTexCoord0; \n
vertexPosEye = gl_ModelViewMatrix *gl_Vertex; \n
normalEye = gl_NormalMatrix *gl_Normal; \n
gl_FrontColor = gl_Color; \n
} \n
);
const char *floorPS = STRINGIFY(
uniform vec3 lightPosEye; // light position in eye space \n
uniform vec3 lightColor; \n
uniform sampler2D tex; \n
uniform sampler2D shadowTex; \n
varying vec4 vertexPosEye; // vertex position in eye space \n
varying vec3 normalEye; \n
void main() \n
{
\n
vec4 shadowPos = gl_TextureMatrix[0] * vertexPosEye; \n
vec4 colorMap = texture2D(tex, gl_TexCoord[0].xy); \n
vec3 N = normalize(normalEye); \n
vec3 L = normalize(lightPosEye - vertexPosEye.xyz); \n
float diffuse = max(0.0, dot(N, L)); \n
vec3 shadow = vec3(1.0) - texture2DProj(shadowTex, shadowPos.xyw).xyz; \n
if (shadowPos.w < 0.0) shadow = lightColor; \n // avoid back projections
gl_FragColor = vec4(gl_Color.xyz *colorMap.xyz *diffuse * shadow, 1.0); \n
} \n
);