MRPT  1.9.9
CSetOfTexturedTriangles.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 
14 
15 #include "opengl_internals.h"
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 
21 using namespace mrpt::math;
22 
25 
26 /*---------------------------------------------------------------
27  ~CTexturedPlane
28  ---------------------------------------------------------------*/
29 CSetOfTexturedTriangles::~CSetOfTexturedTriangles() = default;
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 
44  vector<TTriangle>::const_iterator it;
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 uint8_t CSetOfTexturedTriangles::serializeGetVersion() const { return 2; }
77 void CSetOfTexturedTriangles::serializeTo(
79 {
80  uint32_t n;
81 
82  writeToStreamRender(out);
83  writeToStreamTexturedObject(out);
84 
85  n = (uint32_t)m_triangles.size();
86 
87  out << n;
88 
89  for (uint32_t i = 0; i < n; i++) m_triangles[i].writeToStream(out);
90 }
91 
92 void CSetOfTexturedTriangles::serializeFrom(
94 {
95  switch (version)
96  {
97  case 0:
98  case 1:
99  case 2:
100  {
101  readFromStreamRender(in);
102  if (version >= 2)
103  {
104  readFromStreamTexturedObject(in);
105  }
106  else
107  { // Old version.
108  in >> CTexturedObject::m_textureImage;
109  in >> CTexturedObject::m_enableTransparency;
110  if (CTexturedObject::m_enableTransparency)
111  {
112  in >> CTexturedObject::m_textureImageAlpha;
113  assignImage(
114  CTexturedObject::m_textureImage,
115  CTexturedObject::m_textureImageAlpha);
116  }
117  else
118  assignImage(CTexturedObject::m_textureImage);
119  }
120 
121  uint32_t n;
122  in >> n;
123  m_triangles.resize(n);
124 
125  for (uint32_t i = 0; i < n; i++) m_triangles[i].readFromStream(in);
126  }
127  break;
128  default:
130  };
131  CRenderizableDisplayList::notifyChange();
132 }
133 
135  const mrpt::poses::CPose3D& o, double& dist) const
136 {
138  MRPT_UNUSED_PARAM(dist);
139  throw std::runtime_error(
140  "TODO: TraceRay not implemented in CSetOfTexturedTriangles");
141 }
142 
143 void CSetOfTexturedTriangles::getBoundingBox(
145 {
147  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
148  std::numeric_limits<double>::max());
150  -std::numeric_limits<double>::max(),
151  -std::numeric_limits<double>::max(),
152  -std::numeric_limits<double>::max());
153 
154  for (const auto& t : m_triangles)
155  {
156  keep_min(bb_min.x, t.m_v1.m_x);
157  keep_max(bb_max.x, t.m_v1.m_x);
158  keep_min(bb_min.y, t.m_v1.m_y);
159  keep_max(bb_max.y, t.m_v1.m_y);
160  keep_min(bb_min.z, t.m_v1.m_z);
161  keep_max(bb_max.z, t.m_v1.m_z);
162 
163  keep_min(bb_min.x, t.m_v2.m_x);
164  keep_max(bb_max.x, t.m_v2.m_x);
165  keep_min(bb_min.y, t.m_v2.m_y);
166  keep_max(bb_max.y, t.m_v2.m_y);
167  keep_min(bb_min.z, t.m_v2.m_z);
168  keep_max(bb_max.z, t.m_v2.m_z);
169 
170  keep_min(bb_min.x, t.m_v3.m_x);
171  keep_max(bb_max.x, t.m_v3.m_x);
172  keep_min(bb_min.y, t.m_v3.m_y);
173  keep_max(bb_max.y, t.m_v3.m_y);
174  keep_min(bb_min.z, t.m_v3.m_z);
175  keep_max(bb_max.z, t.m_v3.m_z);
176  }
177 
178  // Convert to coordinates of my parent:
179  m_pose.composePoint(bb_min, bb_min);
180  m_pose.composePoint(bb_max, bb_max);
181 }
182 
183 CSetOfTexturedTriangles::TVertex::TVertex()
184 
185  = default;
186 
187 CSetOfTexturedTriangles::TVertex::TVertex(
188  float x, float y, float z, uint32_t u, uint32_t v)
189  : m_x(x), m_y(y), m_z(z), m_u(u), m_v(v)
190 {
191 }
192 
195 {
196  out << m_x << m_y << m_z << m_u << m_v;
197 }
200 {
201  in >> m_x >> m_y >> m_z >> m_u >> m_v;
202 }
203 
207  : m_v1(v1), m_v2(v2), m_v3(v3)
208 {
209 }
210 
213 {
214  m_v1.writeToStream(out);
215  m_v2.writeToStream(out);
216  m_v3.writeToStream(out);
217 }
220 {
221  m_v1.readFromStream(in);
222  m_v2.readFromStream(in);
223  m_v3.readFromStream(in);
224 }
#define MRPT_START
Definition: exceptions.h:241
GLdouble GLdouble t
Definition: glext.h:3695
GLdouble GLdouble z
Definition: glext.h:3879
GLbyte GLbyte bz
Definition: glext.h:6193
bool traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::math::TPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:2567
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
GLAPI void GLAPIENTRY glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)
GLenum GLsizei n
Definition: glext.h:5136
#define GL_TRIANGLES
Definition: glew.h:277
STL namespace.
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_SMOOTH
Definition: glew.h:636
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
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...
GLfloat GLfloat GLfloat GLfloat v3
Definition: glext.h:4125
GLAPI void GLAPIENTRY glBegin(GLenum mode)
void writeToStream(mrpt::serialization::CArchive &out) const
void readFromStream(mrpt::serialization::CArchive &in)
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
const GLdouble * v
Definition: glext.h:3684
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
GLfloat GLfloat v1
Definition: glext.h:4121
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
void readFromStream(mrpt::serialization::CArchive &in)
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
GLAPI void GLAPIENTRY glEnd(void)
void writeToStream(mrpt::serialization::CArchive &out) const
GLenum GLint GLint y
Definition: glext.h:3542
GLbyte by
Definition: glext.h:6193
GLAPI void GLAPIENTRY glTexCoord2d(GLdouble s, GLdouble t)
const auto bb_min
GLfloat GLfloat GLfloat v2
Definition: glext.h:4123
GLenum GLint x
Definition: glext.h:3542
Lightweight 3D point.
Definition: TPoint3D.h:90
unsigned __int32 uint32_t
Definition: rptypes.h:50
#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