Voilà, pour mon besoin personnel, j'ai codé une classe qui renvoie un buffer contenant une sphere basé sur un icosaèdre.
Pour l'itération, il faut savoir qu'elle doit être entre 0 et 5 compris. L'itération 0 rend une sphère à 20 triangles et chaque itération multiplie par 4 le nombre de triangles pour arriver à 20480 pour une itération de 5.
Les buffers étant limités à 65536 vertices et les triangles étant tous séparés (3 vertices par triangles, pour des raisons de texturing et manipulations), il est impossible d'aller au-dessus de 5 itérations.
Maintenant, si quelqu'un à des suggestions d'améliorations, qu'il n'hésite pas! Je modifierais le code dès que possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #ifndef SPHEREICOSA_H #define SPHEREICOSA_H #include "irrTypes.h" #include "vector3d.h" #include "aabbox3d.h" #include "CMeshBuffer.h" #include "SMaterial.h" class SphereIcosa { public: SphereIcosa(irr::video::SMaterial material, irr::f32 rayon=1, irr::u8 iterations=0); SphereIcosa(irr::f32 rayon=1, irr::u8 iterations=0); virtual ~SphereIcosa(); bool createNewSphere(irr::f32 rayon=1, irr::u8 iterations=0); irr::scene::SMeshBuffer* getBufferCopy(); irr::video::SMaterial& getMaterial(); void setMaterial(irr::video::SMaterial material); irr::core::aabbox3df& getBoundingBox(); protected: irr::f32 Rayon; irr::u8 NbIterations; irr::scene::SMeshBuffer* Buffer; irr::core::aabbox3df BoundingBox; void createBoundingBox(); private: }; #endif // SPHEREICOSA_H |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | #include "SphereIcosa.h" SphereIcosa::SphereIcosa(irr::video::SMaterial material, irr::f32 rayon, irr::u8 iterations) : Rayon(rayon), NbIterations(iterations) { /// initialisation du buffer Buffer = new irr::scene::SMeshBuffer(); Buffer->Material = material; /// créé la sphère createNewSphere(rayon, iterations); } SphereIcosa::SphereIcosa(irr::f32 rayon, irr::u8 iterations) : Rayon(rayon), NbIterations(iterations) { /// créé la sphère createNewSphere(rayon, iterations); } SphereIcosa::~SphereIcosa() { /// libère la mémoire if(Buffer) delete Buffer; } bool SphereIcosa::createNewSphere(irr::f32 rayon, irr::u8 iterations) { /// si le nb de vertex dépasse la limite on retourne if( iterations > 5 ) return false; /// array contenant les vertices irr::core::array<irr::core::vector3df> listevertex; /// calcul des termes de base irr::core::vector3df normalise(1, ((1+irr::core::squareroot(5.f))/2), 0); normalise.setLength(rayon); irr::f32 terme1 = normalise.X; irr::f32 terme2 = normalise.Y; /// calcul des 4 points du plan : XY listevertex.push_back( irr::core::vector3df(-terme1,-terme2,0) ); listevertex.push_back( irr::core::vector3df(-terme1,terme2,0) ); listevertex.push_back( irr::core::vector3df(terme1,terme2,0) ); listevertex.push_back( irr::core::vector3df(terme1,-terme2,0) ); /// calcul des 4 points du plan : YZ listevertex.push_back( irr::core::vector3df(0,-terme1,-terme2) ); listevertex.push_back( irr::core::vector3df(0,terme1,-terme2) ); listevertex.push_back( irr::core::vector3df(0,terme1,terme2) ); listevertex.push_back( irr::core::vector3df(0,-terme1,terme2) ); /// calcul des 4 points du plan : XZ listevertex.push_back( irr::core::vector3df(-terme2,0,-terme1) ); listevertex.push_back( irr::core::vector3df(-terme2,0,terme1) ); listevertex.push_back( irr::core::vector3df(terme2,0,terme1) ); listevertex.push_back( irr::core::vector3df(terme2,0,-terme1) ); /// mise dans le buffer dans la forme première Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[0], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[3], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[7], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 0 ); Buffer->Indices.push_back( 1 ); Buffer->Indices.push_back( 2 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[3], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[0], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[4], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 3 ); Buffer->Indices.push_back( 4 ); Buffer->Indices.push_back( 5 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[2], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[1], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[6], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 6 ); Buffer->Indices.push_back( 7 ); Buffer->Indices.push_back( 8 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[1], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[2], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[5], irr::core::vector3df(0), irr::video::SColor(255,000,255,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 9 ); Buffer->Indices.push_back( 10 ); Buffer->Indices.push_back( 11 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[4], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[5], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[11], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 12 ); Buffer->Indices.push_back( 13 ); Buffer->Indices.push_back( 14 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[5], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[4], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[8], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 15 ); Buffer->Indices.push_back( 16 ); Buffer->Indices.push_back( 17 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[7], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[6], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[9], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 18 ); Buffer->Indices.push_back( 19 ); Buffer->Indices.push_back( 20 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[6], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[7], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[10], irr::core::vector3df(0), irr::video::SColor(255,000,000,255), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 21 ); Buffer->Indices.push_back( 22 ); Buffer->Indices.push_back( 23 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[9], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[8], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[0], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 24 ); Buffer->Indices.push_back( 25 ); Buffer->Indices.push_back( 26 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[8], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[9], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[1], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 27 ); Buffer->Indices.push_back( 28 ); Buffer->Indices.push_back( 29 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[10], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[11], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[2], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 30 ); Buffer->Indices.push_back( 31 ); Buffer->Indices.push_back( 32 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[11], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[10], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[3], irr::core::vector3df(0), irr::video::SColor(255,255,000,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 33 ); Buffer->Indices.push_back( 34 ); Buffer->Indices.push_back( 35 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[0], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[8], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[4], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 36 ); Buffer->Indices.push_back( 37 ); Buffer->Indices.push_back( 38 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[0], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[7], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[9], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 39 ); Buffer->Indices.push_back( 40 ); Buffer->Indices.push_back( 41 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[1], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[5], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[8], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 42 ); Buffer->Indices.push_back( 43 ); Buffer->Indices.push_back( 44 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[1], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[9], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[6], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 45 ); Buffer->Indices.push_back( 46 ); Buffer->Indices.push_back( 47 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[2], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[6], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[10], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 48 ); Buffer->Indices.push_back( 49 ); Buffer->Indices.push_back( 50 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[2], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[11], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[5], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 51 ); Buffer->Indices.push_back( 52 ); Buffer->Indices.push_back( 53 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[3], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[10], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[7], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 54 ); Buffer->Indices.push_back( 55 ); Buffer->Indices.push_back( 56 ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[3], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,0)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[4], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0,1)) ); Buffer->Vertices.push_back( irr::video::S3DVertex( listevertex[11], irr::core::vector3df(0), irr::video::SColor(255,200,200,000), irr::core::vector2df(0.5,0.5)) ); Buffer->Indices.push_back( 57 ); Buffer->Indices.push_back( 58 ); Buffer->Indices.push_back( 59 ); for(irr::s32 nbIterations=0; nbIterations < iterations; ++nbIterations ) { /// variables irr::core::array<irr::video::S3DVertex> newVertices; irr::core::array<irr::u16> newIndices; /// créations des couleurs irr::video::SColor color1(255,255,000,000); irr::video::SColor color2(255,000,255,000); irr::video::SColor color3(255,000,000,255); irr::video::SColor color4(255,255,255,000); /// pour chaque triangles on en créé 4 nouveaux par subdivisions for(irr::s32 a=0; a < (irr::s32)Buffer->Indices.size(); a+=3) { /// on enregistre l'index irr::s32 index = (int)newVertices.size(); /// on enregistre les 3 points existant irr::core::vector3df A( Buffer->Vertices[Buffer->Indices[a]].Pos ); irr::core::vector3df B( Buffer->Vertices[Buffer->Indices[a+1]].Pos ); irr::core::vector3df C( Buffer->Vertices[Buffer->Indices[a+2]].Pos ); /// on calcule les 3 nouveaux points irr::core::vector3df D( irr::core::line3df(A, B).getMiddle().setLength(rayon) ); irr::core::vector3df E( irr::core::line3df(B, C).getMiddle().setLength(rayon) ); irr::core::vector3df F( irr::core::line3df(C, A).getMiddle().setLength(rayon) ); /// on insère les 4 nouveaux triangles /// ADF newVertices.push_back( irr::video::S3DVertex( A, irr::core::vector3df(0), color1, irr::core::vector2df(0,0)) ); newVertices.push_back( irr::video::S3DVertex( D, irr::core::vector3df(0), color1, irr::core::vector2df(0,1)) ); newVertices.push_back( irr::video::S3DVertex( F, irr::core::vector3df(0), color1, irr::core::vector2df(0.5,0.5)) ); newIndices.push_back( index ); newIndices.push_back( index+1 ); newIndices.push_back( index+2 ); /// DBE newVertices.push_back( irr::video::S3DVertex( D, irr::core::vector3df(0), color2, irr::core::vector2df(0,0)) ); newVertices.push_back( irr::video::S3DVertex( B, irr::core::vector3df(0), color2, irr::core::vector2df(0,1)) ); newVertices.push_back( irr::video::S3DVertex( E, irr::core::vector3df(0), color2, irr::core::vector2df(0.5,0.5)) ); newIndices.push_back( index+3 ); newIndices.push_back( index+4 ); newIndices.push_back( index+5 ); /// FDE newVertices.push_back( irr::video::S3DVertex( F, irr::core::vector3df(0), color3, irr::core::vector2df(0,0)) ); newVertices.push_back( irr::video::S3DVertex( D, irr::core::vector3df(0), color3, irr::core::vector2df(0,1)) ); newVertices.push_back( irr::video::S3DVertex( E, irr::core::vector3df(0), color3, irr::core::vector2df(0.5,0.5)) ); newIndices.push_back( index+6 ); newIndices.push_back( index+7 ); newIndices.push_back( index+8 ); /// FEC newVertices.push_back( irr::video::S3DVertex( F, irr::core::vector3df(0), color4, irr::core::vector2df(0,0)) ); newVertices.push_back( irr::video::S3DVertex( E, irr::core::vector3df(0), color4, irr::core::vector2df(0,1)) ); newVertices.push_back( irr::video::S3DVertex( C, irr::core::vector3df(0), color4, irr::core::vector2df(0.5,0.5)) ); newIndices.push_back( index+9 ); newIndices.push_back( index+10 ); newIndices.push_back( index+11 ); } Buffer->Vertices = newVertices; Buffer->Indices = newIndices; } /// on mets à jour les paramètres de création de la sphere Rayon = rayon; NbIterations = iterations; /// on met à jour la Bounding Box createBoundingBox(); /// tout s'est bien passé on quitte return true; } irr::scene::SMeshBuffer* SphereIcosa::getBufferCopy() { irr::scene::SMeshBuffer* tmp = new irr::scene::SMeshBuffer(); tmp->Material = Buffer->Material; tmp->Vertices = Buffer->Vertices; tmp->Indices = Buffer->Indices; return tmp; } irr::video::SMaterial& SphereIcosa::getMaterial() { return Buffer->Material; } void SphereIcosa::setMaterial(irr::video::SMaterial material) { Buffer->Material = material; } irr::core::aabbox3df& SphereIcosa::getBoundingBox() { return BoundingBox; } void SphereIcosa::createBoundingBox() { BoundingBox = irr::core::aabbox3df( irr::core::vector3df(-Rayon), irr::core::vector3df(Rayon) ); } |
1 2 3 4 5 6 | SMaterial blanc; blanc.Lighting = false; SphereIcosa sphere(blanc, 1000, 5); SMeshBuffer* SphereBuffer = sphere.getBufferCopy(); // à mettre entre beginscene et endscene Driver->drawMeshBuffer( SphereBuffer ); |