Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
3d.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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
8#include "gm/gm.h"
10#include "include/core/SkData.h"
13
14#include <cmath>
15
16struct Info {
17 float fNear = 0.05f;
18 float fFar = 4;
19 float fAngle = SK_ScalarPI / 4;
20
21 SkV3 fEye { 0, 0, 1.0f/std::tan(fAngle/2) - 1 };
22 SkV3 fCOA { 0, 0, 0 };
23 SkV3 fUp { 0, 1, 0 };
24};
25
26static SkM44 inv(const SkM44& m) {
28 if (!m.invert(&inverse)) {
29 inverse.setIdentity();
30 }
31 return inverse;
32}
33
34static SkM44 make_ctm(const Info& info, const SkM44& model, SkSize size) {
35 SkM44 camera, perspective, viewport;
36
37 SkScalar w = size.width();
38 SkScalar h = size.height();
39
40 perspective = SkM44::Perspective(info.fNear, info.fFar, info.fAngle);
41 camera = SkM44::LookAt(info.fEye, info.fCOA, info.fUp);
42 viewport.setScale(w*0.5f, h*0.5f, 1);//.postTranslate(r.centerX(), r.centerY(), 0);
43
44 return viewport * perspective * camera * model * inv(viewport);
45}
46
47static void do_draw(SkCanvas* canvas, SkColor color) {
48 SkAutoCanvasRestore acr(canvas, true);
49
50 Info info;
51
52 SkM44 m = SkM44::Rotate({0, 1, 0}, SK_ScalarPI/6);
53
54 canvas->concat(make_ctm(info, m, {300, 300}));
55
56 canvas->translate(150, 150);
58 paint.setColor(color);
59 canvas->drawRect({-100, -100, 100, 100}, paint);
60}
61
62/*
63 * Test calling drawables w/ translate and matrices
64 */
65DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300) {
66 do_draw(real_canvas, 0xFFFF0000);
67
68 SkPictureRecorder recorder;
69 SkCanvas* canvas = recorder.beginRecording(300, 300);
70
71 do_draw(canvas, 0x880000FF);
72
73 auto pic = recorder.finishRecordingAsPicture();
74 real_canvas->drawPicture(pic);
75}
static void do_draw(SkCanvas *canvas, SkColor color)
Definition 3d.cpp:47
static SkM44 make_ctm(const Info &info, const SkM44 &model, SkSize size)
Definition 3d.cpp:34
static SkM44 inv(const SkM44 &m)
Definition 3d.cpp:26
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkColor4f color
uint32_t SkColor
Definition SkColor.h:37
#define SK_ScalarPI
Definition SkScalar.h:21
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void concat(const SkMatrix &matrix)
Definition SkM44.h:150
static SkM44 LookAt(const SkV3 &eye, const SkV3 &center, const SkV3 &up)
Definition SkM44.cpp:331
static SkM44 Rotate(SkV3 axis, SkScalar radians)
Definition SkM44.h:239
@ kUninitialized_Constructor
Definition SkM44.h:167
SkM44 & setScale(SkScalar x, SkScalar y, SkScalar z=1)
Definition SkM44.h:309
static SkM44 Perspective(float near, float far, float angle)
Definition SkM44.cpp:343
SkM44 & setIdentity()
Definition SkM44.h:293
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
SkScalar w
SkScalar h
Definition 3d.cpp:16
SkV3 fUp
Definition 3d.cpp:23
float fAngle
Definition 3d.cpp:19
SkV3 fEye
Definition 3d.cpp:21
float fNear
Definition 3d.cpp:17
float fFar
Definition 3d.cpp:18
SkV3 fCOA
Definition 3d.cpp:22
Definition SkM44.h:56