Repository navigation
Expand file tree
/
Copy pathsphere.cpp
More file actions
69 lines (59 loc) · 2.11 KB
/
Copy pathsphere.cpp
File metadata and controls
69 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cmath>
#include <vector>
#include <iostream>
#include <glm/glm.hpp>
#include "../include/sphere.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 < numIndices; i++) {indices.push_back(0);}
// Calculate 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/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 Triangle Indices
for(int i = 0; i < prec; i++)
{
for(int j = 0; j < prec; 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<int> Sphere::getIndices() {return indices;}
std::vector<glm::vec3> Sphere::getVertices() {return vertices;}
std::vector<glm::vec2> Sphere::getTexCoords() {return texCoords;}
std::vector<glm::vec3> Sphere::getNormals() {return normals;}