Geogram Version 1.9.9
A programming library of geometric algorithms
Loading...
Searching...
No Matches
spheres_fragment_shader.h
1//import <GLUP/current_profile/fragment_shader_preamble.h>
2//import <GLUPES/fragment_shader_state.h>
3//import <GLUP/stdglup.h>
4//import <GLUP/current_profile/toggles.h>
5//import <GLUP/current_profile/primitive.h>
6//import <GLUP/fragment_shader_utils.h>
7//import <GLUP/fragment_ray_tracing.h>
8
9glup_flat glup_in vec4 color;
10glup_flat glup_in vec4 tex_coord;
11glup_flat glup_in vec3 center_world_space;
12glup_flat glup_in float radius;
13glup_flat glup_in glup_id primitive_id;
14
15void main(void) {
16 vec3 C = center_world_space;
17 float r = radius;
18
19 if(glupIsEnabled(GLUP_CLIPPING)) {
20 if(
21 glup_primitive == GLUP_POINTS ||
22 GLUP.clipping_mode == GLUP_CLIP_WHOLE_CELLS
23 ) {
24 if(dot(vec4(center_world_space,1.0),GLUP.world_clip_plane) < 0.0) {
25 discard;
26 }
27 } else if(GLUP.clipping_mode == GLUP_CLIP_STRADDLING_CELLS) {
28 float dist =
29 abs(dot(vec4(center_world_space,1.0),GLUP.world_clip_plane)) /
30 length(GLUP.world_clip_plane.xyz) ;
31 if(dist > r) {
32 discard;
33 }
34 }
35 }
36
37 Ray R = glup_primary_ray();
38 vec3 M,N;
39
40 if(
41 glup_primitive != GLUP_POINTS &&
42 glupIsEnabled(GLUP_CLIPPING) &&
43 GLUP.clipping_mode == GLUP_CLIP_SLICE_CELLS
44 ) {
45 N = GLUP.world_clip_plane.xyz;
46 float w = GLUP.world_clip_plane.w;
47 float t = -(w + dot(N,R.O)) / dot(N,R.V);
48 M = R.O + t*R.V;
49 if(dot(M-C,M-C) > r*r) {
50 discard;
51 }
52 } else {
53 // High-precision ray-sphere intersection
54 // See Ray Tracing Gems, Chapter 7,
55 // Precision Improvements for Ray-Sphere Intersection,
56 // E. Haines, J. Gunther, T. Akenine-Moller
57 vec3 D = R.O-C;
58 float a = dot(R.V,R.V);
59
60 float b_prime = -dot(D,R.V);
61 vec3 H = D + (b_prime/a) * R.V;
62 float delta = r*r - dot(H,H);
63 if(delta < 0.0) {
64 discard;
65 }
66 // Original article: q = b_prime + sign(b_prime)*sqrt(a*delta)
67 // Don't know why they do that, here we know we want t1
68 float q = b_prime - sqrt(a*delta);
69 float t = q/a;
70
71 // Equivalent lower-precision version:
72 /*
73 float b = 2.0*dot(R.V,D);
74 float c = dot(D,D)-r*r;
75 float delta = b*b-4.0*a*c;
76 if(delta < 0.0) {
77 discard;
78 }
79 float t = (-b-sqrt(delta))/(2.0*a);
80 */
81
82 M = R.O + t*R.V;
83 N = M-C;
84 }
85
86 if(
87 glup_primitive != GLUP_POINTS &&
88 glupIsEnabled(GLUP_CLIPPING) &&
89 GLUP.clipping_mode == GLUP_CLIP_STANDARD
90 ) {
91 if(dot(vec4(M,1.0),GLUP.world_clip_plane) < 0.0) {
92 discard;
93 }
94 }
95
96 glup_update_depth(M);
97
98 if(glupIsEnabled(GLUP_PICKING)) {
99 glup_FragColor = glup_picking(int(primitive_id));
100 return;
101 }
102
103 vec4 result;
104 if(glupIsEnabled(GLUP_VERTEX_COLORS)) {
105 result = color;
106 } else {
107 result = GLUP.front_color;
108 }
109 if(glupIsEnabled(GLUP_TEXTURING)) {
110 result = glup_texturing(result, tex_coord);
111 }
112 if(glupIsEnabled(GLUP_LIGHTING)) {
113 N = normalize(GLUP.normal_matrix*N);
114 if(
115 glup_primitive != GLUP_POINTS &&
116 glupIsEnabled(GLUP_CLIPPING) &&
117 GLUP.clipping_mode == GLUP_CLIP_SLICE_CELLS &&
118 N.z < 0.0
119 ) {
120 N = -N;
121 }
122 result = glup_lighting(result, N);
123 }
124 glup_FragColor = result;
125 glup_alpha_discard();
126}
double length(vec3 v)
Computes the length of a vector.
vec3 normalize(vec3 v)
Computes a normalized vector.
T dot(const vecng< 3, T > &v1, const vecng< 3, T > &v2)
Computes the dot product of 2 vectors. vecng
Definition vecg.h:995
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
Definition geometry.h:65
vecng< 4, Numeric::float64 > vec4
Represents points and vectors in 4d.
Definition geometry.h:71