Allolib  1.0
C++ Components For Interactive Multimedia
al_Viewpoint.hpp
1 #ifndef INCLUDE_AL_VIEWPOINT_HPP
2 #define INCLUDE_AL_VIEWPOINT_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 */
38 
39 #include "al/graphics/al_Lens.hpp"
40 #include "al/spatial/al_Pose.hpp"
41 
42 namespace al {
43 
44 Matrix4f view_mat(Pose const&);
45 
48 struct Viewport {
49  int l, b, w, h;
50 
53  Viewport(int w = 640, int h = 480) : l(0), b(0), w(w), h(h) {}
54 
59  Viewport(int l, int b, int w, int h) : l(l), b(b), w(w), h(h) {}
60 
62  Viewport(const Viewport& cpy) : l(cpy.l), b(cpy.b), w(cpy.w), h(cpy.h) {}
63 
65  float aspect() const { return (h != 0 && w != 0) ? float(w) / h : 1; }
66 
68  void set(int l_, int b_, int w_, int h_) {
69  l = l_;
70  b = b_;
71  w = w_;
72  h = h_;
73  }
74  void set(const Viewport& cpy) {
75  l = cpy.l;
76  b = cpy.b;
77  w = cpy.w;
78  h = cpy.h;
79  }
80 
81  bool isEqual(Viewport const& v) {
82  return (l == v.l) && (b == v.b) && (w == v.w) && (h == v.h);
83  }
84 };
85 
87 
92 class Viewpoint {
93  public:
94  Viewpoint() {}
95  Viewpoint(Pose& p) : mPose(&p) {}
96 
97  const Lens& lens() const { return mLens; }
98  Lens& lens() { return mLens; }
99  Viewpoint& lens(Lens const& v) {
100  mLens = v;
101  return *this;
102  }
103 
104  const Pose& pose() const { return *mPose; }
105  Pose& pose() { return *mPose; }
106  Viewpoint& pose(Pose& p) {
107  mPose = &p;
108  return *this;
109  }
110 
111  Matrix4f viewMatrix() const;
112  Matrix4f projMatrix(float aspect_ratio) const;
113  Matrix4f projMatrix(float width, float height) const {
114  return projMatrix(width / height);
115  }
116 
117  private:
118  Lens mLens;
119  Pose* mPose = nullptr;
120 
121  public:
122  enum SpecialType {
123  IDENTITY,
124  ORTHO_FOR_2D,
125  UNIT_ORTHO, // fits [-1:1] X [-1:1] inside
126  UNIT_ORTHO_INCLUSIVE // fits in [-1:1] X [-1:1]
127  };
128 };
129 
130 } // namespace al
131 
132 #endif
A local coordinate frame.
Definition: al_Pose.hpp:63
Viewpoint within a scene.
Definition: al_App.hpp:23
Matrix4< float > Matrix4f
Single-precision 4-by-4 matrix.
Definition: al_Matrix4.hpp:57
Viewport(int w=640, int h=480)
void set(int l_, int b_, int w_, int h_)
Set dimensions.
Viewport(int l, int b, int w, int h)
float aspect() const
Get aspect ratio (width divided by height)
int h
left, bottom, width, height