GtePointLightTextureEffect.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 PointLightTextureEffect::PointLightTextureEffect(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->lightModelPosition = mGeometry->lightModelPosition;
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 lightModelPosition;\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 modelLightDiff = vertexPosition - lightModelPosition.xyz;\n"
135 " vec3 vertexDirection = normalize(modelLightDiff);\n"
136 " float NDotL = -dot(vertexNormal, vertexDirection);\n"
137 " vec3 viewVector = normalize(cameraModelPosition.xyz - vertexPosition.xyz);\n"
138 " vec3 halfVector = normalize(viewVector - vertexDirection);\n"
139 " float NDotH = dot(vertexNormal, halfVector);\n"
140 " vec4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
141 " vec3 lightingColor = materialAmbient.rgb * lightingAmbient.rgb +\n"
142 " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
143 " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
144 "\n"
145 " float distance = length(modelLightDiff);\n"
146 " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
147 " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
148 "\n"
149 " vec4 textureColor = texture(baseSampler, vertexTCoord);\n"
150 "\n"
151 " vec3 color = lightingColor * textureColor.rgb;\n"
152 " pixelColor0.rgb = materialEmissive.rgb + attenuation * color;\n"
153 " pixelColor0.a = materialDiffuse.a * textureColor.a;\n"
154 "}\n";
155 
156 
158 "cbuffer PVWMatrix\n"
159 "{\n"
160 " float4x4 pvwMatrix;\n"
161 "};\n"
162 "\n"
163 "struct VS_INPUT\n"
164 "{\n"
165 " float3 modelPosition : POSITION;\n"
166 " float3 modelNormal : NORMAL;\n"
167 " float2 modelTCoord : TEXCOORD0;\n"
168 "};\n"
169 "\n"
170 "struct VS_OUTPUT\n"
171 "{\n"
172 " float3 vertexPosition : TEXCOORD0;\n"
173 " float3 vertexNormal : TEXCOORD1;\n"
174 " float2 vertexTCoord : TEXCOORD2;\n"
175 " float4 clipPosition : SV_POSITION;\n"
176 "};\n"
177 "\n"
178 "VS_OUTPUT VSMain(VS_INPUT input)\n"
179 "{\n"
180 " VS_OUTPUT output;\n"
181 "\n"
182 " output.vertexPosition = input.modelPosition;\n"
183 " output.vertexNormal = input.modelNormal;\n"
184 " output.vertexTCoord = input.modelTCoord;\n"
185 "#if GTE_USE_MAT_VEC\n"
186 " output.clipPosition = mul(pvwMatrix, float4(input.modelPosition, 1.0f));\n"
187 "#else\n"
188 " output.clipPosition = mul(float4(input.modelPosition, 1.0f), pvwMatrix);\n"
189 "#endif\n"
190 " return output;\n"
191 "}\n"
192 "\n"
193 "cbuffer Material\n"
194 "{\n"
195 " float4 materialEmissive;\n"
196 " float4 materialAmbient;\n"
197 " float4 materialDiffuse;\n"
198 " float4 materialSpecular;\n"
199 "};\n"
200 "\n"
201 "cbuffer Lighting\n"
202 "{\n"
203 " float4 lightingAmbient;\n"
204 " float4 lightingDiffuse;\n"
205 " float4 lightingSpecular;\n"
206 " float4 lightingAttenuation;\n"
207 "};\n"
208 "\n"
209 "cbuffer LightCameraGeometry\n"
210 "{\n"
211 " float4 lightModelPosition;\n"
212 " float4 cameraModelPosition;\n"
213 "};\n"
214 "\n"
215 "Texture2D<float4> baseTexture;\n"
216 "SamplerState baseSampler;\n"
217 "\n"
218 "struct PS_INPUT\n"
219 "{\n"
220 " float3 vertexPosition : TEXCOORD0;\n"
221 " float3 vertexNormal : TEXCOORD1;\n"
222 " float2 vertexTCoord : TEXCOORD2;\n"
223 "};\n"
224 "\n"
225 "struct PS_OUTPUT\n"
226 "{\n"
227 " float4 pixelColor0 : SV_TARGET0;\n"
228 "};\n"
229 "\n"
230 "PS_OUTPUT PSMain(PS_INPUT input)\n"
231 "{\n"
232 " PS_OUTPUT output;\n"
233 "\n"
234 " float3 modelLightDiff = input.vertexPosition - lightModelPosition.xyz;\n"
235 " float3 vertexDirection = normalize(modelLightDiff);\n"
236 " float NDotL = -dot(input.vertexNormal, vertexDirection);\n"
237 " float3 viewVector = normalize(cameraModelPosition.xyz - input.vertexPosition.xyz);\n"
238 " float3 halfVector = normalize(viewVector - vertexDirection);\n"
239 " float NDotH = dot(input.vertexNormal, halfVector);\n"
240 " float4 lighting = lit(NDotL, NDotH, materialSpecular.a);\n"
241 " float3 lightingColor = materialAmbient.rgb * lightingAmbient.rgb +\n"
242 " lighting.y * materialDiffuse.rgb * lightingDiffuse.rgb +\n"
243 " lighting.z * materialSpecular.rgb * lightingSpecular.rgb;\n"
244 "\n"
245 " float distance = length(modelLightDiff);\n"
246 " float attenuation = lightingAttenuation.w / (lightingAttenuation.x + distance *\n"
247 " (lightingAttenuation.y + distance * lightingAttenuation.z));\n"
248 "\n"
249 " float4 textureColor = baseTexture.Sample(baseSampler, input.vertexTCoord);\n"
250 "\n"
251 " float3 color = lightingColor * textureColor.rgb;\n"
252 " output.pixelColor0.rgb = materialEmissive.rgb + attenuation * color;\n"
253 " output.pixelColor0.a = materialDiffuse.a * textureColor.a;\n"
254 " return output;\n"
255 "}\n";
256 
258 {
260  &msHLSLSource
261 };
262 
264 {
266  &msHLSLSource
267 };
static std::string const * msPSSource[ProgramFactory::PF_NUM_API]
virtual void UpdateMaterialConstant()
std::shared_ptr< Material > mMaterial
std::shared_ptr< LightCameraGeometry > mGeometry
PointLightTextureEffect(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)
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()
std::shared_ptr< SamplerState > mSampler
static std::string const msGLSLVSSource
GLuint texture
Definition: glcorearb.h:410
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1292
std::shared_ptr< Texture2 > mTexture
std::shared_ptr< ConstantBuffer > mMaterialConstant
static std::string const * msVSSource[ProgramFactory::PF_NUM_API]
static std::string const msGLSLPSSource
static std::string const msHLSLSource
std::shared_ptr< VisualProgram > mProgram


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