1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #ifdef WIN32 |
---|
16 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
17 | # define WIN32_LEAN_AND_MEAN |
---|
18 | # include <windows.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <cstdio> |
---|
22 | #include <cmath> |
---|
23 | |
---|
24 | #include "core.h" |
---|
25 | #include "lolgl.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, float *&vertex, float *&normal) |
---|
37 | { |
---|
38 | for (int i = 0; i < 20; i++) |
---|
39 | DrawTriangle(vdata[tindices[i][0]], |
---|
40 | vdata[tindices[i][2]], |
---|
41 | vdata[tindices[i][1]], ndiv, r, vertex, normal); |
---|
42 | } |
---|
43 | |
---|
44 | void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c, |
---|
45 | int div, GLfloat r, float *&vertex, float *&normal) |
---|
46 | { |
---|
47 | if (div <= 0) |
---|
48 | { |
---|
49 | *normal++ = a[0]; *normal++ = a[1]; *normal++ = a[2]; |
---|
50 | *vertex++ = a[0] * r; *vertex++ = a[1] * r; *vertex++ = a[2] * r; |
---|
51 | *normal++ = b[0]; *normal++ = b[1]; *normal++ = b[2]; |
---|
52 | *vertex++ = b[0] * r; *vertex++ = b[1] * r; *vertex++ = b[2] * r; |
---|
53 | *normal++ = c[0]; *normal++ = c[1]; *normal++ = c[2]; |
---|
54 | *vertex++ = c[0] * r; *vertex++ = c[1] * r; *vertex++ = c[2] * r; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | GLfloat ab[3], ac[3], bc[3]; |
---|
59 | for (int i = 0; i < 3; i++) |
---|
60 | { |
---|
61 | ab[i] = (a[i] + b[i]) * 0.5; |
---|
62 | ac[i] = (a[i] + c[i]) * 0.5; |
---|
63 | bc[i] = (b[i] + c[i]) * 0.5; |
---|
64 | } |
---|
65 | Normalize(ab); Normalize(ac); Normalize(bc); |
---|
66 | DrawTriangle(a, ab, ac, div - 1, r, vertex, normal); |
---|
67 | DrawTriangle(b, bc, ab, div - 1, r, vertex, normal); |
---|
68 | DrawTriangle(c, ac, bc, div - 1, r, vertex, normal); |
---|
69 | DrawTriangle(ab, bc, ac, div - 1, r, vertex, normal); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void Normalize(GLfloat *a) |
---|
74 | { |
---|
75 | GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]); |
---|
76 | a[0] *= d; a[1] *= d; a[2] *= d; |
---|
77 | } |
---|
78 | |
---|
79 | private: |
---|
80 | float time; |
---|
81 | int initialised; |
---|
82 | GLuint buflist[2]; |
---|
83 | |
---|
84 | static GLfloat const vdata[12][3]; |
---|
85 | static GLuint const tindices[20][3]; |
---|
86 | static GLfloat const X, Z; |
---|
87 | }; |
---|
88 | |
---|
89 | GLfloat const DebugSphereData::X = .525731112119133606; |
---|
90 | GLfloat const DebugSphereData::Z = .850650808352039932; |
---|
91 | |
---|
92 | GLfloat const DebugSphereData::vdata[12][3] = |
---|
93 | { |
---|
94 | {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z}, |
---|
95 | {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X}, |
---|
96 | {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} |
---|
97 | }; |
---|
98 | |
---|
99 | GLuint const DebugSphereData::tindices[20][3] = |
---|
100 | { |
---|
101 | {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1}, |
---|
102 | {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3}, |
---|
103 | {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, |
---|
104 | {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} |
---|
105 | }; |
---|
106 | |
---|
107 | /* |
---|
108 | * Public DebugSphere class |
---|
109 | */ |
---|
110 | |
---|
111 | DebugSphere::DebugSphere() |
---|
112 | : data(new DebugSphereData()) |
---|
113 | { |
---|
114 | data->time = 0.0f; |
---|
115 | data->initialised = 0; |
---|
116 | } |
---|
117 | |
---|
118 | void DebugSphere::TickGame(float deltams) |
---|
119 | { |
---|
120 | Entity::TickGame(deltams); |
---|
121 | |
---|
122 | data->time += 0.003f * deltams; |
---|
123 | while (data->time > 6.0 * M_PI) |
---|
124 | data->time -= 6.0 * M_PI; |
---|
125 | } |
---|
126 | |
---|
127 | void DebugSphere::TickDraw(float deltams) |
---|
128 | { |
---|
129 | Entity::TickDraw(deltams); |
---|
130 | |
---|
131 | if (IsDestroying()) |
---|
132 | { |
---|
133 | if (data->initialised) |
---|
134 | { |
---|
135 | glDeleteBuffers(2, data->buflist); |
---|
136 | data->initialised = 0; |
---|
137 | } |
---|
138 | } |
---|
139 | else if (!data->initialised) |
---|
140 | { |
---|
141 | glGenBuffers(2, data->buflist); |
---|
142 | data->initialised = 1; |
---|
143 | } |
---|
144 | |
---|
145 | float a = sinf(data->time); |
---|
146 | #if 0 |
---|
147 | float b = sinf(data->time * 0.5f); |
---|
148 | #endif |
---|
149 | |
---|
150 | int const ndiv = 2; |
---|
151 | int const ntriangles = 20 * (1 << (ndiv * 2)) |
---|
152 | * (int)(log(1.0f / 0.01f) / log(1.1f) + 0.9999f); |
---|
153 | |
---|
154 | float *vertex = (float *)malloc(ntriangles * 3 * 3 * sizeof(float)); |
---|
155 | float *normal = (float *)malloc(ntriangles * 3 * 3 * sizeof(float)); |
---|
156 | |
---|
157 | float *vertex_parser = vertex; |
---|
158 | float *normal_parser = normal; |
---|
159 | for (float t = 0.01f; t < 1.0f; t *= 1.1f) |
---|
160 | data->DrawSphere(ndiv, t * (60.0f + 40.0f * a), |
---|
161 | vertex_parser, normal_parser); |
---|
162 | |
---|
163 | #if 0 // FIXME: does not work with GLES2 |
---|
164 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
165 | glEnableClientState(GL_NORMAL_ARRAY); |
---|
166 | |
---|
167 | glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]); |
---|
168 | glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float), |
---|
169 | vertex, GL_DYNAMIC_DRAW); |
---|
170 | glVertexPointer(3, GL_FLOAT, 0, NULL); |
---|
171 | |
---|
172 | glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]); |
---|
173 | glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float), |
---|
174 | normal, GL_DYNAMIC_DRAW); |
---|
175 | glNormalPointer(GL_FLOAT, 0, NULL); |
---|
176 | |
---|
177 | #if 0 |
---|
178 | glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT); |
---|
179 | #endif |
---|
180 | glColor4f(1.0f, b, a, 0.1f); |
---|
181 | glBindTexture(GL_TEXTURE_2D, NULL); |
---|
182 | |
---|
183 | glTranslatef(320.0f, 240.0f, 32.0f); |
---|
184 | glDrawArrays(GL_TRIANGLES, 0, ntriangles * 3); |
---|
185 | glTranslatef(-320.0f, -240.0f, -32.0f); |
---|
186 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); |
---|
187 | #if 0 |
---|
188 | glPopAttrib(); |
---|
189 | #endif |
---|
190 | |
---|
191 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
192 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
193 | #endif |
---|
194 | |
---|
195 | free(vertex); |
---|
196 | free(normal); |
---|
197 | } |
---|
198 | |
---|
199 | DebugSphere::~DebugSphere() |
---|
200 | { |
---|
201 | delete data; |
---|
202 | } |
---|
203 | |
---|