Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I change this code to make a Cylinder? #include #include #include #include // glm::translate, glm::rotate, glm::scale, glm::perspective #include Torus.h using namespace std; Torus::Torus()

How can I change this code to make a Cylinder?

#include

#include

#include

#include // glm::translate, glm::rotate, glm::scale, glm::perspective

#include "Torus.h"

using namespace std;

Torus::Torus() {

prec = 48;

inner = 0.5f;

outer = 0.2f;

init();

}

Torus::Torus(float in, float out, int precIn) {

prec = precIn;

inner = in;

outer = out;

init();

}

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

void Torus::init() {

numVertices = (prec + 5) * (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++) { sTangents.push_back(glm::vec3()); }

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

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

// calculate first ring

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

float amt = toRadians(i*360.0f / prec);

glm::mat4 rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 0.0f, 1.0f));

glm::vec3 initPos(rMat * glm::vec4(outer, 0.0f, 0.0f, 1.0f));

vertices[i] = glm::vec3(initPos + glm::vec3(inner, 0.0f, 0.0f));

texCoords[i] = glm::vec2(0.0f, ((float)i / (float)prec));

rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 0.0f, 1.0f));

tTangents[i] = glm::vec3(rMat * glm::vec4(0.0f, -1.0f, 0.0f, 1.0f));

sTangents[i] = glm::vec3(glm::vec3(0.0f, 0.0f, -1.0f));

normals[i] = glm::cross(tTangents[i], sTangents[i]);

}

// rotate the first ring about Y to get the other rings

for (int ring = 1; ring < prec + 1; ring++) {

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

float amt = (float)toRadians((float)ring * 360.0f / (prec));

glm::mat4 rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));

vertices[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(vertices[i], 1.0f));

texCoords[ring*(prec + 1) + i] = glm::vec2((float)ring*2.0f / (float)prec, texCoords[i].t);

if (texCoords[ring*(prec + 1) + i].s > 1.0) texCoords[ring*(prec+1)+i].s -= 1.0f;

rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));

sTangents[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(sTangents[i], 1.0f));

rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));

tTangents[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(tTangents[i], 1.0f));

rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));

normals[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(normals[i], 1.0f));

}

}

// calculate triangle indices

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

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

indices[((ring*prec + i) * 2) * 3 + 0] = ring*(prec + 1) + i;

indices[((ring*prec + i) * 2) * 3 + 1] = (ring + 1)*(prec + 1) + i;

indices[((ring*prec + i) * 2) * 3 + 2] = ring*(prec + 1) + i + 1;

indices[((ring*prec + i) * 2 + 1) * 3 + 0] = ring*(prec + 1) + i + 1;

indices[((ring*prec + i) * 2 + 1) * 3 + 1] = (ring + 1)*(prec + 1) + i;

indices[((ring*prec + i) * 2 + 1) * 3 + 2] = (ring + 1)*(prec + 1) + i + 1;

}

}

}

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

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

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

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

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

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

std::vector Torus::getStangents() { return sTangents; }

std::vector Torus::getTtangents() { return tTangents; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions