GteDirectionalLightEffect.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 DirectionalLightEffect::DirectionalLightEffect(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->lightModelDirection = mGeometry->lightModelDirection;
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 lightModelDirection;\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  " float NDotL = -dot(modelNormal, lightModelDirection.xyz);\n"
108  " vec3 viewVector = normalize(cameraModelPosition.xyz - modelPosition);\n"
109  " vec3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
110  " float NDotH = dot(modelNormal, halfVector);\n"
111  " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
112  "\n"
113  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
114  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
115  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
116  "\n"
117  " vertexColor.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
118  " vertexColor.a = materialDiffuse.a;\n"
119  "#if GTE_USE_MAT_VEC\n"
120  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
121  "#else\n"
122  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
123  "#endif\n"
124  "}\n",
125 
126  "uniform PVWMatrix\n"
127  "{\n"
128  " mat4 pvwMatrix;\n"
129  "};\n"
130  "\n"
131  "layout(location = 0) in vec3 modelPosition;\n"
132  "layout(location = 1) in vec3 modelNormal;\n"
133  "\n"
134  "layout(location = 0) out vec3 vertexPosition;\n"
135  "layout(location = 1) out vec3 vertexNormal;\n"
136  "\n"
137  "void main()\n"
138  "{\n"
139  " vertexPosition = modelPosition;\n"
140  " vertexNormal = modelNormal;\n"
141  "#if GTE_USE_MAT_VEC\n"
142  " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
143  "#else\n"
144  " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
145  "#endif\n"
146  "}\n"
147 };
148 
150 {
151  "layout(location = 0) in vec4 vertexColor;\n"
152  "\n"
153  "layout(location = 0) out vec4 pixelColor0;\n"
154  "\n"
155  "void main()\n"
156  "{\n"
157  " pixelColor0 = vertexColor;\n"
158  "}\n",
159 
161  "uniform Material\n"
162  "{\n"
163  " vec4 materialEmissive;\n"
164  " vec4 materialAmbient;\n"
165  " vec4 materialDiffuse;\n"
166  " vec4 materialSpecular;\n"
167  "};\n"
168  "\n"
169  "uniform Lighting\n"
170  "{\n"
171  " vec4 lightingAmbient;\n"
172  " vec4 lightingDiffuse;\n"
173  " vec4 lightingSpecular;\n"
174  " vec4 lightingAttenuation;\n"
175  "};\n"
176  "\n"
177  "uniform LightCameraGeometry\n"
178  "{\n"
179  " vec4 lightModelDirection;\n"
180  " vec4 cameraModelPosition;\n"
181  "};\n"
182  "\n"
183  "layout(location = 0) in vec3 vertexPosition;\n"
184  "layout(location = 1) in vec3 vertexNormal;\n"
185  "\n"
186  "layout(location = 0) out vec4 pixelColor0;\n"
187  "\n"
188  "void main()\n"
189  "{\n"
190  " vec3 normal = normalize(vertexNormal);\n"
191  " float NDotL = -dot(normal, lightModelDirection.xyz);\n"
192  " vec3 viewVector = normalize(cameraModelPosition.xyz - vertexPosition);\n"
193  " vec3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
194  " float NDotH = dot(normal, halfVector);\n"
195  " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
196  "\n"
197  " vec3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
198  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
199  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
200  "\n"
201  " pixelColor0.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
202  " pixelColor0.a = materialDiffuse.a;\n"
203  "}\n"
204 };
205 
207 {
208  "cbuffer PVWMatrix\n"
209  "{\n"
210  " float4x4 pvwMatrix;\n"
211  "};\n"
212  "\n"
213  "cbuffer Material\n"
214  "{\n"
215  " float4 materialEmissive;\n"
216  " float4 materialAmbient;\n"
217  " float4 materialDiffuse;\n"
218  " float4 materialSpecular;\n"
219  "};\n"
220  "\n"
221  "cbuffer Lighting\n"
222  "{\n"
223  " float4 lightingAmbient;\n"
224  " float4 lightingDiffuse;\n"
225  " float4 lightingSpecular;\n"
226  " float4 lightingAttenuation;\n"
227  "};\n"
228  "\n"
229  "cbuffer LightCameraGeometry\n"
230  "{\n"
231  " float4 lightModelDirection;\n"
232  " float4 cameraModelPosition;\n"
233  "};\n"
234  "\n"
235  "struct VS_INPUT\n"
236  "{\n"
237  " float3 modelPosition : POSITION;\n"
238  " float3 modelNormal : NORMAL;\n"
239  "};\n"
240  "\n"
241  "struct VS_OUTPUT\n"
242  "{\n"
243  " float4 vertexColor : COLOR0;\n"
244  " float4 clipPosition : SV_POSITION;\n"
245  "};\n"
246  "\n"
247  "VS_OUTPUT VSMain(VS_INPUT input)\n"
248  "{\n"
249  " VS_OUTPUT output;\n"
250  "\n"
251  " float NDotL = -dot(input.modelNormal, lightModelDirection.xyz);\n"
252  " float3 viewVector = normalize(cameraModelPosition.xyz - input.modelPosition);\n"
253  " float3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
254  " float NDotH = dot(input.modelNormal, halfVector);\n"
255  " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
256  "\n"
257  " float3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
258  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
259  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
260  "\n"
261  " output.vertexColor.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
262  " output.vertexColor.a = materialDiffuse.a;\n"
263  "#if GTE_USE_MAT_VEC\n"
264  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
265  "#else\n"
266  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
267  "#endif\n"
268  " return output;\n"
269  "}\n"
270  "\n"
271  "struct PS_INPUT\n"
272  "{\n"
273  " float4 vertexColor : COLOR0;\n"
274  "};\n"
275  "\n"
276  "struct PS_OUTPUT\n"
277  "{\n"
278  " float4 pixelColor0 : SV_TARGET0;\n"
279  "};\n"
280  "\n"
281  "PS_OUTPUT PSMain(PS_INPUT input)\n"
282  "{\n"
283  " PS_OUTPUT output;\n"
284  " output.pixelColor0 = input.vertexColor;\n"
285  " return output;\n"
286  "}\n",
287 
288  "cbuffer PVWMatrix\n"
289  "{\n"
290  " float4x4 pvwMatrix;\n"
291  "};\n"
292  "\n"
293  "struct VS_INPUT\n"
294  "{\n"
295  " float3 modelPosition : POSITION;\n"
296  " float3 modelNormal : NORMAL;\n"
297  "};\n"
298  "\n"
299  "struct VS_OUTPUT\n"
300  "{\n"
301  " float3 vertexPosition : TEXCOORD0;\n"
302  " float3 vertexNormal : TEXCOORD1;\n"
303  " float4 clipPosition : SV_POSITION;\n"
304  "};\n"
305  "\n"
306  "VS_OUTPUT VSMain(VS_INPUT input)\n"
307  "{\n"
308  " VS_OUTPUT output;\n"
309  "\n"
310  " output.vertexPosition = input.modelPosition;\n"
311  " output.vertexNormal = input.modelNormal;\n"
312  "#if GTE_USE_MAT_VEC\n"
313  " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
314  "#else\n"
315  " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
316  "#endif\n"
317  " return output;\n"
318  "}\n"
319  "\n"
320  "cbuffer Material\n"
321  "{\n"
322  " float4 materialEmissive;\n"
323  " float4 materialAmbient;\n"
324  " float4 materialDiffuse;\n"
325  " float4 materialSpecular;\n"
326  "};\n"
327  "\n"
328  "cbuffer Lighting\n"
329  "{\n"
330  " float4 lightingAmbient;\n"
331  " float4 lightingDiffuse;\n"
332  " float4 lightingSpecular;\n"
333  " float4 lightingAttenuation;\n"
334  "};\n"
335  "\n"
336  "cbuffer LightCameraGeometry\n"
337  "{\n"
338  " float4 lightModelDirection;\n"
339  " float4 cameraModelPosition;\n"
340  "};\n"
341  "\n"
342  "struct PS_INPUT\n"
343  "{\n"
344  " float3 vertexPosition : TEXCOORD0;\n"
345  " float3 vertexNormal : TEXCOORD1;\n"
346  "};\n"
347  "\n"
348  "struct PS_OUTPUT\n"
349  "{\n"
350  " float4 pixelColor0 : SV_TARGET0;\n"
351  "};\n"
352  "\n"
353  "PS_OUTPUT PSMain(PS_INPUT input)\n"
354  "{\n"
355  " PS_OUTPUT output;\n"
356  "\n"
357  " float3 normal = normalize(input.vertexNormal);\n"
358  " float NDotL = -dot(normal, lightModelDirection.xyz);\n"
359  " float3 viewVector = normalize(cameraModelPosition.xyz - input.vertexPosition);\n"
360  " float3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
361  " float NDotH = dot(normal, halfVector);\n"
362  " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
363  "\n"
364  " float3 color = materialAmbient.rgb * lightingAmbient.rgb +\n"
365  " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
366  " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
367  "\n"
368  " output.pixelColor0.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
369  " output.pixelColor0.a = materialDiffuse.a;\n"
370  " return output;\n"
371  "}\n"
372 };
373 
375 {
376  {
377  &msGLSLVSSource[0],
378  &msHLSLSource[0]
379  },
380  {
381  &msGLSLVSSource[1],
382  &msHLSLSource[1]
383  }
384 };
385 
387 {
388  {
389  &msGLSLPSSource[0],
390  &msHLSLSource[0]
391  },
392  {
393  &msGLSLPSSource[1],
394  &msHLSLSource[1]
395  }
396 };
static std::string const msGLSLPSSource[2]
virtual void UpdateMaterialConstant()
std::shared_ptr< Material > mMaterial
std::shared_ptr< LightCameraGeometry > mGeometry
std::function< void(std::shared_ptr< Buffer > const &)> BufferUpdater
Definition: GteBuffer.h:24
static std::string const msHLSLSource[2]
static std::string const * msPSSource[2][ProgramFactory::PF_NUM_API]
std::shared_ptr< ConstantBuffer > mGeometryConstant
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
DirectionalLightEffect(std::shared_ptr< ProgramFactory > const &factory, BufferUpdater const &bufferUpdater, int select, std::shared_ptr< Material > const &material, std::shared_ptr< Lighting > const &lighting, std::shared_ptr< LightCameraGeometry > const &geometry)
static std::string const * msVSSource[2][ProgramFactory::PF_NUM_API]
std::shared_ptr< VisualProgram > mProgram


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59