Open3D (C++ API)  0.17.0
Shader.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2023 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26// Automatically generated header file for shader.
27
28#pragma once
29
30namespace open3d {
31namespace visualization {
32namespace glsl {
33// clang-format off
34
35const char * const ImageFragmentShader =
36"#version 330\n"
37"\n"
38"in vec2 UV;\n"
39"uniform sampler2D image_texture;\n"
40"\n"
41"out vec4 FragColor;\n"
42"\n"
43"void main()\n"
44"{\n"
45" FragColor = texture(image_texture, UV);\n"
46"}\n"
47;
48
49const char * const ImageMaskFragmentShader =
50"#version 330\n"
51"\n"
52"in vec2 UV;\n"
53"uniform sampler2D image_texture;\n"
54"\n"
55"uniform vec3 mask_color;\n"
56"uniform float mask_alpha;\n"
57"\n"
58"out vec4 FragColor;\n"
59"\n"
60"void main()\n"
61"{\n"
62" FragColor = vec4(mask_color, texture(image_texture, UV).r * mask_alpha);\n"
63"}\n"
64;
65
66const char * const ImageMaskVertexShader =
67"#version 330\n"
68"\n"
69"in vec3 vertex_position;\n"
70"in vec2 vertex_UV;\n"
71"\n"
72"out vec2 UV;\n"
73"\n"
74"void main()\n"
75"{\n"
76" gl_Position = vec4(vertex_position, 1);\n"
77" UV = vertex_UV;\n"
78"}\n"
79;
80
81const char * const ImageVertexShader =
82"#version 330\n"
83"\n"
84"in vec3 vertex_position;\n"
85"in vec2 vertex_UV;\n"
86"\n"
87"out vec2 UV;\n"
88"\n"
89"uniform vec3 vertex_scale;\n"
90"\n"
91"void main()\n"
92"{\n"
93" gl_Position = vec4(vertex_position * vertex_scale, 1);\n"
94" UV = vertex_UV;\n"
95"}\n"
96;
97
98const char * const NormalFragmentShader =
99"#version 330\n"
100"\n"
101"in vec3 vertex_normal_camera;\n"
102"out vec4 FragColor;\n"
103"\n"
104"void main()\n"
105"{\n"
106" FragColor = vec4(vertex_normal_camera * 0.5 + 0.5, 1);\n"
107"}\n"
108;
109
110const char * const NormalVertexShader =
111"#version 330\n"
112"\n"
113"in vec3 vertex_position;\n"
114"in vec3 vertex_normal;\n"
115"\n"
116"out vec3 vertex_normal_camera;\n"
117"\n"
118"uniform mat4 MVP;\n"
119"uniform mat4 V;\n"
120"uniform mat4 M;\n"
121"\n"
122"void main()\n"
123"{\n"
124" gl_Position = MVP * vec4(vertex_position, 1);\n"
125" vertex_normal_camera = (V * M * vec4(vertex_normal, 0)).xyz;\n"
126"}\n"
127;
128
129const char * const PhongFragmentShader =
130"#version 330\n"
131"\n"
132"in vec3 vertex_position_world;\n"
133"in vec3 vertex_normal_camera;\n"
134"in vec3 eye_dir_camera;\n"
135"in mat4 light_dir_camera_4;\n"
136"in vec3 fragment_color;\n"
137"\n"
138"uniform mat4 light_color_4;\n"
139"uniform vec4 light_diffuse_power_4;\n"
140"uniform vec4 light_specular_power_4;\n"
141"uniform vec4 light_specular_shininess_4;\n"
142"uniform vec4 light_ambient;\n"
143"\n"
144"out vec4 FragColor;\n"
145"\n"
146"void main()\n"
147"{\n"
148" vec3 diffuse_color = fragment_color;\n"
149" vec3 ambient_color = light_ambient.xyz * diffuse_color;\n"
150" vec3 specular_color = vec3(1.0, 1.0, 1.0);\n"
151" vec4 cos_theta;\n"
152" vec4 cos_alpha;\n"
153" vec3 n, e, l, r;\n"
154"\n"
155" n = normalize(vertex_normal_camera);\n"
156" e = normalize(eye_dir_camera);\n"
157" l = normalize(light_dir_camera_4[0].xyz);\n"
158" r = reflect(-l, n);\n"
159" cos_theta[0] = clamp(dot(n, l), 0, 1);\n"
160" cos_alpha[0] = clamp(dot(e, r), 0, 1);\n"
161"\n"
162" l= normalize(light_dir_camera_4[1].xyz);\n"
163" r = reflect(-l, n);\n"
164" cos_theta[1] = clamp(dot(n, l), 0, 1);\n"
165" cos_alpha[1] = clamp(dot(e, r), 0, 1);\n"
166"\n"
167" l= normalize(light_dir_camera_4[2].xyz);\n"
168" r = reflect(-l, n);\n"
169" cos_theta[2] = clamp(dot(n, l), 0, 1);\n"
170" cos_alpha[2] = clamp(dot(e, r), 0, 1);\n"
171"\n"
172" l= normalize(light_dir_camera_4[3].xyz);\n"
173" r = reflect(-l, n);\n"
174" cos_theta[3] = clamp(dot(n, l), 0, 1);\n"
175" cos_alpha[3] = clamp(dot(e, r), 0, 1);\n"
176"\n"
177" FragColor = vec4(ambient_color + \n"
178" diffuse_color * light_color_4[0].xyz * light_diffuse_power_4[0] * cos_theta[0] +\n"
179" specular_color * light_color_4[0].xyz * light_specular_power_4[0] * pow(cos_alpha[0], light_specular_shininess_4[0]) +\n"
180" diffuse_color * light_color_4[1].xyz * light_diffuse_power_4[1] * cos_theta[1] +\n"
181" specular_color * light_color_4[1].xyz * light_specular_power_4[1] * pow(cos_alpha[1], light_specular_shininess_4[1]) +\n"
182" diffuse_color * light_color_4[2].xyz * light_diffuse_power_4[2] * cos_theta[2] +\n"
183" specular_color * light_color_4[2].xyz * light_specular_power_4[2] * pow(cos_alpha[2], light_specular_shininess_4[2]) +\n"
184" diffuse_color * light_color_4[3].xyz * light_diffuse_power_4[3] * cos_theta[3] +\n"
185" specular_color * light_color_4[3].xyz * light_specular_power_4[3] * pow(cos_alpha[3], light_specular_shininess_4[3]), 1);\n"
186"}\n"
187;
188
189const char * const PhongVertexShader =
190"#version 330\n"
191"\n"
192"in vec3 vertex_position;\n"
193"in vec3 vertex_normal;\n"
194"in vec3 vertex_color;\n"
195"\n"
196"out vec3 vertex_position_world;\n"
197"out vec3 vertex_normal_camera;\n"
198"out vec3 eye_dir_camera;\n"
199"out mat4 light_dir_camera_4;\n"
200"out vec3 fragment_color;\n"
201"\n"
202"uniform mat4 MVP;\n"
203"uniform mat4 V;\n"
204"uniform mat4 M;\n"
205"uniform mat4 light_position_world_4;\n"
206"\n"
207"void main()\n"
208"{\n"
209" gl_Position = MVP * vec4(vertex_position, 1);\n"
210" vertex_position_world = (M * vec4(vertex_position, 1)).xyz;\n"
211"\n"
212" vec3 vertex_position_camera = (V * M * vec4(vertex_position, 1)).xyz;\n"
213" eye_dir_camera = vec3(0, 0, 0) - vertex_position_camera;\n"
214"\n"
215" vec4 v = vec4(vertex_position_camera, 1);\n"
216" light_dir_camera_4 = V * light_position_world_4 - mat4(v, v, v, v);\n"
217"\n"
218" vertex_normal_camera = (V * M * vec4(vertex_normal, 0)).xyz;\n"
219" if (dot(eye_dir_camera, vertex_normal_camera) < 0.0)\n"
220" vertex_normal_camera = vertex_normal_camera * -1.0;\n"
221"\n"
222" fragment_color = vertex_color;\n"
223"}\n"
224;
225
226const char * const PickingFragmentShader =
227"#version 330\n"
228"\n"
229"in vec4 fragment_color;\n"
230"out vec4 FragColor;\n"
231"\n"
232"void main()\n"
233"{\n"
234" FragColor = fragment_color;\n"
235"}\n"
236;
237
238const char * const PickingVertexShader =
239"#version 330\n"
240"\n"
241"in vec3 vertex_position;\n"
242"in float vertex_index;\n"
243"uniform mat4 MVP;\n"
244"\n"
245"out vec4 fragment_color;\n"
246"\n"
247"void main()\n"
248"{\n"
249" float r, g, b, a;\n"
250" gl_Position = MVP * vec4(vertex_position, 1);\n"
251" r = floor(vertex_index / 16777216.0) / 255.0;\n"
252" g = mod(floor(vertex_index / 65536.0), 256.0) / 255.0;\n"
253" b = mod(floor(vertex_index / 256.0), 256.0) / 255.0;\n"
254" a = mod(vertex_index, 256.0) / 255.0;\n"
255" fragment_color = vec4(r, g, b, a);\n"
256"}\n"
257;
258
259const char * const RGBDImageFragmentShader =
260"#version 330\n"
261"\n"
262"in vec2 UV;\n"
263"uniform sampler2D image_texture;\n"
264"\n"
265"\n"
266"/* built-in option to ensure RGB and D are handled in the same shader,\n"
267" which can be used in 2 passes */\n"
268"#define DEPTH_TEXTURE_MODE 0\n"
269"#define RGB_TEXTURE_MODE 1\n"
270"#define GRAYSCALE_TEXTURE_MODE 2\n"
271"uniform int texture_mode;\n"
272"\n"
273"/* Decides the colormap of the depth image */\n"
274"uniform float depth_max;\n"
275"out vec4 FragColor;\n"
276"\n"
277"float Interpolate(float value, float y0, float x0, float y1, float x1) {\n"
278" if (value < x0) return y0;\n"
279" if (value > x1) return y1;\n"
280" return (value - x0) * (y1 - y0) / (x1 - x0) + y0;\n"
281"}\n"
282"\n"
283"float Jet(float value /* already clamped in [0, 1] */) {\n"
284" if (value <= -0.75) {\n"
285" return 0.0;\n"
286" } else if (value <= -0.25) {\n"
287" return Interpolate(value, 0.0, -0.75, 1.0, -0.25);\n"
288" } else if (value <= 0.25) {\n"
289" return 1.0;\n"
290" } else if (value <= 0.75) {\n"
291" return Interpolate(value, 1.0, 0.25, 0.0, 0.75);\n"
292" } else {\n"
293" return 0.0;\n"
294" }\n"
295"}\n"
296"\n"
297"void main() {\n"
298" if (texture_mode == DEPTH_TEXTURE_MODE) {\n"
299" float depth = texture(image_texture, UV).r;\n"
300" depth = clamp(depth, 0, depth_max);\n"
301" depth = depth / depth_max;\n"
302" depth = 2 * depth - 1;\n"
303" FragColor = vec4(Jet(depth - 0.5), Jet(depth), Jet(depth + 0.5), 1);\n"
304" } else if (texture_mode == RGB_TEXTURE_MODE) {\n"
305" FragColor = texture(image_texture, UV);\n"
306" } else if (texture_mode == GRAYSCALE_TEXTURE_MODE) {\n"
307" float scalar = texture(image_texture, UV).r;\n"
308" FragColor = vec4(vec3(scalar), 1);\n"
309" }\n"
310"}\n"
311;
312
313const char * const Simple2DFragmentShader =
314"#version 330\n"
315"\n"
316"in vec3 fragment_color;\n"
317"out vec4 FragColor;\n"
318"\n"
319"void main()\n"
320"{\n"
321" FragColor = vec4(fragment_color, 1);\n"
322"}\n"
323;
324
325const char * const Simple2DVertexShader =
326"#version 330\n"
327"\n"
328"in vec3 vertex_position;\n"
329"in vec3 vertex_color;\n"
330"\n"
331"out vec3 fragment_color;\n"
332"\n"
333"void main()\n"
334"{\n"
335" gl_Position = vec4(vertex_position, 1);\n"
336" fragment_color = vertex_color;\n"
337"}\n"
338;
339
340const char * const SimpleBlackFragmentShader =
341"#version 330\n"
342"\n"
343"out vec4 FragColor;\n"
344"\n"
345"void main()\n"
346"{\n"
347" FragColor = vec4(0.1, 0.1, 0.1, 1);\n"
348"}\n"
349;
350
351const char * const SimpleBlackVertexShader =
352"#version 330\n"
353"\n"
354"in vec3 vertex_position;\n"
355"uniform mat4 MVP;\n"
356"\n"
357"void main()\n"
358"{\n"
359" gl_Position = MVP * vec4(vertex_position, 1);\n"
360"}\n"
361;
362
363const char * const SimpleFragmentShader =
364"#version 330\n"
365"\n"
366"in vec3 fragment_color;\n"
367"out vec4 FragColor;\n"
368"\n"
369"void main()\n"
370"{\n"
371" FragColor = vec4(fragment_color, 1);\n"
372"}\n"
373;
374
375const char * const SimpleVertexShader =
376"#version 330\n"
377"\n"
378"in vec3 vertex_position;\n"
379"in vec3 vertex_color;\n"
380"uniform mat4 MVP;\n"
381"\n"
382"out vec3 fragment_color;\n"
383"\n"
384"void main()\n"
385"{\n"
386" gl_Position = MVP * vec4(vertex_position, 1);\n"
387" fragment_color = vertex_color;\n"
388"}\n"
389;
390
391const char * const TexturePhongFragmentShader =
392"#version 330\n"
393"\n"
394"in vec3 vertex_position_world;\n"
395"in vec3 vertex_normal_camera;\n"
396"in vec3 eye_dir_camera;\n"
397"in mat4 light_dir_camera_4;\n"
398"in vec2 fragment_uv;\n"
399"\n"
400"uniform mat4 light_color_4;\n"
401"uniform vec4 light_diffuse_power_4;\n"
402"uniform vec4 light_specular_power_4;\n"
403"uniform vec4 light_specular_shininess_4;\n"
404"uniform vec4 light_ambient;\n"
405"uniform sampler2D diffuse_texture;\n"
406"\n"
407"out vec4 FragColor;\n"
408"\n"
409"void main()\n"
410"{\n"
411" vec3 diffuse_color = texture(diffuse_texture, fragment_uv).rgb;\n"
412" vec3 ambient_color = light_ambient.xyz * diffuse_color;\n"
413" vec3 specular_color = vec3(1.0, 1.0, 1.0);\n"
414" vec4 cos_theta;\n"
415" vec4 cos_alpha;\n"
416" vec3 n, e, l, r;\n"
417"\n"
418" n = normalize(vertex_normal_camera);\n"
419" e = normalize(eye_dir_camera);\n"
420" l = normalize(light_dir_camera_4[0].xyz);\n"
421" r = reflect(-l, n);\n"
422" cos_theta[0] = clamp(dot(n, l), 0, 1);\n"
423" cos_alpha[0] = clamp(dot(e, r), 0, 1);\n"
424"\n"
425" l= normalize(light_dir_camera_4[1].xyz);\n"
426" r = reflect(-l, n);\n"
427" cos_theta[1] = clamp(dot(n, l), 0, 1);\n"
428" cos_alpha[1] = clamp(dot(e, r), 0, 1);\n"
429"\n"
430" l= normalize(light_dir_camera_4[2].xyz);\n"
431" r = reflect(-l, n);\n"
432" cos_theta[2] = clamp(dot(n, l), 0, 1);\n"
433" cos_alpha[2] = clamp(dot(e, r), 0, 1);\n"
434"\n"
435" l= normalize(light_dir_camera_4[3].xyz);\n"
436" r = reflect(-l, n);\n"
437" cos_theta[3] = clamp(dot(n, l), 0, 1);\n"
438" cos_alpha[3] = clamp(dot(e, r), 0, 1);\n"
439"\n"
440" FragColor = vec4(ambient_color + \n"
441" diffuse_color * light_color_4[0].xyz * light_diffuse_power_4[0] * cos_theta[0] +\n"
442" specular_color * light_color_4[0].xyz * light_specular_power_4[0] * pow(cos_alpha[0], light_specular_shininess_4[0]) +\n"
443" diffuse_color * light_color_4[1].xyz * light_diffuse_power_4[1] * cos_theta[1] +\n"
444" specular_color * light_color_4[1].xyz * light_specular_power_4[1] * pow(cos_alpha[1], light_specular_shininess_4[1]) +\n"
445" diffuse_color * light_color_4[2].xyz * light_diffuse_power_4[2] * cos_theta[2] +\n"
446" specular_color * light_color_4[2].xyz * light_specular_power_4[2] * pow(cos_alpha[2], light_specular_shininess_4[2]) +\n"
447" diffuse_color * light_color_4[3].xyz * light_diffuse_power_4[3] * cos_theta[3] +\n"
448" specular_color * light_color_4[3].xyz * light_specular_power_4[3] * pow(cos_alpha[3], light_specular_shininess_4[3]), 1);\n"
449"}\n"
450;
451
452const char * const TexturePhongVertexShader =
453"#version 330\n"
454"\n"
455"in vec3 vertex_position;\n"
456"in vec3 vertex_normal;\n"
457"in vec2 vertex_uv;\n"
458"\n"
459"out vec3 vertex_position_world;\n"
460"out vec3 vertex_normal_camera;\n"
461"out vec3 eye_dir_camera;\n"
462"out mat4 light_dir_camera_4;\n"
463"out vec2 fragment_uv;\n"
464"\n"
465"uniform mat4 MVP;\n"
466"uniform mat4 V;\n"
467"uniform mat4 M;\n"
468"uniform mat4 light_position_world_4;\n"
469"\n"
470"void main()\n"
471"{\n"
472" gl_Position = MVP * vec4(vertex_position, 1);\n"
473" vertex_position_world = (M * vec4(vertex_position, 1)).xyz;\n"
474"\n"
475" vec3 vertex_position_camera = (V * M * vec4(vertex_position, 1)).xyz;\n"
476" eye_dir_camera = vec3(0, 0, 0) - vertex_position_camera;\n"
477"\n"
478" vec4 v = vec4(vertex_position_camera, 1);\n"
479" light_dir_camera_4 = V * light_position_world_4 - mat4(v, v, v, v);\n"
480"\n"
481" vertex_normal_camera = (V * M * vec4(vertex_normal, 0)).xyz;\n"
482" if (dot(eye_dir_camera, vertex_normal_camera) < 0.0)\n"
483" vertex_normal_camera = vertex_normal_camera * -1.0;\n"
484"\n"
485" fragment_uv = vertex_uv;\n"
486"}\n"
487"\n"
488;
489
490const char * const TextureSimpleFragmentShader =
491"#version 330\n"
492"\n"
493"in vec2 fragment_uv;\n"
494"out vec4 FragColor;\n"
495"\n"
496"uniform sampler2D diffuse_texture;\n"
497"\n"
498"void main()\n"
499"{\n"
500" FragColor = texture(diffuse_texture, fragment_uv);\n"
501"}\n"
502;
503
504const char * const TextureSimpleVertexShader =
505"#version 330\n"
506"\n"
507"in vec3 vertex_position;\n"
508"in vec2 vertex_uv;\n"
509"uniform mat4 MVP;\n"
510"\n"
511"out vec2 fragment_uv;\n"
512"\n"
513"void main()\n"
514"{\n"
515" gl_Position = MVP * vec4(vertex_position, 1);\n"
516" fragment_uv = vertex_uv;\n"
517"}\n"
518;
519
520// clang-format on
521} // namespace open3d::glsl
522} // namespace open3d::visualization
523} // namespace open3d
524
const char *const NormalVertexShader
Definition: Shader.h:110
const char *const ImageVertexShader
Definition: Shader.h:81
const char *const SimpleVertexShader
Definition: Shader.h:375
const char *const TexturePhongFragmentShader
Definition: Shader.h:391
const char *const PickingFragmentShader
Definition: Shader.h:226
const char *const NormalFragmentShader
Definition: Shader.h:98
const char *const ImageMaskFragmentShader
Definition: Shader.h:49
const char *const Simple2DFragmentShader
Definition: Shader.h:313
const char *const TextureSimpleVertexShader
Definition: Shader.h:504
const char *const TextureSimpleFragmentShader
Definition: Shader.h:490
const char *const Simple2DVertexShader
Definition: Shader.h:325
const char *const TexturePhongVertexShader
Definition: Shader.h:452
const char *const SimpleFragmentShader
Definition: Shader.h:363
const char *const ImageFragmentShader
Definition: Shader.h:35
const char *const ImageMaskVertexShader
Definition: Shader.h:66
const char *const RGBDImageFragmentShader
Definition: Shader.h:259
const char *const PhongFragmentShader
Definition: Shader.h:129
const char *const SimpleBlackVertexShader
Definition: Shader.h:351
const char *const PhongVertexShader
Definition: Shader.h:189
const char *const PickingVertexShader
Definition: Shader.h:238
const char *const SimpleBlackFragmentShader
Definition: Shader.h:340
Definition: PinholeCameraIntrinsic.cpp:16