VTK  9.4.20241118
Camera.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#pragma once
4
5#include "../Types.h"
6
7#include "Object.h"
8
9#include <VisRTX.h>
10#include <cassert>
11#include <string>
12
13namespace RTW
14{
15VTK_ABI_NAMESPACE_BEGIN
16 class Camera : public Object
17 {
18 friend class Renderer;
19
20 public:
21 Camera(const std::string& type) : Object(RTW_CAMERA)
22 {
23 VisRTX::Context* rtx = VisRTX_GetContext();
24
25 if (type == "perspective")
26 this->camera = rtx->CreatePerspectiveCamera();
27 else if (type == "orthographic")
28 this->camera = rtx->CreateOrthographicCamera();
29 else
30 assert(false);
31 }
32
34 {
35 this->camera->Release();
36 }
37
38 void Commit() override
39 {
40 VisRTX::Vec3f pos;
41 if (this->GetVec3f({ "position" }, &pos))
42 {
43 this->camera->SetPosition(pos);
44 }
45
46 VisRTX::Vec3f dir;
47 if (this->GetVec3f({ "direction" }, &dir))
48 {
49 this->camera->SetDirection(dir);
50 }
51
52 VisRTX::Vec3f up;
53 if (this->GetVec3f({ "up" }, &up))
54 {
55 this->camera->SetUp(up);
56 }
57
58 VisRTX::Vec2f imageBegin, imageEnd;
59 if (this->GetVec2f({ "imageStart" }, &imageBegin) && this->GetVec2f({ "imageEnd" }, &imageEnd))
60 {
61 this->camera->SetImageRegion(imageBegin, imageEnd);
62 }
63
64 if (this->camera->GetType() == VisRTX::CameraType::PERSPECTIVE)
65 {
66 VisRTX::PerspectiveCamera* pc = dynamic_cast<VisRTX::PerspectiveCamera*>(this->camera);
67
68 float fovy;
69 if (this->GetFloat({ "fovy" }, &fovy))
70 {
71 pc->SetFovY(fovy);
72 }
73
74 float aspect;
75 if (this->GetFloat({ "aspect" }, &aspect))
76 {
77 pc->SetAspect(aspect);
78 }
79
80 float focalDistance;
81 if (this->GetFloat({ "focusDistance" }, &focalDistance))
82 {
83 pc->SetFocalDistance(focalDistance);
84 }
85
86 float apertureRadius;
87 if (this->GetFloat({ "apertureRadius" }, &apertureRadius))
88 {
89 pc->SetApertureRadius(apertureRadius);
90 }
91 }
92
93 else if (this->camera->GetType() == VisRTX::CameraType::ORTHOGRAPHIC)
94 {
95 VisRTX::OrthographicCamera* oc = dynamic_cast<VisRTX::OrthographicCamera*>(this->camera);
96
97 float height;
98 if (this->GetFloat({ "height" }, &height))
99 {
100 oc->SetHeight(height);
101 }
102
103 float aspect;
104 if (this->GetFloat({ "aspect" }, &aspect))
105 {
106 oc->SetAspect(aspect);
107 }
108 }
109
110 else
111 {
112 assert(false);
113 }
114 }
115
116 private:
117 VisRTX::Camera* camera = nullptr;
118 };
119VTK_ABI_NAMESPACE_END
120}
@ RTW_CAMERA
Definition Types.h:139
void Commit() override
Definition Camera.h:38
Camera(const std::string &type)
Definition Camera.h:21
VisRTX::Vec2f GetVec2f(const std::vector< std::string > &ids, const VisRTX::Vec2f &defaultValue=VisRTX::Vec2f(), bool *found=nullptr) const
Definition Object.h:170
float GetFloat(const std::vector< std::string > &ids, float defaultValue=0.0f, bool *found=nullptr) const
Definition Object.h:136
VisRTX::Vec3f GetVec3f(const std::vector< std::string > &ids, const VisRTX::Vec3f &defaultValue=VisRTX::Vec3f(), bool *found=nullptr) const
Definition Object.h:204
Definition Backend.h:8