Welcome to mirror list, hosted at ThFree Co, Russian Federation.

camera.cpp « hydra « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05f1c32d3c42e52b9c2492c525aead45d54a0484 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2022 NVIDIA Corporation
 * Copyright 2022 Blender Foundation */

#include "hydra/camera.h"
#include "scene/camera.h"

#include <pxr/base/gf/frustum.h>
#include <pxr/imaging/hd/sceneDelegate.h>
#include <pxr/usd/usdGeom/tokens.h>

HDCYCLES_NAMESPACE_OPEN_SCOPE

extern Transform convert_transform(const GfMatrix4d &matrix);

HdCyclesCamera::HdCyclesCamera(const SdfPath &sprimId) : HdCamera(sprimId)
{
#if PXR_VERSION >= 2102
  // Synchronize default values
  _horizontalAperture = _data.GetHorizontalAperture() * GfCamera::APERTURE_UNIT;
  _verticalAperture = _data.GetVerticalAperture() * GfCamera::APERTURE_UNIT;
  _horizontalApertureOffset = _data.GetHorizontalApertureOffset() * GfCamera::APERTURE_UNIT;
  _verticalApertureOffset = _data.GetVerticalApertureOffset() * GfCamera::APERTURE_UNIT;
  _focalLength = _data.GetFocalLength() * GfCamera::FOCAL_LENGTH_UNIT;
  _clippingRange = _data.GetClippingRange();
  _fStop = _data.GetFStop();
  _focusDistance = _data.GetFocusDistance();
#endif
}

HdCyclesCamera::~HdCyclesCamera()
{
}

HdDirtyBits HdCyclesCamera::GetInitialDirtyBitsMask() const
{
  return DirtyBits::AllDirty;
}

void HdCyclesCamera::Sync(HdSceneDelegate *sceneDelegate,
                          HdRenderParam *renderParam,
                          HdDirtyBits *dirtyBits)
{
  if (*dirtyBits == DirtyBits::Clean) {
    return;
  }

  VtValue value;
  const SdfPath &id = GetId();

#if PXR_VERSION >= 2102
  if (*dirtyBits & DirtyBits::DirtyTransform) {
    sceneDelegate->SampleTransform(id, &_transformSamples);

    for (size_t i = 0; i < _transformSamples.count; ++i) {
      if (_transformSamples.times[i] == 0.0f) {
        _transform = _transformSamples.values[i];
        _data.SetTransform(_transform);
        break;
      }
    }
  }
#else
  if (*dirtyBits & DirtyBits::DirtyViewMatrix) {
    sceneDelegate->SampleTransform(id, &_transformSamples);

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->worldToViewMatrix);
    if (!value.IsEmpty()) {
      _worldToViewMatrix = value.Get<GfMatrix4d>();
      _worldToViewInverseMatrix = _worldToViewMatrix.GetInverse();
      _data.SetTransform(_worldToViewInverseMatrix);
    }
  }
