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