SimpleFastOpenAtomicVisualiser
Loading...
Searching...
No Matches
camera.h
Go to the documentation of this file.
1#ifndef CAMERA_H
2#define CAMERA_H
3
4#include <vector>
5#include <cstdint>
6
7#include "glm/glm.hpp"
8#include "glm/gtc/matrix_transform.hpp"
9#include <lua.h>
10
11#include <atom.h>
12#include <util.h>
13#include <LuaNumber.h>
14#include <LuaBool.h>
15
29class Camera
30{
31public:
32
40 Camera(const std::vector<Atom> & atoms, uint16_t resX, uint16_t resY, float fieldOfView)
41 : resX(resX), resY(resY), fieldOfView(fieldOfView)
42 {
43 reset(atoms);
44 }
45
53 Camera(glm::vec3 positionSpherical, uint16_t resX, uint16_t resY, float fieldOfView)
54 : resX(resX), resY(resY), fieldOfView(fieldOfView), positionSpherical(positionSpherical)
55 {
56 reset();
57 }
58
62 void reset()
63 {
64 focus = {0.0, 0.0, 0.0};
65 up = 1.0;
66
67 projection = glm::perspective
68 (
69 glm::radians(fieldOfView), float(resX)/float(resY),
70 0.1f,
71 1000.0f
72 );
73 invProjection = glm::inverse(projection);
74
75 view = glm::lookAt
76 (
77 spherical2cartesian(positionSpherical),
78 focus,
79 glm::vec3(0.0, up, 0.0)
80 );
81 invView = glm::inverse(view);
82
83 pv = projection*view;
84 invPv = glm::inverse(pv);
85 updated = true;
86 }
87
94 void reset(const std::vector<Atom> & atoms)
95 {
96 glm::vec3 ext = extent(atoms);
97 positionSpherical = glm::vec3
98 (
99 2.0f*std::max(std::max(ext.x, ext.y), ext.z),
100 M_PI*0.5f,
101 M_PI
102 );
103 reset();
104 }
105
111 void zoom(float increment) { positionSpherical.x += increment; setView(); }
112
118 void incline(float increment)
119 {
120 positionSpherical.y += increment*up;
121 if (positionSpherical.y > M_PI || positionSpherical.y < 0.0)
122 {
123 positionSpherical.y -= 2.0*increment*up;
124 up = -1.0*up;
125 positionSpherical.z += M_PI;
126 if ( positionSpherical.z < 0) { positionSpherical.z += 2.0*M_PI; }
127 else if ( positionSpherical.z > 2.0*M_PI) { positionSpherical.z = std::fmod(positionSpherical.z, 2.0*M_PI); }
128 }
129 setView();
130 }
131
137 void rotate(float increment)
138 {
139 positionSpherical.z -= increment;
140 if ( positionSpherical.z < 0) { positionSpherical.z += 2.0*M_PI; }
141 else if ( positionSpherical.z > 2.0*M_PI) { positionSpherical.z = std::fmod(positionSpherical.z, 2.0*M_PI); }
142 setView();
143 }
144
151 void setPosition(glm::vec3 positionSpherical)
152 {
153 this->positionSpherical = positionSpherical;
154 setView();
155 }
156
164 glm::vec3 position(bool spherical = false) const
165 {
166 return spherical ? positionSpherical : spherical2cartesian(positionSpherical);
167 }
168
175 void setUp(float up) { this->up = up < 0 ? -1.0 : 1.0; setView(); }
176
182 float getUp() const { return up; }
183
190 void setFieldOfView(float degrees)
191 {
192 fieldOfView = std::min(std::max(30.0f, degrees), 90.0f);
193 reset();
194 }
195
201 float getFieldOfView() const { return fieldOfView; }
202
208 glm::mat4 getProjection() const { return projection; }
209
215 glm::mat4 getInverseProjection() const { return invProjection; }
216
222 glm::mat4 getView() const { return view; }
223
229 glm::mat4 getInverseView() const { return invView; }
230
236 glm::mat4 getPV() const { return pv; }
237
243 glm::mat4 getInversePV() const { return invPv; }
244
245
246 uint16_t getResX() const { return resX; }
247 uint16_t getResY() const { return resY; }
248
257 inline int lua_cameraPosition(lua_State * lua);
258
269 inline int lua_setCameraPosition(lua_State * lua);
270
279 inline int lua_rotateCamera(lua_State * lua);
280
289 inline int lua_zoomCamera(lua_State * lua);
290
299 inline int lua_inclineCamera(lua_State * lua);
300
309 inline int lua_setCameraFieldOfView(lua_State * lua);
310
319 inline int lua_getCameraFieldOfView(lua_State * lua);
320
321 bool updated = false;
322
323private:
324
325 uint16_t resX;
326 uint16_t resY;
327 float fieldOfView;
328
329 glm::vec3 positionSpherical;
330 glm::vec3 focus;
331 float up;
332
333 glm::mat4 projection;
334 glm::mat4 invProjection;
335 glm::mat4 view;
336 glm::mat4 invView;
337 glm::mat4 pv;
338 glm::mat4 invPv;
339
340 void setView()
341 {
342 view = glm::lookAt
343 (
344 spherical2cartesian(positionSpherical),
345 focus,
346 glm::vec3(0.0, up, 0.0)
347 );
348 invView = glm::inverse(view);
349 pv = projection*view;
350 invPv = glm::inverse(pv);
351 updated = true;
352 }
353
354};
355
356#include <luaBindings/camera.h>
357
358#endif /* CAMERA_H */
glm::vec3 extent(const std::vector< Atom > &atoms)
Calculate the extent of some Atoms.
Definition atom.h:195
A 3D projective camera centered on a focus moving on a sphere.
Definition camera.h:30
void reset()
Set the default view.
Definition camera.h:62
bool updated
Definition camera.h:321
glm::mat4 getInverseView() const
Get the inverse View matrix.
Definition camera.h:229
Camera(glm::vec3 positionSpherical, uint16_t resX, uint16_t resY, float fieldOfView)
Construct a new Camera at a given position.
Definition camera.h:53
int lua_setCameraPosition(lua_State *lua)
Set Camera position.
Definition camera.h:47
void setPosition(glm::vec3 positionSpherical)
Set the camera's position.
Definition camera.h:151
int lua_cameraPosition(lua_State *lua)
Get the Camera position.
Definition camera.h:12
void setUp(float up)
Set the up direction.
Definition camera.h:175
glm::mat4 getProjection() const
Get the Projection matrix.
Definition camera.h:208
int lua_rotateCamera(lua_State *lua)
Rotate the Camera.
Definition camera.h:79
glm::mat4 getInversePV() const
Get the inverse Projection*View matrix.
Definition camera.h:243
uint16_t getResY() const
Definition camera.h:247
glm::mat4 getView() const
Get the View matrix.
Definition camera.h:222
void setFieldOfView(float degrees)
Set the Field Of View.
Definition camera.h:190
int lua_getCameraFieldOfView(lua_State *lua)
Set the field of view.
Definition camera.h:199
glm::vec3 position(bool spherical=false) const
Return the cartesian position vector.
Definition camera.h:164
glm::mat4 getInverseProjection() const
Get the inverse Projection matrix.
Definition camera.h:215
int lua_inclineCamera(lua_State *lua)
Incline the camera.
Definition camera.h:139
void rotate(float increment)
Rotate about the y OpenGL axis.
Definition camera.h:137
float getFieldOfView() const
Get the Field Of View.
Definition camera.h:201
int lua_setCameraFieldOfView(lua_State *lua)
Set the field of view.
Definition camera.h:169
Camera(const std::vector< Atom > &atoms, uint16_t resX, uint16_t resY, float fieldOfView)
Construct a new Camera focussing on some Atoms.
Definition camera.h:40
void reset(const std::vector< Atom > &atoms)
Set the default view.
Definition camera.h:94
void zoom(float increment)
Increment the zoom.
Definition camera.h:111
float getUp() const
Get the up direction.
Definition camera.h:182
uint16_t getResX() const
Definition camera.h:246
void incline(float increment)
Incline about the y OpenGL axis.
Definition camera.h:118
glm::mat4 getPV() const
Get the Projection*View matrix.
Definition camera.h:236
int lua_zoomCamera(lua_State *lua)
Zoom the Camera.
Definition camera.h:109
glm::vec3 spherical2cartesian(glm::vec3 rthetaphi)
convert spherical coordinates to cartesian coordinates.
Definition util.h:63