|
| 1 | +! julia.f90 |
| 2 | +! |
| 3 | +! Example program that renders animated Julia set, using the GLSL 3.30 shader |
| 4 | +! `share/julia.fs`. Based on the raylib example `shaders_julia_set.c`. |
| 5 | +! |
| 6 | +! Author: Philipp Engel |
| 7 | +! Licence: ISC |
| 8 | +program main |
| 9 | + use, intrinsic :: iso_c_binding |
| 10 | + use :: raylib |
| 11 | + implicit none (type, external) |
| 12 | + |
| 13 | + integer, parameter :: SCREEN_WIDTH = 800 |
| 14 | + integer, parameter :: SCREEN_HEIGHT = 450 |
| 15 | + |
| 16 | + integer :: inc_speed |
| 17 | + integer :: c_idx |
| 18 | + integer :: offset_idx |
| 19 | + integer :: zoom_idx |
| 20 | + |
| 21 | + logical :: pause |
| 22 | + logical :: controls |
| 23 | + |
| 24 | + real(kind=c_float) :: amount |
| 25 | + real(kind=c_float) :: points(2, 6) |
| 26 | + real(kind=c_float), target :: c(2) |
| 27 | + real(kind=c_float), target :: offset(2) |
| 28 | + real(kind=c_float), target :: zoom |
| 29 | + real(kind=c_float), target :: screen_dims(2) |
| 30 | + |
| 31 | + type(render_texture2d_type) :: target |
| 32 | + type(shader_type) :: shader |
| 33 | + type(vector2_type) :: mouse_pos |
| 34 | + type(vector2_type) :: offset_speed |
| 35 | + |
| 36 | + ! Points of interest. |
| 37 | + points = reshape([ -0.348827, 0.607167, & |
| 38 | + -0.786268, 0.169728, & |
| 39 | + -0.800000, 0.156000, & |
| 40 | + 0.285000, 0.000000, & |
| 41 | + -0.835000, -0.232100, & |
| 42 | + -0.701760, -0.384200 ], [ 2, 6 ]) |
| 43 | + |
| 44 | + call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char) |
| 45 | + call set_target_fps(60) |
| 46 | + |
| 47 | + ! Load julia set shader. Passing `NULL` for vertex shader forces |
| 48 | + ! usage of internal default vertex shader. |
| 49 | + shader = load_shader(c_null_char, 'share/julia.fs' // c_null_char) |
| 50 | + |
| 51 | + ! Create a RenderTexture2D to be used for render to texture. |
| 52 | + target = load_render_texture(get_screen_width(), get_screen_height()) |
| 53 | + |
| 54 | + ! Constant `c` to use in `z^2 + c`. |
| 55 | + c = [ points(1, 1), points(2, 1) ] |
| 56 | + |
| 57 | + ! Offset and zoom to draw the Julia set at (centered on screen and |
| 58 | + ! default size. |
| 59 | + offset = [ -1.0 * (get_screen_width() / 2), & |
| 60 | + -1.0 * (get_screen_height() / 2) ] |
| 61 | + offset_speed = vector2_type(0.0, 0.0) |
| 62 | + |
| 63 | + zoom = 1.0 |
| 64 | + |
| 65 | + ! Get variable (uniform) locations on the shader to connect with the |
| 66 | + ! program. If uniform variable could not be found in the shader, function |
| 67 | + ! returns -1. |
| 68 | + c_idx = get_shader_location(shader, 'c' // c_null_char) |
| 69 | + offset_idx = get_shader_location(shader, 'offset' // c_null_char) |
| 70 | + zoom_idx = get_shader_location(shader, 'zoom' // c_null_char) |
| 71 | + |
| 72 | + screen_dims = [ real(get_screen_width()), real(get_screen_height()) ] |
| 73 | + |
| 74 | + call set_shader_value(shader, & |
| 75 | + get_shader_location(shader, 'screenDims' // c_null_char), & |
| 76 | + c_loc(screen_dims), & |
| 77 | + SHADER_UNIFORM_VEC2) |
| 78 | + |
| 79 | + call set_shader_value(shader, c_idx, c_loc(c), SHADER_UNIFORM_VEC2) |
| 80 | + call set_shader_value(shader, offset_idx, c_loc(offset), SHADER_UNIFORM_VEC2) |
| 81 | + call set_shader_value(shader, zoom_idx, c_loc(zoom), SHADER_UNIFORM_FLOAT) |
| 82 | + |
| 83 | + inc_speed = 0 |
| 84 | + controls = .true. |
| 85 | + pause = .false. |
| 86 | + |
| 87 | + do while (.not. window_should_close()) |
| 88 | + if (is_key_pressed(KEY_ONE) .or. is_key_pressed(KEY_TWO) .or. is_key_pressed(KEY_THREE) .or. & |
| 89 | + is_key_pressed(KEY_FOUR) .or. is_key_pressed(KEY_FIVE) .or. is_key_pressed(KEY_SIX)) then |
| 90 | + if (is_key_pressed(KEY_ONE)) then |
| 91 | + c = points(:, 1) |
| 92 | + else if (is_key_pressed(KEY_TWO)) then |
| 93 | + c = points(:, 2) |
| 94 | + else if (is_key_pressed(KEY_THREE)) then |
| 95 | + c = points(:, 3) |
| 96 | + else if (is_key_pressed(KEY_FOUR)) then |
| 97 | + c = points(:, 4) |
| 98 | + else if (is_key_pressed(KEY_FIVE)) then |
| 99 | + c = points(:, 5) |
| 100 | + else if (is_key_pressed(KEY_SIX)) then |
| 101 | + c = points(:, 6) |
| 102 | + end if |
| 103 | + |
| 104 | + call set_shader_value(shader, c_idx, c_loc(c), SHADER_UNIFORM_VEC2) |
| 105 | + end if |
| 106 | + |
| 107 | + if (is_key_pressed(KEY_SPACE)) pause = .not. pause |
| 108 | + if (is_key_pressed(KEY_F1)) controls = .not. controls |
| 109 | + |
| 110 | + if (.not. pause) then |
| 111 | + if (is_key_pressed(KEY_RIGHT)) then |
| 112 | + inc_speed = inc_speed + 1 |
| 113 | + else if (is_key_pressed(KEY_LEFT)) then |
| 114 | + inc_speed = inc_speed - 1 |
| 115 | + end if |
| 116 | + |
| 117 | + if (is_mouse_button_down(MOUSE_BUTTON_LEFT) .or. & |
| 118 | + is_mouse_button_down(MOUSE_BUTTON_RIGHT)) then |
| 119 | + if (is_mouse_button_down(MOUSE_BUTTON_LEFT)) zoom = zoom + (zoom * 0.003) |
| 120 | + if (is_mouse_button_down(MOUSE_BUTTON_RIGHT)) zoom = zoom - (zoom * 0.003) |
| 121 | + |
| 122 | + mouse_pos = get_mouse_position() |
| 123 | + |
| 124 | + offset_speed%x = mouse_pos%x - (SCREEN_WIDTH / 2) |
| 125 | + offset_speed%y = mouse_pos%y - (SCREEN_HEIGHT / 2) |
| 126 | + |
| 127 | + ! Slowly move camera to target offset. |
| 128 | + offset(1) = offset(1) + get_frame_time() * offset_speed%x * 0.8 |
| 129 | + offset(2) = offset(2) + get_frame_time() * offset_speed%y * 0.8 |
| 130 | + else |
| 131 | + offset_speed = vector2_type(0.0, 0.0) |
| 132 | + end if |
| 133 | + |
| 134 | + amount = get_frame_time() * inc_speed * 0.0005 |
| 135 | + c = c + amount |
| 136 | + |
| 137 | + call set_shader_value(shader, c_idx, c_loc(c), SHADER_UNIFORM_VEC2) |
| 138 | + call set_shader_value(shader, offset_idx, c_loc(offset), SHADER_UNIFORM_VEC2) |
| 139 | + call set_shader_value(shader, zoom_idx, c_loc(zoom), SHADER_UNIFORM_FLOAT) |
| 140 | + end if |
| 141 | + |
| 142 | + ! Enable drawing to texture. |
| 143 | + call begin_texture_mode(target) |
| 144 | + call clear_background(BLACK) |
| 145 | + |
| 146 | + ! Draw a rectangle in shader mode to be used as shader canvas. |
| 147 | + ! Rectangle uses font white character texture coordinates, so shader |
| 148 | + ! can not be applied here directly because input vertexTexCoord do |
| 149 | + ! not represent full screen coordinates (space where want to apply |
| 150 | + ! shader). |
| 151 | + call draw_rectangle(0, 0, get_screen_width(), get_screen_height(), BLACK) |
| 152 | + call end_texture_mode() |
| 153 | + |
| 154 | + call begin_drawing() |
| 155 | + call clear_background(BLACK) |
| 156 | + |
| 157 | + ! Draw the saved texture and rendered julia set with shader. We do |
| 158 | + ! not invert texture on Y, already considered inside shader. |
| 159 | + call begin_shader_mode(shader) |
| 160 | + ! WARNING: If FLAG_WINDOW_HIGHDPI is enabled, HighDPI monitor |
| 161 | + ! scaling should be considered when rendering the RenderTexture2D |
| 162 | + ! to fit in the HighDPI scaled Window. |
| 163 | + call draw_texture_ex(target%texture, vector2_type(0.0, 0.0), 0.0, 1.0, WHITE) |
| 164 | + call end_shader_mode() |
| 165 | + |
| 166 | + if (controls) then |
| 167 | + call draw_text('Press Mouse buttons right/left to zoom in/out and move' // c_null_char, 10, 15, 10, RAYWHITE) |
| 168 | + call draw_text('Press KEY_F1 to toggle these controls' // c_null_char, 10, 30, 10, RAYWHITE) |
| 169 | + call draw_text('Press KEYS [1 - 6] to change point of interest' // c_null_char, 10, 45, 10, RAYWHITE) |
| 170 | + call draw_text('Press KEY_LEFT | KEY_RIGHT to change speed' // c_null_char, 10, 60, 10, RAYWHITE) |
| 171 | + call draw_text('Press KEY_SPACE to pause movement animation' // c_null_char, 10, 75, 10, RAYWHITE) |
| 172 | + end if |
| 173 | + call end_drawing() |
| 174 | + end do |
| 175 | + |
| 176 | + call unload_shader(shader) |
| 177 | + call unload_render_texture(target) |
| 178 | + call close_window() |
| 179 | +end program main |
0 commit comments