LibCarna Version 3.4.0
Loading...
Searching...
No Matches
dvr.frag
1#version 330
2
3/*
4 * Copyright (C) 2010 - 2016 Leonid Kostrykin
5 *
6 * Chair of Medical Engineering (mediTEC)
7 * RWTH Aachen University
8 * Pauwelsstr. 20
9 * 52074 Aachen
10 * Germany
11 *
12 *
13 * Copyright (C) 2021 - 2025 Leonid Kostrykin
14 *
15 */
16
17uniform sampler3D intensities;
18uniform sampler3D normalMap;
19uniform sampler1D colorMap;
20uniform float minIntensity;
21uniform float maxIntensity;
22uniform mat4 modelTexture;
23uniform mat3 normalsView;
24uniform float stepLength;
25uniform float translucency;
26uniform float diffuseLight;
27uniform int lightingEnabled;
28
29in vec4 modelSpaceCoordinates;
30
31layout( location = 0 ) out vec4 _gl_FragColor;
32
33#define EPS 1e-16
34
35
36// ----------------------------------------------------------------------------------
37// Basic Sampling
38// ----------------------------------------------------------------------------------
39
40vec4 sampleAt( vec3 p )
41{
42 float intensity = texture( intensities, p ).r;
43
44 /* Apply intensity clipping.
45 */
46 intensity = clamp(
47 ( intensity - minIntensity + EPS ) / ( maxIntensity - minIntensity + EPS )
48 , 0.0, 1.0 );
49
50 /* Query color in `colorMap`.
51 */
52 vec4 color = texture( colorMap, intensity );
53
54 /* Add lighting.
55 */
56 if( lightingEnabled == 1 )
57 {
58 vec3 normalDirection = texture( normalMap, p ).rgb;
59 vec3 diffuseColor;
60 if( dot( normalDirection, normalDirection ) < 1e-4 )
61 {
62 diffuseColor = vec3( 0, 0, 0 );
63 }
64 else
65 {
66 vec3 normal = normalize( normalsView * normalDirection );
67 vec3 lightDirection = vec3( 0, 0, -1 );
68 float diffuseLightAmount = max( 0, -dot( normal, lightDirection ) );
69 diffuseColor = color.rgb * diffuseLightAmount;
70 }
71 return vec4( mix( color.rgb, diffuseColor, diffuseLight ), color.a );
72 }
73 else
74 {
75 return color;
76 }
77}
78
79
80// ----------------------------------------------------------------------------------
81// Fragment Procedure
82// ----------------------------------------------------------------------------------
83
84void main()
85{
86 if( abs( modelSpaceCoordinates.x ) > 0.5 || abs( modelSpaceCoordinates.y ) > 0.5 || abs( modelSpaceCoordinates.z ) > 0.5 )
87 {
88 discard;
89 }
90
91 vec4 textureCoordinates = modelTexture * modelSpaceCoordinates;
92 vec4 color = sampleAt( textureCoordinates.xyz );
93
94 float alpha = color.a * stepLength / ( 1 + translucency );
95 _gl_FragColor = vec4( color.rgb * alpha, alpha );
96}