Created
December 30, 2020 10:46
-
-
Save Pikachuxxxx/3f2cdca2ffd83fbe00f3588ebff15d19 to your computer and use it in GitHub Desktop.
Script to generate IcoSphere VertexData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::vector<glm::vec3> GenerateIcoSphereVertices(int radius, std::vector<unsigned int>& Indices, int subDivisions /*= 1*/) | |
{ | |
const float S_STEP = 186 / 2048.0f; // horizontal texture step | |
const float T_STEP = 322 / 1024.0f; // vertical texture step | |
const float H_ANGLE = M_PI / 180.0f * 72; // 72 degree = 360 / 5 | |
const float V_ANGLE = atanf(1.0f / 2); // elevation = 26.565 degrees | |
std::vector<float> baseVertices(12 * 3); // array of 12 vertices (x,y,z) | |
int i1, i2; // indices | |
float z,xy; // vertices | |
float hAngle1 = -M_PI / 2 - H_ANGLE / 2; // start from -126 deg at 1st row | |
float hAngle2 = -M_PI / 2; // start from -90 deg at 2nd row | |
// the first top vertex at (0, 0, r) | |
baseVertices[0] = 0; | |
baseVertices[1] = 0; | |
baseVertices[2] = radius; | |
// 10 vertices as 1st and 2nd rows | |
for (int i = 1; i <= 5; ++i) | |
{ | |
i1 = i * 3; // index for first row | |
i2 = (i + 5) * 3; // index for 2nd row | |
z = radius * sinf(V_ANGLE); | |
xy = radius * cosf(V_ANGLE); | |
baseVertices[i1] = xy * cosf(hAngle1); // x | |
baseVertices[i2] = xy * cosf(hAngle2); | |
baseVertices[i1 + 1] = xy * sinf(hAngle1); // y | |
baseVertices[i2 + 1] = xy * sinf(hAngle2); | |
baseVertices[i1 + 2] = z; // z | |
baseVertices[i2 + 2] = -z; | |
// next horizontal angles | |
hAngle1 += H_ANGLE; | |
hAngle2 += H_ANGLE; | |
} | |
// the last bottom vertex at (0, 0, -r) | |
i1 = 11 * 3; | |
baseVertices[i1] = 0; | |
baseVertices[i1 + 1] = 0; | |
baseVertices[i1 + 2] = -radius; | |
// std::vector<glm::vec3> verts; | |
// for(int i = 0; i < baseVertices.size(); i += 3) | |
// { | |
// verts.push_back(glm::vec3(baseVertices[i], baseVertices[i + 1], baseVertices[i + 2])); | |
// } | |
// return verts; | |
std::vector<glm::vec3> vertices; | |
std::vector<glm::vec3> normals; | |
std::vector<glm::vec2> uv; | |
std::vector<unsigned int> indices; | |
vertices.clear(); | |
float *v0, *v1, *v2, *v3, *v4, *v11; // vertex positions | |
float n[3]; // face normal | |
float t0[2], t1[2], t2[2], t3[2], t4[2], t11[2]; // texCoords | |
unsigned int index = 0; | |
v0 = &baseVertices[0]; | |
v11 = &baseVertices[11 * 3]; | |
for(int i = 1; i <= 5; ++i) | |
{ | |
v1 = &baseVertices[i * 3]; | |
if(i < 5) | |
v2 = &baseVertices[(i + 1) * 3]; | |
else | |
v2 = &baseVertices[(i + 5) * 3]; | |
v3 = &baseVertices[(i + 5) * 3]; | |
if((i + 5) < 10) | |
v4 = &baseVertices[(i + 6) * 3]; | |
else | |
v4 = &baseVertices[6 * 3]; | |
// texture coords | |
t0[0] = (2 * i - 1) * S_STEP; t0[1] = 0; | |
t1[0] = (2 * i - 2) * S_STEP; t1[1] = T_STEP; | |
t2[0] = (2 * i - 0) * S_STEP; t2[1] = T_STEP; | |
t3[0] = (2 * i - 1) * S_STEP; t3[1] = T_STEP * 2; | |
t4[0] = (2 * i + 1) * S_STEP; t4[1] = T_STEP * 2; | |
t11[0]= 2 * i * S_STEP; t11[1]= T_STEP * 3; | |
vertices.push_back(glm::vec3(v0[0], v0[1], v0[2])); | |
vertices.push_back(glm::vec3(v1[0], v1[1], v1[2])); | |
vertices.push_back(glm::vec3(v2[0], v2[1], v2[2])); | |
uv.push_back(glm::vec2(t0[0], t0[1])); | |
uv.push_back(glm::vec2(t1[0], t2[1])); | |
uv.push_back(glm::vec2(t2[0], t2[1])); | |
indices.push_back(index); | |
indices.push_back(index + 1); | |
indices.push_back(index + 2); | |
vertices.push_back(glm::vec3(v1[0], v1[1], v1[2])); | |
vertices.push_back(glm::vec3(v3[0], v3[1], v3[2])); | |
vertices.push_back(glm::vec3(v2[0], v2[1], v2[2])); | |
uv.push_back(glm::vec2(t1[0], t1[1])); | |
uv.push_back(glm::vec2(t3[0], t3[1])); | |
uv.push_back(glm::vec2(t2[0], t2[1])); | |
indices.push_back(index + 3); | |
indices.push_back(index + 4); | |
indices.push_back(index + 5); | |
vertices.push_back(glm::vec3(v2[0], v2[1], v2[2])); | |
vertices.push_back(glm::vec3(v3[0], v3[1], v3[2])); | |
vertices.push_back(glm::vec3(v4[0], v4[1], v4[2])); | |
uv.push_back(glm::vec2(t2[0], t2[1])); | |
uv.push_back(glm::vec2(t3[0], t3[1])); | |
uv.push_back(glm::vec2(t4[0], t4[1])); | |
indices.push_back(index + 6); | |
indices.push_back(index + 7); | |
indices.push_back(index + 8); | |
vertices.push_back(glm::vec3(v3[0], v3[1], v3[2])); | |
vertices.push_back(glm::vec3(v11[0], v11[1], v11[2])); | |
vertices.push_back(glm::vec3(v4[0], v4[1], v4[2])); | |
uv.push_back(glm::vec2(t3[0], t3[1])); | |
uv.push_back(glm::vec2(t11[0], t11[1])); | |
uv.push_back(glm::vec2(t4[0], t4[1])); | |
indices.push_back(index + 9); | |
indices.push_back(index + 10); | |
indices.push_back(index + 11); | |
index += 12; | |
} | |
Indices = indices; | |
return vertices; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment