810765028fc156703867b8e93e6b6757ccfac662
[retroray] / src / scr_mod.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include "gaw/gaw.h"
19 #include "app.h"
20 #include "rtk.h"
21 #include "scene.h"
22 #include "cmesh.h"
23 #include "meshgen.h"
24
25 enum {
26         TBN_NEW, TBN_OPEN, TBN_SAVE, TBN_SEP1,
27         TBN_SEL, TBN_MOVE, TBN_ROT, TBN_SCALE, TBN_SEP2,
28         TBN_ADD, TBN_RM, TBN_SEP3,
29         TBN_MTL, TBN_REND, TBN_VIEWREND, TBN_SEP4, TBN_CFG,
30
31         NUM_TOOL_BUTTONS
32 };
33 static const char *tbn_icon_name[] = {
34         "new", "open", "save", 0,
35         "sel", "move", "rot", "scale", 0,
36         "add", "remove", 0,
37         "mtl", "rend", "viewrend", 0, "cfg"
38 };
39 static int tbn_icon_pos[][2] = {
40         {0,0}, {16,0}, {32,0}, {-1,-1},
41         {48,0}, {64,0}, {80,0}, {96,0}, {-1,-1},
42         {112,0}, {112,16}, {-1,-1},
43         {48,16}, {64,16}, {80,16}, {-1,-1}, {96,16}
44 };
45 static rtk_icon *tbn_icons[NUM_TOOL_BUTTONS];
46
47 #define TOOLBAR_HEIGHT  26
48
49
50 static int mdl_init(void);
51 static void mdl_destroy(void);
52 static int mdl_start(void);
53 static void mdl_stop(void);
54 static void mdl_display(void);
55 static void mdl_reshape(int x, int y);
56 static void mdl_keyb(int key, int press);
57 static void mdl_mouse(int bn, int press, int x, int y);
58 static void mdl_motion(int x, int y);
59
60 static void draw_grid(void);
61 static void tbn_callback(rtk_widget *w, void *cls);
62
63 struct app_screen scr_model = {
64         "modeller",
65         mdl_init, mdl_destroy,
66         mdl_start, mdl_stop,
67         mdl_display, mdl_reshape,
68         mdl_keyb, mdl_mouse, mdl_motion
69 };
70
71 static rtk_widget *toolbar;
72 static rtk_iconsheet *icons;
73
74 static struct cmesh *mesh_sph;
75
76 static float cam_theta, cam_phi = 20, cam_dist = 8;
77
78
79 static int mdl_init(void)
80 {
81         int i;
82         rtk_widget *w;
83
84         if(!(icons = rtk_load_iconsheet("data/icons.png"))) {
85                 errormsg("failed to load iconsheet\n");
86                 return -1;
87         }
88         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
89                 if(tbn_icon_name[i]) {
90                         tbn_icons[i] = rtk_define_icon(icons, tbn_icon_name[i],
91                                         tbn_icon_pos[i][0], tbn_icon_pos[i][1], 16, 16);
92                 } else {
93                         tbn_icons[i] = 0;
94                 }
95         }
96
97         if(!(toolbar = rtk_create_window(0, "toolbar", 0, 0, win_width, TOOLBAR_HEIGHT))) {
98                 return -1;
99         }
100         rtk_win_layout(toolbar, RTK_HBOX);
101
102         for(i=0; i<NUM_TOOL_BUTTONS; i++) {
103                 if(!tbn_icons[i]) {
104                         rtk_create_separator(toolbar);
105                 } else {
106                         if(!(w = rtk_create_iconbutton(toolbar, tbn_icons[i], 0))) {
107                                 return -1;
108                         }
109                         rtk_set_callback(w, tbn_callback, (void*)i);
110                 }
111         }
112
113         if(!(mesh_sph = cmesh_alloc())) {
114                 errormsg("failed to allocate sphere vis mesh\n");
115                 return -1;
116         }
117         gen_sphere(mesh_sph, 1.0f, 16, 8, 1.0f, 1.0f);
118         return 0;
119 }
120
121 static void mdl_destroy(void)
122 {
123         cmesh_free(mesh_sph);
124         rtk_free_iconsheet(icons);
125 }
126
127 static int mdl_start(void)
128 {
129         gaw_clear_color(0.125, 0.125, 0.125, 1);
130
131         gaw_enable(GAW_DEPTH_TEST);
132         gaw_enable(GAW_CULL_FACE);
133         gaw_enable(GAW_LIGHTING);
134         gaw_enable(GAW_LIGHT0);
135         return 0;
136 }
137
138 static void mdl_stop(void)
139 {
140 }
141
142 static void mdl_display(void)
143 {
144         int i, num;
145
146         gaw_clear(GAW_COLORBUF | GAW_DEPTHBUF);
147
148         rtk_draw_widget(toolbar);
149
150         gaw_viewport(0, TOOLBAR_HEIGHT, win_width, win_height - TOOLBAR_HEIGHT);
151
152         gaw_matrix_mode(GAW_MODELVIEW);
153         gaw_load_identity();
154         gaw_translate(0, 0, -cam_dist);
155         gaw_rotate(cam_phi, 1, 0, 0);
156         gaw_rotate(cam_theta, 0, 1, 0);
157
158         draw_grid();
159
160         gaw_poly_wire();
161
162         num = scn_num_objects(scn);
163         for(i=0; i<num; i++) {
164                 struct object *obj = scn->objects[i];
165                 struct sphere *sph;
166
167                 calc_object_matrix(obj);
168                 gaw_push_matrix();
169                 gaw_mult_matrix(obj->xform);
170
171                 switch(obj->type) {
172                 case OBJ_SPHERE:
173                         sph = (struct sphere*)obj;
174                         gaw_scale(sph->rad, sph->rad, sph->rad);
175                         cmesh_draw(mesh_sph);
176                         break;
177
178                 default:
179                         break;
180                 }
181
182                 gaw_pop_matrix();
183         }
184
185         gaw_poly_gouraud();
186
187         gaw_viewport(0, 0, win_width, win_height);
188 }
189
190 static void draw_grid(void)
191 {
192         gaw_save();
193         gaw_disable(GAW_LIGHTING);
194
195         gaw_begin(GAW_LINES);
196         gaw_color3f(0.5, 0, 0);
197         gaw_vertex3f(0, 0, 0);
198         gaw_vertex3f(-100, 0, 0);
199         gaw_vertex3f(0, 0, 0);
200         gaw_vertex3f(100, 0, 0);
201         gaw_color3f(0, 0.5, 0);
202         gaw_vertex3f(0, 0, 0);
203         gaw_vertex3f(0, 0, -100);
204         gaw_vertex3f(0, 0, 0);
205         gaw_vertex3f(0, 0, 100);
206         gaw_end();
207
208         gaw_restore();
209 }
210
211 static void mdl_reshape(int x, int y)
212 {
213         float aspect = (float)x / (float)(y - TOOLBAR_HEIGHT);
214
215         gaw_matrix_mode(GAW_PROJECTION);
216         gaw_load_identity();
217         gaw_perspective(50, aspect, 0.5, 100.0);
218
219         rtk_resize(toolbar, win_width, TOOLBAR_HEIGHT);
220 }
221
222 static void mdl_keyb(int key, int press)
223 {
224         if(rtk_input_key(toolbar, key, press)) {
225                 app_redisplay();
226                 return;
227         }
228 }
229
230 static int vpdrag;
231
232 static void mdl_mouse(int bn, int press, int x, int y)
233 {
234         if(!vpdrag && rtk_input_mbutton(toolbar, bn, press, x, y)) {
235                 app_redisplay();
236                 return;
237         }
238
239         if(press) {
240                 vpdrag |= (1 << bn);
241         } else {
242                 vpdrag &= ~(1 << bn);
243         }
244 }
245
246 static void mdl_motion(int x, int y)
247 {
248         int dx, dy;
249
250         if(!vpdrag && rtk_input_mmotion(toolbar, x, y)) {
251                 app_redisplay();
252                 return;
253         }
254
255         dx = x - mouse_x;
256         dy = y - mouse_y;
257
258         if((dx | dy) == 0) return;
259
260         if(mouse_state[0]) {
261                 cam_theta += dx * 0.5f;
262                 cam_phi += dy * 0.5f;
263                 if(cam_phi < -90) cam_phi = -90;
264                 if(cam_phi > 90) cam_phi = 90;
265                 app_redisplay();
266         }
267
268         if(mouse_state[2]) {
269                 cam_dist += dy * 0.1f;
270                 if(cam_dist < 0) cam_dist = 0;
271                 app_redisplay();
272         }
273 }
274
275 static void add_sphere(void)
276 {
277         struct object *obj;
278
279         if(!(obj = create_object(OBJ_SPHERE))) {
280                 return;
281         }
282         scn_add_object(scn, obj);
283 }
284
285 static void tbn_callback(rtk_widget *w, void *cls)
286 {
287         int id = (intptr_t)cls;
288
289         switch(id) {
290         case TBN_ADD:
291                 add_sphere();
292                 break;
293
294         default:
295                 break;
296         }
297 }