OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkLightingShader_DEFINED | 10 #ifndef SkLightingShader_DEFINED |
11 #define SkLightingShader_DEFINED | 11 #define SkLightingShader_DEFINED |
12 | 12 |
13 #include "SkPoint3.h" | 13 #include "SkFlattenable.h" |
| 14 #include "SkLight.h" |
14 #include "SkShader.h" | 15 #include "SkShader.h" |
| 16 #include "SkTDArray.h" |
| 17 |
| 18 class SkBitmap; |
| 19 class SkMatrix; |
15 | 20 |
16 class SK_API SkLightingShader { | 21 class SK_API SkLightingShader { |
17 public: | 22 public: |
18 struct Light { | 23 class Lights : public SkRefCnt { |
19 SkVector3 fDirection; // direction towards the light (+Z is out
of the screen). | 24 public: |
20 // If degenerate, it will be replaced with
(0, 0, 1). | 25 class Builder { |
21 SkColor3f fColor; // linear (unpremul) color. Range is 0..1
in each channel. | 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; |
22 }; | 64 }; |
23 | 65 |
24 /** Returns a shader that lights the diffuse and normal maps with a single l
ight. | 66 /** Returns a shader that lights the diffuse and normal maps with a single l
ight. |
25 | 67 |
26 It returns a shader with a reference count of 1. | 68 It returns a shader with a reference count of 1. |
27 The caller should decrement the shader's reference count when done with
the shader. | 69 The caller should decrement the shader's reference count when done with
the shader. |
28 It is an error for count to be < 2. | 70 It is an error for count to be < 2. |
29 @param diffuse the diffuse bitmap | 71 @param diffuse the diffuse bitmap |
30 @param normal the normal map | 72 @param normal the normal map |
31 @param light the light applied to the normal map | 73 @param light the light applied to the normal map |
(...skipping 11 matching lines...) Expand all Loading... |
43 The normal map is currently assumed to be an 8888 image where the normal
at a texel | 85 The normal map is currently assumed to be an 8888 image where the normal
at a texel |
44 is retrieved by: | 86 is retrieved by: |
45 N.x = R-127; | 87 N.x = R-127; |
46 N.y = G-127; | 88 N.y = G-127; |
47 N.z = B-127; | 89 N.z = B-127; |
48 N.normalize(); | 90 N.normalize(); |
49 The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis
is | 91 The +Z axis is thus encoded in RGB as (127, 127, 255) while the -Z axis
is |
50 (127, 127, 0). | 92 (127, 127, 0). |
51 */ | 93 */ |
52 static SkShader* Create(const SkBitmap& diffuse, const SkBitmap& normal, | 94 static SkShader* Create(const SkBitmap& diffuse, const SkBitmap& normal, |
53 const SkLightingShader::Light& light, const SkColor3
f& ambient, | 95 const Lights* lights, const SkVector& invNormRotatio
n, |
54 const SkMatrix* localMatrix); | 96 const SkMatrix* diffLocalMatrix, const SkMatrix* nor
mLocalMatrix); |
55 | 97 |
56 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() | 98 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
57 }; | 99 }; |
58 | 100 |
59 #endif | 101 #endif |
OLD | NEW |