aboutsummaryrefslogtreecommitdiff
path: root/libvpx/tools/3D-Reconstruction/sketch_3D_reconstruction/Camera.pde
blob: b39dae3a195d9b0e6f16e350260a9a3c867ee390 (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
class Camera {
  // camera's field of view
  float fov;
  // camera's position, look at point and axis
  PVector pos, center, axis;
  PVector init_pos, init_center, init_axis;
  float move_speed;
  float rot_speed;
  Camera(float fov, PVector pos, PVector center, PVector axis) {
    this.fov = fov;
    this.pos = pos;
    this.center = center;
    this.axis = axis;
    this.axis.normalize();
    move_speed = 0.001;
    rot_speed = 0.01 * PI;
    init_pos = pos.copy();
    init_center = center.copy();
    init_axis = axis.copy();
  }

  Camera copy() {
    Camera cam = new Camera(fov, pos.copy(), center.copy(), axis.copy());
    return cam;
  }

  PVector project(PVector pos) {
    PVector proj = MatxVec3(getCameraMat(), PVector.sub(pos, this.pos));
    proj.x = (float)height / 2.0 * proj.x / proj.z / tan(fov / 2.0f);
    proj.y = (float)height / 2.0 * proj.y / proj.z / tan(fov / 2.0f);
    proj.z = proj.z;
    return proj;
  }

  float[] getCameraMat() {
    float[] mat = new float[9];
    PVector dir = PVector.sub(center, pos);
    dir.normalize();
    PVector left = dir.cross(axis);
    left.normalize();
    // processing camera system does not follow right hand rule
    mat[0] = -left.x;
    mat[1] = -left.y;
    mat[2] = -left.z;
    mat[3] = axis.x;
    mat[4] = axis.y;
    mat[5] = axis.z;
    mat[6] = dir.x;
    mat[7] = dir.y;
    mat[8] = dir.z;

    return mat;
  }

  void run() {
    PVector dir, left;
    if (mousePressed) {
      float angleX = (float)mouseX / width * PI - PI / 2;
      float angleY = (float)mouseY / height * PI - PI;
      PVector diff = PVector.sub(center, pos);
      float radius = diff.mag();
      pos.x = radius * sin(angleY) * sin(angleX) + center.x;
      pos.y = radius * cos(angleY) + center.y;
      pos.z = radius * sin(angleY) * cos(angleX) + center.z;
      dir = PVector.sub(center, pos);
      dir.normalize();
      PVector up = new PVector(0, 1, 0);
      left = up.cross(dir);
      left.normalize();
      axis = dir.cross(left);
      axis.normalize();
    }

    if (keyPressed) {
      switch (key) {
        case 'w':
          dir = PVector.sub(center, pos);
          dir.normalize();
          pos = PVector.add(pos, PVector.mult(dir, move_speed));
          center = PVector.add(center, PVector.mult(dir, move_speed));
          break;
        case 's':
          dir = PVector.sub(center, pos);
          dir.normalize();
          pos = PVector.sub(pos, PVector.mult(dir, move_speed));
          center = PVector.sub(center, PVector.mult(dir, move_speed));
          break;
        case 'a':
          dir = PVector.sub(center, pos);
          dir.normalize();
          left = axis.cross(dir);
          left.normalize();
          pos = PVector.add(pos, PVector.mult(left, move_speed));
          center = PVector.add(center, PVector.mult(left, move_speed));
          break;
        case 'd':
          dir = PVector.sub(center, pos);
          dir.normalize();
          left = axis.cross(dir);
          left.normalize();
          pos = PVector.sub(pos, PVector.mult(left, move_speed));
          center = PVector.sub(center, PVector.mult(left, move_speed));
          break;
        case 'r':
          dir = PVector.sub(center, pos);
          dir.normalize();
          float[] mat = getRotationMat3x3(rot_speed, dir.x, dir.y, dir.z);
          axis = MatxVec3(mat, axis);
          axis.normalize();
          break;
        case 'b':
          pos = init_pos.copy();
          center = init_center.copy();
          axis = init_axis.copy();
          break;
        case '+': move_speed *= 2.0f; break;
        case '-': move_speed /= 2.0; break;
        case CODED:
          if (keyCode == UP) {
            pos = PVector.add(pos, PVector.mult(axis, move_speed));
            center = PVector.add(center, PVector.mult(axis, move_speed));
          } else if (keyCode == DOWN) {
            pos = PVector.sub(pos, PVector.mult(axis, move_speed));
            center = PVector.sub(center, PVector.mult(axis, move_speed));
          }
      }
    }
  }
  void open() {
    perspective(fov, float(width) / height, 1e-6, 1e5);
    camera(pos.x, pos.y, pos.z, center.x, center.y, center.z, axis.x, axis.y,
           axis.z);
  }
  void close() {
    ortho(-width, 0, -height, 0);
    camera(0, 0, 0, 0, 0, 1, 0, 1, 0);
  }
}