Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : /*
4 : : MIT License
5 : :
6 : : Copyright (c) 2014-2024 Stephane Cuillerdier (aka aiekick)
7 : :
8 : : Permission is hereby granted, free of charge, to any person obtaining a copy
9 : : of this software and associated documentation files (the "Software"), to deal
10 : : in the Software without restriction, including without limitation the rights
11 : : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 : : copies of the Software, and to permit persons to whom the Software is
13 : : furnished to do so, subject to the following conditions:
14 : :
15 : : The above copyright notice and this permission notice shall be included in all
16 : : copies or substantial portions of the Software.
17 : :
18 : : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 : : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 : : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 : : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 : : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 : : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 : : SOFTWARE.
25 : : */
26 : :
27 : : // ezGL is part of the ezLibs project : https://github.com/aiekick/ezLibs.git
28 : :
29 : :
30 : : #include "mesh.hpp"
31 : : #include <vector>
32 : : #include <memory>
33 : : #include <cmath>
34 : :
35 : : namespace ez {
36 : : namespace gl {
37 : :
38 : : struct ProcDatas {
39 : : std::array<float, 3U> p = {};
40 : : std::array<float, 2U> t = {};
41 : 0 : ProcDatas(){};
42 : 0 : ProcDatas(const float& px, const float& py, const float& pz, const float& tx, const float& ty) : p{px, py, pz}, t{tx, ty} {};
43 : : };
44 : :
45 : : class ProcMesh;
46 : : typedef std::shared_ptr<ProcMesh> ProcMeshPtr;
47 : : typedef std::weak_ptr<ProcMesh> ProcMeshWeak;
48 : :
49 : : class ProcMesh : public Mesh<ProcDatas> {
50 : : public:
51 : 0 : static ProcMeshPtr createUVSphere(const double& vRadius, const uint32_t& vUSubdivs, const uint32_t& vVSubdivs) {
52 : 0 : auto res = std::make_shared<ProcMesh>();
53 : 0 : res->m_This = res;
54 : 0 : assert(vRadius > 0.0 && vUSubdivs > 2U && vVSubdivs > 2U);
55 : 0 :
56 : 0 : std::vector<ProcDatas> vertices;
57 : 0 : std::vector<uint32_t> indices;
58 : 0 :
59 : 0 : ProcDatas p0, p1, p2, p3;
60 : 0 : const double _PI_ = 3.1415926535897932384626433832795;
61 : 0 : const double _2PI_ = _PI_ * 2.0;
62 : 0 : const double s_step = 1.0 / vUSubdivs;
63 : 0 : const double t_step = 1.0 / vVSubdivs;
64 : 0 : const double u_step = _2PI_ * s_step;
65 : 0 : const double v_step = _2PI_ * t_step;
66 : 0 :
67 : 0 : for (double u = 0.0, s = 0.0; u < _2PI_; u += u_step, s += s_step) {
68 : 0 : double xy = vRadius * cos(u);
69 : 0 : double z = vRadius * sin(u);
70 : 0 : for (double v = -_PI_, t = 0.0; v < _PI_; v += v_step, t += t_step) {
71 : 0 : double x = xy * cos(v);
72 : 0 : double y = xy = sin(v);
73 : 0 : vertices.push_back(ProcDatas((float)x, (float)y, (float)z, (float)s, (float)t));
74 : 0 : }
75 : 0 : }
76 : 0 :
77 : 0 : for (uint32_t i = 0U; i < vUSubdivs; ++i) {
78 : 0 : uint32_t k1 = i * (vVSubdivs + 1);
79 : 0 : uint32_t k2 = k1 + vVSubdivs + 1;
80 : 0 :
81 : 0 : for (uint32_t j = 0U; j < vVSubdivs; ++j) {
82 : 0 : if (i != 0U) {
83 : 0 : indices.push_back(k1);
84 : 0 : indices.push_back(k2);
85 : 0 : indices.push_back(k1 + 1U);
86 : 0 : }
87 : 0 :
88 : 0 : if (i != (vUSubdivs - 1U)) {
89 : 0 : indices.push_back(k1 + 1U);
90 : 0 : indices.push_back(k2);
91 : 0 : indices.push_back(k2 + 1U);
92 : 0 : }
93 : 0 :
94 : 0 : ++k1;
95 : 0 : ++k2;
96 : 0 : }
97 : 0 : }
98 : 0 :
99 : 0 : if (!res->init(vertices, {3, 2}, indices, {}, {}, 0, true)) {
100 : 0 : res.reset();
101 : 0 : }
102 : 0 : return res;
103 : 0 : }
104 : :
105 : : private:
106 : : ProcMeshWeak m_This;
107 : : };
108 : :
109 : : } // namespace gl
110 : : } // namespace ez
|