-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspheregl.cpp
85 lines (64 loc) · 1.67 KB
/
spheregl.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "spheregl.h"
using namespace sc;
using namespace vector3_;
#ifdef _MSC_VER
#include <Windows.h>
#else
#endif
#include <GL/gl.h>
#include <GL/glu.h>
//////////////////////////////////////////////
template < typename T >
inline T get_pt_sphere(double id, double jd, double R)
{
double x = R * sin(id * 2 * M_PI) * sin(jd * M_PI);
double y = R * cos(id * 2 * M_PI) * sin(jd * M_PI);
double z = R * cos(jd * M_PI);
return T(x, y, z);
}
//////////////////////////////////////////////
/// \brief SphereGL::SphereGL
///
SphereGL::SphereGL()
{
}
void SphereGL::generate_sphere(double radius, double divider)
{
if(radius == 0)
return;
const int count = 32;
double R = radius / divider;
m_vecs_sphere.clear();
m_inds_sphere.clear();
for(int i = 0; i < count; i++){
double id = (double)i / (count - 1);
for(int j = 0; j < count; j++){
double jd = (double)j / (count - 1);
m_vecs_sphere.push_back(get_pt_sphere<Vector3f>(id, jd, R));
}
}
for(int i = 0; i < count - 1; i++){
for(int j = 0; j < count - 1; j++){
int A = i * count + j;
int B = (i + 1) * count + j;
int C = i * count + j + 1;
m_inds_sphere.push_back(Vector3i(A, B, C));
}
}
glVertexPointer(3, GL_FLOAT, sizeof(Vector3f), m_vecs_sphere.data()->data);
}
void SphereGL::draw_sphere(const Vector3d &cp, double divider)
{
if(!m_inds_sphere.size())
return;
glPushMatrix();
Vector3d cpi(cp);
cpi *= 1.0 / divider;
glTranslated(cpi.x(), cpi.y(), cpi.z());
glColor3f(1, 1, 1);
int cnt = m_inds_sphere.size() * 3;
glEnableClientState(GL_VERTEX_ARRAY);
glDrawElements(GL_LINE_STRIP, cnt, GL_UNSIGNED_INT, m_inds_sphere.data());
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}