1 | // |
---|
2 | // Lol Engine - Fractal tutorial |
---|
3 | // |
---|
4 | // Copyright: (c) 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 | #include <cstring> |
---|
16 | |
---|
17 | #include "core.h" |
---|
18 | #include "lolgl.h" |
---|
19 | #include "loldebug.h" |
---|
20 | |
---|
21 | using namespace std; |
---|
22 | using namespace lol; |
---|
23 | |
---|
24 | #if USE_SDL && defined __APPLE__ |
---|
25 | # include <SDL_main.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #if defined _WIN32 |
---|
29 | # undef main /* FIXME: still needed? */ |
---|
30 | # include <direct.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #ifdef __CELLOS_LV2__ |
---|
34 | static GLint const INTERNAL_FORMAT = GL_ARGB_SCE; |
---|
35 | static GLenum const TEXTURE_FORMAT = GL_BGRA; |
---|
36 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV; |
---|
37 | #elif defined __native_client__ |
---|
38 | static GLint const INTERNAL_FORMAT = GL_RGBA; |
---|
39 | static GLenum const TEXTURE_FORMAT = GL_RGBA; |
---|
40 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE; |
---|
41 | #else |
---|
42 | /* Seems efficient for little endian textures */ |
---|
43 | static GLint const INTERNAL_FORMAT = GL_RGBA; |
---|
44 | static GLenum const TEXTURE_FORMAT = GL_BGRA; |
---|
45 | static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV; |
---|
46 | #endif |
---|
47 | |
---|
48 | class Fractal : public WorldEntity |
---|
49 | { |
---|
50 | public: |
---|
51 | Fractal(ivec2 const &size) |
---|
52 | { |
---|
53 | /* Ensure texture size is a multiple of 16 for better aligned |
---|
54 | * data access. Store the dimensions of a texel for our shader, |
---|
55 | * as well as the half-size of the screen. */ |
---|
56 | m_size = size; |
---|
57 | m_size.x = (m_size.x + 15) & ~15; |
---|
58 | m_size.y = (m_size.y + 15) & ~15; |
---|
59 | m_texel_settings = vec4(vec2(1.0, 1.0) / (vec2)m_size, |
---|
60 | vec2(0.5, 0.5) * (vec2)m_size); |
---|
61 | |
---|
62 | /* Window size decides the world aspect ratio. For instance, 640×480 |
---|
63 | * will be mapped to (-0.66,-0.5) - (0.66,0.5). */ |
---|
64 | #if !defined __native_client__ |
---|
65 | m_window_size = Video::GetSize(); |
---|
66 | #else |
---|
67 | /* FIXME: it's illegal to call this on the game thread! */ |
---|
68 | m_window_size = ivec2(640, 480); |
---|
69 | #endif |
---|
70 | if (m_window_size.y < m_window_size.x) |
---|
71 | m_window2world = 0.5 / m_window_size.y; |
---|
72 | else |
---|
73 | m_window2world = 0.5 / m_window_size.x; |
---|
74 | m_texel2world = (vec2)m_window_size / (vec2)m_size * m_window2world; |
---|
75 | |
---|
76 | m_oldmouse = ivec2(0, 0); |
---|
77 | |
---|
78 | m_pixels = new u8vec4[m_size.x * m_size.y]; |
---|
79 | m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2]; |
---|
80 | m_frame = -1; |
---|
81 | for (int i = 0; i < 4; i++) |
---|
82 | { |
---|
83 | m_deltashift[i] = 0.0; |
---|
84 | m_deltascale[i] = 1.0; |
---|
85 | m_dirty[i] = 2; |
---|
86 | } |
---|
87 | #if defined __CELLOS_LV2__ |
---|
88 | //m_center = f64cmplx(-.22815528839841, -1.11514249704382); |
---|
89 | //m_center = f64cmplx(0.001643721971153, 0.822467633298876); |
---|
90 | m_center = f64cmplx(-0.65823419062254, .50221777363480); |
---|
91 | m_zoom_speed = 0;//-0.0025; |
---|
92 | #else |
---|
93 | m_center = -0.75; |
---|
94 | m_zoom_speed = 0.0; |
---|
95 | #endif |
---|
96 | m_translate = 0; |
---|
97 | m_radius = 5.0; |
---|
98 | m_ready = false; |
---|
99 | |
---|
100 | m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP]; |
---|
101 | for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++) |
---|
102 | { |
---|
103 | double f = (double)i / PALETTE_STEP; |
---|
104 | |
---|
105 | double r = 0.5 * sin(f * 0.27 - 2.5) + 0.5; |
---|
106 | double g = 0.5 * sin(f * 0.13 + 1.1) + 0.5; |
---|
107 | double b = 0.5 * sin(f * 0.21 + 0.4) + 0.5; |
---|
108 | |
---|
109 | if (f < 7.0) |
---|
110 | { |
---|
111 | f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0; |
---|
112 | r *= f; |
---|
113 | g *= f; |
---|
114 | b *= f; |
---|
115 | } |
---|
116 | |
---|
117 | uint8_t red = r * 255.99f; |
---|
118 | uint8_t green = g * 255.99f; |
---|
119 | uint8_t blue = b * 255.99f; |
---|
120 | #if defined __native_client__ |
---|
121 | m_palette[i] = u8vec4(red, green, blue, 255); |
---|
122 | #else |
---|
123 | m_palette[i] = u8vec4(blue, green, red, 255); |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | #if !defined __native_client__ |
---|
128 | m_centertext = new Text(NULL, "gfx/font/ascii.png"); |
---|
129 | m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1)); |
---|
130 | Ticker::Ref(m_centertext); |
---|
131 | |
---|
132 | m_mousetext = new Text(NULL, "gfx/font/ascii.png"); |
---|
133 | m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1)); |
---|
134 | Ticker::Ref(m_mousetext); |
---|
135 | |
---|
136 | m_zoomtext = new Text(NULL, "gfx/font/ascii.png"); |
---|
137 | m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1)); |
---|
138 | Ticker::Ref(m_zoomtext); |
---|
139 | #endif |
---|
140 | |
---|
141 | position = ivec3(0, 0, 0); |
---|
142 | bbox[0] = position; |
---|
143 | bbox[1] = ivec3(m_window_size, 0); |
---|
144 | Input::TrackMouse(this); |
---|
145 | } |
---|
146 | |
---|
147 | ~Fractal() |
---|
148 | { |
---|
149 | Input::UntrackMouse(this); |
---|
150 | #if !defined __native_client__ |
---|
151 | Ticker::Unref(m_centertext); |
---|
152 | Ticker::Unref(m_mousetext); |
---|
153 | Ticker::Unref(m_zoomtext); |
---|
154 | #endif |
---|
155 | delete m_pixels; |
---|
156 | delete m_tmppixels; |
---|
157 | delete m_palette; |
---|
158 | } |
---|
159 | |
---|
160 | inline f64cmplx TexelToWorldOffset(ivec2 texel) |
---|
161 | { |
---|
162 | double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x; |
---|
163 | double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y; |
---|
164 | return m_radius * f64cmplx(dx, dy); |
---|
165 | } |
---|
166 | |
---|
167 | inline f64cmplx ScreenToWorldOffset(ivec2 pixel) |
---|
168 | { |
---|
169 | /* No 0.5 offset here, because we want to be able to position the |
---|
170 | * mouse at (0,0) exactly. */ |
---|
171 | double dx = pixel.x - m_window_size.x / 2; |
---|
172 | double dy = m_window_size.y / 2 - pixel.y; |
---|
173 | return m_radius * m_window2world * f64cmplx(dx, dy); |
---|
174 | } |
---|
175 | |
---|
176 | virtual void TickGame(float deltams) |
---|
177 | { |
---|
178 | WorldEntity::TickGame(deltams); |
---|
179 | |
---|
180 | int prev_frame = m_frame; |
---|
181 | m_frame = (m_frame + 1) % 4; |
---|
182 | |
---|
183 | f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos); |
---|
184 | |
---|
185 | ivec3 buttons = Input::GetMouseButtons(); |
---|
186 | #if !defined __CELLOS_LV2__ |
---|
187 | if (buttons[1]) |
---|
188 | { |
---|
189 | if (clicked[1]) |
---|
190 | m_oldmouse = mousepos; |
---|
191 | m_translate = ScreenToWorldOffset(m_oldmouse) |
---|
192 | - ScreenToWorldOffset(mousepos); |
---|
193 | m_oldmouse = mousepos; |
---|
194 | } |
---|
195 | else if (m_translate != 0.0) |
---|
196 | { |
---|
197 | m_translate *= pow(2.0, -deltams * 0.005); |
---|
198 | if (m_translate.norm() / m_radius < 1e-4) |
---|
199 | m_translate = 0.0; |
---|
200 | } |
---|
201 | |
---|
202 | if ((buttons[0] || buttons[2]) && mousepos.x != -1) |
---|
203 | { |
---|
204 | double zoom = buttons[0] ? -0.0005 : 0.0005; |
---|
205 | m_zoom_speed += deltams * zoom; |
---|
206 | if (m_zoom_speed / zoom > 5) |
---|
207 | m_zoom_speed = 5 * zoom; |
---|
208 | } |
---|
209 | else if (m_zoom_speed) |
---|
210 | { |
---|
211 | m_zoom_speed *= pow(2.0, -deltams * 0.005); |
---|
212 | if (abs(m_zoom_speed) < 1e-5) |
---|
213 | m_zoom_speed = 0.0; |
---|
214 | } |
---|
215 | #endif |
---|
216 | |
---|
217 | if (m_zoom_speed || m_translate != 0.0) |
---|
218 | { |
---|
219 | f64cmplx oldcenter = m_center; |
---|
220 | double oldradius = m_radius; |
---|
221 | double zoom = pow(2.0, deltams * m_zoom_speed); |
---|
222 | if (m_radius * zoom > 8.0) |
---|
223 | { |
---|
224 | m_zoom_speed *= -1.0; |
---|
225 | zoom = 8.0 / m_radius; |
---|
226 | } |
---|
227 | else if (m_radius * zoom < 1e-14) |
---|
228 | { |
---|
229 | m_zoom_speed *= -1.0; |
---|
230 | zoom = 1e-14 / m_radius; |
---|
231 | } |
---|
232 | m_radius *= zoom; |
---|
233 | #if !defined __CELLOS_LV2__ |
---|
234 | m_center += m_translate; |
---|
235 | m_center = (m_center - worldmouse) * zoom + worldmouse; |
---|
236 | worldmouse = m_center + ScreenToWorldOffset(mousepos); |
---|
237 | #endif |
---|
238 | |
---|
239 | /* Store the transformation properties to go from m_frame - 1 |
---|
240 | * to m_frame. */ |
---|
241 | m_deltashift[prev_frame] = (m_center - oldcenter) / oldradius; |
---|
242 | m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x; |
---|
243 | m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y; |
---|
244 | m_deltascale[prev_frame] = m_radius / oldradius; |
---|
245 | m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2; |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | /* If settings didn't change, set transformation from previous |
---|
250 | * frame to identity. */ |
---|
251 | m_deltashift[prev_frame] = 0.0; |
---|
252 | m_deltascale[prev_frame] = 1.0; |
---|
253 | } |
---|
254 | |
---|
255 | /* Transformation from current frame to current frame is always |
---|
256 | * identity. */ |
---|
257 | m_zoom_settings[m_frame][0] = 0.0f; |
---|
258 | m_zoom_settings[m_frame][1] = 0.0f; |
---|
259 | m_zoom_settings[m_frame][2] = 1.0f; |
---|
260 | |
---|
261 | /* Compute transformation from other frames to current frame */ |
---|
262 | for (int i = 0; i < 3; i++) |
---|
263 | { |
---|
264 | int prev_index = (m_frame + 4 - i) % 4; |
---|
265 | int cur_index = (m_frame + 3 - i) % 4; |
---|
266 | |
---|
267 | m_zoom_settings[cur_index][0] = m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x; |
---|
268 | m_zoom_settings[cur_index][1] = m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y; |
---|
269 | m_zoom_settings[cur_index][2] = m_zoom_settings[prev_index][2] * m_deltascale[cur_index]; |
---|
270 | } |
---|
271 | |
---|
272 | /* Precompute texture offset change instead of doing it in GLSL */ |
---|
273 | for (int i = 0; i < 4; i++) |
---|
274 | { |
---|
275 | m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]); |
---|
276 | m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]); |
---|
277 | } |
---|
278 | |
---|
279 | #if !defined __native_client__ |
---|
280 | char buf[128]; |
---|
281 | sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y); |
---|
282 | m_centertext->SetText(buf); |
---|
283 | sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y); |
---|
284 | m_mousetext->SetText(buf); |
---|
285 | sprintf(buf, " zoom: %g", 1.0 / m_radius); |
---|
286 | m_zoomtext->SetText(buf); |
---|
287 | #endif |
---|
288 | |
---|
289 | u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame; |
---|
290 | |
---|
291 | if (m_dirty[m_frame]) |
---|
292 | { |
---|
293 | double const maxsqlen = 1024; |
---|
294 | double const k1 = 1.0 / (1 << 10) / log2(maxsqlen); |
---|
295 | |
---|
296 | m_dirty[m_frame]--; |
---|
297 | |
---|
298 | for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2) |
---|
299 | for (int i = m_frame % 2; i < m_size.x; i += 2) |
---|
300 | { |
---|
301 | |
---|
302 | f64cmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j)); |
---|
303 | f64cmplx r0 = z0; |
---|
304 | //f64cmplx r0(0.28693186889504513, 0.014286693904085048); |
---|
305 | //f64cmplx r0(0.001643721971153, 0.822467633298876); |
---|
306 | //f64cmplx r0(-1.207205434596, 0.315432814901); |
---|
307 | //f64cmplx r0(-0.79192956889854, -0.14632423080102); |
---|
308 | //f64cmplx r0(0.3245046418497685, 0.04855101129280834); |
---|
309 | f64cmplx z; |
---|
310 | int iter = MAX_ITERATIONS; |
---|
311 | for (z = z0; iter && z.sqlen() < maxsqlen; z = z * z + r0) |
---|
312 | --iter; |
---|
313 | |
---|
314 | if (iter) |
---|
315 | { |
---|
316 | double f = iter; |
---|
317 | double n = z.sqlen(); |
---|
318 | if (n > maxsqlen * maxsqlen) |
---|
319 | n = maxsqlen * maxsqlen; |
---|
320 | |
---|
321 | /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */ |
---|
322 | union { double n; uint64_t x; } u = { n }; |
---|
323 | double k = (u.x >> 42) - (((1 << 10) - 1) << 10); |
---|
324 | k *= k1; |
---|
325 | |
---|
326 | /* Approximate log2(k) in [1,2]. */ |
---|
327 | f += (- 0.344847817623168308695977510213252644185 * k |
---|
328 | + 2.024664188044341212602376988171727038739) * k |
---|
329 | - 1.674876738008591047163498125918330313237; |
---|
330 | |
---|
331 | *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)]; |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | *m_pixelstart++ = u8vec4(0, 0, 0, 255); |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | virtual void TickDraw(float deltams) |
---|
342 | { |
---|
343 | WorldEntity::TickDraw(deltams); |
---|
344 | |
---|
345 | static float const vertices[] = |
---|
346 | { |
---|
347 | 1.0f, 1.0f, |
---|
348 | -1.0f, 1.0f, |
---|
349 | -1.0f, -1.0f, |
---|
350 | -1.0f, -1.0f, |
---|
351 | 1.0f, -1.0f, |
---|
352 | 1.0f, 1.0f, |
---|
353 | }; |
---|
354 | |
---|
355 | static float const texcoords[] = |
---|
356 | { |
---|
357 | 1.0f, 1.0f, |
---|
358 | 0.0f, 1.0f, |
---|
359 | 0.0f, 0.0f, |
---|
360 | 0.0f, 0.0f, |
---|
361 | 1.0f, 0.0f, |
---|
362 | 1.0f, 1.0f, |
---|
363 | }; |
---|
364 | |
---|
365 | if (!m_ready) |
---|
366 | { |
---|
367 | /* Create a texture of half the width and twice the height |
---|
368 | * so that we can upload four different subimages each frame. */ |
---|
369 | glGenTextures(1, &m_texid); |
---|
370 | glBindTexture(GL_TEXTURE_2D, m_texid); |
---|
371 | glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, |
---|
372 | m_size.x / 2, m_size.y * 2, 0, |
---|
373 | TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels); |
---|
374 | #if defined __CELLOS_LV2__ |
---|
375 | /* We need this hint because by default the storage type is |
---|
376 | * GL_TEXTURE_SWIZZLED_GPU_SCE. */ |
---|
377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE, |
---|
378 | GL_TEXTURE_TILED_GPU_SCE); |
---|
379 | #endif |
---|
380 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
381 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
382 | |
---|
383 | m_shader = Shader::Create( |
---|
384 | #if !defined __CELLOS_LV2__ |
---|
385 | #if !defined HAVE_GLES_2X |
---|
386 | "#version 120\n" |
---|
387 | #else |
---|
388 | "precision highp float;" |
---|
389 | #endif |
---|
390 | "" |
---|
391 | "uniform mat4 u_ZoomSettings;" |
---|
392 | "uniform vec4 u_TexelSize;" |
---|
393 | "" |
---|
394 | "attribute vec2 a_TexCoord;" |
---|
395 | "attribute vec2 a_Vertex;" |
---|
396 | "" |
---|
397 | "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;" |
---|
398 | "" |
---|
399 | "void main(void)" |
---|
400 | "{" |
---|
401 | " gl_Position = vec4(a_Vertex, 0.0, 1.0);" |
---|
402 | /* Center point in [-.5,.5], apply zoom and translation |
---|
403 | * transformation, and go back to texture coordinates |
---|
404 | * in [0,1]. That's the ideal point we would like to |
---|
405 | * compute the value for. Then add or remove half the |
---|
406 | * size of a texel: the distance from this new point to |
---|
407 | * the final point will be our error. */ |
---|
408 | " vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625);" |
---|
409 | " vec4 zoomscale = vec4(u_ZoomSettings[0][2]," |
---|
410 | " u_ZoomSettings[1][2]," |
---|
411 | " u_ZoomSettings[2][2]," |
---|
412 | " u_ZoomSettings[3][2]);" |
---|
413 | " vec4 zoomtx = vec4(u_ZoomSettings[0][0]," |
---|
414 | " u_ZoomSettings[1][0]," |
---|
415 | " u_ZoomSettings[2][0]," |
---|
416 | " u_ZoomSettings[3][0]);" |
---|
417 | " vec4 zoomty = vec4(u_ZoomSettings[0][1]," |
---|
418 | " u_ZoomSettings[1][1]," |
---|
419 | " u_ZoomSettings[2][1]," |
---|
420 | " u_ZoomSettings[3][1]);" |
---|
421 | /* Pass all this to the fragment shader */ |
---|
422 | " v_CenterX = zoomscale * a_TexCoord.x + zoomtx" |
---|
423 | " + offsets.xyxy * u_TexelSize.x;" |
---|
424 | " v_CenterY = zoomscale * a_TexCoord.y - zoomty" |
---|
425 | " + offsets.xyyx * u_TexelSize.y;" |
---|
426 | /* Precompute the multiple of one texel where our ideal |
---|
427 | * point lies. The fragment shader will call floor() on |
---|
428 | * this value. We add or remove a slight offset to avoid |
---|
429 | * rounding issues at the image's edges. */ |
---|
430 | " v_IndexX = v_CenterX * u_TexelSize.z - offsets.zwzw;" |
---|
431 | " v_IndexY = v_CenterY * u_TexelSize.w - offsets.zwwz;" |
---|
432 | "}", |
---|
433 | |
---|
434 | #if !defined HAVE_GLES_2X |
---|
435 | "#version 120\n" |
---|
436 | #else |
---|
437 | "precision highp float;" |
---|
438 | #endif |
---|
439 | "" |
---|
440 | "uniform vec4 u_TexelSize;" |
---|
441 | "uniform sampler2D in_Texture;" |
---|
442 | "" |
---|
443 | "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;" |
---|
444 | "" |
---|
445 | "void main(void)" |
---|
446 | "{" |
---|
447 | /* Get a pixel coordinate from each slice into rx & ry */ |
---|
448 | " vec4 rx = u_TexelSize.x * (1.0 + 2.0 * floor(v_IndexX));" |
---|
449 | " vec4 ry = u_TexelSize.y * (1.0 + 2.0 * floor(v_IndexY));" |
---|
450 | /* Compute distance to expected pixel in dd */ |
---|
451 | " vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5);" |
---|
452 | " vec4 t0 = step(abs(rx - v05), v05)" |
---|
453 | " * step(abs(ry - v05), v05);" |
---|
454 | " vec4 dx = rx - v_CenterX;" |
---|
455 | " vec4 dy = ry - v_CenterY;" |
---|
456 | //" vec4 dd = t0 * (abs(dx) + abs(dy));" |
---|
457 | //" vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy)));" |
---|
458 | " vec4 dd = t0 / (0.000001 + (dx * dx) + (dy * dy));" |
---|
459 | /* Modify Y coordinate to select proper quarter. */ |
---|
460 | " ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);" |
---|
461 | "" |
---|
462 | #if 1 |
---|
463 | /* Put min(.x,.y) in .x and min(.z,.w) in .z */ |
---|
464 | " vec4 t1 = step(dd, dd.yyww);" |
---|
465 | " rx = mix(rx, rx.yyww, t1);" |
---|
466 | " ry = mix(ry, ry.yyww, t1);" |
---|
467 | " dd = mix(dd, dd.yyww, t1);" |
---|
468 | /* Put min(x,z) in x */ |
---|
469 | " vec4 t2 = step(dd, dd.zzzz);" |
---|
470 | " rx = mix(rx, rx.zzzz, t2);" |
---|
471 | " ry = mix(ry, ry.zzzz, t2);" |
---|
472 | /* Nearest neighbour */ |
---|
473 | " gl_FragColor = texture2D(in_Texture, vec2(rx.x, ry.x));" |
---|
474 | #else |
---|
475 | /* Alternate version: some kind of linear interpolation */ |
---|
476 | " vec4 p0 = texture2D(in_Texture, vec2(rx.x, ry.x));" |
---|
477 | " vec4 p1 = texture2D(in_Texture, vec2(rx.y, ry.y));" |
---|
478 | " vec4 p2 = texture2D(in_Texture, vec2(rx.z, ry.z));" |
---|
479 | " vec4 p3 = texture2D(in_Texture, vec2(rx.w, ry.w));" |
---|
480 | " gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)" |
---|
481 | " * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);" |
---|
482 | #endif |
---|
483 | "}" |
---|
484 | #else |
---|
485 | "void main(float4 in_Position : POSITION," |
---|
486 | " float2 a_TexCoord : TEXCOORD0," |
---|
487 | " out float4 out_Position : POSITION," |
---|
488 | " out float2 out_TexCoord : TEXCOORD0)" |
---|
489 | "{" |
---|
490 | " out_TexCoord = a_TexCoord;" |
---|
491 | " out_Position = in_Position;" |
---|
492 | "}", |
---|
493 | |
---|
494 | "float3 nearest0(float2 p, float4 u_TexelSize)" |
---|
495 | "{" |
---|
496 | " float2 q = p + 0.5 * u_TexelSize.xy;" |
---|
497 | " q -= fmod(q, 2.0 * u_TexelSize.xy);" |
---|
498 | " q += 0.5 * u_TexelSize.xy;" |
---|
499 | " return float3(q * float2(1.0, 0.25)," |
---|
500 | " length(q - p));" |
---|
501 | "}" |
---|
502 | "" |
---|
503 | "float3 nearest1(float2 p, float4 u_TexelSize)" |
---|
504 | "{" |
---|
505 | " float2 q = p - 0.5 * u_TexelSize.xy;" |
---|
506 | " q -= fmod(q, 2.0 * u_TexelSize.xy);" |
---|
507 | " q += 1.5 * u_TexelSize.xy;" |
---|
508 | " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.25)," |
---|
509 | " length(q - p));" |
---|
510 | "}" |
---|
511 | "" |
---|
512 | "float3 nearest2(float2 p, float4 u_TexelSize)" |
---|
513 | "{" |
---|
514 | " float2 q = p + float2(0.5, -0.5) * u_TexelSize.xy;" |
---|
515 | " q -= fmod(q, 2.0 * u_TexelSize.xy);" |
---|
516 | " q += float2(0.5, 1.5) * u_TexelSize.xy;" |
---|
517 | " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.50)," |
---|
518 | " length(q - p));" |
---|
519 | "}" |
---|
520 | "" |
---|
521 | "float3 nearest3(float2 p, float4 u_TexelSize)" |
---|
522 | "{" |
---|
523 | " float2 q = p + float2(-0.5, 0.5) * u_TexelSize.xy;" |
---|
524 | " q -= fmod(q, 2.0 * u_TexelSize.xy);" |
---|
525 | " q += float2(1.5, 0.5) * u_TexelSize.xy;" |
---|
526 | " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.75)," |
---|
527 | " length(q - p));" |
---|
528 | "}" |
---|
529 | "" |
---|
530 | "void main(float2 a_TexCoord : TEXCOORD0," |
---|
531 | " uniform float4 u_TexelSize," |
---|
532 | " uniform sampler2D in_Texture," |
---|
533 | " out float4 out_FragColor : COLOR)" |
---|
534 | "{" |
---|
535 | " float2 coord = a_TexCoord.xy;" |
---|
536 | " coord -= 0.1 * u_TexelSize.xy;" |
---|
537 | " float4 p0 = tex2D(in_Texture, nearest0(coord, u_TexelSize).xy);" |
---|
538 | " float4 p1 = tex2D(in_Texture, nearest1(coord, u_TexelSize).xy);" |
---|
539 | " float4 p2 = tex2D(in_Texture, nearest2(coord, u_TexelSize).xy);" |
---|
540 | " float4 p3 = tex2D(in_Texture, nearest3(coord, u_TexelSize).xy);" |
---|
541 | " out_FragColor = 0.25 * (p0 + p1 + p2 + p3);" |
---|
542 | "}" |
---|
543 | #endif |
---|
544 | ); |
---|
545 | m_vertexattrib = m_shader->GetAttribLocation("a_Vertex"); |
---|
546 | m_texattrib = m_shader->GetAttribLocation("a_TexCoord"); |
---|
547 | m_texeluni = m_shader->GetUniformLocation("u_TexelSize"); |
---|
548 | m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings"); |
---|
549 | m_ready = true; |
---|
550 | |
---|
551 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
552 | /* Method 1: store vertex buffer on the GPU memory */ |
---|
553 | glGenBuffers(1, &m_vbo); |
---|
554 | glBindBuffer(GL_ARRAY_BUFFER, m_vbo); |
---|
555 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, |
---|
556 | GL_STATIC_DRAW); |
---|
557 | glGenBuffers(1, &m_tbo); |
---|
558 | glBindBuffer(GL_ARRAY_BUFFER, m_tbo); |
---|
559 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, |
---|
560 | GL_STATIC_DRAW); |
---|
561 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
562 | /* Method 2: upload vertex information at each frame */ |
---|
563 | #else |
---|
564 | #endif |
---|
565 | |
---|
566 | /* FIXME: this object never cleans up */ |
---|
567 | } |
---|
568 | |
---|
569 | #if !defined HAVE_GLES_2X |
---|
570 | glEnable(GL_TEXTURE_2D); |
---|
571 | #endif |
---|
572 | glBindTexture(GL_TEXTURE_2D, m_texid); |
---|
573 | |
---|
574 | if (m_dirty[m_frame]) |
---|
575 | { |
---|
576 | m_dirty[m_frame]--; |
---|
577 | |
---|
578 | #ifdef __CELLOS_LV2__ |
---|
579 | /* glTexSubImage2D is extremely slow on the PS3, to the point |
---|
580 | * that uploading the whole texture is 40 times faster. */ |
---|
581 | glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, |
---|
582 | m_size.x / 2, m_size.y * 2, 0, |
---|
583 | TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels); |
---|
584 | #else |
---|
585 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2, |
---|
586 | m_size.x / 2, m_size.y / 2, |
---|
587 | TEXTURE_FORMAT, TEXTURE_TYPE, |
---|
588 | m_pixels + m_size.x * m_size.y / 4 * m_frame); |
---|
589 | #endif |
---|
590 | } |
---|
591 | |
---|
592 | m_shader->Bind(); |
---|
593 | m_shader->SetUniform(m_texeluni, m_texel_settings); |
---|
594 | m_shader->SetUniform(m_zoomuni, m_zoom_settings); |
---|
595 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
596 | glBindBuffer(GL_ARRAY_BUFFER, m_vbo); |
---|
597 | glEnableVertexAttribArray(m_vertexattrib); |
---|
598 | glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
599 | |
---|
600 | glBindBuffer(GL_ARRAY_BUFFER, m_tbo); |
---|
601 | glEnableVertexAttribArray(m_texattrib); |
---|
602 | glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
603 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
604 | /* Never used for now */ |
---|
605 | //glEnableVertexAttribArray(m_vertexattrib); |
---|
606 | //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices); |
---|
607 | #else |
---|
608 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
609 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
---|
610 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
611 | glTexCoordPointer(2, GL_FLOAT, 0, texcoords); |
---|
612 | #endif |
---|
613 | |
---|
614 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
615 | |
---|
616 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
617 | glDisableVertexAttribArray(m_vertexattrib); |
---|
618 | glDisableVertexAttribArray(m_texattrib); |
---|
619 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
620 | #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
621 | /* Never used for now */ |
---|
622 | //glDisableVertexAttribArray(m_vertexattrib); |
---|
623 | //glDisableVertexAttribArray(m_texattrib); |
---|
624 | #else |
---|
625 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
626 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
627 | #endif |
---|
628 | } |
---|
629 | |
---|
630 | private: |
---|
631 | static int const MAX_ITERATIONS = 170; |
---|
632 | static int const PALETTE_STEP = 32; |
---|
633 | |
---|
634 | ivec2 m_size, m_window_size, m_oldmouse; |
---|
635 | double m_window2world; |
---|
636 | f64vec2 m_texel2world; |
---|
637 | u8vec4 *m_pixels, *m_tmppixels, *m_palette; |
---|
638 | Shader *m_shader; |
---|
639 | GLuint m_texid; |
---|
640 | #if !defined __CELLOS_LV2__ && !defined __ANDROID__ |
---|
641 | GLuint m_vbo, m_tbo; |
---|
642 | GLuint m_tco; |
---|
643 | #endif |
---|
644 | int m_vertexattrib, m_texattrib, m_texeluni, m_zoomuni; |
---|
645 | int m_frame, m_dirty[4]; |
---|
646 | bool m_ready; |
---|
647 | |
---|
648 | f64cmplx m_center, m_translate; |
---|
649 | double m_zoom_speed, m_radius; |
---|
650 | vec4 m_texel_settings; |
---|
651 | mat4 m_zoom_settings; |
---|
652 | f64cmplx m_deltashift[4]; |
---|
653 | double m_deltascale[4]; |
---|
654 | |
---|
655 | /* Debug information */ |
---|
656 | #if !defined __native_client__ |
---|
657 | Text *m_centertext, *m_mousetext, *m_zoomtext; |
---|
658 | #endif |
---|
659 | }; |
---|
660 | |
---|
661 | int main(int argc, char **argv) |
---|
662 | { |
---|
663 | #if defined _WIN32 |
---|
664 | _chdir("../.."); |
---|
665 | #endif |
---|
666 | |
---|
667 | Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f); |
---|
668 | |
---|
669 | new DebugFps(5, 5); |
---|
670 | new Fractal(ivec2(640, 480)); |
---|
671 | //new DebugRecord("fractalol.ogm", 60.0f); |
---|
672 | |
---|
673 | app.Run(); |
---|
674 | |
---|
675 | return EXIT_SUCCESS; |
---|
676 | } |
---|
677 | |
---|