zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Projector.hxx
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_PROJECTOR_HXX
19 #define ZENI_PROJECTOR_HXX
20 
21 // HXXed below
22 #include <Zeni/Camera.h>
23 #include <Zeni/Coordinate.h>
24 #include <Zeni/Vector3f.h>
25 #include <Zeni/Matrix4f.h>
26 #include <Zeni/Video.h>
27 
28 #include <Zeni/Projector.h>
29 
30 namespace Zeni {
31 
32  Projector::Projector(const std::pair<Point2i, Point2i> &viewport)
33  {
34  init(viewport);
35  }
36 
38  {
39  }
40 
41  void Projector::init(const std::pair<Point2i, Point2i> &viewport)
42  {
43  m_offset = Vector3f(float(viewport.first.x), float(viewport.first.y), 0.0f);
44  m_size = Vector3f(float(viewport.second.x - viewport.first.x), float(viewport.second.y - viewport.first.y), 1.0f);
45  }
46 
47  const Vector3f & Projector::offset() const {
48  return m_offset;
49  }
50 
51  const Vector3f & Projector::size() const {
52  return m_size;
53  }
54 
56  const std::pair<Point2f, Point2f> &camera2d,
57  const std::pair<Point2i, Point2i> &viewport)
58  {
59  init(camera2d, viewport);
60  }
61 
62  Vector3f Projector2D::project(const Vector3f &world_coord) const {
63  return m_world_to_screen * world_coord;
64  }
65 
66  Point3f Projector2D::project(const Point3f &world_coord) const {
67  return project(Vector3f(world_coord));
68  }
69 
70  Point2f Projector2D::project(const Point2f &world_coord) const {
71  return Point2f(project(Vector3f(Point3f(world_coord))));
72  }
73 
74  Vector3f Projector2D::unproject(const Vector3f &screen_coord) const {
75  return m_screen_to_world * screen_coord;
76  }
77 
78  Point3f Projector2D::unproject(const Point3f &screen_coord) const {
79  return unproject(Vector3f(screen_coord));
80  }
81 
82  Point2f Projector2D::unproject(const Point2f &screen_coord) const {
83  return Point2f(unproject(Vector3f(Point3f(screen_coord))));
84  }
85 
86  void Projector2D::init(
87  const std::pair<Point2f, Point2f> &camera2d,
88  const std::pair<Point2i, Point2i> &viewport)
89  {
90  Projector::init(viewport);
91 
92  m_world_to_screen =
93  Matrix4f::Orthographic(float(viewport.first.x), float(viewport.second.x), float(viewport.second.y), float(viewport.first.y), -1.0f, 1.0f).inverted() *
94  Matrix4f::Orthographic(camera2d.first.x, camera2d.second.x, camera2d.second.y, camera2d.first.y, -1.0f, 1.0f);
95 
96  m_screen_to_world = m_world_to_screen.inverted();
97  }
98 
100  const Camera &camera3d,
101  const std::pair<Point2i, Point2i> &viewport)
102  {
103  init(camera3d, viewport);
104  }
105 
106  Vector3f Projector3D::project(const Vector3f &world_coord) const {
107  const float z_value = (Vector3f(m_world_to_camera * world_coord - m_uln).divide_by(m_uln2lrf)).k;
108  const float scale = z_value * m_near2far + 1.0f - z_value;
109  return (((m_world_to_camera * world_coord).divide_by(Vector3f(scale, scale, 1.0f)) - m_uln).divide_by(m_uln2lrf)).multiply_by(size()) + offset();
110  }
111 
112  Point3f Projector3D::project(const Point3f &world_coord) const {
113  return project(Vector3f(world_coord));
114  }
115 
116  Vector3f Projector3D::unproject(const Vector3f &screen_coord) const {
117  const float scale = screen_coord.k * m_near2far + 1.0f - screen_coord.k;
118  return m_camera_to_world * ((screen_coord - offset()).divide_by(size()).multiply_by(m_uln2lrf) + m_uln).multiply_by(Vector3f(scale, scale, 1.0f));
119  }
120 
121  Point3f Projector3D::unproject(const Point3f &screen_coord) const {
122  return unproject(Vector3f(screen_coord));
123  }
124 
125  void Projector3D::init(
126  const Camera &camera3d,
127  const std::pair<Point2i, Point2i> &viewport)
128  {
129  Projector::init(viewport);
130 
131  m_world_to_camera = camera3d.get_view_matrix();
132  m_camera_to_world = m_world_to_camera.inverted();
133 
134  const float aspect = size().i / size().j;
135  const float top = float(tan(0.5f * camera3d.fov_rad)) * camera3d.near_clip;
136  const float bottom = -top;
137  const float left = aspect * bottom;
138  const float right = aspect * top;
139 
140  const float tunneled_near_clip = camera3d.get_tunneled_near_clip();
141  const float tunneled_far_clip = camera3d.get_tunneled_far_clip();
142 
143  m_uln = Vector3f(left, top, -tunneled_near_clip);
144  m_uln2lrf = Vector3f(right, bottom, -tunneled_far_clip) - m_uln;
145 
146  m_near2far = tunneled_far_clip / tunneled_near_clip;
147  }
148 
149 }
150 
151 #include <Zeni/Camera.hxx>
152 #include <Zeni/Coordinate.hxx>
153 #include <Zeni/Vector3f.hxx>
154 #include <Zeni/Matrix4f.hxx>
155 #include <Zeni/Video.hxx>
156 
157 #endif
Vector3f project(const Vector3f &world_coord) const
Map coordinates in the viewing frustum to screen coordinates ([viewport.first.x, viewport.second.x], [viewport.first.y, viewport.second.y], [0, 1])
Definition: Projector.hxx:106
GLint GLint bottom
Definition: glew.h:7291
GLint left
Definition: glew.h:7291
Camera / Point of View.
Definition: Camera.h:49
GLclampf f
Definition: glew.h:3390
int32_t k
Definition: e_log.c:102
Vector3f unproject(const Vector3f &screen_coord) const
Map screen coordinates ([viewport.first.x, viewport.second.x], [viewport.first.y, viewport...
Definition: Projector.hxx:116
const Vector3f & offset() const
Definition: Projector.hxx:47
Vector3f divide_by(const Vector3f &rhs) const
Divide corresponding members.
Definition: Vector3f.hxx:174
float near_clip
Definition: Camera.h:93
float fov_rad
Definition: Camera.h:95
Projector3D(const Camera &camera3d=Camera(), const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()))
Definition: Projector.hxx:99
A 3D Point represented with floats.
Definition: Coordinate.h:133
A Featureful 3-Space Vector Class.
Definition: Vector3f.h:58
void init(const std::pair< Point2i, Point2i > &viewport)
Definition: Projector.hxx:41
static Matrix4f Orthographic(const float &left, const float &right, const float &bottom, const float &top, const float &near, const float &far)
Similar to gluOrtho.
Definition: Matrix4f.hxx:89
GLfloat GLfloat GLfloat top
Definition: glew.h:13816
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:12632
const Vector3f & size() const
Definition: Projector.hxx:51
Vector3f unproject(const Vector3f &screen_coord) const
Map screen coordinates ([viewport.first.x, viewport.second.x], [viewport.first.y, viewport...
Definition: Projector.hxx:74
float get_tunneled_near_clip() const
Get the near clip shifted by tunnel vision.
Definition: Camera.hxx:56
float get_tunneled_far_clip() const
Get the far clip shifted by tunnel vision.
Definition: Camera.hxx:60
Projector(const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()))
Definition: Projector.hxx:32
Matrix4f inverted() const
Get the inverse.
Definition: Matrix4f.cpp:98
Projector2D(const std::pair< Point2f, Point2f > &camera2d=std::make_pair(Point2f(0.0f, 0.0f), Point2f(float(get_Video().get_render_target_size().x), float(get_Video().get_render_target_size().y))), const std::pair< Point2i, Point2i > &viewport=std::make_pair(Point2i(), get_Video().get_render_target_size()))
Definition: Projector.hxx:55
Matrix4f get_view_matrix() const
Equivalent to gluLookAt + tunnel_vision_factor.
Definition: Camera.hxx:72
GLfloat right
Definition: glew.h:13816
A 2D Point represented with floats.
Definition: Coordinate.h:98
Vector3f project(const Vector3f &world_coord) const
Map coordinates in the viewing area to screen coordinates ([viewport.first.x, viewport.second.x], [viewport.first.y, viewport.second.y], [-1, 1])
Definition: Projector.hxx:62