| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkLightingShader_DEFINED | 8 #ifndef SkLightingShader_DEFINED |
| 9 #define SkLightingShader_DEFINED | 9 #define SkLightingShader_DEFINED |
| 10 | 10 |
| 11 #include "SkFlattenable.h" | 11 #include "SkLights.h" |
| 12 #include "SkLight.h" | |
| 13 #include "SkShader.h" | 12 #include "SkShader.h" |
| 14 #include "SkTDArray.h" | |
| 15 | 13 |
| 16 class SkBitmap; | 14 class SkBitmap; |
| 17 class SkMatrix; | 15 class SkMatrix; |
| 18 | 16 |
| 19 class SK_API SkLightingShader { | 17 class SK_API SkLightingShader { |
| 20 public: | 18 public: |
| 21 class Lights : public SkRefCnt { | 19 /** Returns a shader that lights the diffuse and normal maps with a set of l
ights. |
| 22 public: | |
| 23 class Builder { | |
| 24 public: | |
| 25 Builder(const SkLight lights[], int numLights) | |
| 26 : fLights(new Lights(lights, numLights)) {} | |
| 27 | |
| 28 Builder() : fLights(new Lights) {} | |
| 29 | |
| 30 // TODO: limit the number of lights here or just ignore those | |
| 31 // above some maximum? | |
| 32 void add(const SkLight& light) { | |
| 33 if (fLights) { | |
| 34 *fLights->fLights.push() = light; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 const Lights* finish() { | |
| 39 return fLights.release(); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 SkAutoTUnref<Lights> fLights; | |
| 44 }; | |
| 45 | |
| 46 int numLights() const { | |
| 47 return fLights.count(); | |
| 48 } | |
| 49 | |
| 50 const SkLight& light(int index) const { | |
| 51 return fLights[index]; | |
| 52 } | |
| 53 | |
| 54 private: | |
| 55 Lights() {} | |
| 56 Lights(const SkLight lights[], int numLights) : fLights(lights, numLight
s) {} | |
| 57 | |
| 58 SkTDArray<SkLight> fLights; | |
| 59 | |
| 60 typedef SkRefCnt INHERITED; | |
| 61 }; | |
| 62 | |
| 63 /** Returns a shader that lights the diffuse and normal maps with a single l
ight. | |
| 64 | 20 |
| 65 It returns a shader with a reference count of 1. | 21 It returns a shader with a reference count of 1. |
| 66 The caller should decrement the shader's reference count when done with
the shader. | 22 The caller should decrement the shader's reference count when done with
the shader. |
| 67 It is an error for count to be < 2. | 23 It is an error for count to be < 2. |
| 68 @param diffuse the diffuse bitmap | 24 @param diffuse the diffuse bitmap |
| 69 @param normal the normal map | 25 @param normal the normal map |
| 70 @param light the light applied to the normal map | 26 @param lights the lights applied to the normal map |
| 71 @param ambient the linear (unpremul) ambient light color. Range is
0..1/channel. | 27 @param invNormRotation rotation applied to the normal map's normals |
| 72 @param localMatrix the matrix mapping the textures to the dest rect | 28 @param diffLocalMatrix the local matrix for the diffuse texture |
| 29 @param normLocalMatrix the local matrix for the normal map |
| 73 | 30 |
| 74 nullptr will be returned if: | 31 nullptr will be returned if: |
| 75 either 'diffuse' or 'normal' are empty | 32 either 'diffuse' or 'normal' are empty |
| 76 either 'diffuse' or 'normal' are too big (> 65535 on a side) | 33 either 'diffuse' or 'normal' are too big (> 65535 on a side) |
| 77 'diffuse' and 'normal' aren't the same size | 34 'diffuse' and 'normal' aren't the same size |
| 78 | 35 |
| 79 The lighting equation is currently: | 36 The lighting equation is currently: |
| 80 result = LightColor * DiffuseColor * (Normal * LightDir) + AmbientCo
lor | 37 result = LightColor * DiffuseColor * (Normal * LightDir) + AmbientCo
lor |
| 81 | 38 |
| 82 The normal map is currently assumed to be an 8888 image where the normal
at a texel | 39 The normal map is currently assumed to be an 8888 image where the normal
at a texel |
| 83 is retrieved by: | 40 is retrieved by: |
| 84 N.x = R-127; | 41 N.x = R-127; |
| 85 N.y = G-127; | 42 N.y = G-127; |
| 86 N.z = B-127; | 43 N.z = B-127; |
| 87 N.normalize(); | 44 N.normalize(); |
| 88 The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis
is | 45 The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis
is |
| 89 (127, 127, 0). | 46 (127, 127, 0). |
| 90 */ | 47 */ |
| 91 static sk_sp<SkShader> Make(const SkBitmap& diffuse, const SkBitmap& normal, | 48 static sk_sp<SkShader> Make(const SkBitmap& diffuse, const SkBitmap& normal, |
| 92 const Lights* lights, const SkVector& invNormRot
ation, | 49 sk_sp<SkLights> lights, const SkVector& invNormR
otation, |
| 93 const SkMatrix* diffLocalMatrix, const SkMatrix*
normLocalMatrix); | 50 const SkMatrix* diffLocalMatrix, const SkMatrix*
normLocalMatrix); |
| 94 | 51 |
| 95 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() | 52 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
| 96 }; | 53 }; |
| 97 | 54 |
| 98 #endif | 55 #endif |
| OLD | NEW |