blob: cbc664f51ce76ea5f6f4a8f24c30f272f7c8b129 [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"
Mike Reeda3a704a2020-01-10 17:21:40 -050010
11struct Info {
12 float fNear = 0.05f;
13 float fFar = 4;
14 float fAngle = SK_ScalarPI / 4;
15
Mike Reed00a97642020-01-25 18:42:23 -050016 SkV3 fEye { 0, 0, 1.0f/tan(fAngle/2) - 1 };
17 SkV3 fCOA { 0, 0, 0 };
18 SkV3 fUp { 0, 1, 0 };
Mike Reeda3a704a2020-01-10 17:21:40 -050019};
20
Mike Reed00a97642020-01-25 18:42:23 -050021static SkM44 inv(const SkM44& m) {
Florin Malitad5899162020-02-04 10:06:24 -050022 SkM44 inverse(SkM44::kUninitialized_Constructor);
23 if (!m.invert(&inverse)) {
24 inverse.setIdentity();
25 }
Mike Reeda3a704a2020-01-10 17:21:40 -050026 return inverse;
27}
28
Mike Reed00a97642020-01-25 18:42:23 -050029static SkM44 make_ctm(const Info& info, const SkM44& model, SkSize size) {
30 SkM44 camera, perspective, viewport;
Mike Reeda3a704a2020-01-10 17:21:40 -050031
32 SkScalar w = size.width();
33 SkScalar h = size.height();
34
Mike Reed00a97642020-01-25 18:42:23 -050035 perspective = Sk3Perspective(info.fNear, info.fFar, info.fAngle);
36 camera = Sk3LookAt(info.fEye, info.fCOA, info.fUp);
Mike Reeda3a704a2020-01-10 17:21:40 -050037 viewport.setScale(w*0.5f, h*0.5f, 1);//.postTranslate(r.centerX(), r.centerY(), 0);
38
39 return viewport * perspective * camera * model * inv(viewport);
40}
41
42#include "include/core/SkPicture.h"
43#include "include/core/SkPictureRecorder.h"
44
45static void do_draw(SkCanvas* canvas, SkColor color) {
46 SkAutoCanvasRestore acr(canvas, true);
47
48 Info info;
49
Mike Reed00a97642020-01-25 18:42:23 -050050 SkM44 m = SkM44::Rotate({0, 1, 0}, SK_ScalarPI/6);
Mike Reeda3a704a2020-01-10 17:21:40 -050051
Mike Reedd4d3b332020-01-16 16:34:34 -050052 canvas->experimental_concat44(make_ctm(info, m, {300, 300}));
Mike Reeda3a704a2020-01-10 17:21:40 -050053
54 canvas->translate(150, 150);
55 SkPaint paint;
56 paint.setColor(color);
57 canvas->drawRect({-100, -100, 100, 100}, paint);
58}
59
60/*
61 * Test calling drawables w/ translate and matrices
62 */
63DEF_SIMPLE_GM(sk3d_simple, real_canvas, 300, 300) {
64 do_draw(real_canvas, 0xFFFF0000);
65
66 SkPictureRecorder recorder;
67 SkCanvas* canvas = recorder.beginRecording(300, 300);
68
69 do_draw(canvas, 0x880000FF);
70
71 auto pic = recorder.finishRecordingAsPicture();
72 if (true) {
73 real_canvas->drawPicture(pic);
74 } else {
75 auto data = pic->serialize();
76 auto pic2 = SkPicture::MakeFromData(data.get());
77 real_canvas->drawPicture(pic2);
78 }
79}