1 ! Copyright (C) 2023 CapitalEx.
2 ! See https:!factorcode.org/license.txt for BSD license.
3 ! A port of https://github.com/raysan5/raylib/blob/master/examples/models/models_rlgl_solar_system.c
4 USING: accessors classes.struct kernel math math.functions
5 math.trig ranges raylib rlgl sequences combinators.extras ;
8 ! Some raylib combinators
9 : with-drawing ( clear-color quot -- )
10 swap begin-drawing clear-background
11 call end-drawing ; inline
13 : with-matrix ( quot -- )
14 rl-push-matrix call rl-pop-matrix ; inline
16 : while-window-open ( quot -- )
17 [ window-should-close not ] swap while ; inline
19 : with-camera-3d ( camera quot -- )
20 swap begin-mode-3d call end-mode-3d ; inline
22 CONSTANT: screenWidth 800
23 CONSTANT: screenHeight 450
24 CONSTANT: sunRadius 4.0
25 CONSTANT: earthRadius 0.6
26 CONSTANT: earthOrbitRadius 8.0
27 CONSTANT: moonRadius 0.16
28 CONSTANT: moonOrbitRadius 1.5
29 CONSTANT: rotationSpeed 0.2
31 :: draw-sphere-basic ( color -- )
32 16 16 :> ( rings slices )
33 rings 2 + slices 6 * * rl-check-render-batch-limit drop
36 color [ r>> ] [ g>> ] [ b>> ] [ a>> ] quad rl-color4ub
37 rings 2 + [0..b) slices [0..b) [| i j |
38 270 180 rings 1 + / i * + deg>rad cos j 360 * slices / deg>rad sin *
39 270 180 rings 1 + / i * + deg>rad sin
40 270 180 rings 1 + / i * + deg>rad cos j 360 * slices / deg>rad cos *
43 270 180 rings 1 + / i 1 + * + deg>rad cos j 1 + 360 * slices / deg>rad sin *
44 270 180 rings 1 + / i 1 + * + deg>rad sin
45 270 180 rings 1 + / i 1 + * + deg>rad cos j 1 + 360 * slices / deg>rad cos *
48 270 180 rings 1 + / i 1 + * + deg>rad cos j 360 * slices / deg>rad sin *
49 270 180 rings 1 + / i 1 + * + deg>rad sin
50 270 180 rings 1 + / i 1 + * + deg>rad cos j 360 * slices / deg>rad cos *
53 270 180 rings 1 + / i * + deg>rad cos j 360 * slices / deg>rad sin *
54 270 180 rings 1 + / i * + deg>rad sin
55 270 180 rings 1 + / i * + deg>rad cos j 360 * slices / deg>rad cos *
58 270 180 rings 1 + / i * + deg>rad cos j 1 + 360 * slices / deg>rad sin *
59 270 180 rings 1 + / i * + deg>rad sin
60 270 180 rings 1 + / i * + deg>rad cos j 1 + 360 * slices / deg>rad cos *
63 270 180 rings 1 + / i 1 + * + deg>rad cos j 1 + 360 * slices / deg>rad sin *
64 270 180 rings 1 + / i 1 + * + deg>rad sin
65 270 180 rings 1 + / i 1 + * + deg>rad cos j 1 + 360 * slices / deg>rad cos *
70 : draw-sun ( scale -- )
71 dup dup rl-scalef GOLD draw-sphere-basic ;
73 : draw-moon ( radius rotation orbit-radius orbit-rotation -- )
74 0.0 1.0 0.0 rl-rotatef
76 0.0 1.0 0.0 rl-rotatef
78 LIGHTGRAY draw-sphere-basic ;
80 : draw-earth ( radius rotation -- )
81 0.25 1.0 0.0 rl-rotatef dup dup rl-scalef
82 BLUE draw-sphere-basic ;
84 : draw-earth-and-moon ( moonRadius moonRotation
85 moonOrbitRadius moonOrbitRotation
86 earthRadius earthRotation
87 earthOrbitRadius earthOrbitRotation -- )
88 0.0 1.0 0.0 rl-rotatef 0.0 0.0 rl-translatef
89 [ draw-earth ] with-matrix draw-moon ;
91 : draw-solar-system ( moonRadius moonRotation
92 moonOrbitRadius moonOrbitRotation
93 earthRadius earthRotation
94 earthOrbitRadius earthOrbitRotation
96 [ draw-sun ] [ draw-earth-and-moon ] [ with-matrix ] bi@ ;
99 screenWidth screenHeight "raylib [models] example - rlgl module usage with push/pop matrix transformations"
103 16.0 16.0 16.0 <Vector3> >>position
104 0.0 0.0 0.0 <Vector3> >>target
105 0.0 1.0 0.0 <Vector3> >>up
107 CAMERA_PERSPECTIVE >>projection
110 0.0 0.0 :> ( earthRotation! earthOrbitRotation! )
111 0.0 0.0 :> ( moonRotation! moonOrbitRotation! )
114 camera CAMERA_ORBITAL update-camera
116 5.0 rotationSpeed * earthRotation + earthRotation!
117 365.0 360.0 / 5.0 rotationSpeed * * rotationSpeed * earthOrbitRotation + earthOrbitRotation!
118 rotationSpeed 2.0 * moonRotation + moonRotation!
119 rotationSpeed 8.0 * moonOrbitRotation + moonOrbitRotation!
123 moonRadius moonRotation moonOrbitRadius moonOrbitRotation
124 earthRadius earthRotation earthOrbitRadius earthOrbitRotation
125 sunRadius draw-solar-system
127 0.0 0.0 0.0 <Vector3> earthOrbitRadius 1 0 0 <Vector3> 90.0 RED 0.5 fade
133 "EARTH ORBITING AROUND THE SUN!" 400 10 20 MAROON draw-text
134 "Using Factor!" 400 40 20 GOLD draw-text