Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: include/core/SkLights.h

Issue 2237493002: Added PointLights to SkLights::Light (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Added SkLights.cpp Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #ifndef SkLights_DEFINED 9 #ifndef SkLights_DEFINED
10 #define SkLights_DEFINED 10 #define SkLights_DEFINED
11 11
12 #include "../private/SkTArray.h"
12 #include "SkPoint3.h" 13 #include "SkPoint3.h"
13 #include "SkRefCnt.h" 14 #include "SkRefCnt.h"
14 #include "../private/SkTArray.h" 15
15 #include "SkImage.h" 16 class SkReadBuffer;
17 class SkWriteBuffer;
18 class SkImage;
16 19
17 class SK_API SkLights : public SkRefCnt { 20 class SK_API SkLights : public SkRefCnt {
18 public: 21 public:
19 class Light { 22 class Light {
20 public: 23 public:
21 enum LightType { 24 enum LightType {
22 kAmbient_LightType, // only 'fColor' is used 25 kAmbient_LightType, // only 'fColor' is used
23 kDirectional_LightType 26 kDirectional_LightType,
27 kPoint_LightType
24 }; 28 };
25 29
26 Light(const Light& other) 30 Light(const Light& other)
27 : fType(other.fType) 31 : fType(other.fType)
28 , fColor(other.fColor) 32 , fColor(other.fColor)
29 , fDirection(other.fDirection) 33 , fDirection(other.fDirection)
30 , fShadowMap(other.fShadowMap) { 34 , fShadowMap(other.fShadowMap) {
31 } 35 }
32 36
33 Light(Light&& other) 37 Light(Light&& other)
34 : fType(other.fType) 38 : fType(other.fType)
35 , fColor(other.fColor) 39 , fColor(other.fColor)
36 , fDirection(other.fDirection) 40 , fDirection(other.fDirection)
37 , fShadowMap(std::move(other.fShadowMap)) { 41 , fShadowMap(std::move(other.fShadowMap)) {
38 } 42 }
39 43
40 Light(const SkColor3f& color) 44 static Light MakeAmbient(const SkColor3f& color) {
41 : fType(kAmbient_LightType) 45 return Light(kAmbient_LightType, color, SkVector3::Make(0.0f, 0.0f, 1.0f));
42 , fColor(color) {
43 fDirection.set(0.0f, 0.0f, 1.0f);
44 } 46 }
45 47
46 Light(const SkColor3f& color, const SkVector3& dir) 48 static Light MakeDirectional(const SkColor3f& color, const SkVector3& di r) {
47 : fType(kDirectional_LightType) 49 Light light(kDirectional_LightType, color, dir);
48 , fColor(color) 50 if (!light.fDirection.normalize()) {
49 , fDirection(dir) { 51 light.fDirection.set(0.0f, 0.0f, 1.0f);
50 if (!fDirection.normalize()) {
51 fDirection.set(0.0f, 0.0f, 1.0f);
52 } 52 }
53 return light;
54 }
55
56 static Light MakePoint(const SkColor3f& color, const SkPoint3& pos) {
57 return Light(kPoint_LightType, color, pos);
53 } 58 }
54 59
55 LightType type() const { return fType; } 60 LightType type() const { return fType; }
56 const SkColor3f& color() const { return fColor; } 61 const SkColor3f& color() const { return fColor; }
57 const SkVector3& dir() const { 62 const SkVector3& dir() const {
58 SkASSERT(kAmbient_LightType != fType); 63 SkASSERT(kDirectional_LightType == fType);
59 return fDirection; 64 return fDirection;
65 }
66 const SkPoint3& pos() const {
67 SkASSERT(kPoint_LightType == fType);
68 return fDirection;
60 } 69 }
61 70
62 void setShadowMap(sk_sp<SkImage> shadowMap) { 71 void setShadowMap(sk_sp<SkImage> shadowMap) {
63 fShadowMap = std::move(shadowMap); 72 fShadowMap = std::move(shadowMap);
64 } 73 }
65 74
66 SkImage* getShadowMap() const { 75 SkImage* getShadowMap() const {
67 return fShadowMap.get(); 76 return fShadowMap.get();
68 } 77 }
69 78
70 Light& operator= (const Light& b) { 79 Light& operator= (const Light& b) {
71 if (this == &b) { 80 if (this == &b) {
72 return *this; 81 return *this;
73 } 82 }
74 83
75 fColor = b.fColor; 84 fColor = b.fColor;
76 fType = b.fType; 85 fType = b.fType;
77 fDirection = b.fDirection; 86 fDirection = b.fDirection;
78 fShadowMap = b.fShadowMap; 87 fShadowMap = b.fShadowMap;
79 return *this; 88 return *this;
80 } 89 }
81
82 private: 90 private:
83 LightType fType; 91 LightType fType;
84 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel. 92 SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
85 SkVector3 fDirection; // direction towards the light (+Z is out of the screen). 93 SkVector3 fDirection; // For directional lights, holds the direc tion towards the
94 // light (+Z is out of the screen).
86 // If degenerate, it will be replaced with (0, 0, 1). 95 // If degenerate, it will be replaced with (0, 0, 1).
96 // For point lights, holds location of poi nt light
87 sk_sp<SkImage> fShadowMap; 97 sk_sp<SkImage> fShadowMap;
98
robertphillips 2016/08/12 13:34:48 const SkColor3f& color, const SkVector3& dir);
vjiaoblack 2016/08/12 14:12:10 Done.
99 Light(LightType type, const SkColor3f color, const SkVector3 dir);
88 }; 100 };
89 101
90 class Builder { 102 class Builder {
91 public: 103 public:
92 Builder() : fLights(new SkLights) { } 104 Builder() : fLights(new SkLights) { }
93 105
94 void add(const Light& light) { 106 void add(const Light& light);
95 if (fLights) {
96 fLights->fLights.push_back(light);
97 }
98 }
99 107
100 void add(Light&& light) { 108 void add(Light&& light);
101 if (fLights) {
102 fLights->fLights.push_back(std::move(light));
103 }
104 }
105 109
106 sk_sp<SkLights> finish() { 110 sk_sp<SkLights> finish();
107 return std::move(fLights);
108 }
109 111
110 private: 112 private:
111 sk_sp<SkLights> fLights; 113 sk_sp<SkLights> fLights;
112 }; 114 };
113 115
114 int numLights() const { 116 int numLights() const;
robertphillips 2016/08/12 13:34:48 Why out line these ?
115 return fLights.count();
116 }
117 117
118 const Light& light(int index) const { 118 const Light& light(int index) const;
119 return fLights[index];
120 }
121 119
122 Light& light(int index) { 120 Light& light(int index);
123 return fLights[index]; 121
124 } 122 static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
123
124 void flatten(SkWriteBuffer& buf) const;
125 125
126 private: 126 private:
robertphillips 2016/08/12 13:34:48 no ';' needed
vjiaoblack 2016/08/12 14:12:10 Done.
127 SkLights() {} 127 SkLights() {};
128
129 SkTArray<Light> fLights; 128 SkTArray<Light> fLights;
130
131 typedef SkRefCnt INHERITED; 129 typedef SkRefCnt INHERITED;
132 }; 130 };
133 131
134 #endif 132 #endif
OLDNEW
« no previous file with comments | « gyp/core.gypi ('k') | samplecode/SampleLighting.cpp » ('j') | src/core/SkLights.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698