GtePointLightEffect.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 PointLightEffect::PointLightEffect(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->attenuation = mLighting->attenuation;
60 }
61 
63 {
64  InternalGeometry* internalGeometry = mGeometryConstant->Get<InternalGeometry>();
65  internalGeometry->lightModelPosition = mGeometry->lightModelPosition;
66  internalGeometry->cameraModelPosition = mGeometry->cameraModelPosition;
68 }
69 
71 {
73  "uniform PVWMatrix\n"
74  "{\n"
75  " mat4 pvwMatrix;\n"
76  "};\n"
77  "\n"
78  "uniform Material\n"
79  "{\n"
80  " vec4 materialEmissive;\n"
81  " vec4 materialAmbient;\n"
82  " vec4 materialDiffuse;\n"
83  " vec4 materialSpecular;\n"
84  "};\n"
85  "\n"
86  "uniform Lighting\n"
87  "{\n"
88  " vec4 lightingAmbient;\n"
89  " vec4 lightingDiffuse;\n"
90  " vec4 lightingSpecular;\n"
91  " vec4 lightingAttenuation;\n"
92  "};\n"
93  "\n"
94  "uniform LightCameraGeometry\n"
95  "{\n"
96  " vec4 lightModelPosition;\n"
97  " vec4 cameraModelPosition;\n"
98  "};\n"
99  "\n"
100  "layout(location = 0) in vec3 modelPosition;\n"
101  "layout(location = 1) in vec3 modelNormal;\n"
102  "\n"
103  "layout(location = 0) out vec4 vertexColor;\n"
104  "\n"
105  "void main()\n"
106  "{\n"
107  " vec3 modelLightDiff = modelPosition - lightModelPosition.xyz;\n"
108  " vec3 vertexDirection = normalize(modelLightDiff);\n"
109  " float NDotL = -dot(modelNormal, vertexDirection);\n"
110  " vec3 viewVector = normalize(cameraModelPosition.xyz - modelPosition);\n"
111  " vec3 halfVector = normalize(viewVector - vertexDirection);\n"
112  " float NDotH = dot(modelNormal, halfVector);\n"
113  " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
114  "\n"
115  " float distance = length(modelLightDiff);\n"
116  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
117  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
118  "\n"
119  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
120  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
121  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
122  "\n"
123  " vertexColor.rgb = materialEmissive.rgb + attenuation * color;\n"
124  " vertexColor.a = materialDiffuse.a;\n"
125  "#if GTE_USE_MAT_VEC\n"
126  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
127  "#else\n"
128  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
129  "#endif\n"
130  "}\n",
131 
132  "uniform PVWMatrix\n"
133  "{\n"
134  " mat4 pvwMatrix;\n"
135  "};\n"
136  "\n"
137  "layout(location = 0) in vec3 modelPosition;\n"
138  "layout(location = 1) in vec3 modelNormal;\n"
139  "\n"
140  "layout(location = 0) out vec3 vertexPosition;\n"
141  "layout(location = 1) out vec3 vertexNormal;\n"
142  "\n"
143  "void main()\n"
144  "{\n"
145  " vertexPosition = modelPosition;\n"
146  " vertexNormal = modelNormal;\n"
147  "#if GTE_USE_MAT_VEC\n"
148  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
149  "#else\n"
150  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
151  "#endif\n"
152  "}\n"
153 };
154 
156 {
157  "layout(location = 0) in vec4 vertexColor;\n"
158  "\n"
159  "layout(location = 0) out vec4 pixelColor0;\n"
160  "\n"
161  "void main()\n"
162  "{\n"
163  " pixelColor0 = vertexColor;\n"
164  "}\n",
165 
167  "uniform Material\n"
168  "{\n"
169  " vec4 materialEmissive;\n"
170  " vec4 materialAmbient;\n"
171  " vec4 materialDiffuse;\n"
172  " vec4 materialSpecular;\n"
173  "};\n"
174  "\n"
175  "uniform Lighting\n"
176  "{\n"
177  " vec4 lightingAmbient;\n"
178  " vec4 lightingDiffuse;\n"
179  " vec4 lightingSpecular;\n"
180  " vec4 lightingAttenuation;\n"
181  "};\n"
182  "\n"
183  "uniform LightCameraGeometry\n"
184  "{\n"
185  " vec4 lightModelPosition;\n"
186  " vec4 cameraModelPosition;\n"
187  "};\n"
188  "\n"
189  "layout(location = 0) in vec3 vertexPosition;\n"
190  "layout(location = 1) in vec3 vertexNormal;\n"
191  "\n"
192  "layout(location = 0) out vec4 pixelColor0;\n"
193  "\n"
194  "void main()\n"
195  "{\n"
196  " vec3 modelLightDiff = vertexPosition - lightModelPosition.xyz;\n"
197  " vec3 vertexDirection = normalize(modelLightDiff);\n"
198  " float NDotL = -dot(vertexNormal, vertexDirection);\n"
199  " vec3 viewVector = normalize(cameraModelPosition.xyz - vertexPosition.xyz);\n"
200  " vec3 halfVector = normalize(viewVector - vertexDirection);\n"
201  " float NDotH = dot(vertexNormal, halfVector);\n"
202  " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
203  "\n"
204  " float distance = length(modelLightDiff);\n"
205  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
206  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
207  "\n"
208  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
209  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
210  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
211  "\n"
212  " pixelColor0.rgb = materialEmissive.rgb + attenuation * color;\n"
213  " pixelColor0.a = materialDiffuse.a;\n"
214  "}\n"
215 };
216 
218 {
219  "cbuffer PVWMatrix\n"
220  "{\n"
221  " float4x4 pvwMatrix;\n"
222  "};\n"
223  "\n"
224  "cbuffer Material\n"
225  "{\n"
226  " float4 materialEmissive;\n"
227  " float4 materialAmbient;\n"
228  " float4 materialDiffuse;\n"
229  " float4 materialSpecular;\n"
230  "};\n"
231  "\n"
232  "cbuffer Lighting\n"
233  "{\n"
234  " float4 lightingAmbient;\n"
235  " float4 lightingDiffuse;\n"
236  " float4 lightingSpecular;\n"
237  " float4 lightingAttenuation;\n"
238  "};\n"
239  "\n"
240  "cbuffer LightCameraGeometry\n"
241  "{\n"
242  " float4 lightModelPosition;\n"
243  " float4 cameraModelPosition;\n"
244  "};\n"
245  "\n"
246  "struct VS_INPUT\n"
247  "{\n"
248  " float3 modelPosition : POSITION;\n"
249  " float3 modelNormal : NORMAL;\n"
250  "};\n"
251  "\n"
252  "struct VS_OUTPUT\n"
253  "{\n"
254  " float4 vertexColor : COLOR0;\n"
255  " float4 clipPosition : SV_POSITION;\n"
256  "};\n"
257  "\n"
258  "VS_OUTPUT VSMain(VS_INPUT input)\n"
259  "{\n"
260  " VS_OUTPUT output;\n"
261  "\n"
262  " float3 modelLightDiff = input.modelPosition - lightModelPosition.xyz;\n"
263  " float3 vertexDirection = normalize(modelLightDiff);\n"
264  " float NDotL = -dot(input.modelNormal, vertexDirection);\n"
265  " float3 viewVector = normalize(cameraModelPosition.xyz - input.modelPosition);\n"
266  " float3 halfVector = normalize(viewVector - vertexDirection);\n"
267  " float NDotH = dot(input.modelNormal, halfVector);\n"
268  " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
269  "\n"
270  " float distance = length(modelLightDiff);\n"
271  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
272  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
273  "\n"
274  " float3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
275  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
276  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
277  "\n"
278  " output.vertexColor.rgb = materialEmissive.rgb + attenuation * color;\n"
279  " output.vertexColor.a = materialDiffuse.a;\n"
280  "#if GTE_USE_MAT_VEC\n"
281  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
282  "#else\n"
283  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
284  "#endif\n"
285  " return output;\n"
286  "}\n"
287  "\n"
288  "struct PS_INPUT\n"
289  "{\n"
290  " float4 vertexColor : COLOR0;\n"
291  "};\n"
292  "\n"
293  "struct PS_OUTPUT\n"
294  "{\n"
295  " float4 pixelColor0 : SV_TARGET0;\n"
296  "};\n"
297  "\n"
298  "PS_OUTPUT PSMain(PS_INPUT input)\n"
299  "{\n"
300  " PS_OUTPUT output;\n"
301  " output.pixelColor0 = input.vertexColor;\n"
302  " return output;\n"
303  "}\n",
304 
305  "cbuffer PVWMatrix\n"
306  "{\n"
307  " float4x4 pvwMatrix;\n"
308  "};\n"
309  "\n"
310  "struct VS_INPUT\n"
311  "{\n"
312  " float3 modelPosition : POSITION;\n"
313  " float3 modelNormal : NORMAL;\n"
314  "};\n"
315  "\n"
316  "struct VS_OUTPUT\n"
317  "{\n"
318  " float3 vertexPosition : TEXCOORD0;\n"
319  " float3 vertexNormal : TEXCOORD1;\n"
320  " float4 clipPosition : SV_POSITION;\n"
321  "};\n"
322  "\n"
323  "VS_OUTPUT VSMain(VS_INPUT input)\n"
324  "{\n"
325  " VS_OUTPUT output;\n"
326  "\n"
327  " output.vertexPosition = input.modelPosition;\n"
328  " output.vertexNormal = input.modelNormal;\n"
329  "#if GTE_USE_MAT_VEC\n"
330  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
331  "#else\n"
332  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
333  "#endif\n"
334  " return output;\n"
335  "}\n"
336  "\n"
337  "cbuffer Material\n"
338  "{\n"
339  " float4 materialEmissive;\n"
340  " float4 materialAmbient;\n"
341  " float4 materialDiffuse;\n"
342  " float4 materialSpecular;\n"
343  "};\n"
344  "\n"
345  "cbuffer Lighting\n"
346  "{\n"
347  " float4 lightingAmbient;\n"
348  " float4 lightingDiffuse;\n"
349  " float4 lightingSpecular;\n"
350  " float4 lightingAttenuation;\n"
351  "};\n"
352  "\n"
353  "cbuffer LightCameraGeometry\n"
354  "{\n"
355  " float4 lightModelPosition;\n"
356  " float4 cameraModelPosition;\n"
357  "};\n"
358  "\n"
359  "struct PS_INPUT\n"
360  "{\n"
361  " float3 vertexPosition : TEXCOORD0;\n"
362  " float3 vertexNormal : TEXCOORD1;\n"
363  "};\n"
364  "\n"
365  "struct PS_OUTPUT\n"
366  "{\n"
367  " float4 pixelColor0 : SV_TARGET0;\n"
368  "};\n"
369  "\n"
370  "PS_OUTPUT PSMain(PS_INPUT input)\n"
371  "{\n"
372  " PS_OUTPUT output;\n"
373  "\n"
374  " float3 modelLightDiff = input.vertexPosition - lightModelPosition.xyz;\n"
375  " float3 vertexDirection = normalize(modelLightDiff);\n"
376  " float NDotL = -dot(input.vertexNormal, vertexDirection);\n"
377  " float3 viewVector = normalize(cameraModelPosition.xyz - input.vertexPosition.xyz);\n"
378  " float3 halfVector = normalize(viewVector - vertexDirection);\n"
379  " float NDotH = dot(input.vertexNormal, halfVector);\n"
380  " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
381  "\n"
382  " float distance = length(modelLightDiff);\n"
383  " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
384  " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
385  "\n"
386  " float3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
387  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
388  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
389  "\n"
390  " output.pixelColor0.rgb = materialEmissive.rgb + attenuation * color;\n"
391  " output.pixelColor0.a = materialDiffuse.a;\n"
392  " return output;\n"
393  "}\n"
394 };
395 
397 {
398  {
399  &msGLSLVSSource[0],
400  &msHLSLSource[0]
401  },
402  {
403  &msGLSLVSSource[1],
404  &msHLSLSource[1]
405  }
406 };
407 
409 {
410  {
411  &msGLSLPSSource[0],
412  &msHLSLSource[0]
413  },
414  {
415  &msGLSLPSSource[1],
416  &msHLSLSource[1]
417  }
418 };
static std::string const * msPSSource[2][ProgramFactory::PF_NUM_API]
virtual void UpdateMaterialConstant()
std::shared_ptr< Material > mMaterial
static std::string const * msVSSource[2][ProgramFactory::PF_NUM_API]
std::shared_ptr< LightCameraGeometry > mGeometry
PointLightEffect(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)
static std::string const msGLSLVSSource[2]
std::function< void(std::shared_ptr< Buffer > const &)> BufferUpdater
Definition: GteBuffer.h:24
std::shared_ptr< ConstantBuffer > mGeometryConstant
std::shared_ptr< Lighting > mLighting
virtual void UpdateGeometryConstant()
virtual void UpdateLightingConstant()
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
std::shared_ptr< ConstantBuffer > mLightingConstant
static std::string GetShaderSourceLitFunctionGLSL()
static std::string const msGLSLPSSource[2]
virtual void UpdateGeometryConstant()
virtual void UpdateMaterialConstant()
virtual void UpdateLightingConstant()
std::shared_ptr< ConstantBuffer > mMaterialConstant
static std::string const msHLSLSource[2]
std::shared_ptr< VisualProgram > mProgram


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