Skip to content

Commit 423fbb2

Browse files
committed
Updated and added examples.
1 parent 74ba19d commit 423fbb2

File tree

10 files changed

+465
-37
lines changed

10 files changed

+465
-37
lines changed

Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ $(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 camera3d castle cubes flags font keys map plane truck window
24+
examples: camera camera3d castle cubes flags font geometric julia keys map \
25+
plane shapes truck window
2526

2627
camera: $(TARGET) examples/camera.f90
2728
$(FC) $(FFLAGS) $(LDFLAGS) -o camera examples/camera.f90 $(TARGET) $(LDLIBS)
@@ -41,6 +42,12 @@ flags: $(TARGET) examples/flags.f90
4142
font: $(TARGET) examples/font.f90
4243
$(FC) $(FFLAGS) $(LDFLAGS) -o font examples/font.f90 $(TARGET) $(LDLIBS)
4344

45+
geometric: $(TARGET) examples/geometric.f90
46+
$(FC) $(FFLAGS) $(LDFLAGS) -o geometric examples/geometric.f90 $(TARGET) $(LDLIBS)
47+
48+
julia: $(TARGET) examples/julia.f90
49+
$(FC) $(FFLAGS) $(LDFLAGS) -o julia examples/julia.f90 $(TARGET) $(LDLIBS)
50+
4451
keys: $(TARGET) examples/keys.f90
4552
$(FC) $(FFLAGS) $(LDFLAGS) -o keys examples/keys.f90 $(TARGET) $(LDLIBS)
4653

@@ -50,6 +57,9 @@ map: $(TARGET) examples/map.f90
5057
plane: $(TARGET) examples/plane.f90
5158
$(FC) $(FFLAGS) $(LDFLAGS) -o plane examples/plane.f90 $(TARGET) $(LDLIBS)
5259

60+
shapes: $(TARGET) examples/shapes.f90
61+
$(FC) $(FFLAGS) $(LDFLAGS) -o shapes examples/shapes.f90 $(TARGET) $(LDLIBS)
62+
5363
truck: $(TARGET) examples/truck.f90
5464
$(FC) $(FFLAGS) $(LDFLAGS) -o truck examples/truck.f90 $(TARGET) $(LDLIBS)
5565

@@ -58,16 +68,19 @@ window: $(TARGET) examples/window.f90
5868

5969
clean:
6070
if [ `ls -1 *.mod 2>/dev/null | wc -l` -gt 0 ]; then rm *.mod; fi
61-
if [ `ls -1 *.o 2>/dev/null | wc -l` -gt 0 ]; then rm *.o; fi
71+
if [ `ls -1 *.o 2>/dev/null | wc -l` -gt 0 ]; then rm *.o; fi
6272
if [ -e $(TARGET) ]; then rm $(TARGET); fi
6373
if [ -e camera ]; then rm camera; fi
6474
if [ -e camera3d ]; then rm camera3d; fi
6575
if [ -e castle ]; then rm castle; fi
6676
if [ -e cubes ]; then rm cubes; fi
6777
if [ -e flags ]; then rm flags; fi
6878
if [ -e font ]; then rm font; fi
79+
if [ -e geometric ]; then rm geometric; fi
80+
if [ -e julia ]; then rm julia; fi
6981
if [ -e keys ]; then rm keys; fi
7082
if [ -e map ]; then rm map; fi
7183
if [ -e plane ]; then rm plane; fi
84+
if [ -e shapes ]; then rm shapes; fi
7285
if [ -e truck ]; then rm truck; fi
7386
if [ -e window ]; then rm window; fi

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ More examples can be found in `examples/`:
9898
* **castle** renders a 3-D model loaded from file.
9999
* **cubes** renders waving cubes.
100100
* **flags** shows window flags.
101+
* **geometric** renders basic geometric shapes.
101102
* **font** displays text using bitmap fonts.
103+
* **julia** renders animated Julia set (GLSL).
102104
* **keys** demonstrates keyboard input.
103105
* **map** renders a height map.
104106
* **plane** demonstrates pitch/yaw/roll of a 3-D model.
107+
* **shapes** renders basic shapes.
105108
* **truck** rotates a 3-D model loaded from file.
106109
* **window** opens a window with raylib.
107110

examples/castle.f90

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ program main
1818
type(texture2d_type) :: texture
1919
type(vector3_type) :: position
2020

21-
type(material_type), pointer :: material_ptrs(:)
22-
type(material_map_type), pointer :: material_map_ptrs(:)
23-
2421
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
2522
call set_target_fps(60)
2623
call disable_cursor()
@@ -32,14 +29,10 @@ program main
3229
camera%fov_y = 45.0
3330
camera%projection = CAMERA_PERSPECTIVE
3431

35-
model = load_model('share/castle.obj' // c_null_char)
32+
model = load_model('share/castle.obj' // c_null_char)
3633
texture = load_texture('share/castle_diffuse.png' // c_null_char)
3734

38-
! We have to add 1 to the array indices as a work-around, as we can't set
39-
! the lower bounds of the pointer arrays with `c_f_pointer()`.
40-
call c_f_pointer(model%materials, material_ptrs, [ model%material_count ])
41-
call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ])
42-
material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture
35+
call set_model_diffuse(model, texture)
4336

4437
do while (.not. window_should_close())
4538
call update_camera(camera, CAMERA_FIRST_PERSON)
@@ -60,6 +53,19 @@ program main
6053

6154
call unload_texture(texture)
6255
call unload_model(model)
63-
6456
call close_window()
57+
contains
58+
subroutine set_model_diffuse(model, texture)
59+
type(model_type), intent(inout) :: model
60+
type(texture2d_type), intent(inout) :: texture
61+
62+
type(material_type), pointer :: material_ptrs(:)
63+
type(material_map_type), pointer :: material_map_ptrs(:)
64+
65+
! We have to add 1 to the array indices as a work-around, as we can't set
66+
! the lower bounds of the pointer arrays with `c_f_pointer()`.
67+
call c_f_pointer(model%materials, material_ptrs, [ model%material_count ])
68+
call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ])
69+
material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture
70+
end subroutine set_model_diffuse
6571
end program main

