blob: d9938d1c5aad6a22448457350f940eaed86d070c [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"
Kevin Lubickc3bac5b2023-09-25 08:43:57 -040011#include "include/core/SkPicture.h"
12#include "include/core/SkPictureRecorder.h"
Mike Reeda3a704a2020-01-10 17:21:40 -050013
Kevin Lubickbfc7c3a2022-11-29 11:58:51 -050014#include <cmath>
15
Mike Reeda3a704a2020-01-10 17:21:40 -050016struct Info {
17 float fNear = 0.05f;
18 float fFar = 4;
19 float fAngle = SK_ScalarPI / 4;
20
Kevin Lubickbfc7c3a2022-11-29 11:58:51 -050021 SkV3 fEye { 0, 0, 1.0f/std::tan(fAngle/2) - 1 };
Mike Reed00a97642020-01-25 18:42:23 -050022 SkV3 fCOA { 0, 0, 0 };
23 SkV3 fUp { 0, 1, 0 };
Mike Reeda3a704a2020-01-10 17:21:40 -050024};
25
Mike Reed00a97642020-01-25 18:42:23 -050026static SkM44 inv(const SkM44& m) {
Florin Malitad5899162020-02-04 10:06:24 -050027 SkM44 inverse(SkM44::kUninitialized_Constructor);
28 if (!m.invert(&inverse)) {
29 inverse.setIdentity();
30 }
Mike Reeda3a704a2020-01-10 17:21:40 -050031 return inverse;
32}
33
Mike Reed00a97642020-01-25 18:42:23 -050034static SkM44 make_ctm(const Info& info, const SkM44& model, SkSize size) {
35 SkM44 camera, perspective, viewport;
Mike Reeda3a704a2020-01-10 17:21:40 -050036
37 SkScalar w = size.width();
38 SkScalar h = size.height();
39
Michael Ludwige5016702021-03-23 15:42:59 -040040 perspective = SkM44::Perspective(info.fNear, info.fFar, info.fAngle);
41 camera = SkM44::LookAt(info.fEye, info.fCOA, info.fUp);
Mike Reeda3a704a2020-01-10 17:21:40 -050042 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
Mike Reeda3a704a2020-01-10 17:21:40 -050047static void do_draw(SkCanvas* canvas, SkColor color) {
48 SkAutoCanvasRestore acr(canvas, true);
49
50 Info info;
51
Mike Reed00a97642020-01-25 18:42:23 -050052 SkM44 m = SkM44::Rotate({0, 1, 0}, SK_ScalarPI/6);
Mike Reeda3a704a2020-01-10 17:21:40 -050053
Mike Reed3ef77dd2020-04-06 10:41:09 -040054 canvas->concat(make_ctm(info, m, {300, 300}));
Mike Reeda3a704a2020-01-10 17:21:40 -050055
56 canvas->translate(150, 150);
57 SkPaint paint;
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();
John Stiles4250eff2022-02-04 16:09:04 -050074 real_canvas->drawPicture(pic);
Mike Reeda3a704a2020-01-10 17:21:40 -050075}