blob: 5a33fd98674918a18837ef9e691d3c9e324b555b [file] [log] [blame]
Mike Reeda3a704a2020-01-10 17:21:40 -05001/*
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"
9#include "include/core/SkCanvas.h"
Brian Osman8cbedf92020-03-31 10:38:31 -040010#include "include/core/SkData.h"
Mike Reeda3a704a2020-01-10 17:21:40 -050011
12struct Info {
13 float fNear = 0.05f;
14 float fFar = 4;
15 float fAngle = SK_ScalarPI / 4;
16
Mike Reed00a97642020-01-25 18:42:23 -050017 SkV3 fEye { 0, 0, 1.0f/tan(fAngle/2) - 1 };
18 SkV3 fCOA { 0, 0, 0 };
19 SkV3 fUp { 0, 1, 0 };
Mike Reeda3a704a2020-01-10 17:21:40 -050020};
21
Mike Reed00a97642020-01-25 18:42:23 -050022static SkM44 inv(const SkM44& m) {
Florin Malitad5899162020-02-04 10:06:24 -050023 SkM44 inverse(SkM44::kUninitialized_Constructor);
24 if (!m.invert(&inverse)) {
25 inverse.setIdentity();
26 }
Mike Reeda3a704a2020-01-10 17:21:40 -050027 return inverse;
28}
29
Mike Reed00a97642020-01-25 18:42:23 -050030static SkM44 make_ctm(const Info& info, const SkM44& model, SkSize size) {
31 SkM44 camera, perspective, viewport;
Mike Reeda3a704a2020-01-10 17:21:40 -050032
33 SkScalar w = size.width();
34 SkScalar h = size.height();
35
Mike Reed00a97642020-01-25 18:42:23 -050036 perspective = Sk3Perspective(info.fNear, info.fFar, info.fAngle);
37 camera = Sk3LookAt(info.fEye, info.fCOA, info.fUp);
Mike Reeda3a704a2020-01-10 17:21:40 -050038 viewport.setScale(w*0.5f, h*0.5f, 1);//.postTranslate(r.centerX(), r.centerY(), 0);
39
40 return viewport * perspective * camera * model * inv(viewport);
41}
42
43#include "include/core/SkPicture.h"
44#include "include/core/SkPictureRecorder.h"
45
46static void do_draw(SkCanvas* canvas, SkColor color) {
47 SkAutoCanvasRestore acr(canvas, true);
48
49 Info info;
50
Mike Reed00a97642020-01-25 18:42:23 -050051 SkM44 m = SkM44::Rotate({0, 1, 0}, SK_ScalarPI/6);
Mike Reeda3a704a2020-01-10 17:21:40 -050052
Mike Reed3ef77dd2020-04-06 10:41:09 -040053 canvas->concat(make_ctm(info, m, {300, 300}));
Mike Reeda3a704a2020-01-10 17:21:40 -050054
55 canvas->translate(150, 150);
56 SkPaint paint;
57 paint.setColor(color);
58 canvas->drawRect({-100, -100, 100, 100}, paint);
59}
60
61/*
62 * Test calling drawables w/ translate and matrices
63 */
64DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300) {
65 do_draw(real_canvas, 0xFFFF0000);
66
67 SkPictureRecorder recorder;
68 SkCanvas* canvas = recorder.beginRecording(300, 300);
69
70 do_draw(canvas, 0x880000FF);
71
72 auto pic = recorder.finishRecordingAsPicture();
73 if (true) {
74 real_canvas->drawPicture(pic);
75 } else {
76 auto data = pic->serialize();
77 auto pic2 = SkPicture::MakeFromData(data.get());
78 real_canvas->drawPicture(pic2);
79 }
80}