Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UVSphere::UVSphere(float radius, const glm::ivec2& segments) :
- StaticMesh(),
- radius(radius),
- segments(segments)
- {
- std::vector<float> vertices;
- std::vector<float> normals;
- std::vector<float> uvs;
- std::vector<unsigned int> indices;
- bool odd = false;
- float inv_len = 1.0f / radius;
- for (int y = 0; y <= segments.y; ++y)
- {
- for (int x = 0; x <= segments.x; ++x)
- {
- float seg_x = (float)x / (float)segments.x;
- float seg_y = (float)y / (float)segments.y;
- float theta = seg_x * 2.0f * PI;
- float phi = seg_y * PI;
- float pos_x = std::cos(theta) * std::sin(phi) * radius;
- float pos_y = std::sin(-PI / 2 + PI * seg_y) * radius;
- float pos_z = std::sin(theta) * std::sin(phi) * radius;
- vertices.push_back(pos_x);
- vertices.push_back(pos_y);
- vertices.push_back(pos_z);
- normals.push_back(pos_x);
- normals.push_back(pos_y);
- normals.push_back(pos_z);
- uvs.push_back(seg_x);
- uvs.push_back(seg_y);
- }
- }
- for (int y = 0; y < segments.y; ++y)
- {
- if (!odd)
- {
- for (int x = 0; x <= segments.x; ++x)
- {
- indices.push_back(y * (segments.x + 1) + x);
- indices.push_back((y + 1) * (segments.x + 1) + x);
- }
- }
- else
- {
- for (int x = segments.x; x >= 0; --x)
- {
- indices.push_back((y + 1) * (segments.x + 1) + x);
- indices.push_back(y * (segments.x + 1) + x);
- }
- }
- odd = !odd;
- }
- this->setVertices(vertices);
- this->setNormals(normals);
- this->setUvs(uvs);
- this->setIndices(indices);
- this->setDrawMode(StaticMesh::DrawMode::TRIANGLE_STRIP);
- calculateTangents();
- vao = new VertexArray();
- vbo = new VertexBuffer(getVertices().data(), (unsigned int)getVertices().size() * sizeof(float));
- vao->addBuffer(*vbo, 0, 3);
- nbo = new VertexBuffer(getNormals().data(), (unsigned int)getNormals().size() * sizeof(float));
- vao->addBuffer(*nbo, 1, 3);
- uvbo = new VertexBuffer(getUvs().data(), (unsigned int)getUvs().size() * sizeof(float));
- vao->addBuffer(*uvbo, 2, 2);
- tbo = new VertexBuffer(getTangents().data(), (unsigned int)getTangents().size() * sizeof(float));
- vao->addBuffer(*tbo, 3, 3);
- ibo = new IndexBuffer(getIndices().data(), (unsigned int)getIndices().size());
- setVertexCount((unsigned int)getVertices().size() / 3);
- setName("SphereStaticMesh");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement