Flutter Engine
The Flutter Engine
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
DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300)
Definition: 3d.cpp:65
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
uint32_t SkColor
Definition: SkColor.h:37
#define SK_ScalarPI
Definition: SkScalar.h:21
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
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
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
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: SkSize.h:52
Definition: SkM44.h:56