Question: I need help to change the code, so it can become a cylinder? #include #include #include #include #include Cylinder.h using namespace std; Sphere::Sphere() { init(48);

I need help to change the code, so it can become a cylinder?

#include

#include

#include

#include

#include "Cylinder.h"

using namespace std;

Sphere::Sphere() {

init(48);

}

Sphere::Sphere(int prec) {

init(prec);

}

float Sphere::toRadians(float degrees) { return (degrees * 2.0f * 3.14159f) / 360.0f; }

void Sphere::init(int prec) {

numVertices = (prec + 1) * (prec + 1);

numIndices = prec * prec * 6;

for (int i = 0; i < numVertices; i++) { vertices.push_back(glm::vec3()); }

for (int i = 0; i < numVertices; i++) { texCoords.push_back(glm::vec2()); }

for (int i = 0; i < numVertices; i++) { normals.push_back(glm::vec3()); }

for (int i = 0; i < numVertices; i++) { tangents.push_back(glm::vec3()); }

for (int i = 0; i < numIndices; i++) { indices.push_back(0); }

// calculate triangle vertices

for (int i = 0; i <= prec; i++) {

for (int j = 0; j <= prec; j++) {

float y = (float)cos(toRadians(180.0f - i * 180.0f / prec));

float x = -(float)cos(toRadians(j*360.0f / prec))*(float)abs(cos(asin(y)));

float z = (float)sin(toRadians(j*360.0f / (float)(prec)))*(float)abs(cos(asin(y)));

vertices[i*(prec + 1) + j] = glm::vec3(x, y, z);

texCoords[i*(prec + 1) + j] = glm::vec2(((float)j / prec), ((float)i / prec));

normals[i*(prec + 1) + j] = glm::vec3(x, y, z);

// calculate tangent vector

if (((x == 0) && (y == 1) && (z == 0)) || ((x == 0) && (y == -1) && (z == 0))) {

tangents[i*(prec + 1) + j] = glm::vec3(0.0f, 0.0f, -1.0f);

}

else {

tangents[i*(prec + 1) + j] = glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(x, y, z));

}

}

}

// calculate triangle indices

for (int i = 0; i

for (int j = 0; j

indices[6 * (i*prec + j) + 0] = i*(prec + 1) + j;

indices[6 * (i*prec + j) + 1] = i*(prec + 1) + j + 1;

indices[6 * (i*prec + j) + 2] = (i + 1)*(prec + 1) + j;

indices[6 * (i*prec + j) + 3] = i*(prec + 1) + j + 1;

indices[6 * (i*prec + j) + 4] = (i + 1)*(prec + 1) + j + 1;

indices[6 * (i*prec + j) + 5] = (i + 1)*(prec + 1) + j;

}

}

}

int Sphere::getNumVertices() { return numVertices; }

int Sphere::getNumIndices() { return numIndices; }

std::vector Sphere::getIndices() { return indices; }

std::vector Sphere::getVertices() { return vertices; }

std::vector Sphere::getTexCoords() { return texCoords; }

std::vector Sphere::getNormals() { return normals; }

std::vector Sphere::getTangents() { return tangents; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!