Generate sample buffers
(width, height)
| 57 | |
| 58 | |
| 59 | def _gen_buffers(width, height): |
| 60 | """ |
| 61 | Generate sample buffers |
| 62 | """ |
| 63 | channels = [3, 1] |
| 64 | |
| 65 | types = [{'array': 'B', 'oid': symbols.OID_TYPES_UINT8}, |
| 66 | {'array': 'f', 'oid': symbols.OID_TYPES_FLOAT32}] |
| 67 | |
| 68 | tex1 = [0] * width * height * channels[0] |
| 69 | tex2 = [0] * width * height * channels[1] |
| 70 | |
| 71 | c_x = width * 2.0 / 3.0 |
| 72 | c_y = height * 0.5 |
| 73 | scale = 3.0 / width |
| 74 | |
| 75 | for pos_y in range(0, height): |
| 76 | for pos_x in range(0, width): |
| 77 | # Buffer 1: Coloured set |
| 78 | pixel_pos = [pos_x, pos_y] |
| 79 | |
| 80 | buffer_pos = pos_y * channels[0] * width + channels[0] * pos_x |
| 81 | |
| 82 | tex1[buffer_pos + 0] = _gen_color( |
| 83 | pixel_pos, [20.0, 80.0], math.cos, math.cos) |
| 84 | tex1[buffer_pos + 1] = _gen_color( |
| 85 | pixel_pos, [50.0, 200.0], math.sin, math.cos) |
| 86 | tex1[buffer_pos + 2] = _gen_color( |
| 87 | pixel_pos, [30.0, 120.0], math.cos, math.cos) |
| 88 | |
| 89 | # Buffer 2: Mandelbrot set |
| 90 | pixel_pos = complex((pos_x-c_x), (pos_y-c_y)) * scale |
| 91 | buffer_pos = pos_y * channels[1] * width + channels[1] * pos_x |
| 92 | |
| 93 | mandel_z = complex(0, 0) |
| 94 | for _ in range(0, 20): |
| 95 | mandel_z = mandel_z * mandel_z + pixel_pos |
| 96 | |
| 97 | z_norm_squared = mandel_z.real * mandel_z.real +\ |
| 98 | mandel_z.imag * mandel_z.imag |
| 99 | z_threshold = 5.0 |
| 100 | |
| 101 | for channel in range(0, channels[1]): |
| 102 | tex2[buffer_pos + channel] = z_threshold - min(z_threshold, |
| 103 | z_norm_squared) |
| 104 | |
| 105 | tex_arr1 = array.array(types[0]['array'], [int(val) for val in tex1]) |
| 106 | tex_arr2 = array.array(types[1]['array'], tex2) |
| 107 | |
| 108 | if sys.version_info[0] == 2: |
| 109 | mem1 = buffer(tex_arr1) |
| 110 | mem2 = buffer(tex_arr2) |
| 111 | else: |
| 112 | mem1 = memoryview(tex_arr1) |
| 113 | mem2 = memoryview(tex_arr2) |
| 114 | |
| 115 | rowstride = width |
| 116 |