aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/3dgm.cpp
blob: 37830ae87860bad74371d88886b993769c77113f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "Sk3D.h"
#include "SkPath.h"
#include "SkPoint3.h"

class GM3d : public skiagm::GM {
    float   fNear = 0.5;
    float   fFar = 4;
    float   fAngle = SK_ScalarPI / 4;

    SkPoint3    fEye { 0, 0, 4 };
    SkPoint3    fCOA {0,0,0};//{ 0.5f, 0.5f, 0.5f };
    SkPoint3    fUp  { 0, 1, 0 };

    SkMatrix44  fMV;

    SkPoint3    fP3[8];
public:
    GM3d() : fMV(SkMatrix44::kIdentity_Constructor) {}
    ~GM3d() override {}

protected:
    void onOnceBeforeDraw() override {
        int index = 0;
        for (float x = 0; x <= 1; ++x) {
            for (float y = 0; y <= 1; ++y) {
                for (float z = 0; z <= 1; ++z) {
                    fP3[index++] = { x, y, z };
                }
            }
        }
        fMV.setIdentity();
    }

    static void draw_viewport(SkCanvas* canvas, const SkMatrix& viewport) {
        SkPaint p;
        p.setColor(0x10FF0000);

        canvas->save();
        canvas->concat(viewport);
        canvas->drawRect({-1, -1, 1, 1}, p);

        p.setColor(0x80FF0000);
        canvas->drawLine({-1, -1}, {1, 1}, p);
        canvas->drawLine({1, -1}, {-1, 1}, p);
        canvas->restore();
    }

    void onDraw(SkCanvas* canvas) override {
        SkMatrix44  camera(SkMatrix44::kIdentity_Constructor),
                    perspective(SkMatrix44::kIdentity_Constructor),
                    mv = fMV;
        SkMatrix    viewport;

        {
            float w = this->width();
            float h = this->height();
            float s = std::min(w, h);
            viewport.setTranslate(1, -1);
            viewport.postScale(s/2, -s/2);

            draw_viewport(canvas, viewport);
        }

        Sk3Perspective(&perspective, fNear, fFar, fAngle);
        Sk3LookAt(&camera, fEye, fCOA, fUp);
        mv.postConcat(camera);
        mv.postConcat(perspective);
        SkPoint pts[8];
        Sk3MapPts(pts, mv, fP3, 8);
        viewport.mapPoints(pts, 8);

        SkPaint paint;
        paint.setStyle(SkPaint::kStroke_Style);

        SkPath cube;

        cube.moveTo(pts[0]);
        cube.lineTo(pts[2]);
        cube.lineTo(pts[6]);
        cube.lineTo(pts[4]);
        cube.close();

        cube.moveTo(pts[1]);
        cube.lineTo(pts[3]);
        cube.lineTo(pts[7]);
        cube.lineTo(pts[5]);
        cube.close();

        cube.moveTo(pts[0]);    cube.lineTo(pts[1]);
        cube.moveTo(pts[2]);    cube.lineTo(pts[3]);
        cube.moveTo(pts[4]);    cube.lineTo(pts[5]);
        cube.moveTo(pts[6]);    cube.lineTo(pts[7]);

        canvas->drawPath(cube, paint);

        {
            SkPoint3 src[4] = {
                { 0, 0, 0 }, { 2, 0, 0 }, { 0, 2, 0 }, { 0, 0, 2 },
            };
            SkPoint dst[4];
            mv.setConcat(perspective, camera);
            Sk3MapPts(dst, mv, src, 4);
            viewport.mapPoints(dst, 4);
            const char str[] = "XYZ";
            for (int i = 1; i <= 3; ++i) {
                canvas->drawLine(dst[0], dst[i], paint);
            }

            for (int i = 1; i <= 3; ++i) {
                canvas->drawText(&str[i-1], 1, dst[i].fX, dst[i].fY, paint);
            }
        }
    }

    SkISize onISize() override { return { 1024, 768 }; }

    SkString onShortName() override { return SkString("3dgm"); }

    bool onAnimate(const SkAnimTimer&) override { return false; }
    bool onHandleKey(SkUnichar uni) override {
        switch (uni) {
            case 'a': fEye.fX += 0.125f; return true;
            case 'd': fEye.fX -= 0.125f; return true;
            case 'w': fEye.fY += 0.125f; return true;
            case 's': fEye.fY -= 0.125f; return true;
            case 'q': fEye.fZ += 0.125f; return true;
            case 'z': fEye.fZ -= 0.125f; return true;
            default: break;
        }
        return false;
    }

    bool onGetControls(SkMetaData*) override { return false; }
    void onSetControls(const SkMetaData&) override {

    }
};

DEF_GM(return new GM3d;)