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