CUGL
Cornell University Game Library
CUPerspectiveCamera.h
1 //
2 // CUPerspectiveCamera.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides an perspective camera class. While 3d is not a primary
6 // use case for cugl, it is nice to have the support.
7 //
8 // This module is based on the original file PerspectiveCamera by
9 // Mario Zechner, written for LibGDX ( https://libgdx.badlogicgames.com ). It
10 // has been modified to support the CUGL framework.
11 //
12 // This class uses our standard shared-pointer architecture.
13 //
14 // 1. The constructor does not perform any initialization; it just sets all
15 // attributes to their defaults.
16 //
17 // 2. All initialization takes place via init methods, which can fail if an
18 // object is initialized more than once.
19 //
20 // 3. All allocation takes place via static constructors which return a shared
21 // pointer.
22 //
23 // CUGL zlib License:
24 // This software is provided 'as-is', without any express or implied
25 // warranty. In no event will the authors be held liable for any damages
26 // arising from the use of this software.
27 //
28 // Permission is granted to anyone to use this software for any purpose,
29 // including commercial applications, and to alter it and redistribute it
30 // freely, subject to the following restrictions:
31 //
32 // 1. The origin of this software must not be misrepresented; you must not
33 // claim that you wrote the original software. If you use this software
34 // in a product, an acknowledgment in the product documentation would be
35 // appreciated but is not required.
36 //
37 // 2. Altered source versions must be plainly marked as such, and must not
38 // be misrepresented as being the original software.
39 //
40 // 3. This notice may not be removed or altered from any source distribution.
41 //
42 // Author: Walker White
43 // Version: 6/24/16
44 
45 #ifndef __CU_PERSPECTIVE_CAMERA_H__
46 #define __CU_PERSPECTIVE_CAMERA_H__
47 
48 
49 #endif /* __CU_PERSPECTIVE_CAMERA_H__ */
50 
51 #define DEFAULT_CAMERA_FOV 67
52 
53 #include "CUCamera.h"
54 
55 namespace cugl {
56 
62 class PerspectiveCamera : public Camera {
63 protected:
65  float _fieldOfView = 67;
66 
69 
70 #pragma mark -
71 #pragma mark Constructors
72 public:
79  _fieldOfView = 0; _initialized = false;
80  }
81 
86 
87 
91  void dispose() override;
92 
108  bool init(const Size& size, float fieldOfView = DEFAULT_CAMERA_FOV) {
109  return init(0,0,size.width,size.height,fieldOfView);
110  }
111 
128  bool init(float width, float height, float fieldOfView = DEFAULT_CAMERA_FOV) {
129  return init(0,0,width,height,fieldOfView);
130  }
131 
150  bool init(const Rect& rect, float fieldOfView = DEFAULT_CAMERA_FOV) {
151  return init(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height,fieldOfView);
152  }
153 
173  bool init(const Vec2& origin, const Size& size, float fieldOfView = DEFAULT_CAMERA_FOV) {
174  return init(origin.x, origin.y, size.width, size.height,fieldOfView);
175  }
176 
198  bool init(float x, float y, float width, float height, float fieldOfView = DEFAULT_CAMERA_FOV);
199 
200 #pragma mark -
201 #pragma mark Static Constructors
202 
217  static std::shared_ptr<PerspectiveCamera> alloc(const Size& size, float fieldOfView = DEFAULT_CAMERA_FOV) {
218  std::shared_ptr<PerspectiveCamera> result = std::make_shared<PerspectiveCamera>();
219  return (result->init(size,fieldOfView) ? result : nullptr);
220  }
221 
238  static std::shared_ptr<PerspectiveCamera> alloc(float width, float height,
239  float fieldOfView = DEFAULT_CAMERA_FOV) {
240  std::shared_ptr<PerspectiveCamera> result = std::make_shared<PerspectiveCamera>();
241  return (result->init(width,height,fieldOfView) ? result : nullptr);
242  }
243 
262  static std::shared_ptr<PerspectiveCamera> alloc(const Rect& rect, float fieldOfView = DEFAULT_CAMERA_FOV) {
263  std::shared_ptr<PerspectiveCamera> result = std::make_shared<PerspectiveCamera>();
264  return (result->init(rect,fieldOfView) ? result : nullptr);
265  }
266 
286  static std::shared_ptr<PerspectiveCamera> alloc(const Vec2& origin, const Size& size,
287  float fieldOfView = DEFAULT_CAMERA_FOV) {
288  std::shared_ptr<PerspectiveCamera> result = std::make_shared<PerspectiveCamera>();
289  return (result->init(origin,size,fieldOfView) ? result : nullptr);
290  }
291 
313  static std::shared_ptr<PerspectiveCamera> alloc(float x, float y, float width, float height,
314  float fieldOfView = DEFAULT_CAMERA_FOV) {
315  std::shared_ptr<PerspectiveCamera> result = std::make_shared<PerspectiveCamera>();
316  return (result->init(x,y,width,height,fieldOfView) ? result : nullptr);
317  }
318 
319 #pragma mark -
320 #pragma mark Setters
321 
334  void set(const Size& size, float fieldOfView = DEFAULT_CAMERA_FOV) {
335  set(0, 0, size.width, size.height, fieldOfView);
336  }
337 
352  void set(float width, float height, float fieldOfView = DEFAULT_CAMERA_FOV) {
353  set(0, 0, width, height, fieldOfView);
354  }
355 
372  void set(const Rect& rect, float fieldOfView = DEFAULT_CAMERA_FOV) {
373  set(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, fieldOfView);
374  }
375 
393  void set(const Vec2& origin, const Size& size, float fieldOfView = DEFAULT_CAMERA_FOV) {
394  set(origin.x, origin.y, size.width, size.height, fieldOfView);
395  }
396 
416  void set(float x, float y, float width, float height, float fieldOfView = DEFAULT_CAMERA_FOV);
417 
423  void update() override;
424 
425 #pragma mark -
426 #pragma mark Attributes
427 
437  float getFieldOfView() const { return _fieldOfView; }
438 
452  void setFieldOfView(float fov);
453 
454 };
455 
456 }
Definition: CUSize.h:57
bool _initialized
Definition: CUPerspectiveCamera.h:68
float x
Definition: CUVec2.h:66
void setFieldOfView(float fov)
float y
Definition: CUVec2.h:68
bool init(const Rect &rect, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:150
Definition: CUVec2.h:61
static std::shared_ptr< PerspectiveCamera > alloc(float width, float height, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:238
void set(float width, float height, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:352
void set(const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:334
float width
Definition: CUSize.h:61
void set(const Rect &rect, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:372
static std::shared_ptr< PerspectiveCamera > alloc(const Vec2 &origin, const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:286
static std::shared_ptr< PerspectiveCamera > alloc(float x, float y, float width, float height, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:313
bool init(const Vec2 &origin, const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:173
static std::shared_ptr< PerspectiveCamera > alloc(const Rect &rect, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:262
bool init(float width, float height, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:128
float height
Definition: CUSize.h:63
void update() override
~PerspectiveCamera()
Definition: CUPerspectiveCamera.h:85
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
float getFieldOfView() const
Definition: CUPerspectiveCamera.h:437
Size size
Definition: CURect.h:51
Definition: CUPerspectiveCamera.h:62
static std::shared_ptr< PerspectiveCamera > alloc(const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:217
Definition: CUCamera.h:56
float _fieldOfView
Definition: CUPerspectiveCamera.h:65
Definition: CUAnimationNode.h:52
bool init(const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:108
void dispose() override
PerspectiveCamera()
Definition: CUPerspectiveCamera.h:78
void set(const Vec2 &origin, const Size &size, float fieldOfView=DEFAULT_CAMERA_FOV)
Definition: CUPerspectiveCamera.h:393