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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* Pure Data Packet module.
* Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* a very naive approach to triangular meshes */
// $$TODO: some serious memory corruption in this file our the list implementation
#include "GL/gl.h"
#include <math.h>
//#include <GL/glut.h>
#include "pdp_opengl.h"
#include "pdp_3dp_base.h"
#include "pdp_mesh.h"
/* PD OBJECT */
typedef struct _pdp_3d_drawmesh
{
t_pdp_3dp_base x_base;
t_pdp_dpd_commandfactory x_clist;
t_mesh *x_mesh;
int x_wireframe;
int x_flatshading;
} t_pdp_3d_drawmesh;
/* MESHCOMMAND OBJECT */
typedef struct _meshcommand
{
t_pdp_dpd_command x_head;
int x_context_packet;
int x_texture_packet;
t_pdp_3d_drawmesh *x_mother;
t_pdp_method x_method;
int x_wireframe;
int x_flatshading;
float x_step;
float x_d0;
float x_r0;
int x_normal_type;
} t_meshcommand;
/* MESHCOMMAND METHODS */
/* draw the mesh */
static void meshcommand_draw(t_meshcommand *x)
{
int i = 0;
t_pdp_atom *it;
t_pdp_list *tl = x->x_mother->x_mesh->triangles;
t_triangle *t;
GLenum mode = (x->x_wireframe) ? GL_LINE_LOOP : GL_TRIANGLES;
//glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glLineWidth(5);
glBegin(mode);
if (x->x_flatshading){
PDP_POINTER_IN(tl, it, t){
glNormal3fv(t->n);
for (i=0; i<3; i++){
glVertex3fv(t->v[i]->c);
}
}
}
else{
PDP_POINTER_IN(tl, it, t){
for (i=0; i<3; i++){
glNormal3fv(t->v[i]->n);
glVertex3fv(t->v[i]->c);
}
}
}
glEnd();
}
static void meshcommand_relax(t_meshcommand *x)
{
mesh_relax(x->x_mother->x_mesh, x->x_step, x->x_d0, x->x_r0);
}
/* the main subcommand dispatcher */
static void meshcommand_execute(t_meshcommand *x)
{
int p = x->x_context_packet;
/* check if it's a valid buffer we can draw in */
if (pdp_packet_3Dcontext_isvalid(p)){
/* setup rendering context */
pdp_packet_3Dcontext_set_rendering_context(p);
/* call the command method */
if (x->x_method) (x->x_method)(x);
}
/* you know the drill: command done, sword in belly. */
pdp_dpd_command_suicide(x);
}
static void meshcommand_split_all_four(t_meshcommand *x)
{
mesh_split_all_four(x->x_mother->x_mesh);
}
static void meshcommand_split_all_three(t_meshcommand *x){
mesh_split_all_three(x->x_mother->x_mesh);
}
static void meshcommand_split_random_three(t_meshcommand *x){
mesh_split_random_three(x->x_mother->x_mesh);
}
static void meshcommand_reset(t_meshcommand *x)
{
mesh_free(x->x_mother->x_mesh);
x->x_mother->x_mesh = mesh_new_tetra();
}
static void meshcommand_debug(t_meshcommand *x)
{
mesh_debug(x->x_mother->x_mesh);
}
static void meshcommand_calculate_normals(t_meshcommand *x)
{
x->x_mother->x_mesh->normal_type = x->x_normal_type;
mesh_calculate_normals(x->x_mother->x_mesh);
}
/* PD OBJECT METHODS */
/* return a new command object */
void *pdp_3d_drawmesh_get_command_object(t_pdp_3d_drawmesh *x)
{
t_meshcommand *c = (t_meshcommand *)pdp_dpd_commandfactory_get_new_command(&x->x_clist);
c->x_context_packet = pdp_3dp_base_get_context_packet(x);
c->x_mother = x;
c->x_method = (t_pdp_method)meshcommand_draw; //default command is draw
c->x_wireframe = x->x_wireframe;
c->x_flatshading = x->x_flatshading;
return c;
}
/* schedule a command */
static void pdp_3d_drawmesh_queue_command(t_pdp_3d_drawmesh *x, t_meshcommand *c)
{
pdp_3dp_base_queue_command(x, c, (t_pdp_method)meshcommand_execute, 0, 0);
}
static void pdp_3d_drawmesh_queue_simple_command(t_pdp_3d_drawmesh *x, t_pdp_method method)
{
t_meshcommand *c = (t_meshcommand *)pdp_3d_drawmesh_get_command_object(x);
c->x_method = method;
pdp_3dp_base_queue_command(x, c, (t_pdp_method)meshcommand_execute, 0, 0);
}
//NOTE: only the meshcommands are entitled to use the mesh (thread issues)
//therefore all mesh manipulations must be queued as a command
static void pdp_3d_drawmesh_debug(t_pdp_3d_drawmesh *x)
{
pdp_3d_drawmesh_queue_simple_command(x, (t_pdp_method)meshcommand_debug);
}
static void pdp_3d_drawmesh_relax(t_pdp_3d_drawmesh *x, t_floatarg step,
t_floatarg d0, t_floatarg r0)
{
t_meshcommand *c = (t_meshcommand *)pdp_3d_drawmesh_get_command_object(x);
c->x_step = step;
c->x_d0 = d0;
c->x_r0 = r0;
c->x_method = (t_pdp_method)meshcommand_relax;
pdp_3d_drawmesh_queue_command(x, c);
}
void pdp_3d_drawmesh_normal(t_pdp_3d_drawmesh *x, t_symbol *s)
{
t_meshcommand *c = (t_meshcommand *)pdp_3d_drawmesh_get_command_object(x);
if (gensym("sphere") == s) c->x_normal_type = MESH_NORMAL_SPHERE;
else if (gensym("prism") == s) c->x_normal_type = MESH_NORMAL_PRISM;
else if (gensym("random") == s) c->x_normal_type = MESH_NORMAL_RANDOM;
else if (gensym("average") == s) c->x_normal_type = MESH_NORMAL_AVERAGE;
c->x_method = (t_pdp_method)meshcommand_calculate_normals;
pdp_3d_drawmesh_queue_command(x, c);
}
/* this is used by the standard drawing routine, so doesn't need to be scheduled */
void pdp_3d_drawmesh_wireframe(t_pdp_3d_drawmesh *x, t_float f)
{
x->x_wireframe = (f != 0.0f);
}
void pdp_3d_drawmesh_flatshading(t_pdp_3d_drawmesh *x, t_float f)
{
x->x_flatshading = (f != 0.0f);
}
static void pdp_3d_drawmesh_split_all_four(t_pdp_3d_drawmesh *x)
{
pdp_3d_drawmesh_queue_simple_command(x, (t_pdp_method)meshcommand_split_all_four);
}
static void pdp_3d_drawmesh_split_all_three(t_pdp_3d_drawmesh *x)
{
pdp_3d_drawmesh_queue_simple_command(x, (t_pdp_method)meshcommand_split_all_three);
}
static void pdp_3d_drawmesh_split_random_three(t_pdp_3d_drawmesh *x)
{
pdp_3d_drawmesh_queue_simple_command(x, (t_pdp_method)meshcommand_split_random_three);
}
static void pdp_3d_drawmesh_reset(t_pdp_3d_drawmesh *x)
{
pdp_3d_drawmesh_queue_simple_command(x, (t_pdp_method)meshcommand_reset);
}
t_class *pdp_3d_drawmesh_class;
void pdp_3d_drawmesh_free(t_pdp_3d_drawmesh *x)
{
/* queue needs to finish before mesh is deleted */
pdp_3dp_base_queue_wait(x);
mesh_free(x->x_mesh);
pdp_3dp_base_free(x);
pdp_dpd_commandfactory_free(&x->x_clist);
}
void *pdp_3d_drawmesh_new(t_symbol *s, t_floatarg p0, t_floatarg p1, t_floatarg p2, t_floatarg p3)
{
t_pdp_3d_drawmesh *x = (t_pdp_3d_drawmesh *)pd_new(pdp_3d_drawmesh_class);
/* super init */
pdp_3dp_base_init(x);
/* create dpd outlet */
pdp_3dp_base_add_outlet(x, (t_pdp_method)meshcommand_execute, 0);
/* init command list */
pdp_dpd_commandfactory_init(&x->x_clist, sizeof(t_meshcommand));
/* register command factory method */
pdp_dpd_base_register_command_factory_method(x, (t_pdp_newmethod)pdp_3d_drawmesh_get_command_object);
/* initialize triangular mesh with a simply connected manifold */
x->x_mesh = mesh_new_tetra();
x->x_wireframe = 0;
x->x_flatshading = 0;
return (void *)x;
}
#ifdef __cplusplus
extern "C"
{
#endif
void pdp_3d_drawmesh_setup(void)
{
pdp_3d_drawmesh_class = class_new(gensym("3dp_drawmesh"), (t_newmethod)pdp_3d_drawmesh_new,
(t_method)pdp_3d_drawmesh_free, sizeof(t_pdp_3d_drawmesh), 0, A_DEFSYMBOL,
A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_NULL);
pdp_3dp_base_setup(pdp_3d_drawmesh_class);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_split_random_three, gensym("split3random"), A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_split_all_three, gensym("split3"), A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_split_all_four, gensym("split4"), A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_reset, gensym("reset"), A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_normal, gensym("normal"), A_SYMBOL, A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_relax, gensym("springrelax"),
A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_debug, gensym("info"), A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_wireframe, gensym("wireframe"), A_FLOAT, A_NULL);
class_addmethod(pdp_3d_drawmesh_class, (t_method)pdp_3d_drawmesh_flatshading, gensym("flatshading"), A_FLOAT, A_NULL);
}
#ifdef __cplusplus
}
#endif
|