GteLightingEffect.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.1 (2016/11/13)
7 
8 #include <GTEnginePCH.h>
11 using namespace gte;
12 
13 LightingEffect::LightingEffect(std::shared_ptr<ProgramFactory> const& factory,
14  BufferUpdater const& updater, std::string const* vsSource[], std::string const* psSource[],
15  std::shared_ptr<Material> const& material, std::shared_ptr<Lighting> const& lighting,
16  std::shared_ptr<LightCameraGeometry> const& geometry)
17  :
18  mMaterial(material),
19  mLighting(lighting),
20  mGeometry(geometry)
21 {
22  int api = factory->GetAPI();
23  mProgram = factory->CreateFromSources(*vsSource[api], *psSource[api], "");
24  if (mProgram)
25  {
26  mBufferUpdater = updater;
27  mPVWMatrixConstant = std::make_shared<ConstantBuffer>(sizeof(Matrix4x4<float>), true);
28  mProgram->GetVShader()->Set("PVWMatrix", mPVWMatrixConstant);
29  }
30 }
31 
32 void LightingEffect::SetPVWMatrixConstant(std::shared_ptr<ConstantBuffer> const& pvwMatrix)
33 {
34  mPVWMatrixConstant = pvwMatrix;
35  mProgram->GetVShader()->Set("PVWMatrix", mPVWMatrixConstant);
36 }
37 
39 {
41  {
43  }
44 }
45 
47 {
49  {
51  }
52 }
53 
55 {
57  {
59  }
60 }
61 
63 {
64  // HLSL and Cg have a 'lit' function for computing coefficients of the
65  // ambient, diffuse, and specular lighting contributions. GLSL does not.
66  // This string is prepended to any GLSL shader that needs the 'lit'
67  // function.
68  // ambient = 1.
69  // diffuse = ((n dot l) < 0) ? 0 : n dot l.
70  // specular = ((n dot l) < 0) || ((n dot h) < 0) ? 0 : (pow(n dot h, m)).
71  // Where the vector N is the normal vector, L is the direction to light
72  // and H is the half vector, and all three vectors are unit length. The
73  // inputs are NdotL = Dot(N,L), NdotH = Dot(N,H), and m is the specular
74  // exponent (stored in Material:diffuse[3] in GTEngine).
75 
76  static std::string const sourceGLSL =
77  "vec4 lit(float NdotL, float NdotH, float m)\n"
78  "{\n"
79  " float ambient = 1.0;\n"
80  " float diffuse = max(NdotL, 0.0);\n"
81  " float specular = step(0.0, NdotL) * pow(max(NdotH, 0.0), m);\n"
82  " return vec4(ambient, diffuse, specular, 1.0);\n"
83  "}\n";
84 
85  return sourceGLSL;
86 }
87 
virtual void UpdateMaterialConstant()
LightingEffect(std::shared_ptr< ProgramFactory > const &factory, BufferUpdater const &updater, std::string const *vsSource[], std::string const *psSource[], std::shared_ptr< Material > const &material, std::shared_ptr< Lighting > const &lighting, std::shared_ptr< LightCameraGeometry > const &geometry)
void SetPVWMatrixConstant(std::shared_ptr< ConstantBuffer > const &pvwMatrix)
std::function< void(std::shared_ptr< Buffer > const &)> BufferUpdater
Definition: GteBuffer.h:24
std::shared_ptr< ConstantBuffer > mGeometryConstant
virtual void UpdateGeometryConstant()
virtual void UpdateLightingConstant()
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
BufferUpdater mBufferUpdater
std::shared_ptr< ConstantBuffer > mLightingConstant
static std::string GetShaderSourceLitFunctionGLSL()
std::shared_ptr< ConstantBuffer > mMaterialConstant
std::shared_ptr< ConstantBuffer > mPVWMatrixConstant
std::shared_ptr< VisualProgram > mProgram


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