|
26 | 26 | DEALINGS IN THE SOFTWARE. |
27 | 27 | */ |
28 | 28 |
|
29 | | -/// \author James Hughes |
30 | | -/// \date March 2013 |
| 29 | +// author James Hughes |
| 30 | +// date March 2013 |
31 | 31 |
|
32 | 32 | #include <gl-platform/GLPlatform.hpp> |
33 | 33 | #include <Interface/Modules/Render/ES/SRCamera.h> |
34 | 34 |
|
35 | 35 | namespace SCIRun { |
36 | | -namespace Render { |
37 | | - |
38 | | -//------------------------------------------------------------------------------ |
39 | | -SRCamera::SRCamera(SRInterface& iface) : |
40 | | - mTrafoSeq(0), |
41 | | - mPerspective(true), |
42 | | - mInvertVal(-1), |
43 | | - mFOV(getDefaultFOVY()), |
44 | | - mZNear(getDefaultZNear()), |
45 | | - mZFar(getDefaultZFar()), |
46 | | - mRadius(-1.0), |
47 | | - mInterface(iface), |
48 | | - mArcLookAt(new spire::ArcLookAt()) |
49 | | -{ |
50 | | - setAsPerspective(); |
51 | | - |
52 | | - glm::mat4 cam; |
53 | | - cam[3] = (glm::vec4(0.0f, 0.0f, 7.0f, 1.0f)); |
54 | | - mRadius = -1.0; |
55 | | -} |
56 | | - |
57 | | -//------------------------------------------------------------------------------ |
58 | | -void SRCamera::setAsPerspective() |
59 | | -{ |
60 | | - mPerspective = true; |
61 | | - |
62 | | - float aspect = static_cast<float>(mInterface.getScreenWidthPixels()) / |
63 | | - static_cast<float>(mInterface.getScreenHeightPixels()); |
64 | | - |
65 | | - mP = glm::perspective(mFOV, aspect, mZNear, mZFar); |
66 | | -} |
67 | | - |
68 | | -//------------------------------------------------------------------------------ |
69 | | -void SRCamera::setAsOrthographic(float halfWidth, float halfHeight) |
70 | | -{ |
71 | | - mPerspective = false; |
72 | | - |
73 | | - mP = glm::ortho(-halfWidth, halfWidth, |
74 | | - -halfHeight, halfHeight, |
75 | | - mZNear, mZFar); |
76 | | -} |
77 | | - |
78 | | -//------------------------------------------------------------------------------ |
79 | | -void SRCamera::buildTransform() |
80 | | -{ |
81 | | - ++mTrafoSeq; |
82 | | - |
83 | | - mV = mArcLookAt->getWorldViewTransform(); |
84 | | - mIV = glm::affineInverse(mV); |
85 | | - mPIV = mP * mIV; |
86 | | -} |
87 | | - |
88 | | -//------------------------------------------------------------------------------ |
89 | | -void SRCamera::applyTransform() |
90 | | -{ |
91 | | - buildTransform(); |
92 | | -} |
93 | | - |
94 | | -//------------------------------------------------------------------------------ |
95 | | -void SRCamera::mouseDownEvent(const glm::ivec2& pos, SRInterface::MouseButton btn) |
96 | | -{ |
97 | | - glm::vec2 screenSpace = calculateScreenSpaceCoords(pos); |
98 | | - mArcLookAt->doReferenceDown(screenSpace); |
99 | | -} |
100 | | - |
101 | | -//------------------------------------------------------------------------------ |
102 | | -void SRCamera::mouseMoveEvent(const glm::ivec2& pos, SRInterface::MouseButton btn) |
103 | | -{ |
104 | | - glm::vec2 screenSpace = calculateScreenSpaceCoords(pos); |
105 | | - switch (mInterface.getMouseMode()) |
106 | | - { |
107 | | - case SRInterface::MOUSE_OLDSCIRUN: |
108 | | - if (btn == SRInterface::MOUSE_LEFT && !lockPanning_) mArcLookAt->doPan(screenSpace); |
109 | | - if (btn == SRInterface::MOUSE_RIGHT && !lockZoom_) mArcLookAt->doZoom(screenSpace); |
110 | | - if (btn == SRInterface::MOUSE_MIDDLE && !lockRotation_) mArcLookAt->doRotation(screenSpace); |
111 | | - break; |
112 | | - |
113 | | - case SRInterface::MOUSE_NEWSCIRUN: |
114 | | - if (btn == SRInterface::MOUSE_LEFT && !lockRotation_) mArcLookAt->doRotation(screenSpace); |
115 | | - if (btn == SRInterface::MOUSE_RIGHT && !lockPanning_) mArcLookAt->doPan(screenSpace); |
116 | | - break; |
117 | | - } |
118 | | -} |
119 | | - |
120 | | -//------------------------------------------------------------------------------ |
121 | | -void SRCamera::mouseWheelEvent(int32_t delta, int zoomSpeed) |
122 | | -{ |
123 | | - if (mInterface.getMouseMode() != SRInterface::MOUSE_OLDSCIRUN && !lockZoom_) |
124 | | - { |
125 | | - //mArcLookAt->doZoom(-static_cast<float>(delta) / 100.0f, zoomSpeed); |
126 | | - mArcLookAt->doZoom(mInvertVal*static_cast<float>(delta) / 100.0f, zoomSpeed); |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -//------------------------------------------------------------------------------ |
131 | | -void SRCamera::doAutoView(const Core::Geometry::BBox& bbox) |
132 | | -{ |
133 | | - // Convert core geom bbox to AABB. |
134 | | - Core::Geometry::Point bboxMin = bbox.get_min(); |
135 | | - Core::Geometry::Point bboxMax = bbox.get_max(); |
136 | | - glm::vec3 min(bboxMin.x(), bboxMin.y(), bboxMin.z()); |
137 | | - glm::vec3 max(bboxMax.x(), bboxMax.y(), bboxMax.z()); |
138 | | - |
139 | | - spire::AABB aabb(min, max); |
140 | | - |
141 | | - /// \todo Use real FOV-Y when we allow the user to change the FOV. |
142 | | - mArcLookAt->autoview(aabb, getDefaultFOVY()); |
143 | | - |
144 | | - setClippingPlanes(bbox); |
145 | | -} |
146 | | - |
147 | | -void SRCamera::setClippingPlanes(const Core::Geometry::BBox& bbox) |
148 | | -{ |
149 | | - buildTransform(); //make sure matricies are up to date |
150 | | - mRadius = (bbox.get_max() - bbox.get_min()).length() / 2.0f; |
151 | | - |
152 | | - Core::Geometry::Point c = Core::Geometry::Point(bbox.get_max() + bbox.get_min()); |
153 | | - glm::vec4 center(c.x()/2.0,c.y()/2.0,c.z()/2.0, 1.0); |
154 | | - center = mIV * center; |
155 | | - |
156 | | - if(mRadius > 0.0) |
157 | | - { |
158 | | - mZFar = -center.z + mRadius; |
159 | | - mZNear = std::max(mZFar/1000.0f, -center.z - mRadius); |
160 | | - } |
161 | | - else |
162 | | - { |
163 | | - mZFar = getDefaultZFar(); |
164 | | - mZNear = getDefaultZNear(); |
165 | | - } |
166 | | - |
167 | | - setAsPerspective(); |
168 | | -} |
169 | | - |
170 | | -//------------------------------------------------------------------------------ |
171 | | -void SRCamera::setView(const glm::vec3& view, const glm::vec3& up) |
172 | | -{ |
173 | | - mArcLookAt->setView(view, up); |
174 | | -} |
175 | | - |
176 | | -//------------------------------------------------------------------------------ |
177 | | -void SRCamera::setZoomInverted(bool value) |
178 | | -{ |
179 | | - if (value) |
180 | | - mInvertVal = 1; |
181 | | - else |
182 | | - mInvertVal = -1; |
183 | | -} |
184 | | - |
185 | | -//------------------------------------------------------------------------------ |
186 | | -glm::vec2 SRCamera::calculateScreenSpaceCoords(const glm::ivec2& mousePos) |
187 | | -{ |
188 | | - float windowOriginX = 0.0f; |
189 | | - float windowOriginY = 0.0f; |
190 | | - |
191 | | - // Transform incoming mouse coordinates into screen space. |
192 | | - glm::vec2 mouseScreenSpace; |
193 | | - mouseScreenSpace.x = 2.0f * (static_cast<float>(mousePos.x) - windowOriginX) |
194 | | - / static_cast<float>(mInterface.getScreenWidthPixels()) - 1.0f; |
195 | | - mouseScreenSpace.y = 2.0f * (static_cast<float>(mousePos.y) - windowOriginY) |
196 | | - / static_cast<float>(mInterface.getScreenHeightPixels()) - 1.0f; |
197 | | - |
198 | | - // Rotation with flipped axes feels much more natural. It places it inside |
199 | | - // the correct OpenGL coordinate system (with origin in the center of the |
200 | | - // screen). |
201 | | - mouseScreenSpace.y = -mouseScreenSpace.y; |
202 | | - |
203 | | - return mouseScreenSpace; |
204 | | -} |
205 | | - |
206 | | -} // namespace Render |
| 36 | + namespace Render { |
| 37 | + |
| 38 | + //---------------------------------------------------------------------------------------------- |
| 39 | + SRCamera::SRCamera(SRInterface& iface) : |
| 40 | + mInterface(iface), |
| 41 | + mArcLookAt(new spire::ArcLookAt()) |
| 42 | + { |
| 43 | + setAsPerspective(); |
| 44 | + } |
| 45 | + |
| 46 | + //---------------------------------------------------------------------------------------------- |
| 47 | + void SRCamera::buildTransform() |
| 48 | + { |
| 49 | + // todo fix this method to return mV instead of mIV |
| 50 | + mIV = mArcLookAt->getWorldViewTransform(); |
| 51 | + mV = glm::affineInverse(mIV); |
| 52 | + mVP = mP * mV; |
| 53 | + } |
| 54 | + |
| 55 | + //---------------------------------------------------------------------------------------------- |
| 56 | + void SRCamera::setAsPerspective() |
| 57 | + { |
| 58 | + mPerspective = true; |
| 59 | + mP = glm::perspective(mFOVY, getAspect(), mZNear, mZFar); |
| 60 | + buildTransform(); |
| 61 | + } |
| 62 | + |
| 63 | + //---------------------------------------------------------------------------------------------- |
| 64 | + void SRCamera::setAsOrthographic(float halfWidth, float halfHeight) |
| 65 | + { |
| 66 | + mPerspective = false; |
| 67 | + mP = glm::ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, mZNear, mZFar); |
| 68 | + buildTransform(); |
| 69 | + } |
| 70 | + |
| 71 | + //---------------------------------------------------------------------------------------------- |
| 72 | + void SRCamera::setView(const glm::vec3& view, const glm::vec3& up) |
| 73 | + { |
| 74 | + mArcLookAt->setView(view, up); |
| 75 | + setClippingPlanes(); |
| 76 | + } |
| 77 | + |
| 78 | + //---------------------------------------------------------------------------------------------- |
| 79 | + void SRCamera::mouseDownEvent(const glm::ivec2& pos, SRInterface::MouseButton btn) |
| 80 | + { |
| 81 | + glm::vec2 screenSpace = calculateScreenSpaceCoords(pos); |
| 82 | + mArcLookAt->doReferenceDown(screenSpace); |
| 83 | + } |
| 84 | + |
| 85 | + //---------------------------------------------------------------------------------------------- |
| 86 | + void SRCamera::mouseMoveEvent(const glm::ivec2& pos, SRInterface::MouseButton btn) |
| 87 | + { |
| 88 | + glm::vec2 screenSpace = calculateScreenSpaceCoords(pos); |
| 89 | + switch (mInterface.getMouseMode()) |
| 90 | + { |
| 91 | + case SRInterface::MOUSE_OLDSCIRUN: |
| 92 | + if (btn == SRInterface::MOUSE_LEFT && !lockPanning_) mArcLookAt->doPan(screenSpace); |
| 93 | + if (btn == SRInterface::MOUSE_RIGHT && !lockZoom_) mArcLookAt->doZoom(screenSpace); |
| 94 | + if (btn == SRInterface::MOUSE_MIDDLE && !lockRotation_) mArcLookAt->doRotation(screenSpace); |
| 95 | + break; |
| 96 | + |
| 97 | + case SRInterface::MOUSE_NEWSCIRUN: |
| 98 | + if (btn == SRInterface::MOUSE_LEFT && !lockRotation_) mArcLookAt->doRotation(screenSpace); |
| 99 | + if (btn == SRInterface::MOUSE_RIGHT && !lockPanning_) mArcLookAt->doPan(screenSpace); |
| 100 | + break; |
| 101 | + } |
| 102 | + setClippingPlanes(); |
| 103 | + } |
| 104 | + |
| 105 | + //---------------------------------------------------------------------------------------------- |
| 106 | + void SRCamera::mouseWheelEvent(int32_t delta, int zoomSpeed) |
| 107 | + { |
| 108 | + if (mInterface.getMouseMode() != SRInterface::MOUSE_OLDSCIRUN && !lockZoom_) |
| 109 | + { |
| 110 | + mArcLookAt->doZoom(mInvertVal*static_cast<float>(delta) / 100.0f, zoomSpeed); |
| 111 | + setClippingPlanes(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + //---------------------------------------------------------------------------------------------- |
| 116 | + void SRCamera::setSceneBoundingBox(const Core::Geometry::BBox& bbox) |
| 117 | + { |
| 118 | + mSceneBBox = bbox; |
| 119 | + setClippingPlanes(); |
| 120 | + } |
| 121 | + |
| 122 | + //---------------------------------------------------------------------------------------------- |
| 123 | + void SRCamera::doAutoView() |
| 124 | + { |
| 125 | + if(mSceneBBox.valid()) |
| 126 | + { |
| 127 | + // Convert core geom bbox to AABB. |
| 128 | + Core::Geometry::Point bboxMin = mSceneBBox.get_min(); |
| 129 | + Core::Geometry::Point bboxMax = mSceneBBox.get_max(); |
| 130 | + glm::vec3 min(bboxMin.x(), bboxMin.y(), bboxMin.z()); |
| 131 | + glm::vec3 max(bboxMax.x(), bboxMax.y(), bboxMax.z()); |
| 132 | + |
| 133 | + spire::AABB aabb(min, max); |
| 134 | + |
| 135 | + // todo Use real FOV-Y when we allow the user to change the FOV. |
| 136 | + mArcLookAt->autoview(aabb, mFOVY); |
| 137 | + setClippingPlanes(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + //---------------------------------------------------------------------------------------------- |
| 142 | + void SRCamera::setClippingPlanes() |
| 143 | + { |
| 144 | + mRadius = (mSceneBBox.get_max() - mSceneBBox.get_min()).length() / 2.0f; |
| 145 | + |
| 146 | + if(mRadius > 0.0) |
| 147 | + { |
| 148 | + buildTransform(); // make sure matricies are up to date |
| 149 | + Core::Geometry::Point c = Core::Geometry::Point(mSceneBBox.get_max() + mSceneBBox.get_min()); |
| 150 | + glm::vec4 center(c.x()/2.0,c.y()/2.0,c.z()/2.0, 1.0); |
| 151 | + center = mV * center; |
| 152 | + |
| 153 | + mZFar = -center.z + mRadius; |
| 154 | + mZNear = std::max(mZFar/1000.0f, -center.z - mRadius); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + mZFar = getDefaultZFar(); |
| 159 | + mZNear = getDefaultZNear(); |
| 160 | + } |
| 161 | + |
| 162 | + setAsPerspective(); |
| 163 | + } |
| 164 | + |
| 165 | + //---------------------------------------------------------------------------------------------- |
| 166 | + void SRCamera::setZoomInverted(bool value) |
| 167 | + { |
| 168 | + if (value) |
| 169 | + mInvertVal = 1; |
| 170 | + else |
| 171 | + mInvertVal = -1; |
| 172 | + } |
| 173 | + |
| 174 | + //---------------------------------------------------------------------------------------------- |
| 175 | + glm::vec2 SRCamera::calculateScreenSpaceCoords(const glm::ivec2& mousePos) |
| 176 | + { |
| 177 | + float windowOriginX = 0.0f; |
| 178 | + float windowOriginY = 0.0f; |
| 179 | + |
| 180 | + // Transform incoming mouse coordinates into screen space. |
| 181 | + glm::vec2 mouseScreenSpace; |
| 182 | + mouseScreenSpace.x = 2.0f * (static_cast<float>(mousePos.x) - windowOriginX) |
| 183 | + / static_cast<float>(mInterface.getScreenWidthPixels()) - 1.0f; |
| 184 | + mouseScreenSpace.y = 2.0f * (static_cast<float>(mousePos.y) - windowOriginY) |
| 185 | + / static_cast<float>(mInterface.getScreenHeightPixels()) - 1.0f; |
| 186 | + |
| 187 | + // Rotation with flipped axes feels much more natural. It places it inside the |
| 188 | + // correct OpenGL coordinate system (with origin in the center of the screen). |
| 189 | + mouseScreenSpace.y = -mouseScreenSpace.y; |
| 190 | + |
| 191 | + return mouseScreenSpace; |
| 192 | + } |
| 193 | + |
| 194 | + } // namespace Render |
207 | 195 | } // namespace SCIRun |
0 commit comments