SkLights.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkLights_DEFINED
  8. #define SkLights_DEFINED
  9. #include "SkPoint3.h"
  10. #include "SkRefCnt.h"
  11. #include "../private/SkTArray.h"
  12. class SkColorSpaceXformer;
  13. class SkReadBuffer;
  14. class SkWriteBuffer;
  15. /** \class SkLights
  16. SkLights encapsulates a set of directional, point and ambient lights for use with the
  17. SkLightingShader.
  18. */
  19. class SK_API SkLights : public SkRefCnt {
  20. public:
  21. class Light {
  22. public:
  23. enum LightType {
  24. kDirectional_LightType,
  25. kPoint_LightType
  26. };
  27. Light(const Light& other)
  28. : fType(other.fType)
  29. , fColor(other.fColor)
  30. , fDirOrPos(other.fDirOrPos)
  31. , fIntensity(other.fIntensity) {}
  32. Light(Light&& other)
  33. : fType(other.fType)
  34. , fColor(other.fColor)
  35. , fDirOrPos(other.fDirOrPos)
  36. , fIntensity(other.fIntensity) {}
  37. static Light MakeDirectional(const SkColor3f& color, const SkVector3& dir) {
  38. Light light(kDirectional_LightType, color, dir, 0.0f);
  39. if (!light.fDirOrPos.normalize()) {
  40. light.fDirOrPos.set(0.0f, 0.0f, 1.0f);
  41. }
  42. return light;
  43. }
  44. static Light MakePoint(const SkColor3f& color, const SkPoint3& pos, SkScalar intensity) {
  45. return Light(kPoint_LightType, color, pos, intensity);
  46. }
  47. LightType type() const { return fType; }
  48. const SkColor3f& color() const { return fColor; }
  49. const SkVector3& dir() const {
  50. SkASSERT(kDirectional_LightType == fType);
  51. return fDirOrPos;
  52. }
  53. const SkPoint3& pos() const {
  54. SkASSERT(kPoint_LightType == fType);
  55. return fDirOrPos;
  56. }
  57. SkScalar intensity() const {
  58. SkASSERT(kPoint_LightType == fType);
  59. return fIntensity;
  60. }
  61. Light& operator=(const Light& other) {
  62. if (this == &other) {
  63. return *this;
  64. }
  65. fType = other.fType;
  66. fColor = other.fColor;
  67. fDirOrPos = other.fDirOrPos;
  68. fIntensity = other.fIntensity;
  69. return *this;
  70. }
  71. bool operator==(const Light& other) {
  72. return (fType == other.fType) &&
  73. (fColor == other.fColor) &&
  74. (fDirOrPos == other.fDirOrPos) &&
  75. (fIntensity == other.fIntensity);
  76. }
  77. bool operator!=(const Light& other) { return !(this->operator==(other)); }
  78. private:
  79. friend class SkLights;
  80. Light(LightType type, const SkColor3f& color, const SkVector3& dirOrPos,
  81. SkScalar intensity)
  82. : fType(type)
  83. , fColor(color)
  84. , fDirOrPos(dirOrPos)
  85. , fIntensity(intensity) {}
  86. LightType fType;
  87. SkColor3f fColor; // linear (unpremul) color. Range is 0..1 in each channel.
  88. SkVector3 fDirOrPos; // For directional lights, holds the direction towards the
  89. // light (+Z is out of the screen).
  90. // If degenerate, it will be replaced with (0, 0, 1).
  91. // For point lights, holds location of point light
  92. SkScalar fIntensity; // For point lights, dictates the light intensity.
  93. // Simply a multiplier to the final light output value.
  94. };
  95. class Builder {
  96. public:
  97. Builder() : fLights(new SkLights) {}
  98. void add(const Light& light) {
  99. if (fLights) {
  100. fLights->fLights.push_back(light);
  101. }
  102. }
  103. void add(Light&& light) {
  104. if (fLights) {
  105. fLights->fLights.push_back(std::move(light));
  106. }
  107. }
  108. void setAmbientLightColor(const SkColor3f& color) {
  109. if (fLights) {
  110. fLights->fAmbientLightColor = color;
  111. }
  112. }
  113. sk_sp<SkLights> finish() {
  114. return std::move(fLights);
  115. }
  116. private:
  117. sk_sp<SkLights> fLights;
  118. };
  119. /** Returns number of lights not including the ambient light.
  120. @return number of lights not including the ambient light
  121. */
  122. int numLights() const { return fLights.count(); }
  123. /** Returns the index-th light.
  124. @param index the index of the desired light
  125. @return the index-th light
  126. */
  127. const Light& light(int index) const { return fLights[index]; }
  128. /** Returns the ambient light.
  129. @return the ambient light
  130. */
  131. const SkColor3f& ambientLightColor() const {
  132. return fAmbientLightColor;
  133. }
  134. /**
  135. * Recreate an SkLights object that was serialized into a buffer.
  136. *
  137. * @param SkReadBuffer Serialized blob data.
  138. * @return A new SkLights representing the serialized data, or NULL if the buffer is
  139. * invalid.
  140. */
  141. static sk_sp<SkLights> MakeFromBuffer(SkReadBuffer& buf);
  142. /**
  143. * Serialize to a buffer.
  144. *
  145. * @param buffer the write buffer to write out to
  146. */
  147. void flatten(SkWriteBuffer& buf) const;
  148. private:
  149. friend class SkLightingShaderImpl;
  150. SkLights() : fAmbientLightColor(SkColor3f::Make(0.0f, 0.0f, 0.0f)) {}
  151. sk_sp<SkLights> makeColorSpace(SkColorSpaceXformer* xformer) const;
  152. SkTArray<Light> fLights;
  153. SkColor3f fAmbientLightColor;
  154. typedef SkRefCnt INHERITED;
  155. };
  156. #endif