#endif

  if (*dirtyBits & DirtyBits::DirtyProjMatrix) {
    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->projectionMatrix);
    if (!value.IsEmpty()) {
      _projectionMatrix = value.Get<GfMatrix4d>();
      const float focalLength = _data.GetFocalLength();  // Get default focal length
#if PXR_VERSION >= 2102
      _data.SetFromViewAndProjectionMatrix(GetViewMatrix(), _projectionMatrix, focalLength);
#else
      if (_projectionMatrix[2][3] < -0.5) {
        _data.SetProjection(GfCamera::Perspective);

        const float horizontalAperture = (2.0 * focalLength) / _projectionMatrix[0][0];
        _data.SetHorizontalAperture(horizontalAperture);
        _data.SetHorizontalApertureOffset(0.5 * horizontalAperture * _projectionMatrix[2][0]);
        const float verticalAperture = (2.0 * focalLength) / _projectionMatrix[1][1];
        _data.SetVerticalAperture(verticalAperture);
        _data.SetVerticalApertureOffset(0.5 * verticalAperture * _projectionMatrix[2][1]);

        _data.SetClippingRange(
            GfRange1f(_projectionMatrix[3][2] / (_projectionMatrix[2][2] - 1.0),
                      _projectionMatrix[3][2] / (_projectionMatrix[2][2] + 1.0)));
      }
      else {
        _data.SetProjection(GfCamera::Orthographic);

        const float horizontalAperture = (2.0 / GfCamera::APERTURE_UNIT) / _projectionMatrix[0][0];
        _data.SetHorizontalAperture(horizontalAperture);
        _data.SetHorizontalApertureOffset(-0.5 * horizontalAperture * _projectionMatrix[3][0]);
        const float verticalAperture = (2.0 / GfCamera::APERTURE_UNIT) / _projectionMatrix[1][1];
        _data.SetVerticalAperture(verticalAperture);
        _data.SetVerticalApertureOffset(-0.5 * verticalAperture * _projectionMatrix[3][1]);

        const double nearMinusFarHalf = 1.0 / _projectionMatrix[2][2];
        const double nearPlusFarHalf = nearMinusFarHalf * _projectionMatrix[3][2];
        _data.SetClippingRange(
            GfRange1f(nearPlusFarHalf + nearMinusFarHalf, nearPlusFarHalf - nearMinusFarHalf));
      }
#endif
    }
  }

  if (*dirtyBits & DirtyBits::DirtyWindowPolicy) {
    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->windowPolicy);
    if (!value.IsEmpty()) {
      _windowPolicy = value.Get<CameraUtilConformWindowPolicy>();
    }
  }

  if (*dirtyBits & DirtyBits::DirtyClipPlanes) {
    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->clipPlanes);
    if (!value.IsEmpty()) {
      _clipPlanes = value.Get<std::vector<GfVec4d>>();
    }
  }

  if (*dirtyBits & DirtyBits::DirtyParams) {
#if PXR_VERSION >= 2102
    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->projection);
    if (!value.IsEmpty()) {
      _projection = value.Get<Projection>();
      _data.SetProjection(_projection != Orthographic ? GfCamera::Perspective :
                                                        GfCamera::Orthographic);
    }
#else
    value = sceneDelegate->GetCameraParamValue(id, UsdGeomTokens->projection);
    if (!value.IsEmpty()) {
      _data.SetProjection(value.Get<TfToken>() != UsdGeomTokens->orthographic ?
                              GfCamera::Perspective :
                              GfCamera::Orthographic);
    }
#endif

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->horizontalAperture);
    if (!value.IsEmpty()) {
      const auto horizontalAperture = value.Get<float>();
#if PXR_VERSION >= 2102
      _horizontalAperture = horizontalAperture;
#endif
      _data.SetHorizontalAperture(horizontalAperture / GfCamera::APERTURE_UNIT);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->verticalAperture);
    if (!value.IsEmpty()) {
      const auto verticalAperture = value.Get<float>();
#if PXR_VERSION >= 2102
      _verticalAperture = verticalAperture;
#endif
      _data.SetVerticalAperture(verticalAperture / GfCamera::APERTURE_UNIT);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->horizontalApertureOffset);
    if (!value.IsEmpty()) {
      const auto horizontalApertureOffset = value.Get<float>();
#if PXR_VERSION >= 2102
      _horizontalApertureOffset = horizontalApertureOffset;
#endif
      _data.SetHorizontalApertureOffset(horizontalApertureOffset / GfCamera::APERTURE_UNIT);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->verticalApertureOffset);
    if (!value.IsEmpty()) {
      const auto verticalApertureOffset = value.Get<float>();
#if PXR_VERSION >= 2102
      _verticalApertureOffset = verticalApertureOffset;
#endif
      _data.SetVerticalApertureOffset(verticalApertureOffset / GfCamera::APERTURE_UNIT);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->focalLength);
    if (!value.IsEmpty()) {
      const auto focalLength = value.Get<float>();
#if PXR_VERSION >= 2102
      _focalLength = focalLength;
#endif
      _data.SetFocalLength(focalLength / GfCamera::FOCAL_LENGTH_UNIT);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->clippingRange);
    if (!value.IsEmpty()) {
      const auto clippingRange = value.Get<GfRange1f>();
#if PXR_VERSION >= 2102
      _clippingRange = clippingRange;
#endif
      _data.SetClippingRange(clippingRange);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->fStop);
    if (!value.IsEmpty()) {
      const auto fStop = value.Get<float>();
#if PXR_VERSION >= 2102
      _fStop = fStop;
#endif
      _data.SetFStop(fStop);
    }

    value = sceneDelegate->GetCameraParamValue(id, HdCameraTokens->focusDistance);
    if (!value.IsEmpty()) {
      const auto focusDistance = value.Get<float>();
#if PXR_VERSION >= 2102
      _focusDistance = focusDistance;
#endif
      _data.SetFocusDistance(focusDistance);
    }
  }

  *dirtyBits = DirtyBits::Clean;
}

