[OpenGL GLFW GLAD] Generowanie Sfery
Ostatnio zmodyfikowano 2025-02-01 23:07
tBane Temat założony przez niniejszego użytkownika |
[OpenGL GLFW GLAD] Generowanie Sfery » 2025-01-26 21:43:40 Witam! Poszukuję algorytmu do generowania sfery. Potrzebuję aby algorytm generował listę std::vector < float > _vertices w taki sposób, że są w niej kolejno zapisywane x,y,z,u,v czyli najpierw współrzędne x,y,z a potem współrzędne textury u,v. Obecnie mam taki kod ale on nie działa... std::vector < float > generate_sphere_vertices( float radius = 2, int segments = 4, int rings = 4 ) { std::vector < float > vertices; for( int i = 0; i <= rings; ++i ) { float v = float( i ) / rings; float theta = v * M_PI; float sinTheta = sin( theta ); float cosTheta = cos( theta ); for( int j = 0; j <= segments; ++j ) { float u = float( j ) / segments; float phi = u * 2.0f * M_PI; float sinPhi = sin( phi ); float cosPhi = cos( phi ); float x = radius * sinTheta * cosPhi; float y = radius * cosTheta; float z = radius * sinTheta * sinPhi; vertices.push_back( x ); vertices.push_back( y ); vertices.push_back( z ); vertices.push_back( u ); vertices.push_back( v ); } } return vertices; }
|
|
pekfos |
» 2025-01-26 22:47:27 Co znaczy że nie działa? |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-01-26 23:18:22 Znaczy, że nie generuje sfery..
//edit Bo algorytm z tego co rozumiem wyznacza tylko wierzchołki a ja potrzebuję aby ściany były. |
|
pekfos |
» 2025-02-01 19:30:05 Bo algorytm z tego co rozumiem wyznacza tylko wierzchołki a ja potrzebuję aby ściany były. Dobrze rozumiesz i spełnia to wymagania które sam przedstawiłeś na start: Potrzebuję aby algorytm generował listę std::vector < float > _vertices w taki sposób, że są w niej kolejno zapisywane x,y,z,u,v czyli najpierw współrzędne x,y,z a potem współrzędne textury u,v. To teraz napisz drugą funkcję która wygeneruje listę trójkątów do złożenia z tej listy wierzchołków. |
|
tBane Temat założony przez niniejszego użytkownika |
» 2025-02-01 23:07:44 Ok. Z pomocą ChatGPT udało mi się zdobyć algorytm. Wrzucam jakby ktoś kiedyś potrzebował. std::vector < float > generate_sphere_vertices( float radius = 1.0f, int segments = 16, int rings = 16 ) { std::vector < float > vertices; for( int i = 0; i < rings; ++i ) { float v0 = float( i ) / rings; float v1 = float( i + 1 ) / rings; float theta0 = v0 * M_PI; float theta1 = v1 * M_PI; float sinTheta0 = sin( theta0 ); float cosTheta0 = cos( theta0 ); float sinTheta1 = sin( theta1 ); float cosTheta1 = cos( theta1 ); for( int j = 0; j < segments; ++j ) { float u0 = float( j ) / segments; float u1 = float( j + 1 ) / segments; float phi0 = u0 * 2.0f * M_PI; float phi1 = u1 * 2.0f * M_PI; float sinPhi0 = sin( phi0 ); float cosPhi0 = cos( phi0 ); float sinPhi1 = sin( phi1 ); float cosPhi1 = cos( phi1 ); vertices.push_back( radius * sinTheta0 * cosPhi0 ); vertices.push_back( radius * cosTheta0 ); vertices.push_back( radius * sinTheta0 * sinPhi0 ); vertices.push_back( u0 ); vertices.push_back( v0 ); vertices.push_back( radius * sinTheta1 * cosPhi0 ); vertices.push_back( radius * cosTheta1 ); vertices.push_back( radius * sinTheta1 * sinPhi0 ); vertices.push_back( u0 ); vertices.push_back( v1 ); vertices.push_back( radius * sinTheta1 * cosPhi1 ); vertices.push_back( radius * cosTheta1 ); vertices.push_back( radius * sinTheta1 * sinPhi1 ); vertices.push_back( u1 ); vertices.push_back( v1 ); vertices.push_back( radius * sinTheta0 * cosPhi0 ); vertices.push_back( radius * cosTheta0 ); vertices.push_back( radius * sinTheta0 * sinPhi0 ); vertices.push_back( u0 ); vertices.push_back( v0 ); vertices.push_back( radius * sinTheta1 * cosPhi1 ); vertices.push_back( radius * cosTheta1 ); vertices.push_back( radius * sinTheta1 * sinPhi1 ); vertices.push_back( u1 ); vertices.push_back( v1 ); vertices.push_back( radius * sinTheta0 * cosPhi1 ); vertices.push_back( radius * cosTheta0 ); vertices.push_back( radius * sinTheta0 * sinPhi1 ); vertices.push_back( u1 ); vertices.push_back( v0 ); } } return vertices; }
|
|
« 1 » |