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 <GLUPGLSL/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
9in VertexData {
10 vec4 color;
11 vec4 tex_coord;
12 vec3 center_world_space;
13 float radius;
14} FragmentIn;
15
16void main(void) {
17
18 vec3 C = FragmentIn.center_world_space;
19 float r = FragmentIn.radius;
20
21 if(glupIsEnabled(GLUP_CLIPPING)) {
22 if(
23 glup_primitive == GLUP_POINTS ||
24 GLUP.clipping_mode == GLUP_CLIP_WHOLE_CELLS
25 ) {
26 if(dot(vec4(C,1.0),GLUP.world_clip_plane) < 0.0) {
27 discard;
28 }
29 } else if(GLUP.clipping_mode == GLUP_CLIP_STRADDLING_CELLS) {
30 float dist =
31 abs(dot(vec4(C,1.0),GLUP.world_clip_plane)) /
32 length(GLUP.world_clip_plane.xyz) ;
33 if(dist > r) {
34 discard;
35 }
36 }
37 }
38
39 Ray R = glup_primary_ray();
40 vec3 M,N;
41
42 if(
43 glup_primitive != GLUP_POINTS &&
44 glupIsEnabled(GLUP_CLIPPING) &&
45 GLUP.clipping_mode == GLUP_CLIP_SLICE_CELLS
46 ) {
47 N = GLUP.world_clip_plane.xyz;
48 float w = GLUP.world_clip_plane.w;
49 float t = -(w + dot(N,R.O)) / dot(N,R.V);
50 M = R.O + t*R.V;
51 if(dot(M-C,M-C) > r*r) {
52 discard;
53 }
54 } else {
55 // High-precision ray-sphere intersection
56 // See Ray Tracing Gems, Chapter 7,
57 // Precision Improvements for Ray-Sphere Intersection,
58 // E. Haines, J. Gunther, T. Akenine-Moller
59 vec3 D = R.O-C;
60 float a = dot(R.V,R.V);
61 float b_prime = -dot(D,R.V);
62 vec3 H = D + (b_prime/a) * R.V;
63 float delta = r*r - dot(H,H);
64 if(delta < 0.0) {
65 discard;
66 }
67 // Original article: q = b_prime + sign(b_prime)*sqrt(a*delta)
68 // Don't know why they do that, here we know we want t1
69 float q = b_prime - sqrt(a*delta);
70 float t = q/a;
71
72 /*
73 // Equivalent lower-precision version:
74 float a = dot(R.V,R.V);
75 float b = 2.0*dot(R.V,D);
76 float c = dot(D,D)-r*r;
77 float delta = b*b-4.0*a*c;
78 if(delta < 0.0) {
79 discard;
80 }
81 float t = (-b-sqrt(delta))/(2.0*a);
82 */
83
84 M = R.O + t*R.V;
85 N = M-C;
86 }
87
88 if(
89 glup_primitive != GLUP_POINTS &&
90 glupIsEnabled(GLUP_CLIPPING) &&
91 GLUP.clipping_mode == GLUP_CLIP_STANDARD
92 ) {
93 if(dot(vec4(M,1.0),GLUP.world_clip_plane) < 0.0) {
94 discard;
95 }
96 }
97
98 glup_update_depth(M);
99
100 if(glupIsEnabled(GLUP_PRIMITIVE_FILTERING)) {
101 glup_primitive_filter(gl_PrimitiveID);
102 }
103
104 if(glupIsEnabled(GLUP_PICKING)) {
105 glup_FragColor = glup_picking(gl_PrimitiveID);
106 return;
107 }
108
109 vec4 result;
110 if(glupIsEnabled(GLUP_VERTEX_COLORS)) {
111 result = FragmentIn.color;
112 } else {
113 result = GLUP.front_color;
114 }
115 if(glupIsEnabled(GLUP_TEXTURING)) {
116 result = glup_texturing(result, FragmentIn.tex_coord);
117 }
118 if(glupIsEnabled(GLUP_LIGHTING)) {
119 N = normalize(GLUP.normal_matrix*N);
120 if(
121 glup_primitive != GLUP_POINTS &&
122 glupIsEnabled(GLUP_CLIPPING) &&
123 GLUP.clipping_mode == GLUP_CLIP_SLICE_CELLS &&
124 N.z < 0.0
125 ) {
126 N = -N;
127 }
128 result = glup_lighting(result, N);
129 }
130 glup_FragColor = result;
131 glup_alpha_discard();
132}
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