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

Unified Diff: tools/sk_tool_utils.cpp

Issue 1298763002: Move normal map creation methods to sk_tools (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cleanup Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/sk_tool_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/sk_tool_utils.cpp
diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp
index a6d381e2cfdb1c39fa155ed6eb1958c8a0195a71..6e76fc14c29d64452f4008ed0210a4c1be44658b 100644
--- a/tools/sk_tool_utils.cpp
+++ b/tools/sk_tool_utils.cpp
@@ -12,6 +12,7 @@
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkCommonFlags.h"
+#include "SkPoint3.h"
#include "SkShader.h"
#include "SkTestScalerContext.h"
#include "SkTextBlob.h"
@@ -209,5 +210,112 @@ void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPain
memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
}
+static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) {
+ SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
+ unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
+ unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255);
+ unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
+ *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
+}
+
+void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
+ const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
+ dst.fTop + (dst.height() / 2.0f));
+ const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f);
+
+ SkVector3 norm;
+
+ for (int y = dst.fTop; y < dst.fBottom; ++y) {
+ for (int x = dst.fLeft; x < dst.fRight; ++x) {
+ norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
+ norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
+
+ SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
+ if (tmp >= 1.0f) {
+ norm.set(0.0f, 0.0f, 1.0f);
+ } else {
+ norm.fZ = sqrt(1.0f - tmp);
+ }
+
+ norm_to_rgb(bm, x, y, norm);
+ }
+ }
+}
+
+void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
+ const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
+ dst.fTop + (dst.height() / 2.0f));
+
+ SkIRect inner = dst;
+ inner.inset(dst.width()/4, dst.height()/4);
+
+ SkPoint3 norm;
+ const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
+ const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
+ const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2);
+ const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
+
+ for (int y = dst.fTop; y < dst.fBottom; ++y) {
+ for (int x = dst.fLeft; x < dst.fRight; ++x) {
+ if (inner.contains(x, y)) {
+ norm.set(0.0f, 0.0f, 1.0f);
+ } else {
+ SkScalar locX = x + 0.5f - center.fX;
+ SkScalar locY = y + 0.5f - center.fY;
+
+ if (locX >= 0.0f) {
+ if (locY > 0.0f) {
+ norm = locX >= locY ? right : down; // LR corner
+ } else {
+ norm = locX > -locY ? right : up; // UR corner
+ }
+ } else {
+ if (locY > 0.0f) {
+ norm = -locX > locY ? left : down; // LL corner
+ } else {
+ norm = locX > locY ? up : left; // UL corner
+ }
+ }
+ }
+
+ norm_to_rgb(bm, x, y, norm);
+ }
+ }
+}
+
+void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
+ const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
+ dst.fTop + (dst.height() / 2.0f));
+
+ static const SkScalar k1OverRoot3 = 0.5773502692f;
+
+ SkPoint3 norm;
+ const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3);
+ const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3);
+ const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2);
+
+ for (int y = dst.fTop; y < dst.fBottom; ++y) {
+ for (int x = dst.fLeft; x < dst.fRight; ++x) {
+ SkScalar locX = x + 0.5f - center.fX;
+ SkScalar locY = y + 0.5f - center.fY;
+
+ if (locX >= 0.0f) {
+ if (locY > 0.0f) {
+ norm = locX >= locY ? rightUp : down; // LR corner
+ } else {
+ norm = rightUp;
+ }
+ } else {
+ if (locY > 0.0f) {
+ norm = -locX > locY ? leftUp : down; // LL corner
+ } else {
+ norm = leftUp;
+ }
+ }
+
+ norm_to_rgb(bm, x, y, norm);
+ }
+ }
+}
} // namespace sk_tool_utils
« no previous file with comments | « tools/sk_tool_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698