1 | // |
---|
2 | // Lol Engine - Fractal tutorial |
---|
3 | // |
---|
4 | // Copyright: (c) 2011-2012 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 | #include <cstring> |
---|
16 | |
---|
17 | #include "core.h" |
---|
18 | #include "lolgl.h" |
---|
19 | #include "loldebug.h" |
---|
20 | |
---|
21 | using namespace lol; |
---|
22 | |
---|
23 | #if defined _WIN32 |
---|
24 | # include <direct.h> |
---|
25 | # if defined USE_D3D9 |
---|
26 | # define FAR |
---|
27 | # define NEAR |
---|
28 | # include <d3d9.h> |
---|
29 | # endif |
---|
30 | #endif |
---|
31 | |
---|
32 | extern char const *lolfx_11_fractal; |
---|
33 | |
---|
34 | #if defined USE_D3D9 |
---|
35 | extern IDirect3DDevice9 *g_d3ddevice; |
---|
36 | #elif defined _XBOX |
---|
37 | extern D3DDevice *g_d3ddevice; |
---|
38 | #elif __CELLOS_LV2__ |
---|
39 | static GLint const INTERNAL_FORMAT = GL_ARGB_SCE; |
---|
40 | static GLenum const TEXTURE_FORMAT = GL_BGRA; |
---|
41 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV; |
---|
42 | #elif defined __native_client__ || defined HAVE_GLES_2X |
---|
43 | static GLint const INTERNAL_FORMAT = GL_RGBA; |
---|
44 | static GLenum const TEXTURE_FORMAT = GL_RGBA; |
---|
45 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE; |
---|
46 | #else |
---|
47 | /* Seems efficient for little endian textures */ |
---|
48 | static GLint const INTERNAL_FORMAT = GL_RGBA; |
---|
49 | static GLenum const TEXTURE_FORMAT = GL_BGRA; |
---|
50 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV; |
---|
51 | #endif |
---|
52 | |
---|
53 | class Fractal : public WorldEntity |
---|
54 | { |
---|
55 | public: |
---|
56 | Fractal(ivec2 const &size) |
---|
57 | { |
---|
58 | /* Ensure texture size is a multiple of 16 for better aligned |
---|
59 | * data access. Store the dimensions of a texel for our shader, |
---|
60 | * as well as the half-size of the screen. */ |
---|
61 | m_size = size; |
---|
62 | m_size.x = (m_size.x + 15) & ~15; |
---|
63 | m_size.y = (m_size.y + 15) & ~15; |
---|
64 | m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / m_size.xyxy; |
---|
65 | m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * m_size.xyxy; |
---|
66 | |
---|
67 | /* Window size decides the world aspect ratio. For instance, 640×480 |
---|
68 | * will be mapped to (-0.66,-0.5) - (0.66,0.5). */ |
---|
69 | #if !defined __native_client__ |
---|
70 | m_window_size = Video::GetSize(); |
---|
71 | #else |
---|
72 | /* FIXME: it's illegal to call this on the game thread! */ |
---|
73 | m_window_size = ivec2(640, 480); |
---|
74 | #endif |
---|
75 | if (m_window_size.y < m_window_size.x) |
---|
76 | m_window2world = 0.5 / m_window_size.y; |
---|
77 | else |
---|
78 | m_window2world = 0.5 / m_window_size.x; |
---|
79 | m_texel2world = (vec2)m_window_size / m_size * m_window2world; |
---|
80 | |
---|
81 | m_oldmouse = ivec2(0, 0); |
---|
82 | |
---|
83 | m_pixels = new u8vec4[m_size.x * m_size.y]; |
---|
84 | m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2]; |
---|
85 | m_frame = -1; |
---|
86 | m_slices = 4; |
---|
87 | for (int i = 0; i < 4; i++) |
---|
88 | { |
---|
89 | m_deltashift[i] = real("0"); |
---|
90 | m_deltascale[i] = real("1"); |
---|
91 | m_dirty[i] = 2; |
---|
92 | } |
---|
93 | #if defined __CELLOS_LV2__ || defined _XBOX |
---|
94 | //m_center = rcmplx(-.22815528839841, -1.11514249704382); |
---|
95 | //m_center = rcmplx(0.001643721971153, 0.822467633298876); |
---|
96 | m_center = rcmplx("-0.65823419062254", "0.50221777363480"); |
---|
97 | m_zoom_speed = -0.025; |
---|
98 | #else |
---|
99 | m_center = rcmplx(-0.75, 0.0); |
---|
100 | m_zoom_speed = 0.0; |
---|
101 | #endif |
---|
102 | m_translate = rcmplx(0.0, 0.0); |
---|
103 | m_radius = 5.0; |
---|
104 | m_ready = false; |
---|
105 | m_drag = false; |
---|
106 | |
---|
107 | m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP]; |
---|
108 | for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++) |
---|
109 | { |
---|
110 | double f = (double)i / PALETTE_STEP; |
---|
111 | |
---|
112 | double r = 0.5 * lol::sin(f * 0.27 + 2.0) + 0.5; |
---|
113 | double g = 0.5 * lol::sin(f * 0.17 - 1.8) + 0.5; |
---|
114 | double b = 0.5 * lol::sin(f * 0.21 - 2.6) + 0.5; |
---|
115 | |
---|
116 | if (f < 7.0) |
---|
117 | { |
---|
118 | f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0; |
---|
119 | r *= f; |
---|
120 | g *= f; |
---|
121 | b *= f; |
---|
122 | } |
---|
123 | |
---|
124 | uint8_t red = r * 255.99f; |
---|
125 | uint8_t green = g * 255.99f; |
---|
126 | uint8_t blue = b * 255.99f; |
---|
127 | #if defined __CELLOS_LV2__ || defined _XBOX |
---|
128 | m_palette[i] = u8vec4(255, red, green, blue); |
---|
129 | #elif defined __native_client__ |
---|
130 | m_palette[i] = u8vec4(red, green, blue, 255); |
---|
131 | #else |
---|
132 | m_palette[i] = u8vec4(blue, green, red, 255); |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | #if !defined __native_client__ |
---|
137 | m_centertext = new Text(NULL, "src/data/font/ascii.png"); |
---|
138 | m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1)); |
---|
139 | Ticker::Ref(m_centertext); |
---|
140 | |
---|
141 | m_mousetext = new Text(NULL, "src/data/font/ascii.png"); |
---|
142 | m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1)); |
---|
143 | Ticker::Ref(m_mousetext); |
---|
144 | |
---|
145 | m_zoomtext = new Text(NULL, "src/data/font/ascii.png"); |
---|
146 | m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1)); |
---|
147 | Ticker::Ref(m_zoomtext); |
---|
148 | #endif |
---|
149 | |
---|
150 | m_position = ivec3(0, 0, 0); |
---|
151 | m_bbox[0] = m_position; |
---|
152 | m_bbox[1] = ivec3(m_window_size, 0); |
---|
153 | Input::TrackMouse(this); |
---|
154 | |
---|
155 | /* Spawn worker threads and wait for their readiness. */ |
---|
156 | for (int i = 0; i < MAX_THREADS; i++) |
---|
157 | m_threads[i] = new Thread(DoWorkHelper, this); |
---|
158 | for (int i = 0; i < MAX_THREADS; i++) |
---|
159 | m_spawnqueue.Pop(); |
---|
160 | } |
---|
161 | |
---|
162 | ~Fractal() |
---|
163 | { |
---|
164 | /* Signal worker threads for completion and wait for |
---|
165 | * them to quit. */ |
---|
166 | for (int i = 0; i < MAX_THREADS; i++) |
---|
167 | m_jobqueue.Push(-1); |
---|
168 | for (int i = 0; i < MAX_THREADS; i++) |
---|
169 | m_donequeue.Pop(); |
---|
170 | |
---|
171 | Input::UntrackMouse(this); |
---|
172 | #if !defined __native_client__ |
---|
173 | Ticker::Unref(m_centertext); |
---|
174 | Ticker::Unref(m_mousetext); |
---|
175 | Ticker::Unref(m_zoomtext); |
---|
176 | #endif |
---|
177 | delete m_pixels; |
---|
178 | delete m_tmppixels; |
---|
179 | delete m_palette; |
---|
180 | } |
---|
181 | |
---|
182 | inline dcmplx TexelToWorldOffset(vec2 texel) |
---|
183 | { |
---|
184 | double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x; |
---|
185 | double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y; |
---|
186 | return m_radius * dcmplx(dx, dy); |
---|
187 | } |
---|
188 | |
---|
189 | inline dcmplx ScreenToWorldOffset(vec2 pixel) |
---|
190 | { |
---|
191 | /* No 0.5 offset here, because we want to be able to position the |
---|
192 | * mouse at (0,0) exactly. */ |
---|
193 | double dx = pixel.x - m_window_size.x / 2; |
---|
194 | double dy = m_window_size.y / 2 - pixel.y; |
---|
195 | return m_radius * m_window2world * dcmplx(dx, dy); |
---|
196 | } |
---|
197 | |
---|
198 | virtual void TickGame(float seconds) |
---|
199 | { |
---|
200 | WorldEntity::TickGame(seconds); |
---|
201 | |
---|
202 | int prev_frame = (m_frame + 4) % 4; |
---|
203 | m_frame = (m_frame + 1) % 4; |
---|
204 | |
---|
205 | rcmplx worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos)); |
---|
206 | |
---|
207 | ivec3 buttons = Input::GetMouseButtons(); |
---|
208 | #if !defined __CELLOS_LV2__ && !defined _XBOX |
---|
209 | if (buttons[1]) |
---|
210 | { |
---|
211 | if (!m_drag) |
---|
212 | { |
---|
213 | m_oldmouse = m_mousepos; |
---|
214 | m_drag = true; |
---|
215 | } |
---|
216 | m_translate = ScreenToWorldOffset(m_oldmouse) |
---|
217 | - ScreenToWorldOffset(m_mousepos); |
---|
218 | /* XXX: the purpose of this hack is to avoid translating by |
---|
219 | * an exact number of pixels. If this were to happen, the step() |
---|
220 | * optimisation for i915 cards in our shader would behave |
---|
221 | * incorrectly because a quarter of the pixels in the image |
---|
222 | * would have tie rankings in the distance calculation. */ |
---|
223 | m_translate *= real(1023.0 / 1024.0); |
---|
224 | m_oldmouse = m_mousepos; |
---|
225 | } |
---|
226 | else |
---|
227 | { |
---|
228 | m_drag = false; |
---|
229 | if (m_translate != rcmplx(0.0, 0.0)) |
---|
230 | { |
---|
231 | m_translate *= real(std::pow(2.0, -seconds * 5.0)); |
---|
232 | if ((double)m_translate.norm() < m_radius * 1e-4) |
---|
233 | m_translate = rcmplx(0.0, 0.0); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | if ((buttons[0] || buttons[2]) && m_mousepos.x != -1) |
---|
238 | { |
---|
239 | double zoom = buttons[0] ? -0.5 : 0.5; |
---|
240 | m_zoom_speed += zoom * seconds; |
---|
241 | if (m_zoom_speed / zoom > 5e-3f) |
---|
242 | m_zoom_speed = zoom * 5e-3f; |
---|
243 | } |
---|
244 | else if (m_zoom_speed) |
---|
245 | { |
---|
246 | m_zoom_speed *= std::pow(2.0, -seconds * 5.0); |
---|
247 | if (abs(m_zoom_speed) < 1e-5 || m_drag) |
---|
248 | m_zoom_speed = 0.0; |
---|
249 | } |
---|
250 | #endif |
---|
251 | |
---|
252 | if (m_zoom_speed || m_translate != rcmplx(0.0, 0.0)) |
---|
253 | { |
---|
254 | rcmplx oldcenter = m_center; |
---|
255 | double oldradius = m_radius; |
---|
256 | double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed); |
---|
257 | if (m_radius * zoom > 8.0) |
---|
258 | { |
---|
259 | m_zoom_speed *= -1.0; |
---|
260 | zoom = 8.0 / m_radius; |
---|
261 | } |
---|
262 | else if (m_radius * zoom < 1e-14) |
---|
263 | { |
---|
264 | m_zoom_speed *= -1.0; |
---|
265 | zoom = 1e-14 / m_radius; |
---|
266 | } |
---|
267 | m_radius *= zoom; |
---|
268 | #if !defined __CELLOS_LV2__ && !defined _XBOX |
---|
269 | m_center += m_translate; |
---|
270 | m_center = (m_center - worldmouse) * real(zoom) + worldmouse; |
---|
271 | worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos)); |
---|
272 | #endif |
---|
273 | |
---|
274 | /* Store the transformation properties to go from m_frame - 1 |
---|
275 | * to m_frame. */ |
---|
276 | m_deltashift[prev_frame] = (m_center - oldcenter) / real(oldradius); |
---|
277 | m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x; |
---|
278 | m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y; |
---|
279 | m_deltascale[prev_frame] = m_radius / oldradius; |
---|
280 | m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2; |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | /* If settings didn't change, set transformation from previous |
---|
285 | * frame to identity. */ |
---|
286 | m_deltashift[prev_frame] = real::R_0; |
---|
287 | m_deltascale[prev_frame] = real::R_1; |
---|
288 | } |
---|
289 | |
---|
290 | /* Transformation from current frame to current frame is always |
---|
291 | * identity. */ |
---|
292 | m_zoom_settings[m_frame][0] = 0.0f; |
---|
293 | m_zoom_settings[m_frame][1] = 0.0f; |
---|
294 | m_zoom_settings[m_frame][2] = 1.0f; |
---|
295 | |
---|
296 | /* Compute transformation from other frames to current frame */ |
---|
297 | for (int i = 0; i < 3; i++) |
---|
298 | { |
---|
299 | int prev_index = (m_frame + 4 - i) % 4; |
---|
300 | int cur_index = (m_frame + 3 - i) % 4; |
---|
301 | |
---|
302 | m_zoom_settings[cur_index][0] = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x; |
---|
303 | m_zoom_settings[cur_index][1] = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y; |
---|
304 | m_zoom_settings[cur_index][2] = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index]; |
---|
305 | } |
---|
306 | |
---|
307 | /* Precompute texture offset change instead of doing it in GLSL */ |
---|
308 | for (int i = 0; i < 4; i++) |
---|
309 | { |
---|
310 | m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]); |
---|
311 | m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]); |
---|
312 | } |
---|
313 | |
---|
314 | #if !defined __native_client__ |
---|
315 | char buf[256]; |
---|
316 | sprintf(buf, "center: "); |
---|
317 | m_center.x.sprintf(buf + strlen(buf), 30); |
---|
318 | sprintf(buf + strlen(buf), " "); |
---|
319 | m_center.y.sprintf(buf + strlen(buf), 30); |
---|
320 | m_centertext->SetText(buf); |
---|
321 | sprintf(buf, " mouse: "); |
---|
322 | worldmouse.x.sprintf(buf + strlen(buf), 30); |
---|
323 | sprintf(buf + strlen(buf), " "); |
---|
324 | worldmouse.y.sprintf(buf + strlen(buf), 30); |
---|
325 | m_mousetext->SetText(buf); |
---|
326 | sprintf(buf, " zoom: %g", 1.0 / m_radius); |
---|
327 | m_zoomtext->SetText(buf); |
---|
328 | #endif |
---|
329 | |
---|
330 | if (m_dirty[m_frame]) |
---|
331 | { |
---|
332 | m_dirty[m_frame]--; |
---|
333 | |
---|
334 | for (int i = 0; i < m_size.y; i += MAX_LINES * 2) |
---|
335 | m_jobqueue.Push(i); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | static void *DoWorkHelper(void *data) |
---|
340 | { |
---|
341 | Fractal *that = (Fractal *)data; |
---|
342 | that->m_spawnqueue.Push(0); |
---|
343 | for ( ; ; ) |
---|
344 | { |
---|
345 | int line = that->m_jobqueue.Pop(); |
---|
346 | if (line == -1) |
---|
347 | break; |
---|
348 | that->DoWork(line); |
---|
349 | that->m_donequeue.Push(0); |
---|
350 | } |
---|
351 | that->m_donequeue.Push(0); |
---|
352 | return NULL; |
---|
353 | }; |
---|
354 | |
---|
355 | void DoWork(int line) |
---|
356 | { |
---|
357 | double const maxsqlen = 1024; |
---|
358 | double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0)); |
---|
359 | |
---|
360 | int jmin = ((m_frame + 1) % 4) / 2 + line; |
---|
361 | int jmax = jmin + MAX_LINES * 2; |
---|
362 | if (jmax > m_size.y) |
---|
363 | jmax = m_size.y; |
---|
364 | u8vec4 *m_pixelstart = m_pixels |
---|
365 | + m_size.x * (m_size.y / 4 * m_frame + line / 4); |
---|
366 | |
---|
367 | dcmplx c = (dcmplx)m_center; |
---|
368 | |
---|
369 | for (int j = jmin; j < jmax; j += 2) |
---|
370 | for (int i = m_frame % 2; i < m_size.x; i += 2) |
---|
371 | { |
---|
372 | dcmplx z0 = c + TexelToWorldOffset(ivec2(i, j)); |
---|
373 | dcmplx z1, z2, z3, r0 = z0; |
---|
374 | //dcmplx r0(0.28693186889504513, 0.014286693904085048); |
---|
375 | //dcmplx r0(0.001643721971153, 0.822467633298876); |
---|
376 | //dcmplx r0(-1.207205434596, 0.315432814901); |
---|
377 | //dcmplx r0(-0.79192956889854, -0.14632423080102); |
---|
378 | //dcmplx r0(0.3245046418497685, 0.04855101129280834); |
---|
379 | int iter = MAX_ITERATIONS - 4; |
---|
380 | for (;;) |
---|
381 | { |
---|
382 | /* Unroll the loop: tests are more expensive to do at each |
---|
383 | * iteration than the few extra multiplications. */ |
---|
384 | z1 = z0 * z0 + r0; |
---|
385 | z2 = z1 * z1 + r0; |
---|
386 | z3 = z2 * z2 + r0; |
---|
387 | z0 = z3 * z3 + r0; |
---|
388 | if (sqlength(z0) >= maxsqlen) |
---|
389 | break; |
---|
390 | iter -= 4; |
---|
391 | if (iter < 4) |
---|
392 | break; |
---|
393 | } |
---|
394 | |
---|
395 | if (iter) |
---|
396 | { |
---|
397 | double n = sqlength(z0); |
---|
398 | |
---|
399 | if (sqlength(z1) >= maxsqlen) { iter += 3; n = sqlength(z1); } |
---|
400 | else if (sqlength(z2) >= maxsqlen) { iter += 2; n = sqlength(z2); } |
---|
401 | else if (sqlength(z3) >= maxsqlen) { iter += 1; n = sqlength(z3); } |
---|
402 | |
---|
403 | if (n > maxsqlen * maxsqlen) |
---|
404 | n = maxsqlen * maxsqlen; |
---|
405 | |
---|
406 | /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */ |
---|
407 | double f = iter; |
---|
408 | union { double n; uint64_t x; } u = { n }; |
---|
409 | double k = (u.x >> 42) - (((1 << 10) - 1) << 10); |
---|
410 | k *= k1; |
---|
411 | |
---|
412 | /* Approximate log2(k) in [1,2]. */ |
---|
413 | f += (- 0.344847817623168308695977510213252644185 * k |
---|
414 | + 2.024664188044341212602376988171727038739) * k |
---|
415 | - 1.674876738008591047163498125918330313237; |
---|
416 | |
---|
417 | *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)]; |
---|
418 | } |
---|
419 | else |
---|
420 | { |
---|
421 | #if defined __CELLOS_LV2__ || defined _XBOX |
---|
422 | *m_pixelstart++ = u8vec4(255, 0, 0, 0); |
---|
423 | #else |
---|
424 | *m_pixelstart++ = u8vec4(0, 0, 0, 255); |
---|
425 | #endif |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | virtual void TickDraw(float seconds) |
---|
431 | { |
---|
432 | WorldEntity::TickDraw(seconds); |
---|
433 | |
---|
434 | static float const vertices[] = |
---|
435 | { |
---|
436 | 1.0f, 1.0f, |
---|
437 | -1.0f, 1.0f, |
---|
438 | -1.0f, -1.0f, |
---|
439 | -1.0f, -1.0f, |
---|
440 | 1.0f, -1.0f, |
---|
441 | 1.0f, 1.0f, |
---|
442 | }; |
---|
443 | |
---|
444 | static float const texcoords[] = |
---|
445 | { |
---|
446 | 1.0f, 1.0f, |
---|
447 | 0.0f, 1.0f, |
---|
448 | 0.0f, 0.0f, |
---|
449 | 0.0f, 0.0f, |
---|
450 | 1.0f, 0.0f, |
---|
451 | 1.0f, 1.0f, |
---|
452 | }; |
---|
453 | |
---|
454 | if (!m_ready) |
---|
455 | { |
---|
456 | #if !defined _XBOX && !defined USE_D3D9 |
---|
457 | /* Create a texture of half the width and twice the height |
---|
458 | * so that we can upload four different subimages each frame. */ |
---|
459 | glGenTextures(1, &m_texid); |
---|
460 | glBindTexture(GL_TEXTURE_2D, m_texid); |
---|
461 | glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, |
---|
462 | m_size.x / 2, m_size.y * 2, 0, |
---|
463 | TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels); |
---|
464 | # if defined __CELLOS_LV2__ |
---|
465 | /* We need this hint because by default the storage type is |
---|
466 | * GL_TEXTURE_SWIZZLED_GPU_SCE. */ |
---|
467 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE, |
---|
468 | GL_TEXTURE_TILED_GPU_SCE); |
---|
469 | # endif |
---|
470 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
471 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
472 | #elif defined _XBOX |
---|
473 | /* By default the X360 will swizzle the texture. Ask for linear. */ |
---|
474 | g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1, |
---|
475 | D3DUSAGE_WRITEONLY, D3DFMT_LIN_A8R8G8B8, |
---|
476 | D3DPOOL_DEFAULT, &m_tex, NULL); |
---|
477 | #else |
---|
478 | g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1, |
---|
479 | D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, |
---|
480 | D3DPOOL_SYSTEMMEM, &m_tex, NULL); |
---|
481 | #endif |
---|
482 | |
---|
483 | m_shader = Shader::Create(lolfx_11_fractal); |
---|
484 | |
---|
485 | m_vertexattrib = m_shader->GetAttribLocation("a_Vertex", VertexUsage::Position, 0); |
---|
486 | m_texattrib = m_shader->GetAttribLocation("a_TexCoord", VertexUsage::TexCoord, 0); |
---|
487 | m_texeluni = m_shader->GetUniformLocation("u_TexelSize"); |
---|
488 | m_screenuni = m_shader->GetUniformLocation("u_ScreenSize"); |
---|
489 | m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings"); |
---|
490 | |
---|
491 | m_vdecl = |
---|
492 | new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position), |
---|
493 | VertexStream<vec2>(VertexUsage::TexCoord)); |
---|
494 | m_vbo = new VertexBuffer(sizeof(vertices)); |
---|
495 | m_tbo = new VertexBuffer(sizeof(texcoords)); |
---|
496 | |
---|
497 | void *tmp = m_vbo->Lock(0, 0); |
---|
498 | memcpy(tmp, vertices, sizeof(vertices)); |
---|
499 | m_vbo->Unlock(); |
---|
500 | |
---|
501 | tmp = m_tbo->Lock(0, 0); |
---|
502 | memcpy(tmp, texcoords, sizeof(texcoords)); |
---|
503 | m_tbo->Unlock(); |
---|
504 | |
---|
505 | /* FIXME: this object never cleans up */ |
---|
506 | m_ready = true; |
---|
507 | } |
---|
508 | |
---|
509 | #if defined _XBOX || defined USE_D3D9 |
---|
510 | |
---|
511 | #else |
---|
512 | # if !defined HAVE_GLES_2X |
---|
513 | glEnable(GL_TEXTURE_2D); |
---|
514 | # endif |
---|
515 | glBindTexture(GL_TEXTURE_2D, m_texid); |
---|
516 | #endif |
---|
517 | |
---|
518 | if (m_dirty[m_frame]) |
---|
519 | { |
---|
520 | for (int i = 0; i < m_size.y; i += MAX_LINES * 2) |
---|
521 | m_donequeue.Pop(); |
---|
522 | |
---|
523 | m_dirty[m_frame]--; |
---|
524 | |
---|
525 | #if defined _XBOX || defined USE_D3D9 |
---|
526 | D3DLOCKED_RECT rect; |
---|
527 | # if defined _XBOX |
---|
528 | m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE); |
---|
529 | # else |
---|
530 | m_tex->LockRect(0, &rect, NULL, |
---|
531 | D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE); |
---|
532 | # endif |
---|
533 | for (int j = 0; j < m_size.y * 2; j++) |
---|
534 | { |
---|
535 | u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4; |
---|
536 | for (int i = 0; i < m_size.x / 2; i++) |
---|
537 | line[i] = m_pixels[m_size.x / 2 * j + i]; |
---|
538 | } |
---|
539 | m_tex->UnlockRect(0); |
---|
540 | #elif defined __CELLOS_LV2__ |
---|
541 | /* glTexSubImage2D is extremely slow on the PS3, to the point |
---|
542 | * that uploading the whole texture is 40 times faster. */ |
---|
543 | glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, |
---|
544 | m_size.x / 2, m_size.y * 2, 0, |
---|
545 | TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels); |
---|
546 | #else |
---|
547 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2, |
---|
548 | m_size.x / 2, m_size.y / 2, |
---|
549 | TEXTURE_FORMAT, TEXTURE_TYPE, |
---|
550 | m_pixels + m_size.x * m_size.y / 4 * m_frame); |
---|
551 | #endif |
---|
552 | } |
---|
553 | |
---|
554 | m_shader->Bind(); |
---|
555 | m_shader->SetUniform(m_texeluni, m_texel_settings); |
---|
556 | m_shader->SetUniform(m_screenuni, m_screen_settings); |
---|
557 | m_shader->SetUniform(m_zoomuni, m_zoom_settings); |
---|
558 | m_vdecl->Bind(); |
---|
559 | m_vdecl->SetStream(m_vbo, m_vertexattrib); |
---|
560 | m_vdecl->SetStream(m_tbo, m_texattrib); |
---|
561 | #if defined _XBOX || defined USE_D3D9 |
---|
562 | g_d3ddevice->SetTexture(0, m_tex); |
---|
563 | g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); |
---|
564 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
565 | #else |
---|
566 | #endif |
---|
567 | m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2); |
---|
568 | m_vdecl->Unbind(); |
---|
569 | } |
---|
570 | |
---|
571 | private: |
---|
572 | static int const MAX_ITERATIONS = 340; |
---|
573 | static int const PALETTE_STEP = 32; |
---|
574 | static int const MAX_THREADS = 8; |
---|
575 | static int const MAX_LINES = 8; |
---|
576 | |
---|
577 | ivec2 m_size, m_window_size, m_oldmouse; |
---|
578 | double m_window2world; |
---|
579 | dvec2 m_texel2world; |
---|
580 | u8vec4 *m_pixels, *m_tmppixels, *m_palette; |
---|
581 | |
---|
582 | Shader *m_shader; |
---|
583 | ShaderAttrib m_vertexattrib, m_texattrib; |
---|
584 | ShaderUniform m_texeluni, m_screenuni, m_zoomuni; |
---|
585 | |
---|
586 | VertexDeclaration *m_vdecl; |
---|
587 | VertexBuffer *m_vbo, *m_tbo; |
---|
588 | #if defined USE_D3D9 |
---|
589 | IDirect3DTexture9 *m_tex; |
---|
590 | #elif defined _XBOX |
---|
591 | D3DTexture *m_tex; |
---|
592 | #else |
---|
593 | GLuint m_texid; |
---|
594 | #endif |
---|
595 | int m_frame, m_slices, m_dirty[4]; |
---|
596 | bool m_ready, m_drag; |
---|
597 | |
---|
598 | rcmplx m_deltashift[4], m_center, m_translate; |
---|
599 | real m_deltascale[4]; |
---|
600 | double m_zoom_speed, m_radius; |
---|
601 | |
---|
602 | vec4 m_texel_settings, m_screen_settings; |
---|
603 | mat4 m_zoom_settings; |
---|
604 | |
---|
605 | /* Worker threads */ |
---|
606 | Thread *m_threads[MAX_THREADS]; |
---|
607 | Queue<int> m_spawnqueue, m_jobqueue, m_donequeue; |
---|
608 | |
---|
609 | /* Debug information */ |
---|
610 | #if !defined __native_client__ |
---|
611 | Text *m_centertext, *m_mousetext, *m_zoomtext; |
---|
612 | #endif |
---|
613 | }; |
---|
614 | |
---|
615 | int main(int argc, char **argv) |
---|
616 | { |
---|
617 | Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f); |
---|
618 | |
---|
619 | #if defined _MSC_VER && !defined _XBOX |
---|
620 | _chdir(".."); |
---|
621 | #elif defined _WIN32 && !defined _XBOX |
---|
622 | _chdir("../.."); |
---|
623 | #endif |
---|
624 | |
---|
625 | new DebugFps(5, 5); |
---|
626 | new Fractal(ivec2(640, 480)); |
---|
627 | //new DebugRecord("fractalol.ogm", 60.0f); |
---|
628 | |
---|
629 | app.Run(); |
---|
630 | |
---|
631 | return EXIT_SUCCESS; |
---|
632 | } |
---|
633 | |
---|