File: vtkLighting_s.glsl

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (228 lines) | stat: -rw-r--r-- 6,585 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// ============================================================================
//
//  Program:   Visualization Toolkit
//  Module:    vtkLighting_s.glsl
//
//  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
//  All rights reserved.
//  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
//
//     This software is distributed WITHOUT ANY WARRANTY; without even
//     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//     PURPOSE.  See the above copyright notice for more information.
//
// ============================================================================

// This file defines some lighting functions.
// They can be used either in a vertex or fragment shader.

#version 110

// Example in vertex shader:
// Reminder: two-sided/one-sided is controlled by GL_VERTEX_PROGRAM_TWO_SIDE
//
// vec4 heyeCoords=gl_ModelViewMatrix*gl_Vertex;
// vec3 eyeCoords=heyeCoords.xyz/heyeCoords.w;
// vec3 n=gl_Normalmatrix*gl_Normal;
// n=normalize(n);
// separateSpecularColor(gl_FrontMaterial,eyeCoords,n,gl_FrontColor,gl_FrontSecondaryColor);
 // If two-sided.
// separateSpecularColor(gl_BackMaterial,eyeCoords,n,gl_BackColor,gl_BackSecondaryColor);

 // Typical:
// gl_FrontColor=singleColor(gl_FrontMaterial,eyeCoords,n);

// VTK_LIGHTING_NUMBER_OF_LIGHTS has to be defined (by shader source string
// concatenation) to be the number of lights on, contiguous from 0 to
// VTK_LIGHTING_NUMBER_OF_LIGHTS-1
// it has to be less than gl_MaxLights (typically 8)

// Per light computation
// (it means the scene ambient term is missing).
// lightSource is usually as gl_LightSource[i]
void lightSeparateSpecularColor(gl_LightSourceParameters lightSource,
                                gl_MaterialParameters m,
                                vec3 surfacePosEyeCoords,
                                vec3 n,
                                bool twoSided,
                                inout vec4 cpri,
                                inout vec4 csec)
{
  vec3 ldir;
  vec3 h;
  float att;
  float spot;
  float shininessFactor;
  vec3 wReverseRayDir=surfacePosEyeCoords;

  if(lightSource.position.w!=0.0)
    {
    // ldir=light direction
    vec3 lightPos=lightSource.position.xyz/lightSource.position.w;
    ldir=lightPos-surfacePosEyeCoords;
    float sqrDistance=dot(ldir,ldir);
    ldir=normalize(ldir);
    h=normalize(ldir+normalize(wReverseRayDir));
    att=1.0/(lightSource.constantAttenuation+lightSource.linearAttenuation*sqrt(sqrDistance)+lightSource.quadraticAttenuation*sqrDistance);
    // USED
    }
  else
    {
    att=1.0;
    ldir=lightSource.position.xyz;
    ldir=normalize(ldir);
    h=normalize(ldir+wReverseRayDir);
    }

  if(att>0.0)
    {
    // USED
    if(lightSource.spotCutoff==180.0)
      {
      spot=1.0;
      // NOT USED
      }
    else
      {
      // USED

      float coef=-dot(ldir,normalize(lightSource.spotDirection));
      if(coef>=lightSource.spotCosCutoff)
        {
        spot=pow(coef,lightSource.spotExponent);
        // USED
        }
      else
        {
        spot=0.0;
        // NOT USED
        }
      }
    if(spot>0.0)
      {
      // USED

      // LIT operation...
      float nDotL=dot(n,ldir);
      float nDotH=dot(n,h);

      // separate nDotL and nDotH for two-sided shading, otherwise we
      // get black spots.

      if(nDotL<0.0) // two-sided shading
        {
//        nDotL=-nDotL; // mostly NOT USED
        nDotL=0.0;
        }

      if(nDotH<0.0) // two-sided shading
        {
//        nDotH=-nDotH; // mostly USED, except on the back face of the plane.
        nDotH=0.0;
        }

      // ambient term for this light
      vec4 cpril=m.ambient*lightSource.ambient;// acm*adi

//      cpri=cpril;
//      return;

      // diffuse term for this light
      if(nDotL>0.0)
        {
        // USED
        cpril+=m.diffuse*lightSource.diffuse*nDotL; // dcm*dcli
        }


      // specular term for this light
      shininessFactor=pow(nDotH,m.shininess); // srm

      cpri+=att*spot*cpril;

      // scm*scli
      csec+=att*spot*
        m.specular*lightSource.specular*shininessFactor;

      }
    }
}

// Ignore Scene ambient. Useful in multipass, if the ambient was already
// taken into account in a previous pass.
void initBlackColors(out vec4 cpri,
                     out vec4 csec)
{
  cpri=vec4(0.0,0.0,0.0,1.0);
  csec=vec4(0.0,0.0,0.0,1.0);
}

void initColorsWithAmbient(gl_MaterialParameters m,
                           out vec4 cpri,
                           out vec4 csec)
{
  cpri=m.emission+m.ambient*gl_LightModel.ambient; // ecm+acm*acs
  csec=vec4(0.0,0.0,0.0,1.0);
}

#ifdef VTK_LIGHTING_NUMBER_OF_LIGHTS

// This is convenience method to use in shader but you better do
// this computation on the CPU and send the result as a uniform.

// True if any enabled light is a positional one.
bool needSurfacePositionInEyeCoordinates()
{
 int i=0;
 bool result=false;
 while(!result && i<VTK_LIGHTING_NUMBER_OF_LIGHTS)
  {
   result=gl_LightSource[i].position.w!=0.0;
   ++i;
  }
 return result;
}

// Lighting computation based on a material m,
// a position on the surface expressed in eye coordinate (typically a vertex
//  position in a vertex shader, something interpolated in a fragment shader),
// a unit normal `n' to the surface in eye coordinates.
// Most of the components are in cpri (primary color), the specular
// component is in csec (secondary color).
// Useful for blending color and textures.
void separateSpecularColor(gl_MaterialParameters m,
                           vec3 surfacePosEyeCoords,
                           vec3 n,
                           bool twoSided,
                           out vec4 cpri,
                           out vec4 csec)
{
  initColorsWithAmbient(m,cpri,csec);

  // For each light,
  int i=0;
  while(i<VTK_LIGHTING_NUMBER_OF_LIGHTS)
    {
    lightSeparateSpecularColor(gl_LightSource[i],m,surfacePosEyeCoords,n,
                               twoSided,cpri,csec);
    ++i;
    }
}

// Lighting computation based on a material m,
// a position on the surface expressed in eye coordinate (typically a vertex
//  position in a vertex shader, something interpolated in a fragment shader),
// a unit normal to the surface in eye coordinates.
// The result includes the specular component.
vec4 singleColor(gl_MaterialParameters m,
                 vec3 surfacePosEyeCoords,
                 vec3 n,
                 bool twoSided)
{
  vec4 cpri;
  vec4 csec;
  separateSpecularColor(m,surfacePosEyeCoords,n,twoSided,cpri,csec);
  return cpri+csec;
}

#endif