GteDirectionalLightTextureEffect.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 DirectionalLightTextureEffect::DirectionalLightTextureEffect(std::shared_ptr<ProgramFactory> const& factory,
13  BufferUpdater const& updater, std::shared_ptr<Material> const& material,
14  std::shared_ptr<Lighting> const& lighting, std::shared_ptr<LightCameraGeometry> const& geometry,
15  std::shared_ptr<Texture2> const& texture, SamplerState::Filter filter, SamplerState::Mode mode0,
16  SamplerState::Mode mode1)
17  :
18  LightingEffect(factory, updater, msVSSource, msPSSource, material, lighting, geometry),
19  mTexture(texture)
20 {
21  mSampler = std::make_shared<SamplerState>();
22  mSampler->filter = filter;
23  mSampler->mode[0] = mode0;
24  mSampler->mode[1] = mode1;
25 
26  mMaterialConstant = std::make_shared<ConstantBuffer>(sizeof(InternalMaterial), true);
28 
29  mLightingConstant = std::make_shared<ConstantBuffer>(sizeof(InternalLighting), true);
31 
32  mGeometryConstant = std::make_shared<ConstantBuffer>(sizeof(InternalGeometry), true);
34 
35  mProgram->GetPShader()->Set("Material", mMaterialConstant);
36  mProgram->GetPShader()->Set("Lighting", mLightingConstant);
37  mProgram->GetPShader()->Set("LightCameraGeometry", mGeometryConstant);
38 #if defined(GTE_DEV_OPENGL)
39  mProgram->GetPShader()->Set("baseSampler", mTexture);
40 #else
41  mProgram->GetPShader()->Set("baseTexture", mTexture);
42 #endif
43  mProgram->GetPShader()->Set("baseSampler", mSampler);
44 }
45 
47 {
48  InternalMaterial* internalMaterial = mMaterialConstant->Get<InternalMaterial>();
49  internalMaterial->emissive = mMaterial->emissive;
50  internalMaterial->ambient = mMaterial->ambient;
51  internalMaterial->diffuse = mMaterial->diffuse;
52  internalMaterial->specular = mMaterial->specular;
54 }
55 
57 {
58  InternalLighting* internalLighting = mLightingConstant->Get<InternalLighting>();
59  internalLighting->ambient = mLighting->ambient;
60  internalLighting->diffuse = mLighting->diffuse;
61  internalLighting->specular = mLighting->specular;
62  internalLighting->attenuation = mLighting->attenuation;
64 }
65 
67 {
68  InternalGeometry* internalGeometry = mGeometryConstant->Get<InternalGeometry>();
69  internalGeometry->lightModelDirection = mGeometry->lightModelDirection;
70  internalGeometry->cameraModelPosition = mGeometry->cameraModelPosition;
72 }
73 
75 "uniform PVWMatrix\n"
76 "{\n"
77 " mat4 pvwMatrix;\n"
78 "};\n"
79 "\n"
80 "layout(location = 0) in vec3 modelPosition;\n"
81 "layout(location = 1) in vec3 modelNormal;\n"
82 "layout(location = 2) in vec2 modelTCoord;\n"
83 "\n"
84 "layout(location = 0) out vec3 vertexPosition;\n"
85 "layout(location = 1) out vec3 vertexNormal;\n"
86 "layout(location = 2) out vec2 vertexTCoord;\n"
87 "\n"
88 "void main()\n"
89 "{\n"
90 " vertexPosition = modelPosition;\n"
91 " vertexNormal = modelNormal;\n"
92 " vertexTCoord = modelTCoord;\n"
93 "#if GTE_USE_MAT_VEC\n"
94 " gl_Position = pvwMatrix * vec4(modelPosition, 1.0f);\n"
95 "#else\n"
96 " gl_Position = vec4(modelPosition, 1.0f) * pvwMatrix;\n"
97 "#endif\n"
98 "}\n";
99 
102 "uniform Material\n"
103 "{\n"
104 " vec4 materialEmissive;\n"
105 " vec4 materialAmbient;\n"
106 " vec4 materialDiffuse;\n"
107 " vec4 materialSpecular;\n"
108 "};\n"
109 "\n"
110 "uniform Lighting\n"
111 "{\n"
112 " vec4 lightingAmbient;\n"
113 " vec4 lightingDiffuse;\n"
114 " vec4 lightingSpecular;\n"
115 " vec4 lightingAttenuation;\n"
116 "};\n"
117 "\n"
118 "uniform LightCameraGeometry\n"
119 "{\n"
120 " vec4 lightModelDirection;\n"
121 " vec4 cameraModelPosition;\n"
122 "};\n"
123 "\n"
124 "uniform sampler2D baseSampler;\n"
125 "\n"
126 "layout(location = 0) in vec3 vertexPosition;\n"
127 "layout(location = 1) in vec3 vertexNormal;\n"
128 "layout(location = 2) in vec2 vertexTCoord;\n"
129 "\n"
130 "layout(location = 0) out vec4 pixelColor0;\n"
131 "\n"
132 "void main()\n"
133 "{\n"
134 " vec3 normal = normalize(vertexNormal);\n"
135 " float NDotL = -dot(normal, lightModelDirection.xyz);\n"
136 " vec3 viewVector = normalize(cameraModelPosition.xyz - vertexPosition);\n"
137 " vec3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
138 " float NDotH = dot(normal, halfVector);\n"
139 " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
140 " vec3 lightingColor = materialAmbient.rgb * lightingAmbient.rgb +\n"
141 " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
142 " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
143 "\n"
144 " vec4 textureColor = texture(baseSampler, vertexTCoord);\n"
145 "\n"
146 " vec3 color = lightingColor * textureColor.rgb;\n"
147 " pixelColor0.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
148 " pixelColor0.a = materialDiffuse.a * textureColor.a;\n"
149 "}\n";
150 
151 
153 "cbuffer PVWMatrix\n"
154 "{\n"
155 " float4x4 pvwMatrix;\n"
156 "};\n"
157 "\n"
158 "struct VS_INPUT\n"
159 "{\n"
160 " float3 modelPosition : POSITION;\n"
161 " float3 modelNormal : NORMAL;\n"
162 " float2 modelTCoord : TEXCOORD0;\n"
163 "};\n"
164 "\n"
165 "struct VS_OUTPUT\n"
166 "{\n"
167 " float3 vertexPosition : TEXCOORD0;\n"
168 " float3 vertexNormal : TEXCOORD1;\n"
169 " float2 vertexTCoord : TEXCOORD2;\n"
170 " float4 clipPosition : SV_POSITION;\n"
171 "};\n"
172 "\n"
173 "VS_OUTPUT VSMain(VS_INPUT input)\n"
174 "{\n"
175 " VS_OUTPUT output;\n"
176 "\n"
177 " output.vertexPosition = input.modelPosition;\n"
178 " output.vertexNormal = input.modelNormal;\n"
179 " output.vertexTCoord = input.modelTCoord;\n"
180 "#if GTE_USE_MAT_VEC\n"
181 " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
182 "#else\n"
183 " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
184 "#endif\n"
185 " return output;\n"
186 "}\n"
187 "\n"
188 "cbuffer Material\n"
189 "{\n"
190 " float4 materialEmissive;\n"
191 " float4 materialAmbient;\n"
192 " float4 materialDiffuse;\n"
193 " float4 materialSpecular;\n"
194 "};\n"
195 "\n"
196 "cbuffer Lighting\n"
197 "{\n"
198 " float4 lightingAmbient;\n"
199 " float4 lightingDiffuse;\n"
200 " float4 lightingSpecular;\n"
201 " float4 lightingAttenuation;\n"
202 "};\n"
203 "\n"
204 "cbuffer LightCameraGeometry\n"
205 "{\n"
206 " float4 lightModelDirection;\n"
207 " float4 cameraModelPosition;\n"
208 "};\n"
209 "\n"
210 "Texture2D<float4> baseTexture;\n"
211 "SamplerState baseSampler;\n"
212 "\n"
213 "struct PS_INPUT\n"
214 "{\n"
215 " float3 vertexPosition : TEXCOORD0;\n"
216 " float3 vertexNormal : TEXCOORD1;\n"
217 " float2 vertexTCoord : TEXCOORD2;\n"
218 "};\n"
219 "\n"
220 "struct PS_OUTPUT\n"
221 "{\n"
222 " float4 pixelColor0 : SV_TARGET0;\n"
223 "};\n"
224 "\n"
225 "PS_OUTPUT PSMain(PS_INPUT input)\n"
226 "{\n"
227 " PS_OUTPUT output;\n"
228 "\n"
229 " float3 normal = normalize(input.vertexNormal);\n"
230 " float NDotL = -dot(normal, lightModelDirection.xyz);\n"
231 " float3 viewVector = normalize(cameraModelPosition.xyz - input.vertexPosition);\n"
232 " float3 halfVector = normalize(viewVector - lightModelDirection.xyz);\n"
233 " float NDotH = dot(normal, halfVector);\n"
234 " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
235 " float3 lightingColor = materialAmbient.rgb * lightingAmbient.rgb +\n"
236 " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
237 " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
238 "\n"
239 " float4 textureColor = baseTexture.Sample(baseSampler, input.vertexTCoord);\n"
240 "\n"
241 " float3 color = lightingColor * textureColor.rgb;\n"
242 " output.pixelColor0.rgb = materialEmissive.rgb + lightingAttenuation.w * color;\n"
243 " output.pixelColor0.a = materialDiffuse.a * textureColor.a;\n"
244 " return output;\n"
245 "}\n";
246 
248 {
250  &msHLSLSource
251 };
252 
254 {
256  &msHLSLSource
257 };
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
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()
GLuint texture
Definition: glcorearb.h:410
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1292
std::shared_ptr< ConstantBuffer > mMaterialConstant
static std::string const * msVSSource[ProgramFactory::PF_NUM_API]
static std::string const * msPSSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< VisualProgram > mProgram
DirectionalLightTextureEffect(std::shared_ptr< ProgramFactory > const &factory, BufferUpdater const &updater, std::shared_ptr< Material > const &material, std::shared_ptr< Lighting > const &lighting, std::shared_ptr< LightCameraGeometry > const &geometry, std::shared_ptr< Texture2 > const &texture, SamplerState::Filter filter, SamplerState::Mode mode0, SamplerState::Mode mode1)


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