-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.f
144 lines (109 loc) · 3.5 KB
/
model.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
depend 3dpack/v3d.f
stage actor: cam
transform: t
transform: t2
transform: t3
transform: camt
ALLEGRO_PRIM_LINE_LIST constant LINE_LIST
ALLEGRO_PRIM_LINE_STRIP constant LINE_STRIP
ALLEGRO_PRIM_LINE_LOOP constant LINE_LOOP
ALLEGRO_PRIM_TRIANGLE_LIST constant TRIANGLE_LIST
ALLEGRO_PRIM_TRIANGLE_STRIP constant TRIANGLE_STRIP
ALLEGRO_PRIM_TRIANGLE_FAN constant TRIANGLE_FAN
ALLEGRO_PRIM_POINT_LIST constant POINT_LIST
\ Model data structs
struct %modeldata
%modeldata 2 cells sfield vertices \ pointer, count
%modeldata 2 cells sfield indices \ pointer, count
%modeldata svar primtype
: >count cell+ ;
: f, sf, ;
: v, ( x y z u v -- )
1pf 1pf 1pf 1pf 1pf f, f, f, f, f, fore 4@ 4, ;
: vertices: ( modeldata -- modeldata adr )
here dup third vertices ! white ;
: ;vertices ( modeldata adr -- )
here swap - /ALLEGRO_VERTEX i/ 1p swap vertices >count ! ;
: modeldata ( primtype -- )
0 , 0 , 0 , 0 , , ;
: indices: ( modeldata -- modeldata adr )
here over indices ! here decimal ;
: ;indices ( modeldata adr -- )
locals| adr model |
here adr - cell/ model indices >count ! fixed ;
: v[] ( n modeldata - adr ) vertices @ swap /ALLEGRO_VERTEX * + ;
\ Model objects
extend: _actor
%v3d sizeof field pos
%v3d sizeof field scl
\ 0 field rtn \ tilt, pan, roll (i.e. pitch, yaw, roll)
var tilt
var pan
var roll
var mdl <adr
var tex <adr
;class
_actor prototype as
1 1 1 scl 3!
1e 1sf constant (1e)
0e 1sf constant (0e)
create axis 3 cells allot
: modelview
t 0transform
t (1e) (0e) (0e) roll @ 1pf d>r 1sf al_rotate_transform_3d
t (0e) (1e) (0e) pan @ 1pf d>r 1sf al_rotate_transform_3d
(0e) (0e) (1e) axis 3!
t axis dup >y over >z al_transform_coordinates_3d
t axis 3@ tilt @ 1pf d>r 1sf al_rotate_transform_3d
t2 0transform
t2 scl 3@ 3af al_scale_transform_3d
t2 t al_compose_transform
t2 pos 3@ 3af al_translate_transform_3d
t2 t3 16 cells move
t2 camt al_compose_transform
t2 al_use_transform
;
: texture@ tex @ dup if >bmp then ;
: model ( -- )
mdl @ -exit
modelview
mdl @ indices @ if
mdl @ vertices @
0 \ vertex decl
texture@
mdl @ indices 2@ 1i
mdl @ primtype @
al_draw_indexed_prim
else
mdl @ vertices 2@ 1i >r
0 \ vertex decl
texture@
0 \ first vertex
r> \ last vertex
mdl @ primtype @
al_draw_prim
then
;
: camera-transform ( -- )
camt identity
camt pos 3@ 3negate 3af al_translate_transform_3d
camt (0e) (0e) (1e) roll @ negate >rad 1af al_rotate_transform_3d
camt (0e) (1e) (0e) pan @ negate >rad 1af al_rotate_transform_3d
camt (1e) (0e) (0e) tilt @ negate >rad 1af al_rotate_transform_3d
;
: /model mdl ! draw> model ;
\ : -camt camt al_identity_transform ;
\ : /globalmodel mdl ! draw> +state -camt model -state ;
0 value (code)
: veach ( xt modeldata -- ) ( ALLEGRO_VERTEX -- )
swap >code to (code) vertices 2@ for
dup >r (code) call r> /ALLEGRO_VERTEX +
loop drop ;
: veach> ( modeldata -- <code> )
r> code> swap veach ;
: transform-model ( modeldata -- ) \ uses the "t" transform
veach> t swap vtransform ;
create (v) 3 cells allot
: 3f@p dup sf@ f>p swap cell+ dup sf@ f>p swap cell+ sf@ f>p ;
: local ( x y z -- x y z ) \ transform local point (note: expensive!)
modelview 3af (v) 3! t3 (v) vtransform (v) 3f@p ;