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