Carna  Version 3.0.1
dvr.frag
1 #version 330
2 
3 /*
4  * Copyright (C) 2010 - 2015 Leonid Kostrykin
5  *
6  * Chair of Medical Engineering (mediTEC)
7  * RWTH Aachen University
8  * Pauwelsstr. 20
9  * 52074 Aachen
10  * Germany
11  *
12  */
13 
14 uniform sampler3D huVolume;
15 uniform sampler3D normalMap;
16 uniform sampler1D colorMap;
17 uniform mat4 modelTexture;
18 uniform mat3 normalsView;
19 uniform float stepLength;
20 uniform float translucence;
21 uniform float diffuseLight;
22 uniform int lightingEnabled;
23 
24 in vec4 modelSpaceCoordinates;
25 
26 out vec4 gl_FragColor;
27 
28 
29 // ----------------------------------------------------------------------------------
30 // Basic Sampling
31 // ----------------------------------------------------------------------------------
32 
33 vec4 sampleAt( vec3 p )
34 {
35  float intensity = texture( huVolume, p ).r;
36  vec4 color = texture( colorMap, intensity );
37 
38  if( lightingEnabled == 1 )
39  {
40  vec3 normalDirection = texture( normalMap, p ).rgb;
41  vec3 diffuseColor;
42  if( dot( normalDirection, normalDirection ) < 1e-4 )
43  {
44  diffuseColor = vec3( 0, 0, 0 );
45  }
46  else
47  {
48  vec3 normal = normalize( normalsView * normalDirection );
49  vec3 lightDirection = vec3( 0, 0, -1 );
50  float diffuseLightAmount = max( 0, -dot( normal, lightDirection ) );
51  diffuseColor = color.rgb * diffuseLightAmount;
52  }
53  return vec4( mix( color.rgb, diffuseColor, diffuseLight ), color.a );
54  }
55  else
56  {
57  return color;
58  }
59 }
60 
61 
62 // ----------------------------------------------------------------------------------
63 // Fragment Procedure
64 // ----------------------------------------------------------------------------------
65 
66 void main()
67 {
68  if( abs( modelSpaceCoordinates.x ) > 0.5 || abs( modelSpaceCoordinates.y ) > 0.5 || abs( modelSpaceCoordinates.z ) > 0.5 )
69  {
70  discard;
71  }
72 
73  vec4 textureCoordinates = modelTexture * modelSpaceCoordinates;
74  vec4 color = sampleAt( textureCoordinates.xyz );
75 
76  float alpha = color.a * stepLength / ( 1 + translucence );
77  gl_FragColor = vec4( color.rgb * alpha, alpha );
78 }