-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtexturedsphere.cpp
51 lines (44 loc) · 1.73 KB
/
texturedsphere.cpp
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
#include "texturedsphere.h"
#include <cmath>
#include <iostream>
TexturedSphere::TexturedSphere(int meridians, int latitudes)
: m_meridians(meridians), m_latitudes(latitudes), m_triangleCount(0)
{
// Add 1 to meridians because the prime meridian is in there twice
// Add 2 to latitudes because of the north and south poles (each meridian's poles have to be separate for texture coordinates)
m_vertices.reserve((m_meridians+1) * (m_latitudes+2));
for (size_t i = 0; i < m_meridians + 1; i++)
{
for (size_t j = 0; j < m_latitudes + 2; j++)
{
// texCoord in the range [(0,0), (1,1)]
glm::vec2 texCoord((float)i / m_meridians, (float)j / (m_latitudes+1));
// theta = longitude from 0 to 2pi
// phi = latitude from -pi/2 to pi/2
double theta, phi;
theta = 2*M_PI * texCoord.x;
phi = M_PI * texCoord.y - M_PI_2;
glm::vec3 pos;
pos.y = (float)std::sin(phi);
pos.x = (float)std::cos(phi) * std::cos(theta);
pos.z = (float)std::cos(phi) * std::sin(theta);
m_vertices.push_back({pos, texCoord});
}
}
// Calculate triangle indices
for (size_t i = 0; i < m_meridians; i++)
{
// Construct triangles between successive meridians
for (size_t j = 0; j < m_latitudes + 1; j++)
{
m_indices.push_back(i * (m_latitudes+2) + j);
m_indices.push_back(i * (m_latitudes+2) + j+1);
m_indices.push_back((i+1) * (m_latitudes+2) + j+1);
m_triangleCount++;
m_indices.push_back((i+1) * (m_latitudes+2) + j+1);
m_indices.push_back((i+1) * (m_latitudes+2) + j);
m_indices.push_back(i * (m_latitudes+2) + j);
m_triangleCount++;
}
}
}