GteSpotLightEffect.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
12 SpotLightEffect::SpotLightEffect(std::shared_ptr<ProgramFactory> const& factory,
13  BufferUpdater const& updater, int select, std::shared_ptr<Material> const& material,
14  std::shared_ptr<Lighting> const& lighting, std::shared_ptr<LightCameraGeometry> const& geometry)
15  :
16  LightingEffect(factory, updater, msVSSource[select & 1], msPSSource[select & 1],
17  material, lighting, geometry)
18 {
19  mMaterialConstant = std::make_shared<ConstantBuffer>(sizeof(InternalMaterial), true);
21 
22  mLightingConstant = std::make_shared<ConstantBuffer>(sizeof(InternalLighting), true);
24 
25  mGeometryConstant = std::make_shared<ConstantBuffer>(sizeof(InternalGeometry), true);
27 
28  if ((select & 1) == 0)
29  {
30  mProgram->GetVShader()->Set("Material", mMaterialConstant);
31  mProgram->GetVShader()->Set("Lighting", mLightingConstant);
32  mProgram->GetVShader()->Set("LightCameraGeometry", mGeometryConstant);
33  }
34  else
35  {
36  mProgram->GetPShader()->Set("Material", mMaterialConstant);
37  mProgram->GetPShader()->Set("Lighting", mLightingConstant);
38  mProgram->GetPShader()->Set("LightCameraGeometry", mGeometryConstant);
39  }
40 }
41 
43 {
44  InternalMaterial* internalMaterial = mMaterialConstant->Get<InternalMaterial>();
45  internalMaterial->emissive = mMaterial->emissive;
46  internalMaterial->ambient = mMaterial->ambient;
47  internalMaterial->diffuse = mMaterial->diffuse;
48  internalMaterial->specular = mMaterial->specular;
50 }
51 
53 {
54  InternalLighting* internalLighting = mLightingConstant->Get<InternalLighting>();
55  internalLighting->ambient = mLighting->ambient;
56  internalLighting->diffuse = mLighting->diffuse;
57  internalLighting->specular = mLighting->specular;
58  internalLighting->spotCutoff = mLighting->spotCutoff;
59  internalLighting->attenuation = mLighting->attenuation;
61 }
62 
64 {
65  InternalGeometry* internalGeometry = mGeometryConstant->Get<InternalGeometry>();
66  internalGeometry->lightModelPosition = mGeometry->lightModelPosition;
67  internalGeometry->lightModelDirection = mGeometry->lightModelDirection;
68  internalGeometry->cameraModelPosition = mGeometry->cameraModelPosition;
70 }
71 
73 {
75  "uniform PVWMatrix\n"
76  "{\n"
77  " mat4 pvwMatrix;\n"
78  "};\n"
79  "\n"
80  "uniform Material\n"
81  "{\n"
82  " vec4 materialEmissive;\n"
83  " vec4 materialAmbient;\n"
84  " vec4 materialDiffuse;\n"
85  " vec4 materialSpecular;\n"
86  "};\n"
87  "\n"
88  "uniform Lighting\n"
89  "{\n"
90  " vec4 lightingAmbient;\n"
91  " vec4 lightingDiffuse;\n"
92  " vec4 lightingSpecular;\n"
93  " vec4 lightingSpotCutoff;\n"
94  " vec4 lightingAttenuation;\n"
95  "};\n"
96  "\n"
97  "uniform LightCameraGeometry\n"
98  "{\n"
99  " vec4 lightModelPosition;\n"
100  " vec4 lightModelDirection;\n"
101  " vec4 cameraModelPosition;\n"
102  "};\n"
103  "\n"
104  "layout(location = 0) in vec3 modelPosition;\n"
105  "layout(location = 1) in vec3 modelNormal;\n"
106  "\n"
107  "layout(location = 0) out vec4 vertexColor;\n"
108  "\n"
109  "void main()\n"
110  "{\n"
111  " vec4 lighting;\n"
112  " vec3 modelLightDiff = modelPosition - lightModelPosition.xyz;\n"
113  " vec3 vertexDirection = normalize(modelLightDiff);\n"
114  " float vertexCosAngle = dot(lightModelDirection.xyz, vertexDirection);\n"
115  " if (vertexCosAngle >= lightingSpotCutoff.y)\n"
116  " {\n"
117  " float NDotL = -dot(modelNormal, vertexDirection);\n"
118  " vec3 viewVector = normalize(cameraModelPosition.xyz - modelPosition);\n"
119  " vec3 halfVector = normalize(viewVector - vertexDirection);\n"
120  " float NDotH = dot(modelNormal, halfVector);\n"
121  " lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
122  " lighting.w = pow(abs(vertexCosAngle), lightingSpotCutoff.w);\n"
123  " }\n"
124  " else\n"
125  " {\n"
126  " lighting = vec4(1.0f, 0.0f, 0.0f, 0.0f);\n"
127  " }\n"
128  "\n"
129  " float distance = length(modelLightDiff);\n"
130  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
131  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
132  "\n"
133  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb + lighting.w * (\n"
134  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
135  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb);\n"
136  "\n"
137  " vertexColor.rgb = materialEmissive.rgb + attenuation * color;\n"
138  " vertexColor.a = materialDiffuse.a;\n"
139  "#if GTE_USE_MAT_VEC\n"
140  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
141  "#else\n"
142  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
143  "#endif\n"
144  "}\n",
145 
146  "uniform PVWMatrix\n"
147  "{\n"
148  " mat4 pvwMatrix;\n"
149  "};\n"
150  "\n"
151  "layout(location = 0) in vec3 modelPosition;\n"
152  "layout(location = 1) in vec3 modelNormal;\n"
153  "\n"
154  "layout(location = 0) out vec3 vertexPosition;\n"
155  "layout(location = 1) out vec3 vertexNormal;\n"
156  "\n"
157  "void main()\n"
158  "{\n"
159  " vertexPosition = modelPosition;\n"
160  " vertexNormal = modelNormal;\n"
161  "#if GTE_USE_MAT_VEC\n"
162  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
163  "#else\n"
164  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
165  "#endif\n"
166  "}\n"
167 };
168 
170 {
171  "layout(location = 0) in vec4 vertexColor;\n"
172  "\n"
173  "layout(location = 0) out vec4 pixelColor0;\n"
174  "\n"
175  "void main()\n"
176  "{\n"
177  " pixelColor0 = vertexColor;\n"
178  "}\n",
179 
181  "uniform Material\n"
182  "{\n"
183  " vec4 materialEmissive;\n"
184  " vec4 materialAmbient;\n"
185  " vec4 materialDiffuse;\n"
186  " vec4 materialSpecular;\n"
187  "};\n"
188  "\n"
189  "uniform Lighting\n"
190  "{\n"
191  " vec4 lightingAmbient;\n"
192  " vec4 lightingDiffuse;\n"
193  " vec4 lightingSpecular;\n"
194  " vec4 lightingSpotCutoff;\n"
195  " vec4 lightingAttenuation;\n"
196  "};\n"
197  "\n"
198  "uniform LightCameraGeometry\n"
199  "{\n"
200  " vec4 lightModelPosition;\n"
201  " vec4 lightModelDirection;\n"
202  " vec4 cameraModelPosition;\n"
203  "};\n"
204  "\n"
205  "layout(location = 0) in vec3 vertexPosition;\n"
206  "layout(location = 1) in vec3 vertexNormal;\n"
207  "\n"
208  "layout(location = 0) out vec4 pixelColor0;\n"
209  "\n"
210  "void main()\n"
211  "{\n"
212  " vec4 lighting;\n"
213  " vec3 normal = normalize(vertexNormal);\n"
214  " vec3 modelLightDiff = vertexPosition - lightModelPosition.xyz;\n"
215  " vec3 vertexDirection = normalize(modelLightDiff);\n"
216  " float vertexCosAngle = dot(lightModelDirection.xyz, vertexDirection);\n"
217  " if (vertexCosAngle >= lightingSpotCutoff.y)\n"
218  " {\n"
219  " float NDotL = -dot(normal, vertexDirection);\n"
220  " vec3 viewVector = normalize(cameraModelPosition.xyz - vertexPosition);\n"
221  " vec3 halfVector = normalize(viewVector - vertexDirection);\n"
222  " float NDotH = dot(normal, halfVector);\n"
223  " lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
224  " lighting.w = pow(abs(vertexCosAngle), lightingSpotCutoff.w);\n"
225  " }\n"
226  " else\n"
227  " {\n"
228  " lighting = vec4(1.0f, 0.0f, 0.0f, 0.0f);\n"
229  " }\n"
230  "\n"
231  " // Compute the distance-based attenuation.\n"
232  " float distance = length(modelLightDiff);\n"
233  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
234  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
235  "\n"
236  " // Compute the lighting color.\n"
237  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb + lighting.w * (\n"
238  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
239  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb);\n"
240  "\n"
241  " // Compute the pixel color.\n"
242  " pixelColor0.rgb = materialEmissive.rgb + attenuation*color;\n"
243  " pixelColor0.a = materialDiffuse.a;\n"
244  "}\n"
245 };
246 
248 {
249  "cbuffer PVWMatrix\n"
250  "{\n"
251  " float4x4 pvwMatrix;\n"
252  "};\n"
253  "\n"
254  "cbuffer Material\n"
255  "{\n"
256  " float4 materialEmissive;\n"
257  " float4 materialAmbient;\n"
258  " float4 materialDiffuse;\n"
259  " float4 materialSpecular;\n"
260  "};\n"
261  "\n"
262  "cbuffer Lighting\n"
263  "{\n"
264  " float4 lightingAmbient;\n"
265  " float4 lightingDiffuse;\n"
266  " float4 lightingSpecular;\n"
267  " float4 lightingSpotCutoff;\n"
268  " float4 lightingAttenuation;\n"
269  "};\n"
270  "\n"
271  "cbuffer LightCameraGeometry\n"
272  "{\n"
273  " float4 lightModelPosition;\n"
274  " float4 lightModelDirection;\n"
275  " float4 cameraModelPosition;\n"
276  "};\n"
277  "\n"
278  "struct VS_INPUT\n"
279  "{\n"
280  " float3 modelPosition : POSITION;\n"
281  " float3 modelNormal : NORMAL;\n"
282  "};\n"
283  "\n"
284  "struct VS_OUTPUT\n"
285  "{\n"
286  " float4 vertexColor : COLOR0;\n"
287  " float4 clipPosition : SV_POSITION;\n"
288  "};\n"
289  "\n"
290  "VS_OUTPUT VSMain(VS_INPUT input)\n"
291  "{\n"
292  " VS_OUTPUT output;\n"
293  "\n"
294  " float4 lighting;\n"
295  " float3 modelLightDiff = input.modelPosition - lightModelPosition.xyz;\n"
296  " float3 vertexDirection = normalize(modelLightDiff);\n"
297  " float vertexCosAngle = dot(lightModelDirection.xyz, vertexDirection);\n"
298  " if (vertexCosAngle >= lightingSpotCutoff.y)\n"
299  " {\n"
300  " float NDotL = -dot(input.modelNormal, vertexDirection);\n"
301  " float3 viewVector = normalize(cameraModelPosition.xyz - input.modelPosition);\n"
302  " float3 halfVector = normalize(viewVector - vertexDirection);\n"
303  " float NDotH = dot(input.modelNormal, halfVector);\n"
304  " lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
305  " lighting.w = pow(abs(vertexCosAngle), lightingSpotCutoff.w);\n"
306  " }\n"
307  " else\n"
308  " {\n"
309  " lighting = float4(1.0f, 0.0f, 0.0f, 0.0f);\n"
310  " }\n"
311  "\n"
312  " float distance = length(modelLightDiff);\n"
313  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
314  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
315  "\n"
316  " float3 color = materialAmbient.rgb * lightingAmbient.rgb + lighting.w * (\n"
317  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
318  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb);\n"
319  "\n"
320  " output.vertexColor.rgb = materialEmissive.rgb + attenuation * color;\n"
321  " output.vertexColor.a = materialDiffuse.a;\n"
322  "#if GTE_USE_MAT_VEC\n"
323  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
324  "#else\n"
325  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
326  "#endif\n"
327  " return output;\n"
328  "}\n"
329  "\n"
330  "struct PS_INPUT\n"
331  "{\n"
332  " float4 vertexColor : COLOR0;\n"
333  "};\n"
334  "\n"
335  "struct PS_OUTPUT\n"
336  "{\n"
337  " float4 pixelColor0 : SV_TARGET0;\n"
338  "};\n"
339  "\n"
340  "PS_OUTPUT PSMain(PS_INPUT input)\n"
341  "{\n"
342  " PS_OUTPUT output;\n"
343  " output.pixelColor0 = input.vertexColor;\n"
344  " return output;\n"
345  "}\n",
346 
347  "cbuffer PVWMatrix\n"
348  "{\n"
349  " float4x4 pvwMatrix;\n"
350  "};\n"
351  "\n"
352  "struct VS_INPUT\n"
353  "{\n"
354  " float3 modelPosition : POSITION;\n"
355  " float3 modelNormal : NORMAL;\n"
356  "};\n"
357  "\n"
358  "struct VS_OUTPUT\n"
359  "{\n"
360  " float3 vertexPosition : TEXCOORD0;\n"
361  " float3 vertexNormal : TEXCOORD1;\n"
362  " float4 clipPosition : SV_POSITION;\n"
363  "};\n"
364  "\n"
365  "VS_OUTPUT VSMain(VS_INPUT input)\n"
366  "{\n"
367  " VS_OUTPUT output;\n"
368  "\n"
369  " output.vertexPosition = input.modelPosition;\n"
370  " output.vertexNormal = input.modelNormal;\n"
371  "#if GTE_USE_MAT_VEC\n"
372  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
373  "#else\n"
374  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
375  "#endif\n"
376  " return output;\n"
377  "}\n"
378  "\n"
379  "cbuffer Material\n"
380  "{\n"
381  " float4 materialEmissive;\n"
382  " float4 materialAmbient;\n"
383  " float4 materialDiffuse;\n"
384  " float4 materialSpecular;\n"
385  "};\n"
386  "\n"
387  "cbuffer Lighting\n"
388  "{\n"
389  " float4 lightingAmbient;\n"
390  " float4 lightingDiffuse;\n"
391  " float4 lightingSpecular;\n"
392  " float4 lightingSpotCutoff;\n"
393  " float4 lightingAttenuation;\n"
394  "};\n"
395  "\n"
396  "cbuffer LightCameraGeometry\n"
397  "{\n"
398  " float4 lightModelPosition;\n"
399  " float4 lightModelDirection;\n"
400  " float4 cameraModelPosition;\n"
401  "};\n"
402  "\n"
403  "struct PS_INPUT\n"
404  "{\n"
405  " float3 vertexPosition : TEXCOORD0;\n"
406  " float3 vertexNormal : TEXCOORD1;\n"
407  "};\n"
408  "\n"
409  "struct PS_OUTPUT\n"
410  "{\n"
411  " float4 pixelColor0 : SV_TARGET0;\n"
412  "};\n"
413  "\n"
414  "PS_OUTPUT PSMain(PS_INPUT input)\n"
415  "{\n"
416  " PS_OUTPUT output;\n"
417  "\n"
418  " float4 lighting;\n"
419  " float3 normal = normalize(input.vertexNormal);\n"
420  " float3 modelLightDiff = input.vertexPosition - lightModelPosition.xyz;\n"
421  " float3 vertexDirection = normalize(modelLightDiff);\n"
422  " float vertexCosAngle = dot(lightModelDirection.xyz, vertexDirection);\n"
423  " if (vertexCosAngle >= lightingSpotCutoff.y)\n"
424  " {\n"
425  " float NDotL = -dot(normal, vertexDirection);\n"
426  " float3 viewVector = normalize(cameraModelPosition.xyz - input.vertexPosition);\n"
427  " float3 halfVector = normalize(viewVector - vertexDirection);\n"
428  " float NDotH = dot(normal, halfVector);\n"
429  " lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
430  " lighting.w = pow(abs(vertexCosAngle), lightingSpotCutoff.w);\n"
431  " }\n"
432  " else\n"
433  " {\n"
434  " lighting = float4(1.0f, 0.0f, 0.0f, 0.0f);\n"
435  " }\n"
436  "\n"
437  " // Compute the distance-based attenuation.\n"
438  " float distance = length(modelLightDiff);\n"
439  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
440  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
441  "\n"
442  " // Compute the lighting color.\n"
443  " float3 color = materialAmbient.rgb * lightingAmbient.rgb + lighting.w * (\n"
444  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
445  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb);\n"
446  "\n"
447  " // Compute the pixel color.\n"
448  " output.pixelColor0.rgb = materialEmissive.rgb + attenuation*color;\n"
449  " output.pixelColor0.a = materialDiffuse.a;\n"
450  " return output;\n"
451  "}\n"
452 };
453 
455 {
456  {
457  &msGLSLVSSource[0],
458  &msHLSLSource[0]
459  },
460  {
461  &msGLSLVSSource[1],
462  &msHLSLSource[1]
463  }
464 };
465 
467 {
468  {
469  &msGLSLPSSource[0],
470  &msHLSLSource[0]
471  },
472  {
473  &msGLSLPSSource[1],
474  &msHLSLSource[1]
475  }
476 };
virtual void UpdateMaterialConstant()
std::shared_ptr< Material > mMaterial
virtual void UpdateLightingConstant()
static std::string const msHLSLSource[2]
std::shared_ptr< LightCameraGeometry > mGeometry
std::function< void(std::shared_ptr< Buffer > const &)> BufferUpdater
Definition: GteBuffer.h:24
static std::string const * msVSSource[2][ProgramFactory::PF_NUM_API]
std::shared_ptr< ConstantBuffer > mGeometryConstant
SpotLightEffect(std::shared_ptr< ProgramFactory > const &factory, BufferUpdater const &updater, int select, std::shared_ptr< Material > const &material, std::shared_ptr< Lighting > const &lighting, std::shared_ptr< LightCameraGeometry > const &geometry)
std::shared_ptr< Lighting > mLighting
virtual void UpdateGeometryConstant()
virtual void UpdateLightingConstant()
static std::string const msGLSLVSSource[2]
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
std::shared_ptr< ConstantBuffer > mLightingConstant
static std::string GetShaderSourceLitFunctionGLSL()
std::shared_ptr< ConstantBuffer > mMaterialConstant
virtual void UpdateGeometryConstant()
static std::string const * msPSSource[2][ProgramFactory::PF_NUM_API]
virtual void UpdateMaterialConstant()
static std::string const msGLSLPSSource[2]
std::shared_ptr< VisualProgram > mProgram


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01