Debian Codegraph @

libs/opengl/src/TTriangle.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, 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/TTriangle.h>
13 #include <mrpt/serialization/CArchive.h>
14
15 using namespace mrpt::opengl;
16
17 // packet size= 3 vertices, each:
18 // XYZ (float) + normal (XYZ float) + RGBA (u8) + UV (float)
19 static_assert(sizeof(TTriangle) == (sizeof(float) * (3 + 3 + 2) + 4) * 3);
20
21 void TTriangle::computeNormals()
22 {
23 const float ax = x(1) - x(0);
24 const float ay = y(1) - y(0);
25 const float az = z(1) - z(0);
26 const float bx = x(2) - x(0);
27 const float by = y(2) - y(0);
28 const float bz = z(2) - z(0);
29
30 const mrpt::math::TVector3Df no = {ay * bz - az * by, -ax * bz + az * bx, ax * by - ay * bx};
31 for (auto& v : vertices) v.normal = no;
32 }
33
34 void TTriangle::writeTo(mrpt::serialization::CArchive& o) const
35 {
36 for (const auto& p : vertices)
37 {
38 const auto& pp = p.xyzrgba;
39 o << pp.pt << pp.r << pp.g << pp.b << pp.a << p.normal;
40 }
41 }
42 void TTriangle::readFrom(mrpt::serialization::CArchive& in)
43 {
44 for (auto& p : vertices)
45 {
46 auto& pp = p.xyzrgba;
47 in >> pp.pt >> pp.r >> pp.g >> pp.b >> pp.a >> p.normal;
48 }
49 }