examples/geometric.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
! geometric.f90
2+
!
3+
! Example program that renders geometric shapes. Based on the raylib example
4+
! `models_geometric_shapes.c`.
5+
!
6+
! Author: Philipp Engel
7+
! Licence: ISC
8+
program main
9+
use, intrinsic :: iso_c_binding, only: c_null_char
10+
use :: raylib
11+
implicit none (type, external)
12+
13+
integer, parameter :: SCREEN_WIDTH = 800
14+
integer, parameter :: SCREEN_HEIGHT = 450
15+
16+
type(camera3d_type) :: camera
17+
18+
call set_config_flags(FLAG_MSAA_4X_HINT)
19+
call init_window(SCREEN_WIDTH, SCREEN_HEIGHT, 'Fortran + raylib' // c_null_char)
20+
call set_target_fps(60)
21+
22+
! Define camera to look into our 3-D world.
23+
camera%position = vector3_type(0.0, 10.0, 10.0)
24+
camera%target = vector3_type(0.0, 0.0, 0.0)
25+
camera%up = vector3_type(0.0, 1.0, 0.0)
26+
camera%fov_y = 45.0
27+
camera%projection = CAMERA_PERSPECTIVE
28+
29+
do while (.not. window_should_close())
30+
call begin_drawing()
31+
call clear_background(RAYWHITE)
32+
33+
call begin_mode3d(camera)
34+
call draw_cube (vector3_type(-4.0, 0.0, 2.0), 2.0, 5.0, 2.0, RED)
35+
call draw_cube_wires(vector3_type(-4.0, 0.0, 2.0), 2.0, 5.0, 2.0, GOLD)
36+
call draw_cube_wires(vector3_type(-4.0, 0.0, -2.0), 3.0, 6.0, 2.0, MAROON)
37+
38+
call draw_sphere (vector3_type(-1.0, 0.0, -2.0), 1.0, GREEN)
39+
call draw_sphere_wires(vector3_type( 1.0, 0.0, 2.0), 2.0, 16, 16, LIME)
40+
41+
call draw_cylinder (vector3_type(4.0, 0.0, -2.0), 1.0, 2.0, 3.0, 4, SKYBLUE)
42+
call draw_cylinder_wires(vector3_type(4.0, 0.0, -2.0), 1.0, 2.0, 3.0, 4, DARKBLUE)
43+
call draw_cylinder_wires(vector3_type(4.5, -1.0, 2.0), 1.0, 1.0, 2.0, 6, BROWN)
44+
45+
call draw_cylinder (vector3_type(1.0, 0.0, -4.0), 0.0, 1.5, 3.0, 8, GOLD)
46+
call draw_cylinder_wires(vector3_type(1.0, 0.0, -4.0), 0.0, 1.5, 3.0, 8, PINK)
47+
48+
call draw_capsule (vector3_type(-3.0, 1.5, -4.0), vector3_type(-4.0, -1.0, -4.0), 1.2, 8, 8, VIOLET)
49+
call draw_capsule_wires(vector3_type(-3.0, 1.5, -4.0), vector3_type(-4.0, -1.0, -4.0), 1.2, 8, 8, PURPLE)
50+
51+
call draw_grid(10, 1.0)
52+
call end_mode3d()
53+
54+
call draw_fps(10, 10)
55+
call end_drawing()
56+
end do
57+
58+
call close_window()
59+
end program main

examples/julia.f90

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

examples/map.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ program main
1818
type(mesh_type) :: mesh
1919
type(model_type) :: model
2020
type(texture2d_type) :: texture
21-
type(vector3_type) :: map_position
21+
type(vector3_type) :: position
2222

2323
type(material_type), pointer :: material_ptrs(:)
2424
type(material_map_type), pointer :: material_map_ptrs(:)
@@ -44,7 +44,7 @@ program main
4444
call c_f_pointer(material_ptrs(1)%maps, material_map_ptrs, [ MATERIAL_MAP_BRDF + 1 ])
4545
material_map_ptrs(MATERIAL_MAP_DIFFUSE + 1)%texture = texture
4646

47-
map_position = vector3_type(-8.0, 0.0, -8.0)
47+
position = vector3_type(-8.0, 0.0, -8.0)
4848

4949
call unload_image(image)
5050

@@ -55,7 +55,7 @@ program main
5555
call clear_background(RAYWHITE)
5656

5757
call begin_mode3d(camera)
58-
call draw_model(model, map_position, 1.0, RED)
58+
call draw_model(model, position, 1.0, RED)
5959
call draw_grid(20, 1.0)
6060
call end_mode3d()
6161

0 commit comments

Comments
 (0)