void HdCyclesCamera::Finalize(HdRenderParam *renderParam)
{
  HdCamera::Finalize(renderParam);
}

void HdCyclesCamera::ApplyCameraSettings(Camera *cam) const
{
  ApplyCameraSettings(_data, _windowPolicy, cam);

  array<Transform> motion(_transformSamples.count);
  for (size_t i = 0; i < _transformSamples.count; ++i)
    motion[i] = convert_transform(_transformSamples.values[i]) *
                transform_scale(1.0f, 1.0f, -1.0f);
  cam->set_motion(motion);
}

void HdCyclesCamera::ApplyCameraSettings(const GfCamera &dataUnconformedWindow,
                                         CameraUtilConformWindowPolicy windowPolicy,
                                         Camera *cam)
{
  const float width = cam->get_full_width();
  const float height = cam->get_full_height();

  auto data = dataUnconformedWindow;
  CameraUtilConformWindow(&data, windowPolicy, width / height);

  static_assert(GfCamera::Perspective == CAMERA_PERSPECTIVE &&
                GfCamera::Orthographic == CAMERA_ORTHOGRAPHIC);
  cam->set_camera_type(static_cast<CameraType>(data.GetProjection()));

  auto viewplane = data.GetFrustum().GetWindow();
  auto focalLength = 1.0f;
  if (data.GetProjection() == GfCamera::Perspective) {
    viewplane *= 2.0 / viewplane.GetSize()[1];  // Normalize viewplane
    focalLength = data.GetFocalLength() * 1e-3f;

    cam->set_fov(GfDegreesToRadians(data.GetFieldOfView(GfCamera::FOVVertical)));
  }

  cam->set_sensorwidth(data.GetHorizontalAperture() * GfCamera::APERTURE_UNIT);
  cam->set_sensorheight(data.GetVerticalAperture() * GfCamera::APERTURE_UNIT);

  cam->set_nearclip(data.GetClippingRange().GetMin());
  cam->set_farclip(data.GetClippingRange().GetMax());

  cam->set_viewplane_left(viewplane.GetMin()[0]);
  cam->set_viewplane_right(viewplane.GetMax()[0]);
  cam->set_viewplane_bottom(viewplane.GetMin()[1]);
  cam->set_viewplane_top(viewplane.GetMax()[1]);

  if (data.GetFStop() != 0.0f) {
    cam->set_focaldistance(data.GetFocusDistance());
    cam->set_aperturesize(focalLength / (2.0f * data.GetFStop()));
  }

  cam->set_matrix(convert_transform(data.GetTransform()) * transform_scale(1.0f, 1.0f, -1.0f));
}

void HdCyclesCamera::ApplyCameraSettings(const GfMatrix4d &worldToViewMatrix,
                                         const GfMatrix4d &projectionMatrix,
                                         const std::vector<GfVec4d> &clipPlanes,
                                         Camera *cam)
{
#if PXR_VERSION >= 2102
  GfCamera data;
  data.SetFromViewAndProjectionMatrix(worldToViewMatrix, projectionMatrix);

  ApplyCameraSettings(data, CameraUtilFit, cam);
#else
  TF_CODING_ERROR("Not implemented");
#endif
}

HDCYCLES_NAMESPACE_CLOSE_SCOPE