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