Skip to content

Commit f6ee269

Browse files
committed
Added example.
1 parent 5215916 commit f6ee269

File tree

6 files changed

+10993
-1
lines changed

6 files changed

+10993
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $(TARGET): src/raylib.f90 src/raylib_util.f90
2121
$(FC) $(FFLAGS) -c src/raylib_util.f90
2222
$(AR) $(ARFLAGS) $(TARGET) raylib.o raylib_camera.o raylib_math.o raylib_util.o
2323

24-
examples: camera castle cubes flags camera3d font keys map truck window
24+
examples: camera camera3d castle cubes flags font keys map plane truck window
2525

2626
camera: $(TARGET) examples/camera.f90
2727
$(FC) $(FFLAGS) $(LDFLAGS) -o camera examples/camera.f90 $(TARGET) $(LDLIBS)
@@ -47,6 +47,9 @@ keys: $(TARGET) examples/keys.f90
4747
map: $(TARGET) examples/map.f90
4848
$(FC) $(FFLAGS) $(LDFLAGS) -o map examples/map.f90 $(TARGET) $(LDLIBS)
4949

50+
plane: $(TARGET) examples/plane.f90
51+
$(FC) $(FFLAGS) $(LDFLAGS) -o plane examples/plane.f90 $(TARGET) $(LDLIBS)
52+
5053
truck: $(TARGET) examples/truck.f90
5154
$(FC) $(FFLAGS) $(LDFLAGS) -o truck examples/truck.f90 $(TARGET) $(LDLIBS)
5255

@@ -65,5 +68,6 @@ clean:
6568
if [ -e font ]; then rm font; fi
6669
if [ -e keys ]; then rm keys; fi
6770
if [ -e map ]; then rm map; fi
71+
if [ -e plane ]; then rm plane; fi
6872
if [ -e truck ]; then rm truck; fi
6973
if [ -e window ]; then rm window; fi

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ More examples can be found in `examples/`:
101101
* **font** displays text using bitmap fonts.
102102
* **keys** demonstrates keyboard input.
103103
* **map** renders a height map.
104+
* **plane** demonstrates pitch/yaw/roll of a 3-D model.
104105
* **truck** rotates a 3-D model loaded from file.
105106
* **window** opens a window with raylib.
106107

examples/plane.f90

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

fpm.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ name = "map"
5656
source-dir = "examples"
5757
main = "map.f90"
5858

59+
[[executable]]
60+
name = "plane"
61+
source-dir = "examples"
62+
main = "plane.f90"
63+
5964
[[executable]]
6065
name = "truck"
6166
source-dir = "examples"

0 commit comments

Comments
 (0)