duration -> durationInSec
[agianapa.git] / socg / sphere.h
blob78fd6c90caa6d379d858868938afb270581b1cb9
1 #ifndef SPHERE_H
2 #define SPHERE_H
4 #include <QtOpenGL>
5 #include <cmath>
7 class Sphere {
8 public:
9 Sphere(float radius = 0.5f, unsigned int slices = 20,
10 unsigned int stacks = 20, bool hasCulling = true)
12 m_radius = radius;
13 m_slices = slices;
14 m_stacks = stacks;
15 m_hasCulling = hasCulling;
17 ~Sphere() {};
19 // Setters
20 void setRadius(float radius) { m_radius = radius; }
21 void setSlices(unsigned int slices) { m_slices = slices; }
22 void setStacks(unsigned int stacks) { m_stacks = stacks; }
23 void setCulling(bool hasCulling) { m_hasCulling = hasCulling; }
25 // Getters
26 float getRadius(void) const { return m_radius; }
27 unsigned int getSlices(void) const { return m_slices; }
28 unsigned int getStacks(void) const { return m_stacks; }
29 bool getCulling(void) const { return m_hasCulling; }
31 // Draw
32 void draw(void) const
34 GLfloat theta, phi, r, x, y, z, stepTheta, stepPhi;
36 qDebug("Sphere::draw()");
37 qDebug("Stacks = %u\tSlices = %u\tr = %f", m_stacks, m_slices, m_radius);
39 if (m_hasCulling)
40 glEnable(GL_CULL_FACE);
41 else
42 glDisable(GL_CULL_FACE);
44 stepTheta = M_PI / m_stacks;
45 stepPhi = 2 * M_PI / m_slices;
46 r = m_radius;
48 for (theta = 0; theta <= M_PI; theta += stepTheta) {
49 glBegin(GL_QUAD_STRIP);
50 for (phi = 0; phi <= 2.0 * M_PI + 0.1; phi += stepPhi) {
51 x = r * sin(theta) * cos(phi);
52 y = r * sin(theta) * sin(phi);
53 z = r * cos(theta);
54 glVertex3f(x, y, z);
56 x = r * sin(theta + stepTheta) * cos(phi);
57 y = r * sin(theta + stepTheta) * sin(phi);
58 z = r * cos(theta + stepTheta);
59 glVertex3f(x, y, z);
61 glEnd();
64 // The boundary edges of the polygon are drawn as line segments.
65 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
68 private:
69 float m_radius;
70 unsigned int m_slices;
71 unsigned int m_stacks;
72 bool m_hasCulling;
75 #endif // SPHERE_H