Main MRPT website > C++ reference for MRPT 1.9.9
CSetOfTexturedTriangles.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
13 #include <mrpt/utils/CStream.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 using namespace mrpt::utils;
21 using namespace mrpt::math;
22 
25 
26 /*---------------------------------------------------------------
27  ~CTexturedPlane
28  ---------------------------------------------------------------*/
29 CSetOfTexturedTriangles::~CSetOfTexturedTriangles() {}
30 /*---------------------------------------------------------------
31  render
32  ---------------------------------------------------------------*/
33 void CSetOfTexturedTriangles::render_texturedobj() const
34 {
35 #if MRPT_HAS_OPENGL_GLUT
37 
39 
41 
42  float ax, ay, az, bx, by, bz;
43 
45  for (it = m_triangles.begin(); it != m_triangles.end(); ++it)
46  {
47  // Compute the normal vector:
48  // ---------------------------------
49  ax = it->m_v2.m_x - it->m_v1.m_x;
50  ay = it->m_v2.m_y - it->m_v1.m_y;
51  az = it->m_v2.m_z - it->m_v1.m_z;
52 
53  bx = it->m_v3.m_x - it->m_v1.m_x;
54  by = it->m_v3.m_y - it->m_v1.m_y;
55  bz = it->m_v3.m_z - it->m_v1.m_z;
56 
57  glNormal3f(ay * bz - az * by, -ax * bz + az * bx, ax * by - ay * bx);
58 
60  float(it->m_v1.m_u) / r_width, float(it->m_v1.m_v) / r_height);
61  glVertex3f(it->m_v1.m_x, it->m_v1.m_y, it->m_v1.m_z);
63  float(it->m_v2.m_u) / r_width, float(it->m_v2.m_v) / r_height);
64  glVertex3f(it->m_v2.m_x, it->m_v2.m_y, it->m_v2.m_z);
66  float(it->m_v3.m_u) / r_width, float(it->m_v3.m_v) / r_height);
67  glVertex3f(it->m_v3.m_x, it->m_v3.m_y, it->m_v3.m_z);
68  }
69 
70  glEnd();
71 
72  MRPT_END
73 #endif
74 }
75 
76 /*---------------------------------------------------------------
77  Implements the writing to a CStream capability of
78  CSerializable objects
79  ---------------------------------------------------------------*/
80 void CSetOfTexturedTriangles::writeToStream(
81  mrpt::utils::CStream& out, int* version) const
82 {
83  if (version)
84  *version = 2;
85  else
86  {
87  uint32_t n;
88 
89  writeToStreamRender(out);
90  writeToStreamTexturedObject(out);
91 
92  n = (uint32_t)m_triangles.size();
93 
94  out << n;
95 
96  for (uint32_t i = 0; i < n; i++) m_triangles[i].writeToStream(out);
97  }
98 }
99 
100 /*---------------------------------------------------------------
101  Implements the reading from a CStream capability of
102  CSerializable objects
103  ---------------------------------------------------------------*/
104 void CSetOfTexturedTriangles::readFromStream(
105  mrpt::utils::CStream& in, int version)
106 {
107  switch (version)
108  {
109  case 0:
110  case 1:
111  case 2:
112  {
113  readFromStreamRender(in);
114  if (version >= 2)
115  {
116  readFromStreamTexturedObject(in);
117  }
118  else
119  { // Old version.
120  in >> CTexturedObject::m_textureImage;
121  in >> CTexturedObject::m_enableTransparency;
122  if (CTexturedObject::m_enableTransparency)
123  {
124  in >> CTexturedObject::m_textureImageAlpha;
125  assignImage(
126  CTexturedObject::m_textureImage,
127  CTexturedObject::m_textureImageAlpha);
128  }
129  else
130  assignImage(CTexturedObject::m_textureImage);
131  }
132 
133  uint32_t n;
134  in >> n;
135  m_triangles.resize(n);
136 
137  for (uint32_t i = 0; i < n; i++) m_triangles[i].readFromStream(in);
138  }
139  break;
140  default:
142  };
143  CRenderizableDisplayList::notifyChange();
144 }
145 
147  const mrpt::poses::CPose3D& o, double& dist) const
148 {
150  MRPT_UNUSED_PARAM(dist);
151  throw std::runtime_error(
152  "TODO: TraceRay not implemented in CSetOfTexturedTriangles");
153 }
154 
155 void CSetOfTexturedTriangles::getBoundingBox(
156  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
157 {
158  bb_min = mrpt::math::TPoint3D(
159  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
160  std::numeric_limits<double>::max());
161  bb_max = mrpt::math::TPoint3D(
162  -std::numeric_limits<double>::max(),
163  -std::numeric_limits<double>::max(),
164  -std::numeric_limits<double>::max());
165 
166  for (size_t i = 0; i < m_triangles.size(); i++)
167  {
168  const TTriangle& t = m_triangles[i];
169 
170  keep_min(bb_min.x, t.m_v1.m_x);
171  keep_max(bb_max.x, t.m_v1.m_x);
172  keep_min(bb_min.y, t.m_v1.m_y);
173  keep_max(bb_max.y, t.m_v1.m_y);
174  keep_min(bb_min.z, t.m_v1.m_z);
175  keep_max(bb_max.z, t.m_v1.m_z);
176 
177  keep_min(bb_min.x, t.m_v2.m_x);
178  keep_max(bb_max.x, t.m_v2.m_x);
179  keep_min(bb_min.y, t.m_v2.m_y);
180  keep_max(bb_max.y, t.m_v2.m_y);
181  keep_min(bb_min.z, t.m_v2.m_z);
182  keep_max(bb_max.z, t.m_v2.m_z);
183 
184  keep_min(bb_min.x, t.m_v3.m_x);
185  keep_max(bb_max.x, t.m_v3.m_x);
186  keep_min(bb_min.y, t.m_v3.m_y);
187  keep_max(bb_max.y, t.m_v3.m_y);
188  keep_min(bb_min.z, t.m_v3.m_z);
189  keep_max(bb_max.z, t.m_v3.m_z);
190  }
191 
192  // Convert to coordinates of my parent:
193  m_pose.composePoint(bb_min, bb_min);
194  m_pose.composePoint(bb_max, bb_max);
195 }
196 
197 CSetOfTexturedTriangles::TVertex::TVertex()
198  : m_x(0.0), m_y(0.0), m_z(0.0), m_u(0), m_v(0)
199 {
200 }
201 
203  float x, float y, float z, uint32_t u, uint32_t v)
204  : m_x(x), m_y(y), m_z(z), m_u(u), m_v(v)
205 {
206 }
207 
209  mrpt::utils::CStream& out) const
210 {
211  out << m_x << m_y << m_z << m_u << m_v;
212 }
214 {
215  in >> m_x >> m_y >> m_z >> m_u >> m_v;
216 }
217 
221  : m_v1(v1), m_v2(v2), m_v3(v3)
222 {
223 }
224 
226  mrpt::utils::CStream& out) const
227 {
228  m_v1.writeToStream(out);
229  m_v2.writeToStream(out);
230  m_v3.writeToStream(out);
231 }
234 {
235  m_v1.readFromStream(in);
236  m_v2.readFromStream(in);
237  m_v3.readFromStream(in);
238 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLdouble GLdouble t
Definition: glext.h:3689
GLdouble GLdouble z
Definition: glext.h:3872
GLbyte GLbyte bz
Definition: glext.h:6105
bool traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::poses::CPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:2582
GLAPI void GLAPIENTRY glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)
GLenum GLsizei n
Definition: glext.h:5074
#define GL_TRIANGLES
Definition: glew.h:276
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void writeToStream(mrpt::utils::CStream &out) const
#define GL_SMOOTH
Definition: glew.h:635
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
void writeToStream(mrpt::utils::CStream &out) const
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLfloat GLfloat GLfloat GLfloat v3
Definition: glext.h:4109
double x
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
IMPLEMENTS_SERIALIZABLE(CSetOfTexturedTriangles, CRenderizableDisplayList, mrpt::opengl) CSetOfTexturedTriangles
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...
Definition: bits.h:227
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
#define MRPT_START
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:4105
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...
Definition: bits.h:220
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEnd(void)
GLenum GLint GLint y
Definition: glext.h:3538
GLbyte by
Definition: glext.h:6105
GLAPI void GLAPIENTRY glTexCoord2d(GLdouble s, GLdouble t)
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:47



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019