|
| 1 | +! plane.f90 |
| 2 | +! |
| 3 | +! Example program that shows pitch/yaw/roll of a 3-D model. Based on the |
| 4 | +! raylib example `models_yaw_pitch_roll.c`. |
| 5 | +! |
| 6 | +! Author: Philipp Engel |
| 7 | +! Licence: ISC |
| 8 | +program main |
| 9 | + use, intrinsic :: iso_c_binding |
| 10 | + use :: raylib |
| 11 | + use :: raylib_math |
| 12 | + implicit none (type, external) |
| 13 | + |
| 14 | + integer, parameter :: SCREEN_WIDTH = 800 |
| 15 | + integer, parameter :: SCREEN_HEIGHT = 450 |
| 16 | + |
| 17 | + real :: pitch, roll, yaw |
| 18 | + type(camera3d_type) :: camera |
| 19 | + type(model_type) :: model |
| 20 | + type(texture2d_type) :: texture |
| 21 | + type(vector3_type) :: position |
| 22 | + type(vector3_type) :: rotation |
| 23 | + |
| 24 | + type(material_type), pointer :: material_ptrs(:) |
| 25 | + type(material_map_type), pointer :: material_map_ptrs(:) |
| 26 | + |
| 27 | + call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char) |
| 28 | + call set_target_fps(60) |
| 29 | + call disable_cursor() |
| 30 | + |
| 31 | + ! Define camera to look into our 3-D world. |
| 32 | + camera%position = vector3_type(0.0, 50.0, -120.0) |
| 33 | + camera%target = vector3_type(0.0, 0.0, 0.0) |
| 34 | + camera%up = vector3_type(0.0, 1.0, 0.0) |
| 35 | + camera%fov_y = 30.0 |
| 36 | + camera%projection = CAMERA_PERSPECTIVE |
| 37 | + |
| 38 | + model = load_model('share/plane.obj' // c_null_char) |
| 39 | + texture = load_texture('share/plane_diffuse.png' // c_null_char) |
| 40 | + |
| 41 | + ! We have to add 1 to the array indices as a work-around, as we can't set |
| 42 | + ! the lower bounds of the pointer arrays with `c_f_pointer()`. |
| 43 | + call c_f_pointer(model%materials, material_ptrs, [ model%material_count ]) |
| 44 | + call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ]) |
| 45 | + material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture |
| 46 | + |
| 47 | + ! Initial values. |
| 48 | + pitch = 0.0 |
| 49 | + roll = 0.0 |
| 50 | + yaw = 0.0 |
| 51 | + |
| 52 | + position = vector3_type(0.0, -8.0, 0.0) |
| 53 | + |
| 54 | + do while (.not. window_should_close()) |
| 55 | + ! Plane pitch (x-axis) controls. |
| 56 | + if (is_key_down(KEY_DOWN)) then |
| 57 | + pitch = pitch + 0.6 |
| 58 | + else if (is_key_down(KEY_UP)) then |
| 59 | + pitch = pitch - 0.6 |
| 60 | + else |
| 61 | + if (pitch > 0.3) then |
| 62 | + pitch = pitch - 0.3 |
| 63 | + else if (pitch < -0.3) then |
| 64 | + pitch = pitch + 0.3 |
| 65 | + end if |
| 66 | + end if |
| 67 | + |
| 68 | + ! Plane yaw (y-axis) controls. |
| 69 | + if (is_key_down(KEY_S)) then |
| 70 | + yaw = yaw - 1.0 |
| 71 | + else if (is_key_down(KEY_A)) then |
| 72 | + yaw = yaw + 1.0 |
| 73 | + else |
| 74 | + if (yaw > 0.0) then |
| 75 | + yaw = yaw - 0.5 |
| 76 | + else if (yaw < 0.0) then |
| 77 | + yaw = yaw + 0.5 |
| 78 | + end if |
| 79 | + end if |
| 80 | + |
| 81 | + ! Plane roll (z-axis) controls. |
| 82 | + if (is_key_down(KEY_LEFT)) then |
| 83 | + roll = roll - 1.0 |
| 84 | + else if (is_key_down(KEY_RIGHT)) then |
| 85 | + roll = roll + 1.0 |
| 86 | + else |
| 87 | + if (roll > 0.0) then |
| 88 | + roll = roll - 0.5 |
| 89 | + else if (roll < 0.0) then |
| 90 | + roll = roll + 0.5 |
| 91 | + end if |
| 92 | + end if |
| 93 | + |
| 94 | + rotation%x = pitch * (PI / 180.0) |
| 95 | + rotation%y = yaw * (PI / 180.0) |
| 96 | + rotation%z = roll * (PI / 180.0) |
| 97 | + |
| 98 | + model%transform = matrix_rotate_xyz(rotation) |
| 99 | + |
| 100 | + call begin_drawing() |
| 101 | + call clear_background(RAYWHITE) |
| 102 | + |
| 103 | + ! Draw 3D model (recomended to draw 3D always before 2D). |
| 104 | + call begin_mode3d(camera) |
| 105 | + call draw_model(model, position, 1.0, WHITE) |
| 106 | + call draw_grid(10, 10.0) |
| 107 | + call end_mode3d() |
| 108 | + |
| 109 | + ! Draw controls info. |
| 110 | + call draw_rectangle(30, 370, 260, 70, fade(GREEN, 0.5)) |
| 111 | + call draw_rectangle_lines(30, 370, 260, 70, fade(DARKGREEN, 0.5)) |
| 112 | + call draw_text('Pitch controlled with: KEY_UP / KEY_DOWN', 40, 380, 10, DARKGRAY) |
| 113 | + call draw_text('Roll controlled with: KEY_LEFT / KEY_RIGHT', 40, 400, 10, DARKGRAY) |
| 114 | + call draw_text('Yaw controlled with: KEY_A / KEY_S', 40, 420, 10, DARKGRAY) |
| 115 | + |
| 116 | + call draw_text('(c) WWI Plane Model created by GiaHanLam', SCREEN_WIDTH - 240, SCREEN_HEIGHT - 20, 10, DARKGRAY) |
| 117 | + call draw_fps(10, 10) |
| 118 | + call end_drawing() |
| 119 | + end do |
| 120 | + |
| 121 | + call unload_texture(texture) |
| 122 | + call unload_model(model) |
| 123 | + call close_window() |
| 124 | +end program main |
0 commit comments