From 5120d97be542480ff44ca0a236c3fa15eaac6e78 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 7 Nov 2011 15:37:00 +0000 Subject: [PATCH] tutorial: continuous color in the Mandelbrot viewer. --- test/tutorial/tut03.cpp | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/test/tutorial/tut03.cpp b/test/tutorial/tut03.cpp index 59b08192..7ccb2c86 100644 --- a/test/tutorial/tut03.cpp +++ b/test/tutorial/tut03.cpp @@ -27,6 +27,7 @@ using namespace lol; #if defined _WIN32 # undef main /* FIXME: still needed? */ +# include #endif class Fractal : public WorldEntity @@ -114,18 +115,40 @@ public: /* FIXME: this object never cleans up */ } - m_angle += deltams * 0.001f; + m_angle += deltams * 0.0001f; - cmplx r0(cosf(m_angle), 0.3f * sinf(m_angle)); + cmplx r0(cosf(m_angle), 0.5f * sinf(m_angle)); for (int j = 0; j < m_size.y; j++) for (int i = 0; i < m_size.x; i++) { cmplx x0(4.0f / m_size.x * i - 2.5f, 3.0f / m_size.y * j - 1.5f); cmplx r = x0 * r0; + cmplx z; int iter = 20; - for (cmplx z = r; iter && z.sqlen() < 4.0f; z = z * z + r) + for (z = r; iter && z.sqlen() < 4.0f; z = z * z + r) --iter; - m_pixels[j * m_size.x + i] = u8vec4(0, iter * 12, iter * 12, 0); + + float f = iter; + float n = z.sqlen(); + if (n > 36.0f) + f += 2.0f; + if (n > 16.0f) + f += 1.0f + (n - 16.0f) / 20.0f; + else if (n > 4.0f) + f += (n - 4.0f) / 12.0f; + + if (iter) + { + uint8_t red = 255 - f * 11; + uint8_t green = 255 - f * 11; + uint8_t blue = (f * 23 > 255) ? 511 - f * 23 : 255; + //uint8_t blue = f * 36 < 255 ? f * 36 : 255; + m_pixels[j * m_size.x + i] = u8vec4(red, green, blue, 0); + } + else + { + m_pixels[j * m_size.x + i] = u8vec4(0, 0, 0, 0); + } } glEnable(GL_TEXTURE_2D); @@ -185,6 +208,10 @@ private: int main() { +#if defined _WIN32 + _chdir("../.."); +#endif + Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f); new DebugFps(5, 5);