MRPT  1.9.9
CFrustum.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/opengl/CFrustum.h>
13 #include <mrpt/opengl/gl_utils.h>
15 
16 #include "opengl_internals.h"
17 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
26 /*---------------------------------------------------------------
27  render
28  ---------------------------------------------------------------*/
29 void CFrustum::render_dl() const
30 {
31 #if MRPT_HAS_OPENGL_GLUT
32  if (m_color.A != 255 || (m_draw_planes && m_planes_color.A != 255))
33  {
36  }
37  else
38  {
41  }
42 
43  // Compute the 8 corners of the frustum:
44  TPoint3Df pts[8];
45  for (int j = 0; j < 2; j++)
46  {
47  const float r = j == 0 ? m_min_distance : m_max_distance;
48  for (int i = 0; i < 4; i++) pts[4 * j + i].x = r;
49  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
50  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
51  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
52  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
53  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
54  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
55  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
56  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
57  }
58 
59  // Render lines:
60  if (m_draw_lines)
61  {
62  glDisable(GL_LIGHTING); // Disable lights when drawing lines
63 
64  const int draw_path[] = {0, 1, 3, 2, 0, 4, 6, 2,
65  3, 7, 6, 4, 5, 7, 5, 1};
66 
67  // wireframe:
68  glLineWidth(m_lineWidth);
71  glColor4ub(m_color.R, m_color.G, m_color.B, m_color.A);
72 
73  for (int i : draw_path) glVertex3fv(&pts[i].x);
74 
75  glEnd();
76 
77  glEnable(GL_LIGHTING); // Disable lights when drawing lines
78  }
79 
80  if (m_draw_planes)
81  {
82  // solid:
84  glColor4ub(
85  m_planes_color.R, m_planes_color.G, m_planes_color.B,
86  m_planes_color.A);
87 
88  gl_utils::renderQuadWithNormal(pts[0], pts[2], pts[6], pts[4]);
89  gl_utils::renderQuadWithNormal(pts[2], pts[3], pts[7], pts[6]);
90  gl_utils::renderQuadWithNormal(pts[4], pts[6], pts[7], pts[5]);
91  gl_utils::renderQuadWithNormal(pts[1], pts[5], pts[7], pts[3]);
92  gl_utils::renderQuadWithNormal(pts[1], pts[5], pts[7], pts[3]);
93  gl_utils::renderQuadWithNormal(pts[4], pts[5], pts[1], pts[0]);
94 
95  glEnd();
96  }
97 
99 
100 #endif
101 }
102 
103 // Ctors
105  : m_fov_horz_left(mrpt::DEG2RAD(45)),
106  m_fov_horz_right(mrpt::DEG2RAD(45)),
107  m_fov_vert_down(mrpt::DEG2RAD(30)),
108  m_fov_vert_up(mrpt::DEG2RAD(30)),
109 
110  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
111 {
118  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
120 }
121 
123  float near_distance, float far_distance, float horz_FOV_degrees,
124  float vert_FOV_degrees, float lineWidth, bool draw_lines, bool draw_planes)
125  : m_min_distance(near_distance),
126  m_max_distance(far_distance),
127  m_fov_horz_left(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
128  m_fov_horz_right(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
129  m_fov_vert_down(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
130  m_fov_vert_up(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
131  m_draw_lines(draw_lines),
132  m_draw_planes(draw_planes),
133  m_lineWidth(lineWidth),
134  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
135 {
136 }
137 
140 {
141  writeToStreamRender(out);
142  // version 0
147 }
148 
150 {
151  switch (version)
152  {
153  case 0:
160  break;
161  default:
163  };
165 }
166 
167 bool CFrustum::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
168 {
170  MRPT_UNUSED_PARAM(dist);
171  THROW_EXCEPTION("TO DO");
172 }
173 
174 // setters:
176  const float near_distance, const float far_distance)
177 {
178  m_min_distance = near_distance;
179  m_max_distance = far_distance;
181 }
182 void CFrustum::setHorzFOV(const float fov_horz_degrees)
183 {
184  m_fov_horz_right = m_fov_horz_left = 0.5f * mrpt::DEG2RAD(fov_horz_degrees);
190 }
191 void CFrustum::setVertFOV(const float fov_vert_degrees)
192 {
193  m_fov_vert_down = m_fov_vert_up = 0.5f * mrpt::DEG2RAD(fov_vert_degrees);
196  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
199 }
201  const float fov_horz_left_degrees, const float fov_horz_right_degrees)
202 {
203  m_fov_horz_left = mrpt::DEG2RAD(fov_horz_left_degrees);
204  m_fov_horz_right = mrpt::DEG2RAD(fov_horz_right_degrees);
210 }
212  const float fov_vert_down_degrees, const float fov_vert_up_degrees)
213 {
214  m_fov_vert_down = mrpt::DEG2RAD(fov_vert_down_degrees);
215  m_fov_vert_up = mrpt::DEG2RAD(fov_vert_up_degrees);
218  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
221 }
222 
225 {
226  // Compute the 8 corners of the frustum:
227  TPoint3Df pts[8];
228  for (int j = 0; j < 2; j++)
229  {
230  const float r = j == 0 ? m_min_distance : m_max_distance;
231  for (int i = 0; i < 4; i++) pts[4 * j + i].x = r;
232  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
233  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
234  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
235  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
236  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
237  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
238  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
239  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
240  }
241 
242  bb_min = TPoint3D(
243  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
244  std::numeric_limits<double>::max());
245  bb_max = TPoint3D(
246  -std::numeric_limits<double>::max(),
247  -std::numeric_limits<double>::max(),
248  -std::numeric_limits<double>::max());
249  for (auto& pt : pts)
250  {
251  keep_min(bb_min.x, pt.x);
252  keep_min(bb_min.y, pt.y);
253  keep_min(bb_min.z, pt.z);
254 
255  keep_max(bb_max.x, pt.x);
256  keep_max(bb_max.y, pt.y);
257  keep_max(bb_max.z, pt.z);
258  }
259 
260  // Convert to coordinates of my parent:
263 }
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
Definition: CFrustum.cpp:167
GLAPI void GLAPIENTRY glVertex3fv(const GLfloat *v)
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::math::CMatrixFixed< double, 3, 3 > *out_jacobian_df_dpoint=nullptr, mrpt::math::CMatrixFixed< double, 3, 6 > *out_jacobian_df_dpose=nullptr, mrpt::math::CMatrixFixed< double, 3, 6 > *out_jacobian_df_dse3=nullptr, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
Definition: CPose3D.cpp:368
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void renderQuadWithNormal(const mrpt::math::TPoint3Df &p1, const mrpt::math::TPoint3Df &p2, const mrpt::math::TPoint3Df &p3, const mrpt::math::TPoint3Df &p4)
Can be used by derived classes to draw a quad with a normal vector computed automatically - to be cal...
Definition: gl_utils.cpp:217
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
double DEG2RAD(const double x)
Degrees to radians.
#define GL_TRIANGLES
Definition: glew.h:277
float m_min_distance
Near and far planes.
Definition: CFrustum.h:57
void setVertFOV(const float fov_vert_degrees)
Changes vertical FOV (symmetric)
Definition: CFrustum.cpp:191
STL namespace.
uint8_t B
Definition: TColor.h:46
#define GL_ONE_MINUS_SRC_ALPHA
Definition: glew.h:288
uint8_t G
Definition: TColor.h:46
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
#define GL_DEPTH_TEST
Definition: glew.h:402
void setHorzFOV(const float fov_horz_degrees)
Changes horizontal FOV (symmetric)
Definition: CFrustum.cpp:182
#define GL_LIGHTING
Definition: glew.h:386
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:54
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
unsigned char uint8_t
Definition: rptypes.h:44
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
This base provides a set of functions for maths stuff.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void writeToStreamRender(mrpt::serialization::CArchive &out) const
Lightweight 3D point (float version).
Definition: TPoint3D.h:21
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CFrustum.cpp:149
CFrustum()
Basic empty constructor.
Definition: CFrustum.cpp:104
GLAPI void GLAPIENTRY glBegin(GLenum mode)
#define GL_BLEND
Definition: glew.h:433
uint8_t R
Definition: TColor.h:46
GLAPI void GLAPIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
#define GL_SRC_ALPHA
Definition: glew.h:287
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
mrpt::img::TColor m_planes_color
Definition: CFrustum.h:64
float m_fov_horz_left
Semi FOVs (in radians)
Definition: CFrustum.h:59
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
void setNearFarPlanes(const float near_distance, const float far_distance)
Changes distance of near & far planes.
Definition: CFrustum.cpp:175
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
GLuint in
Definition: glext.h:7391
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CFrustum.cpp:138
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
void setVertFOVAsymmetric(const float fov_vert_down_degrees, const float fov_vert_up_degrees)
Changes vertical FOV (asymmetric)
Definition: CFrustum.cpp:211
const auto bb_max
GLAPI void GLAPIENTRY glEnd(void)
A solid or wireframe frustum in 3D (a rectangular truncated pyramid), with arbitrary (possibly assyme...
Definition: CFrustum.h:51
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
Definition: CFrustum.cpp:223
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const auto bb_min
#define GL_LINE_STRIP
Definition: glew.h:276
GLenum GLint x
Definition: glext.h:3542
Lightweight 3D point.
Definition: TPoint3D.h:90
void setHorzFOVAsymmetric(const float fov_horz_left_degrees, const float fov_horz_right_degrees)
Changes horizontal FOV (asymmetric)
Definition: CFrustum.cpp:200
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CFrustum.cpp:139
void readFromStreamRender(mrpt::serialization::CArchive &in)
uint8_t A
Definition: TColor.h:46
float m_fov_vert_down
Semi FOVs (in radians)
Definition: CFrustum.h:61
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019