1 | // |
---|
2 | // Deus Hax (working title) |
---|
3 | // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | // |
---|
5 | |
---|
6 | #if defined HAVE_CONFIG_H |
---|
7 | # include "config.h" |
---|
8 | #endif |
---|
9 | |
---|
10 | #ifdef WIN32 |
---|
11 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
12 | # define WIN32_LEAN_AND_MEAN |
---|
13 | # include <windows.h> |
---|
14 | #endif |
---|
15 | #if defined __APPLE__ && defined __MACH__ |
---|
16 | # include <OpenGL/gl.h> |
---|
17 | #else |
---|
18 | # define GL_GLEXT_PROTOTYPES |
---|
19 | # include <GL/gl.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <cstdio> |
---|
23 | #include <cmath> |
---|
24 | |
---|
25 | #include "core.h" |
---|
26 | #include "debugsphere.h" |
---|
27 | |
---|
28 | /* |
---|
29 | * DebugSphere implementation class |
---|
30 | */ |
---|
31 | |
---|
32 | class DebugSphereData |
---|
33 | { |
---|
34 | friend class DebugSphere; |
---|
35 | |
---|
36 | void DrawSphere(int ndiv, GLfloat r) |
---|
37 | { |
---|
38 | glBegin(GL_TRIANGLES); |
---|
39 | for (int i = 0; i < 20; i++) |
---|
40 | DrawTriangle(vdata[tindices[i][0]], |
---|
41 | vdata[tindices[i][2]], |
---|
42 | vdata[tindices[i][1]], ndiv, r); |
---|
43 | glEnd(); |
---|
44 | } |
---|
45 | |
---|
46 | void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c, |
---|
47 | int div, GLfloat r) |
---|
48 | { |
---|
49 | if (div <= 0) |
---|
50 | { |
---|
51 | glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r); |
---|
52 | glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r); |
---|
53 | glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, c[2] * r); |
---|
54 | } |
---|
55 | else |
---|
56 | { |
---|
57 | GLfloat ab[3], ac[3], bc[3]; |
---|
58 | for (int i = 0; i < 3; i++) |
---|
59 | { |
---|
60 | ab[i] = (a[i] + b[i]) * 0.5; |
---|
61 | ac[i] = (a[i] + c[i]) * 0.5; |
---|
62 | bc[i] = (b[i] + c[i]) * 0.5; |
---|
63 | } |
---|
64 | Normalize(ab); Normalize(ac); Normalize(bc); |
---|
65 | DrawTriangle(a, ab, ac, div - 1, r); |
---|
66 | DrawTriangle(b, bc, ab, div - 1, r); |
---|
67 | DrawTriangle(c, ac, bc, div - 1, r); |
---|
68 | DrawTriangle(ab, bc, ac, div - 1, r); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void Normalize(GLfloat *a) |
---|
73 | { |
---|
74 | GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); |
---|
75 | a[0] *= d; a[1] *= d; a[2] *= d; |
---|
76 | } |
---|
77 | |
---|
78 | private: |
---|
79 | float time; |
---|
80 | |
---|
81 | static GLfloat const vdata[12][3]; |
---|
82 | static GLuint const tindices[20][3]; |
---|
83 | static GLfloat const X, Z; |
---|
84 | }; |
---|
85 | |
---|
86 | GLfloat const DebugSphereData::X = .525731112119133606; |
---|
87 | GLfloat const DebugSphereData::Z = .850650808352039932; |
---|
88 | |
---|
89 | GLfloat const DebugSphereData::vdata[12][3] = |
---|
90 | { |
---|
91 | {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z}, |
---|
92 | {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X}, |
---|
93 | {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} |
---|
94 | }; |
---|
95 | |
---|
96 | GLuint const DebugSphereData::tindices[20][3] = |
---|
97 | { |
---|
98 | {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1}, |
---|
99 | {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3}, |
---|
100 | {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, |
---|
101 | {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} |
---|
102 | }; |
---|
103 | |
---|
104 | /* |
---|
105 | * Public DebugSphere class |
---|
106 | */ |
---|
107 | |
---|
108 | DebugSphere::DebugSphere() |
---|
109 | { |
---|
110 | data = new DebugSphereData(); |
---|
111 | data->time = 0.0f; |
---|
112 | } |
---|
113 | |
---|
114 | void DebugSphere::TickGame(float deltams) |
---|
115 | { |
---|
116 | Entity::TickGame(deltams); |
---|
117 | |
---|
118 | data->time += 0.003f * deltams; |
---|
119 | while (data->time > 6.0 * M_PI) |
---|
120 | data->time -= 6.0 * M_PI; |
---|
121 | } |
---|
122 | |
---|
123 | void DebugSphere::TickDraw(float deltams) |
---|
124 | { |
---|
125 | Entity::TickDraw(deltams); |
---|
126 | |
---|
127 | float a = sinf(data->time); |
---|
128 | float b = sinf(data->time * 0.5f); |
---|
129 | |
---|
130 | glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT); |
---|
131 | glBindTexture(GL_TEXTURE_2D, NULL); |
---|
132 | glColor4f(1.0f, b, a, 0.1f); |
---|
133 | glTranslatef(320.0f, 240.0f, 32.0f); |
---|
134 | for (float t = 0.01f; t < 1.0f; t *= 1.1f) |
---|
135 | data->DrawSphere(2, t * (60.0f + 40.0f * a)); |
---|
136 | glTranslatef(-320.0f, -240.0f, -32.0f); |
---|
137 | glPopAttrib(); |
---|
138 | } |
---|
139 | |
---|
140 | DebugSphere::~DebugSphere() |
---|
141 | { |
---|
142 | delete data; |
---|
143 | } |
---|
144 | |
---|