Max 5 API Reference
00001 /* 00002 * Copyright 2004-2005 - Cycling '74 00003 * Derek Gerstmann - derek@cycling74.com 00004 * 00005 * Slab interface for grid based computations across a 2d plane. 00006 * 00007 */ 00008 00009 /*************************************************************************/ 00010 00011 #include "jit.gl.h" 00012 #include "jit.gl.texture.h" 00013 #include "jit.gl.shader.h" 00014 #include "jit.gl.ob3d.internal.h" 00015 #include "jit.common.h" 00016 #include "jit.vecmath.h" 00017 00018 /*************************************************************************/ 00019 00020 #define MIN_X (0) 00021 #define MIN_Y (1) 00022 #define MAX_X (2) 00023 #define MAX_Y (3) 00024 #define MIN_S (0) 00025 #define MIN_T (1) 00026 #define MAX_S (2) 00027 #define MAX_T (3) 00028 #define EDGE_L (0) 00029 #define EDGE_R (1) 00030 #define EDGE_B (2) 00031 #define EDGE_T (3) 00032 00033 #define JIT_GL_MAX_TEXTUREUNITS (8) 00034 00035 //#ifdef MAC_VERSION 00036 #define JIT_GL_SLAB_COLORMODE_SCALAR 2 00037 //#else 00038 //#define JIT_GL_SLAB_COLORMODE_SCALAR 1 00039 //#endif 00040 00041 00042 /*************************************************************************/ 00043 00044 // the classname object 00045 typedef struct _jit_gl_slab 00046 { 00047 t_object ob; // slab object 00048 void *ob3d; // object 3d 00049 t_symbol *shape; // slab shape 00050 t_symbol *type; // texture type 00051 long adapt; // flag for adapt mode 00052 long geomtype; // opengl geometry type 00053 long dim[3]; // output dim 00054 long subdiv[2]; // slab subdiv 00055 float depth; // slab z offset 00056 float rect[4]; // slab rectangle (MIN XY, MAX XY) 00057 long edges[4]; // slab edge flags 00058 long wrap[4]; // slab wrap (periodic) flags 00059 float texrect[4 * JIT_GL_MAX_TEXTUREUNITS]; // minimum texcoord for each unit (ST) 00060 long displaylist; // displaylist flag 00061 long dlid; // displaylist id 00062 long update; // update flag 00063 long elements; // element count 00064 float *vertex; // vertex positions 00065 long vertexcount; // number of vertex positions 00066 long vertexbytes; // number of bytes allocated for vertex array 00067 float **texcoord; // texture coordinates for each unit 00068 long texcoordcount; // number of texture coords per unit 00069 long texcoordbytes; // number of bytes allocated for texcoord array per unit 00070 long unitcount; // number of texture units 00071 long activeinput; // current active input 00072 long inputcount; // number of inputs 00073 long firsttime; // is always the hardest 00074 t_symbol *colormode; // colormode argb/uyvy 00075 long useinternal[JIT_GL_MAX_TEXTUREUNITS]; // use internal texture objects 00076 t_jit_gl_texture *internal[JIT_GL_MAX_TEXTUREUNITS]; // input internal texture objects 00077 t_symbol *input[JIT_GL_MAX_TEXTUREUNITS]; // input texture names 00078 t_jit_gl_texture *output; // output texture object (via capture) 00079 t_jit_gl_shader *shader; // local shader object 00080 double dimscale[2]; // if in adapt mode, use this to scale output dims based on leftmost input 00081 } t_jit_gl_slab; 00082 00083 /*************************************************************************/ 00084 00085 void *_jit_gl_slab_class; 00086 00087 t_jit_err jit_gl_slab_init(void); 00088 t_jit_gl_slab *jit_gl_slab_new(t_symbol * dest_name); 00089 void jit_gl_slab_free(t_jit_gl_slab *x); 00090 void jit_gl_slab_destroy(t_jit_gl_slab *x); 00091 void jit_gl_slab_destroy_geometry(t_jit_gl_slab *x); 00092 00093 void jit_gl_slab_sendinput(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00094 void jit_gl_slab_sendoutput(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00095 void jit_gl_slab_sendshader(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00096 t_max_err jit_gl_slab_sendshader_gimmeback(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv, t_atom *rv); 00097 00098 t_jit_err jit_gl_slab_dest_closing(t_jit_gl_slab *x); 00099 t_jit_err jit_gl_slab_drawto(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00100 t_jit_err jit_gl_slab_draw(t_jit_gl_slab *x); 00101 t_jit_err jit_gl_slab_dest_changed(t_jit_gl_slab *x); 00102 void jit_gl_slab_recalc( t_jit_gl_slab *x ); 00103 00104 t_jit_err jit_gl_slab_jit_matrix(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00105 t_jit_err jit_gl_slab_jit_gl_texture(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00106 00107 t_jit_err jit_gl_slab_setattr_shader(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00108 t_jit_err jit_gl_slab_getattr_shader(t_jit_gl_slab *x, void *attr, long *argc, t_atom **argv); 00109 00110 t_jit_err jit_gl_slab_getattr_out_name(t_jit_gl_slab *x, void *attr, long *ac, t_atom **av); 00111 t_jit_err jit_gl_slab_setattr_dim(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00112 t_jit_err jit_gl_slab_setattr_dimscale(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00113 t_jit_err jit_gl_slab_setattr_type(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00114 t_jit_err jit_gl_slab_setattr_adapt(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00115 t_jit_err jit_gl_slab_setattr_subdiv(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00116 t_jit_err jit_gl_slab_setattr_activeinput(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00117 t_jit_err jit_gl_slab_setattr_inputs(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00118 t_jit_err jit_gl_slab_setattr_displaylist(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00119 t_jit_err jit_gl_slab_setattr_shape(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00120 t_jit_err jit_gl_slab_setattr_depth(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00121 t_jit_err jit_gl_slab_setattr_rect(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00122 t_jit_err jit_gl_slab_setattr_texrect(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00123 t_jit_err jit_gl_slab_setattr_edges(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00124 t_jit_err jit_gl_slab_setattr_wrap(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00125 t_jit_err jit_gl_slab_setattr_colormode(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv); 00126 00127 void jit_gl_slab_get_color(t_jit_gl_slab *x, float *color); 00128 void jit_gl_slab_get_dest_name(t_jit_gl_slab *x, t_symbol *dest); 00129 00130 void jit_gl_slab_matrix(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv); 00131 t_jit_err jit_gl_slab_make_texture(t_jit_gl_slab *x); 00132 t_jit_err jit_gl_slab_make_points(t_jit_gl_slab *x); 00133 t_jit_err jit_gl_slab_evaluate(t_jit_gl_slab *x); 00134 00135 void jit_gl_slab_draw_grid(t_jit_gl_slab *x); 00136 00137 extern t_symbol *ps_bind; 00138 extern t_symbol *ps_unbind; 00139 extern t_symbol *ps_drawto; 00140 extern t_symbol *ps_colormode; 00141 extern t_symbol *ps_argb; 00142 extern t_symbol *ps_uyvy; 00143 t_symbol *ps_jit_gl_slab; 00144 t_symbol *ps_jit_gl_slab_flip; 00145 t_symbol *ps_jit_gl_slab_name; 00146 t_symbol *ps_jit_gl_slab_texture; 00147 t_symbol *ps_jit_gl_slab_shader; 00148 t_symbol *ps_jit_gl_slab_capture; 00149 t_symbol *ps_jit_gl_slab_automatic; 00150 t_symbol *ps_jit_gl_slab_shape_tri; 00151 t_symbol *ps_jit_gl_slab_shape_quad; 00152 t_symbol *ps_jit_gl_slab_shape_grid; 00153 t_symbol *ps_jit_gl_slab_shape_edges; 00154 t_symbol *ps_jit_gl_slab_sendshader; 00155 static t_symbol *ps_auto; 00156 00157 /*************************************************************************/ 00158 00159 t_jit_err jit_gl_slab_init(void) 00160 { 00161 long attrflags=0; 00162 long ob3d_flags=0; 00163 void *attr; 00164 void *ob3d; 00165 00166 // create class 00167 _jit_gl_slab_class = jit_class_new("jit_gl_slab",(method)jit_gl_slab_new,(method)jit_gl_slab_free, 00168 sizeof(t_jit_gl_slab), A_DEFSYM, 0L); 00169 00170 // set up object extension for 3d object, customized with flags 00171 ob3d_flags |= JIT_OB3D_NO_ROTATION_SCALE; 00172 ob3d_flags |= JIT_OB3D_NO_POLY_VARS; 00173 ob3d_flags |= JIT_OB3D_NO_FOG; 00174 ob3d_flags |= JIT_OB3D_NO_MATRIXOUTPUT; 00175 ob3d_flags |= JIT_OB3D_NO_LIGHTING_MATERIAL; 00176 ob3d_flags |= JIT_OB3D_IS_SLAB; 00177 ob3d = jit_ob3d_setup(_jit_gl_slab_class, calcoffset(t_jit_gl_slab, ob3d), ob3d_flags); 00178 00179 // add ob3d methods 00180 jit_class_addmethod(_jit_gl_slab_class, (method)jit_object_register, "register", A_CANT, 0L); 00181 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_draw, "ob3d_draw", A_CANT, 0L); 00182 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_dest_changed, "dest_changed", A_CANT, 0L); 00183 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_dest_closing, "dest_closing", A_CANT, 0L); 00184 00185 // stream methods 00186 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_jit_matrix, "jit_matrix", A_GIMME, 0L); 00187 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_jit_gl_texture, "jit_gl_texture", A_GIMME, 0L); 00188 00189 // set parameter values (also allow as attr argument) 00190 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader, "param", A_DEFER_LOW, 0L); 00191 jit_class_method_addargsafe(_jit_gl_slab_class, "param", "param"); 00192 00193 // add send methods 00194 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendinput, "sendinput", A_DEFER_LOW, 0L); 00195 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendoutput, "sendoutput", A_DEFER_LOW, 0L); 00196 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader, "sendshader", A_DEFER_LOW, 0L); 00197 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader, "read", A_DEFER_LOW, 0L); 00198 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader, "dump", A_DEFER_LOW, 0L); 00199 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader, "dispose", A_DEFER_LOW, 0L); 00200 00201 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader_gimmeback, "getparamlist", A_GIMMEBACK, 0L); 00202 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader_gimmeback, "getparamtype", A_GIMMEBACK, 0L); 00203 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader_gimmeback, "getparamval", A_GIMMEBACK, 0L); 00204 jit_class_addmethod(_jit_gl_slab_class, (method)jit_gl_slab_sendshader_gimmeback, "getparamdefault", A_GIMMEBACK, 0L); 00205 00206 // public attributes 00207 attrflags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_USURP_LOW; 00208 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"dim",_jit_sym_long,2,attrflags, 00209 (method)0L,(method)jit_gl_slab_setattr_dim,0/*fix*/,calcoffset(t_jit_gl_slab,dim)); 00210 jit_class_addattr(_jit_gl_slab_class,attr); 00211 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"dimscale",_jit_sym_float64,2,attrflags, 00212 (method)0L,(method)jit_gl_slab_setattr_dimscale,0/*fix*/,calcoffset(t_jit_gl_slab,dimscale)); 00213 jit_class_addattr(_jit_gl_slab_class,attr); 00214 attr = jit_object_new(_jit_sym_jit_attr_offset,"adapt",_jit_sym_long,attrflags, 00215 (method)0L,(method)jit_gl_slab_setattr_adapt,calcoffset(t_jit_gl_slab, adapt)); 00216 jit_class_addattr(_jit_gl_slab_class,attr); 00217 attr = jit_object_new(_jit_sym_jit_attr_offset,"type",_jit_sym_symbol,attrflags, 00218 (method)0L,(method)jit_gl_slab_setattr_type,calcoffset(t_jit_gl_slab, type)); 00219 jit_class_addattr(_jit_gl_slab_class,attr); 00220 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"subdiv",_jit_sym_long,2,attrflags, 00221 (method)0L,(method)jit_gl_slab_setattr_subdiv,0/*fix*/,calcoffset(t_jit_gl_slab,subdiv)); 00222 jit_class_addattr(_jit_gl_slab_class,attr); 00223 attr = jit_object_new(_jit_sym_jit_attr_offset,"shape",_jit_sym_symbol,attrflags, 00224 (method)0L,(method)jit_gl_slab_setattr_shape,calcoffset(t_jit_gl_slab, shape)); 00225 jit_class_addattr(_jit_gl_slab_class,attr); 00226 attr = jit_object_new(_jit_sym_jit_attr_offset,"displaylist",_jit_sym_long,attrflags, 00227 (method)0L,(method)jit_gl_slab_setattr_displaylist,calcoffset(t_jit_gl_slab, displaylist)); 00228 jit_class_addattr(_jit_gl_slab_class,attr); 00229 attr = jit_object_new(_jit_sym_jit_attr_offset,"depth",_jit_sym_float32,attrflags, 00230 (method)0L,(method)jit_gl_slab_setattr_depth,calcoffset(t_jit_gl_slab, depth)); 00231 jit_class_addattr(_jit_gl_slab_class,attr); 00232 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"rect",_jit_sym_float32,4,attrflags, 00233 (method)0L,(method)jit_gl_slab_setattr_rect,NULL/*fixed*/,calcoffset(t_jit_gl_slab,rect)); 00234 jit_class_addattr(_jit_gl_slab_class,attr); 00235 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"texrect",_jit_sym_float32,4*JIT_GL_MAX_TEXTUREUNITS,attrflags, 00236 (method)0L,(method)jit_gl_slab_setattr_texrect,NULL/*fixed*/,calcoffset(t_jit_gl_slab,texrect)); 00237 jit_class_addattr(_jit_gl_slab_class,attr); 00238 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"edges",_jit_sym_long,4,attrflags, 00239 (method)0L,(method)jit_gl_slab_setattr_edges,NULL/*fixed*/,calcoffset(t_jit_gl_slab,edges)); 00240 jit_class_addattr(_jit_gl_slab_class,attr); 00241 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"wrap",_jit_sym_long,4,attrflags, 00242 (method)0L,(method)jit_gl_slab_setattr_wrap,NULL/*fixed*/,calcoffset(t_jit_gl_slab,wrap)); 00243 jit_class_addattr(_jit_gl_slab_class,attr); 00244 attr = jit_object_new(_jit_sym_jit_attr_offset,"inputs",_jit_sym_long, attrflags, 00245 (method)0L,(method)jit_gl_slab_setattr_inputs,calcoffset(t_jit_gl_slab, inputcount)); 00246 jit_class_addattr(_jit_gl_slab_class,attr); 00247 attr = jit_object_new(_jit_sym_jit_attr_offset,"file",_jit_sym_symbol,attrflags, 00248 (method)jit_gl_slab_getattr_shader,(method)jit_gl_slab_setattr_shader,0); 00249 jit_class_addattr(_jit_gl_slab_class,attr); 00250 attr = jit_object_new(_jit_sym_jit_attr_offset,"colormode",_jit_sym_symbol,attrflags, 00251 (method)0L,(method)jit_gl_slab_setattr_colormode,calcoffset(t_jit_gl_slab, colormode)); 00252 jit_class_addattr(_jit_gl_slab_class,attr); 00253 attr = jit_object_new(_jit_sym_jit_attr_offset,"activeinput",_jit_sym_long, attrflags, 00254 (method)0L,(method)jit_gl_slab_setattr_activeinput,calcoffset(t_jit_gl_slab, activeinput)); 00255 jit_class_addattr(_jit_gl_slab_class,attr); 00256 00257 attrflags = JIT_ATTR_GET_DEFER_LOW | JIT_ATTR_SET_OPAQUE_USER; 00258 attr = jit_object_new(_jit_sym_jit_attr_offset,"out_name",_jit_sym_symbol, attrflags, 00259 (method)jit_gl_slab_getattr_out_name,(method)0L,0); 00260 jit_class_addattr(_jit_gl_slab_class,attr); 00261 00262 // register class 00263 jit_class_register(_jit_gl_slab_class); 00264 00265 // generate symbols 00266 ps_jit_gl_slab = gensym("jit_gl_slab"); 00267 ps_jit_gl_slab_name = gensym("name"); 00268 ps_jit_gl_slab_flip = gensym("flip"); 00269 ps_jit_gl_slab_texture = gensym("texture"); 00270 ps_jit_gl_slab_shader = gensym("shader"); 00271 ps_jit_gl_slab_capture = gensym("capture"); 00272 ps_jit_gl_slab_automatic = gensym("automatic"); 00273 ps_jit_gl_slab_shape_tri = gensym("tri"); 00274 ps_jit_gl_slab_shape_quad = gensym("quad"); 00275 ps_jit_gl_slab_shape_grid = gensym("grid"); 00276 ps_jit_gl_slab_shape_edges = gensym("edges"); 00277 ps_jit_gl_slab_sendshader = gensym("sendshader"); 00278 ps_auto = gensym("auto"); 00279 return JIT_ERR_NONE; 00280 } 00281 00282 /*************************************************************************/ 00283 00284 void jit_gl_slab_sendinput(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00285 { 00286 t_atom av2[256]; 00287 long i, j, c; 00288 00289 if (!argc) 00290 return; 00291 00292 if (argv->a_type==A_SYM) { 00293 i = 0; 00294 c = JIT_GL_MAX_TEXTUREUNITS; 00295 } else { 00296 c = jit_atom_getlong(argv); 00297 i = c-1; 00298 argc--; 00299 argv++; 00300 } 00301 00302 // get method name 00303 s = jit_atom_getsym(argv); 00304 00305 argc--; 00306 if (argc>0) 00307 argv++; 00308 else { 00309 argc = 0; 00310 argv = NULL; 00311 } 00312 00313 for (;i<c;i++) { 00314 if (x->internal[i] && !x->input[i]) 00315 { 00316 // call method 00317 object_method_typed(x->internal[i],s,argc,argv,NULL); 00318 } 00319 } 00320 } 00321 00322 void jit_gl_slab_sendoutput(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00323 { 00324 if (x->output) 00325 { 00326 s = jit_atom_getsym(argv); 00327 00328 argc--; 00329 if (argc) 00330 argv++; 00331 else 00332 argv = NULL; 00333 00334 object_method_typed(x->output,s,argc,argv,NULL); 00335 } 00336 } 00337 00338 void jit_gl_slab_sendshader(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00339 { 00340 if (x->shader) 00341 { 00342 if (s==ps_jit_gl_slab_sendshader) 00343 { 00344 s = jit_atom_getsym(argv); 00345 00346 argc--; 00347 if (argc) 00348 argv++; 00349 else 00350 argv = NULL; 00351 } 00352 00353 object_method_typed(x->shader,s,argc,argv,NULL); 00354 } 00355 } 00356 00357 t_max_err jit_gl_slab_sendshader_gimmeback(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv, t_atom *rv) 00358 { 00359 if (x->shader) 00360 object_method_typed(x->shader,s,argc,argv,rv); 00361 return MAX_ERR_NONE; 00362 } 00363 00364 00365 /*************************************************************************/ 00366 00367 t_jit_gl_slab *jit_gl_slab_new(t_symbol * dest_name) 00368 { 00369 long i; 00370 t_jit_gl_slab *x; 00371 t_atom av[4]; 00372 00373 if (x=(t_jit_gl_slab *)jit_object_alloc(_jit_gl_slab_class)) { 00374 00375 x->shape = ps_jit_gl_slab_shape_quad; 00376 x->type = ps_auto; 00377 00378 x->adapt = TRUE; 00379 x->elements = 0; 00380 x->vertexbytes = x->vertexcount = 0; 00381 x->vertex = NULL; 00382 x->texcoordbytes = x->texcoordcount = 0; 00383 x->texcoord = NULL; 00384 x->unitcount = 2; 00385 x->inputcount = 2; 00386 00387 // edgeflags 00388 x->edges[EDGE_L] = TRUE; 00389 x->edges[EDGE_R] = TRUE; 00390 x->edges[EDGE_B] = TRUE; 00391 x->edges[EDGE_T] = TRUE; 00392 00393 // wrap mode 00394 x->wrap[EDGE_L] = FALSE; 00395 x->wrap[EDGE_R] = FALSE; 00396 x->wrap[EDGE_B] = FALSE; 00397 x->wrap[EDGE_T] = FALSE; 00398 00399 x->subdiv[0] = 1; 00400 x->subdiv[1] = 1; 00401 x->dim[0] = 256; 00402 x->dim[1] = 256; 00403 x->dim[2] = 0; 00404 00405 x->dimscale[0] = 1.; 00406 x->dimscale[1] = 1.; 00407 00408 // init display list 00409 x->displaylist = 0; 00410 x->dlid = -1; 00411 00412 x->depth = 0.0f; 00413 00414 x->rect[0] = 0.0f; 00415 x->rect[1] = 0.0f; 00416 x->rect[2] = 1.0f; 00417 x->rect[3] = 1.0f; 00418 00419 for(i = 0; i < (4 * JIT_GL_MAX_TEXTUREUNITS); i += 4) 00420 { 00421 x->texrect[i + 0] = 0.0f; 00422 x->texrect[i + 1] = 0.0f; 00423 x->texrect[i + 2] = 1.0f; 00424 x->texrect[i + 3] = 1.0f; 00425 } 00426 00427 x->activeinput = 0; 00428 for(i = 0; i < JIT_GL_MAX_TEXTUREUNITS; i++) 00429 { 00430 x->input[i] = NULL; 00431 x->useinternal[i] = FALSE; 00432 x->internal[i] = NULL; 00433 if (i < x->inputcount) { 00434 x->internal[i] = jit_gl_texture_new(dest_name); 00435 jit_attr_setsym(x->internal[i],_jit_sym_name,jit_symbol_unique()); 00436 jit_attr_setsym(x->internal[i],gensym("defaultimage"),gensym("black")); 00437 } 00438 } 00439 00440 x->colormode = ps_argb; 00441 x->firsttime = 1; 00442 00443 x->output = jit_gl_texture_new(dest_name); 00444 jit_attr_setsym(x->output,_jit_sym_name,jit_symbol_unique()); 00445 jit_attr_setlong(x->output, ps_jit_gl_slab_flip, 0); 00446 jit_attr_setsym(x->output,gensym("defaultimage"),gensym("black")); 00447 jit_attr_setlong(x->output, gensym("capture_depthbits"), 0); 00448 00449 x->shader = jit_gl_shader_new(dest_name); 00450 jit_attr_setsym(x->shader,_jit_sym_name,jit_symbol_unique()); 00451 00452 // create and attach ob3d 00453 jit_ob3d_new(x, dest_name); 00454 00455 // set default color to white 00456 jit_atom_setfloat(av,1.); 00457 jit_atom_setfloat(av+1,1.); 00458 jit_atom_setfloat(av+2,1.); 00459 jit_atom_setfloat(av+3,1.); 00460 jit_object_method(x,gensym("color"),4,av); 00461 00462 x->update = TRUE; 00463 jit_gl_slab_make_points(x); 00464 jit_gl_slab_recalc(x); 00465 00466 } else { 00467 x = NULL; 00468 } 00469 return x; 00470 } 00471 00472 void jit_gl_slab_free(t_jit_gl_slab *x) 00473 { 00474 jit_gl_slab_destroy(x); 00475 jit_ob3d_free(x); 00476 } 00477 00478 void jit_gl_slab_destroy(t_jit_gl_slab *x) 00479 { 00480 long i; 00481 GLuint id; 00482 00483 // destroy geometry 00484 jit_gl_slab_destroy_geometry(x); 00485 00486 // destroy inputs 00487 for(i = 0; i < JIT_GL_MAX_TEXTUREUNITS; i++) 00488 { 00489 x->input[i] = NULL; 00490 x->useinternal[i] = FALSE; 00491 if(x->internal[i]) 00492 jit_gl_texture_free(x->internal[i]); 00493 x->internal[i] = NULL; 00494 } 00495 00496 // destroy outputs 00497 if(x->output) 00498 jit_object_free(x->output); 00499 x->output = NULL; 00500 00501 if(x->shader) 00502 jit_object_free(x->shader); 00503 x->shader = NULL; 00504 } 00505 00506 void jit_gl_slab_destroy_geometry(t_jit_gl_slab *x) 00507 { 00508 long i; 00509 GLuint id; 00510 00511 if (jit_ob3d_set_context(x) == JIT_ERR_NONE) 00512 { 00513 id = x->dlid; 00514 if (x->dlid > 0) 00515 glDeleteLists(id,1); 00516 x->dlid = -1; 00517 } 00518 00519 if(x->vertex) 00520 jit_freebytes(x->vertex, x->vertexbytes); 00521 x->vertexbytes = x->vertexcount = 0; 00522 x->vertex = NULL; 00523 00524 if(x->texcoord) 00525 { 00526 for(i = 0; i < x->unitcount && x->texcoord[i]; i++) 00527 { 00528 jit_freebytes(x->texcoord[i], x->texcoordbytes); 00529 x->texcoord[i] = NULL; 00530 } 00531 jit_freebytes(x->texcoord, x->unitcount * sizeof(float*)); 00532 } 00533 x->texcoordbytes = x->texcoordcount = 0; 00534 x->texcoord = NULL; 00535 } 00536 00537 /*************************************************************************/ 00538 00539 t_jit_err jit_gl_slab_jit_matrix(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00540 { 00541 t_jit_err err = JIT_ERR_NONE; 00542 t_symbol *dest = NULL; 00543 t_jit_object *matrix; 00544 t_jit_matrix_info info; 00545 long i; 00546 00547 t_symbol *name = jit_atom_getsym(argv); 00548 00549 if(name) 00550 { 00551 matrix = (t_jit_object*)jit_object_findregistered(name); 00552 if (!matrix) 00553 { 00554 post ("jit.gl.slab: couldn't get matrix object!"); 00555 return JIT_ERR_GENERIC; 00556 } 00557 00558 if(x->activeinput >= 0 && x->activeinput < JIT_GL_MAX_TEXTUREUNITS) 00559 { 00560 // toggle use flag 00561 x->useinternal[x->activeinput] = TRUE; 00562 00563 // get matrix info 00564 jit_object_method(matrix, _jit_sym_getinfo, &info); 00565 00566 // adapt to leftmost input dim 00567 //if(x->adapt && x->activeinput == 0) 00568 if((x->activeinput == 0 || x->firsttime)) 00569 { 00570 if (x->adapt) { 00571 // use current info and resize output texture 00572 x->dim[0] = info.dim[0]; 00573 if (x->colormode==ps_uyvy) 00574 x->dim[0] *= JIT_GL_SLAB_COLORMODE_SCALAR; 00575 x->dim[1] = info.dim[1]; 00576 x->dim[0] = (long)(ABS((double)x->dim[0]*x->dimscale[0]) + 0.5); 00577 x->dim[1] = (long)(ABS((double)x->dim[1]*x->dimscale[1]) + 0.5); 00578 jit_attr_setlong_array(x->output, _jit_sym_dim, 2, x->dim); 00579 } 00580 if (x->type==ps_auto) 00581 jit_attr_setsym(x->output, _jit_sym_type, info.type); 00582 } 00583 00584 // reset external referenced texture 00585 x->input[x->activeinput] = NULL; 00586 00587 // submit matrix to active texture 00588 jit_gl_texture_jit_matrix(x->internal[x->activeinput], s, argc, argv); 00589 00590 x->firsttime = 0; 00591 00592 // recalc 00593 jit_gl_slab_recalc(x); 00594 } 00595 } 00596 00597 return JIT_ERR_NONE; 00598 } 00599 00600 t_jit_err jit_gl_slab_jit_gl_texture(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00601 { 00602 t_symbol *type; 00603 t_jit_object *texture; 00604 t_jit_err err = JIT_ERR_NONE; 00605 char *bp = NULL; 00606 00607 t_symbol *name = jit_atom_getsym(argv); 00608 00609 if (name) 00610 { 00611 texture = (t_jit_object*)jit_object_findregistered(name); 00612 if (!texture) 00613 { 00614 post ("jit.gl.slab: couldn't get texture object!"); 00615 return JIT_ERR_GENERIC; 00616 } 00617 // post ("jit.gl.slab: recieved texture object: %s", JIT_SYM_SAFECSTR(name)); 00618 00619 if(x->activeinput >= 0 && x->activeinput < JIT_GL_MAX_TEXTUREUNITS) 00620 { 00621 // cache input texture name 00622 x->input[x->activeinput] = name; 00623 00624 // disable internal texture object 00625 x->useinternal[x->activeinput] = FALSE; 00626 00627 // adapt to leftmost input dim 00628 if(x->activeinput == 0) 00629 { 00630 if (x->adapt) { 00631 jit_attr_getlong_array(texture,_jit_sym_dim,2,x->dim); 00632 x->dim[0] = (long)(ABS((double)x->dim[0]*x->dimscale[0]) + 0.5); 00633 x->dim[1] = (long)(ABS((double)x->dim[1]*x->dimscale[1]) + 0.5); 00634 x->dim[2] = 0; 00635 if (x->output) 00636 jit_attr_setlong_array(x->output, _jit_sym_dim, 2, x->dim); 00637 } 00638 00639 if (x->type==ps_auto) { 00640 type = jit_attr_getsym(texture, _jit_sym_type); 00641 if (x->output) 00642 jit_attr_setsym(x->output, _jit_sym_type, type); 00643 } 00644 } 00645 00646 jit_gl_slab_recalc(x); 00647 00648 } 00649 } 00650 00651 return JIT_ERR_NONE; 00652 } 00653 00654 00655 void jit_gl_slab_get_color(t_jit_gl_slab *x, float *rgba) 00656 { 00657 long ac=0; 00658 t_atom *av=NULL; 00659 00660 jit_object_method(x,gensym("getcolor"),&ac,&av); 00661 if ((ac==4)&&av) 00662 { 00663 rgba[0] = jit_atom_getfloat(av); 00664 rgba[1] = jit_atom_getfloat(av+1); 00665 rgba[2] = jit_atom_getfloat(av+2); 00666 rgba[3] = jit_atom_getfloat(av+3); 00667 jit_freebytes(av,ac*sizeof(t_atom)); 00668 } 00669 } 00670 00671 t_jit_err jit_gl_slab_dest_closing(t_jit_gl_slab *x) 00672 { 00673 if (x->dlid > 0) 00674 { 00675 glDeleteLists(x->dlid,1); 00676 x->dlid = -1; 00677 x->update = 1; 00678 } 00679 return JIT_ERR_NONE; 00680 } 00681 00682 t_jit_err jit_gl_slab_drawto(t_jit_gl_slab *x, t_symbol *s, int argc, t_atom *argv) 00683 { 00684 long i; 00685 00686 if (x->shader) 00687 object_attr_setvalueof(x->shader,s,argc,argv); 00688 if (x->output) 00689 object_attr_setvalueof(x->output,s,argc,argv); 00690 00691 for (i=0;i<x->inputcount;i++) 00692 { 00693 if (x->internal[i]) 00694 object_attr_setvalueof(x->internal[i],s,argc,argv); 00695 } 00696 jit_ob3d_dest_name_set((t_jit_object *)x, NULL, argc, argv); 00697 return JIT_ERR_NONE; 00698 } 00699 00700 t_jit_err jit_gl_slab_draw(t_jit_gl_slab *x) 00701 { 00702 GLuint id = 0, t; 00703 t_jit_err err = JIT_ERR_NONE; 00704 t_atom a; 00705 00706 if(!x) 00707 return JIT_ERR_INVALID_PTR; 00708 00709 glMatrixMode(GL_MODELVIEW); 00710 glLoadIdentity(); 00711 glMatrixMode(GL_PROJECTION); 00712 glLoadIdentity(); 00713 gluOrtho2D(0.0, 1.0, 0.0, 1.0); 00714 glMatrixMode(GL_MODELVIEW); 00715 glDisable(GL_LIGHTING); 00716 00717 // bind shader 00718 if (x->shader) 00719 { 00720 jit_atom_setobj(&a, x); 00721 object_method_typed(x->shader, ps_bind, 1, &a, NULL); 00722 } 00723 00724 if (x->update) 00725 { 00726 jit_gl_slab_recalc(x); 00727 00728 if (x->displaylist) 00729 { 00730 if (x->dlid > 0) 00731 { 00732 id = x->dlid; 00733 glDeleteLists(id, 1); 00734 x->dlid = -1; 00735 } 00736 if(x->vertex && x->elements) 00737 { 00738 id = glGenLists(1); 00739 00740 if(id) 00741 { 00742 glNewList(id, GL_COMPILE); 00743 { 00744 // enable and submit vertex array 00745 glEnableClientState(GL_VERTEX_ARRAY); 00746 glVertexPointer(3, GL_FLOAT, 0, x->vertex); 00747 00748 // enable and submit each texture array 00749 for(t = 0; t < (GLuint)x->unitcount; t++) 00750 { 00751 glClientActiveTextureARB(GL_TEXTURE0_ARB + t); 00752 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 00753 glTexCoordPointer(2, GL_FLOAT, 0, x->texcoord[t]); 00754 } 00755 00756 // draw elements 00757 glDrawArrays(GL_QUADS, 0, x->elements * 4); 00758 00759 // disble vertex arrays 00760 glDisableClientState(GL_VERTEX_ARRAY); 00761 00762 // disable each texture array 00763 for(t = 0; t < (GLuint)x->unitcount; t++) 00764 { 00765 glClientActiveTextureARB(GL_TEXTURE0_ARB + t); 00766 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 00767 } 00768 } 00769 glEndList(); 00770 x->dlid = id; 00771 } 00772 } 00773 } 00774 x->update = FALSE; 00775 } 00776 00777 // draw our OpenGL geometry. 00778 jit_gl_slab_draw_grid(x); 00779 00780 // unbind shader 00781 if (x->shader) 00782 { 00783 jit_atom_setobj(&a, x); 00784 object_method_typed(x->shader, ps_unbind, 1, &a, NULL); 00785 } 00786 00787 return err; 00788 } 00789 00790 t_jit_err jit_gl_slab_dest_changed(t_jit_gl_slab *x) 00791 { 00792 long units; 00793 t_jit_err err; 00794 t_atom a[1]; 00795 00796 if (x->dlid>0) 00797 x->update = TRUE; 00798 00799 jit_atom_setsym(a, jit_attr_getsym(x, gensym("drawto"))); 00800 jit_gl_slab_drawto(x, gensym("drawto"), 1, a); 00801 00802 return JIT_ERR_NONE; 00803 } 00804 00805 /*************************************************************************/ 00806 00807 void jit_gl_slab_recalc( t_jit_gl_slab *x ) 00808 { 00809 long i; 00810 t_symbol *textures[JIT_GL_MAX_TEXTUREUNITS]; 00811 t_symbol *name; 00812 00813 // reecreate geometry if input count has changed 00814 if(x->unitcount != x->inputcount) 00815 { 00816 jit_gl_slab_destroy_geometry(x); 00817 x->unitcount = x->inputcount; 00818 jit_gl_slab_make_points(x); 00819 } 00820 00821 // get input texture names for external and internal textures 00822 for(i = 0; i < x->inputcount; i++) 00823 { 00824 if (!x->internal[i]) 00825 { 00826 x->internal[i] = jit_gl_texture_new(jit_attr_getsym(x,gensym("drawto"))); 00827 jit_attr_setsym(x->internal[i],_jit_sym_name,jit_symbol_unique()); 00828 jit_attr_setsym(x->internal[i],gensym("defaultimage"),gensym("black")); 00829 } 00830 00831 if(x->input[i] && !x->useinternal[i]) 00832 name = x->input[i]; 00833 else if (x->useinternal[i]) 00834 name = jit_attr_getsym(x->internal[i], ps_jit_gl_slab_name); 00835 else 00836 name = NULL; 00837 00838 textures[i] = name ? name : _jit_sym_nothing; 00839 // jit_object_post((t_object *)x,"jit.gl.slab: adding texture %d -> %s", i, JIT_SYM_SAFECSTR(textures[i])); 00840 } 00841 00842 // add input textures to ob3d texture list 00843 jit_attr_setsym_array(x, ps_jit_gl_slab_texture, x->inputcount, textures); 00844 00845 // add output texture to ob3d capture list 00846 jit_attr_setsym(x,ps_jit_gl_slab_capture,jit_attr_getsym(x->output, ps_jit_gl_slab_name)); 00847 00848 // dont set ob3d shader attribute, we need to do this internal to our draw routine 00849 // jit_attr_setsym(x,ps_jit_gl_slab_shader,jit_attr_getsym(x->shader, ps_jit_gl_slab_name)); 00850 00851 // disable ob3d automatic mode 00852 jit_attr_setlong(x,ps_jit_gl_slab_automatic,0); 00853 00854 // reset update flag 00855 x->update = FALSE; 00856 } 00857 00858 t_jit_err jit_gl_slab_getattr_out_name(t_jit_gl_slab *x, void *attr, long *ac, t_atom **av) 00859 { 00860 if ((*ac)&&(*av)) { 00861 //memory passed in, use it 00862 } else { 00863 //otherwise allocate memory 00864 *ac = 1; 00865 if (!(*av = jit_getbytes(sizeof(t_atom)*(*ac)))) { 00866 *ac = 0; 00867 return JIT_ERR_OUT_OF_MEM; 00868 } 00869 } 00870 jit_atom_setsym(*av,jit_attr_getsym(x->output,_jit_sym_name)); 00871 // jit_object_post((t_object *)x,"jit.gl.slab: sending output: %s", JIT_SYM_SAFECSTR(jit_attr_getsym(x->output,_jit_sym_name))); 00872 00873 return JIT_ERR_NONE; 00874 } 00875 00876 t_jit_err jit_gl_slab_setattr_dim(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 00877 { 00878 long i; 00879 long v; 00880 long update = FALSE; 00881 00882 if (x) 00883 { 00884 for(i = 0; i < JIT_MATH_MIN(argc, 2); i++) 00885 { 00886 v = jit_atom_getlong(argv+i); 00887 if(x->dim[i] != JIT_MATH_MAX(v,1)) 00888 { 00889 x->dim[i] = v; 00890 update = TRUE; 00891 } 00892 } 00893 if(update) 00894 { 00895 jit_attr_setlong_array(x->output, _jit_sym_dim, 2, x->dim); 00896 jit_gl_slab_recalc(x); 00897 } 00898 00899 return JIT_ERR_NONE; 00900 } 00901 return JIT_ERR_INVALID_PTR; 00902 } 00903 00904 t_jit_err jit_gl_slab_setattr_dimscale(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 00905 { 00906 long i; 00907 double v; 00908 long update = FALSE; 00909 00910 if (x) 00911 { 00912 for(i = 0; i < JIT_MATH_MIN(argc, 2); i++) 00913 { 00914 v = jit_atom_getfloat(argv+i); 00915 if(x->dimscale[i] != JIT_MATH_MIN(v,0.)) 00916 { 00917 x->dimscale[i] = v; 00918 update = TRUE; 00919 } 00920 } 00921 if(update) 00922 { 00923 // don't really want to do this, just update with next adapting matrix 00924 // leaving as placeholder if we need to in future 00925 //jit_attr_setlong_array(x->output, _jit_sym_dim, 2, x->dim); 00926 jit_gl_slab_recalc(x); 00927 } 00928 00929 return JIT_ERR_NONE; 00930 } 00931 return JIT_ERR_INVALID_PTR; 00932 } 00933 00934 t_jit_err jit_gl_slab_setattr_type(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 00935 { 00936 long i; 00937 t_symbol *v; 00938 00939 if (x) 00940 { 00941 v = jit_atom_getsym(argv); 00942 if(x->type != v) 00943 { 00944 x->type = v; 00945 if (x->type!=ps_auto) 00946 jit_attr_setsym(x->output, _jit_sym_type, x->type); 00947 jit_gl_slab_recalc(x); 00948 } 00949 00950 return JIT_ERR_NONE; 00951 } 00952 return JIT_ERR_INVALID_PTR; 00953 } 00954 00955 t_jit_err jit_gl_slab_setattr_adapt(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 00956 { 00957 long i; 00958 long v; 00959 00960 if (x) 00961 { 00962 v = jit_atom_getlong(argv); 00963 v = v > 0 ? TRUE : FALSE; 00964 if(x->adapt != v) 00965 { 00966 x->adapt = v; 00967 jit_gl_slab_recalc(x); 00968 } 00969 00970 return JIT_ERR_NONE; 00971 } 00972 return JIT_ERR_INVALID_PTR; 00973 } 00974 00975 t_jit_err jit_gl_slab_setattr_subdiv(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 00976 { 00977 long i; 00978 long v; 00979 long update = FALSE; 00980 00981 if (x) 00982 { 00983 for(i = 0; i < JIT_MATH_MIN(argc, 2); i++) 00984 { 00985 v = jit_atom_getlong(argv+i); 00986 if(x->subdiv[i] != JIT_MATH_MIN(v,1)) 00987 { 00988 x->subdiv[i] = v; 00989 update = TRUE; 00990 } 00991 } 00992 if(update) 00993 { 00994 jit_gl_slab_make_points(x); 00995 jit_gl_slab_recalc(x); 00996 } 00997 00998 return JIT_ERR_NONE; 00999 } 01000 return JIT_ERR_INVALID_PTR; 01001 } 01002 01003 t_jit_err jit_gl_slab_setattr_activeinput(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01004 { 01005 long i; 01006 long v; 01007 01008 if (x) 01009 { 01010 v = jit_atom_getlong(argv); 01011 if(x->activeinput != v) 01012 { 01013 x->activeinput = v; 01014 //jit_gl_slab_recalc(x); 01015 } 01016 return JIT_ERR_NONE; 01017 } 01018 return JIT_ERR_INVALID_PTR; 01019 } 01020 01021 t_jit_err jit_gl_slab_setattr_inputs(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01022 { 01023 long i; 01024 long v; 01025 01026 if (x) 01027 { 01028 v = jit_atom_getlong(argv); 01029 if(x->inputcount != v) 01030 { 01031 x->inputcount = v; 01032 jit_gl_slab_recalc(x); 01033 } 01034 return JIT_ERR_NONE; 01035 } 01036 return JIT_ERR_INVALID_PTR; 01037 } 01038 01039 t_jit_err jit_gl_slab_setattr_displaylist(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01040 { 01041 long i; 01042 long v; 01043 01044 if (x) 01045 { 01046 v = jit_atom_getlong(argv); 01047 if(x->displaylist != v) 01048 { 01049 x->displaylist = v; 01050 jit_gl_slab_recalc(x); 01051 } 01052 return JIT_ERR_NONE; 01053 } 01054 return JIT_ERR_INVALID_PTR; 01055 } 01056 01057 t_jit_err jit_gl_slab_setattr_depth(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01058 { 01059 long i; 01060 float v; 01061 01062 if (x) 01063 { 01064 v = jit_atom_getfloat(argv); 01065 if(x->depth != v) 01066 { 01067 x->depth = v; 01068 jit_gl_slab_make_points(x); 01069 jit_gl_slab_recalc(x); 01070 } 01071 return JIT_ERR_NONE; 01072 } 01073 return JIT_ERR_INVALID_PTR; 01074 } 01075 01076 01077 t_jit_err jit_gl_slab_setattr_shader(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01078 { 01079 long i; 01080 t_symbol *v; 01081 t_atom a; 01082 01083 if (x&&x->shader) 01084 { 01085 object_attr_setvalueof(x->shader, (t_symbol *) jit_object_method(attr,_jit_sym_getname), argc, argv); 01086 jit_gl_slab_recalc(x); 01087 return JIT_ERR_NONE; 01088 } 01089 return JIT_ERR_INVALID_PTR; 01090 } 01091 01092 t_jit_err jit_gl_slab_getattr_shader(t_jit_gl_slab *x, void *attr, long *argc, t_atom **argv) 01093 { 01094 long i; 01095 t_symbol *v; 01096 t_atom a; 01097 01098 if (x&&x->shader) 01099 { 01100 object_attr_getvalueof(x->shader, (t_symbol *) jit_object_method(attr,_jit_sym_getname), argc, argv); 01101 return JIT_ERR_NONE; 01102 } 01103 return JIT_ERR_INVALID_PTR; 01104 } 01105 01106 t_jit_err jit_gl_slab_setattr_shape(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01107 { 01108 long i; 01109 t_symbol *v; 01110 01111 if (x) 01112 { 01113 v = jit_atom_getsym(argv); 01114 if(v == ps_jit_gl_slab_shape_tri || 01115 v == ps_jit_gl_slab_shape_quad || 01116 v == ps_jit_gl_slab_shape_grid || 01117 v == ps_jit_gl_slab_shape_edges) 01118 { 01119 if(x->shape != v) 01120 { 01121 x->shape = v; 01122 jit_gl_slab_make_points(x); 01123 jit_gl_slab_recalc(x); 01124 } 01125 } 01126 else 01127 { 01128 jit_object_error((t_object *)x,"jit.gl.slab: invalid shape specified."); 01129 } 01130 return JIT_ERR_NONE; 01131 } 01132 return JIT_ERR_INVALID_PTR; 01133 } 01134 01135 t_jit_err jit_gl_slab_setattr_rect(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01136 { 01137 long i; 01138 double v; 01139 long update = FALSE; 01140 01141 if (x) 01142 { 01143 for(i = 0; i < JIT_MATH_MIN(argc, 4); i++) 01144 { 01145 v = jit_atom_getfloat(argv+i); 01146 if(x->rect[i] != v) 01147 { 01148 x->rect[i] = v; 01149 update = TRUE; 01150 } 01151 } 01152 if(update) 01153 { 01154 jit_gl_slab_make_points(x); 01155 jit_gl_slab_recalc(x); 01156 } 01157 01158 return JIT_ERR_NONE; 01159 } 01160 return JIT_ERR_INVALID_PTR; 01161 } 01162 01163 t_jit_err jit_gl_slab_setattr_texrect(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01164 { 01165 long i; 01166 double v; 01167 long update = FALSE; 01168 01169 if (x) 01170 { 01171 for(i = 0; i < JIT_MATH_MIN(argc, 4 * JIT_GL_MAX_TEXTUREUNITS); i++) 01172 { 01173 v = jit_atom_getfloat(argv+i); 01174 if(x->texrect[i] != v) 01175 { 01176 x->texrect[i] = v; 01177 update = TRUE; 01178 } 01179 } 01180 if(update) 01181 { 01182 jit_gl_slab_make_points(x); 01183 jit_gl_slab_recalc(x); 01184 } 01185 01186 return JIT_ERR_NONE; 01187 } 01188 return JIT_ERR_INVALID_PTR; 01189 } 01190 01191 t_jit_err jit_gl_slab_setattr_edges(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01192 { 01193 long i; 01194 long v; 01195 long update = FALSE; 01196 01197 if (x) 01198 { 01199 for(i = 0; i < JIT_MATH_MIN(argc, 4); i++) 01200 { 01201 v = jit_atom_getlong(argv+i); 01202 v = (v > 0) ? TRUE : FALSE; 01203 if(x->edges[i] != v) 01204 { 01205 x->edges[i] = v; 01206 update = TRUE; 01207 } 01208 } 01209 if(update) 01210 { 01211 if(x->shape == ps_jit_gl_slab_shape_edges) 01212 jit_gl_slab_make_points(x); 01213 jit_gl_slab_recalc(x); 01214 } 01215 01216 return JIT_ERR_NONE; 01217 } 01218 return JIT_ERR_INVALID_PTR; 01219 } 01220 01221 t_jit_err jit_gl_slab_setattr_wrap(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01222 { 01223 long i; 01224 long v; 01225 long update = FALSE; 01226 01227 if (x) 01228 { 01229 for(i = 0; i < JIT_MATH_MIN(argc, 4); i++) 01230 { 01231 v = jit_atom_getlong(argv+i); 01232 v = (v > 0) ? TRUE : FALSE; 01233 if(x->wrap[i] != v) 01234 { 01235 x->wrap[i] = v; 01236 update = TRUE; 01237 } 01238 } 01239 if(update) 01240 { 01241 if(x->shape == ps_jit_gl_slab_shape_edges) 01242 jit_gl_slab_make_points(x); 01243 jit_gl_slab_recalc(x); 01244 } 01245 return JIT_ERR_NONE; 01246 } 01247 return JIT_ERR_INVALID_PTR; 01248 } 01249 01250 t_jit_err jit_gl_slab_setattr_colormode(t_jit_gl_slab *x, void *attr, long argc, t_atom *argv) 01251 { 01252 long i; 01253 t_symbol *v; 01254 01255 if (x) 01256 { 01257 v = jit_atom_getsym(argv); 01258 if(v == ps_argb || 01259 v == ps_uyvy) 01260 { 01261 if(x->colormode != v) 01262 { 01263 x->colormode = v; 01264 for (i=0;i<x->inputcount;i++) { 01265 if (x->internal[i]) 01266 { 01267 jit_attr_setsym(x->internal[i],ps_colormode,x->colormode); 01268 } 01269 } 01270 jit_gl_slab_recalc(x); 01271 } 01272 } 01273 else 01274 { 01275 jit_object_error((t_object *)x,"jit.gl.slab: invalid colormode specified."); 01276 } 01277 return JIT_ERR_NONE; 01278 } 01279 return JIT_ERR_INVALID_PTR; 01280 } 01281 01282 01283 //****************************************************************************** 01284 01285 void jit_gl_slab_draw_grid(t_jit_gl_slab *x) 01286 { 01287 long i, t; 01288 float color[4]; 01289 float center[2]; 01290 t_jit_gl_drawinfo drawinfo; 01291 01292 if(!x || !jit_gl_get_context()) 01293 return; 01294 01295 center[0] = ((x->rect[MAX_X] - x->rect[MIN_X]) * 0.5f) + x->rect[MIN_X]; 01296 center[1] = ((x->rect[MAX_Y] - x->rect[MIN_Y]) * 0.5f) + x->rect[MIN_Y]; 01297 01298 // get the ob3d color 01299 jit_gl_slab_get_color(x, color); 01300 01301 if(x->displaylist && (x->dlid > 0)) 01302 { 01303 // use display list 01304 glColor4fv(color); 01305 glNormal3f(center[0], center[1], 1.0); 01306 glCallList(x->dlid); 01307 } 01308 else if(x->vertex && x->elements && x->texcoord) 01309 { 01310 glColor4fv(color); 01311 glNormal3f(center[0], center[1], 1.0); 01312 01313 // enable and submit vertex array 01314 glEnableClientState(GL_VERTEX_ARRAY); 01315 glVertexPointer(3, GL_FLOAT, 0, x->vertex); 01316 01317 // enable and submit each texture array 01318 for(t = 0; t < x->unitcount; t++) 01319 { 01320 glClientActiveTextureARB(GL_TEXTURE0_ARB + t); 01321 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 01322 glTexCoordPointer(2, GL_FLOAT, 0, x->texcoord[t]); 01323 } 01324 01325 // draw elements 01326 glDrawArrays(GL_QUADS, 0, x->elements * 4); 01327 01328 // disble vertex arrays 01329 glDisableClientState(GL_VERTEX_ARRAY); 01330 01331 // disable each texture array 01332 for(t = 0; t < x->unitcount; t++) 01333 { 01334 glClientActiveTextureARB(GL_TEXTURE0_ARB + t); 01335 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 01336 } 01337 } 01338 } 01339 01340 t_jit_err jit_gl_slab_make_points(t_jit_gl_slab *x) 01341 { 01342 float dist; 01343 long i, j, t, it; 01344 long w, h; 01345 float fx, fy; 01346 float sx, sy; 01347 float cx, cy; 01348 float dx, dy; 01349 float fs[JIT_GL_MAX_TEXTUREUNITS], ft[JIT_GL_MAX_TEXTUREUNITS]; 01350 float ss[JIT_GL_MAX_TEXTUREUNITS], st[JIT_GL_MAX_TEXTUREUNITS]; 01351 float cs[JIT_GL_MAX_TEXTUREUNITS], ct[JIT_GL_MAX_TEXTUREUNITS]; 01352 float ds[JIT_GL_MAX_TEXTUREUNITS], dt[JIT_GL_MAX_TEXTUREUNITS]; 01353 float *pv, *pt[JIT_GL_MAX_TEXTUREUNITS]; 01354 float pw, ph; 01355 float tw, th; 01356 01357 // destroy geometry 01358 jit_gl_slab_destroy_geometry(x); 01359 01360 // get width and height 01361 w = x->subdiv[0] + 1; 01362 h = x->subdiv[1] + 1; 01363 01364 // setup for grid mode 01365 if(x->shape != ps_jit_gl_slab_shape_grid) 01366 { 01367 w = h = 2; 01368 } 01369 01370 // allocate 01371 x->vertexcount = 3 * w * h * 4; 01372 x->vertexbytes = x->vertexcount * sizeof(float); 01373 x->vertex = jit_getbytes(x->vertexbytes); 01374 if(!x->vertex) 01375 { 01376 x->vertexbytes = x->vertexcount = 0; 01377 x->vertex = NULL; 01378 return JIT_ERR_INVALID_PTR; 01379 } 01380 01381 pv = x->vertex; 01382 01383 // allocate 01384 x->texcoord = jit_getbytes(x->unitcount * sizeof(float*)); 01385 if(!x->texcoord) 01386 { 01387 x->texcoordbytes = x->texcoordcount = 0; 01388 x->texcoord = NULL; 01389 return JIT_ERR_INVALID_PTR; 01390 } 01391 01392 x->texcoordcount = 2 * w * h * 4; 01393 x->texcoordbytes = x->texcoordcount * sizeof(float); 01394 for(i = 0; i < x->unitcount; i++) 01395 { 01396 x->texcoord[i] = jit_getbytes(x->texcoordbytes); 01397 if(!x->texcoord[i]) 01398 { 01399 x->texcoord[i] = NULL; 01400 return JIT_ERR_INVALID_PTR; 01401 } 01402 01403 pt[i] = x->texcoord[i]; 01404 } 01405 01406 if(x->shape == ps_jit_gl_slab_shape_quad) 01407 { 01408 it = 0; 01409 x->elements = 1; 01410 x->geomtype = GL_QUADS; 01411 01412 // LL 01413 for (i = 0; i < x->unitcount; i++) 01414 { 01415 *(pt[i]) = x->texrect[MIN_S]; pt[i]++; 01416 *(pt[i]) = x->texrect[MIN_T]; pt[i]++; 01417 } 01418 *pv = x->rect[MIN_X]; pv++; 01419 *pv = x->rect[MIN_Y]; pv++; 01420 *pv = x->depth; pv++; 01421 01422 // LR 01423 for (i = 0; i < x->unitcount; i++) 01424 { 01425 *(pt[i]) = x->texrect[MAX_S]; pt[i]++; 01426 *(pt[i]) = x->texrect[MIN_T]; pt[i]++; 01427 } 01428 *pv = x->rect[MAX_X]; pv++; 01429 *pv = x->rect[MIN_Y]; pv++; 01430 *pv = x->depth; pv++; 01431 01432 // UR 01433 for (i = 0; i < x->unitcount; i++) 01434 { 01435 *(pt[i]) = x->texrect[MAX_S]; pt[i]++; 01436 *(pt[i]) = x->texrect[MAX_T]; pt[i]++; 01437 } 01438 *pv = x->rect[MAX_X]; pv++; 01439 *pv = x->rect[MAX_Y]; pv++; 01440 *pv = x->depth; pv++; 01441 01442 01443 // UL 01444 for (i = 0; i < x->unitcount; i++) 01445 { 01446 *(pt[i]) = x->texrect[MIN_S]; pt[i]++; 01447 *(pt[i]) = x->texrect[MAX_T]; pt[i]++; 01448 } 01449 *pv = x->rect[MIN_X]; pv++; 01450 *pv = x->rect[MAX_Y]; pv++; 01451 *pv = x->depth; pv++; 01452 } 01453 else if(x->shape == ps_jit_gl_slab_shape_grid) 01454 { 01455 x->geomtype = GL_QUADS; 01456 sx = x->rect[MAX_X] - x->rect[MIN_X]; 01457 sy = x->rect[MAX_Y] - x->rect[MIN_Y]; 01458 cx = (sx * 0.5f) + x->rect[MAX_X]; 01459 cy = (sy * 0.5f) + x->rect[MAX_Y]; 01460 dx = sx / (float)x->subdiv[0]; 01461 dy = sy / (float)x->subdiv[1]; 01462 01463 for(j = 0; j < x->unitcount; j++) 01464 { 01465 fs[j] = x->texrect[j * 2 + MIN_S]; 01466 ft[j] = x->texrect[j * 2 + MIN_T]; 01467 ss[j] = x->texrect[j * 2 + MAX_S] - x->texrect[j * 2 + MIN_S]; 01468 st[j] = x->texrect[j * 2 + MAX_T] - x->texrect[j * 2 + MIN_T]; 01469 cs[j] = (ss[j] * 0.5f) + x->texrect[j * 2 + MIN_S]; 01470 ct[j] = (st[j] * 0.5f) + x->texrect[j * 2 + MIN_T]; 01471 ds[j] = ss[j] / (float)x->subdiv[0]; 01472 dt[j] = st[j] / (float)x->subdiv[1]; 01473 } 01474 01475 it = 0; 01476 x->elements = 0; 01477 for (fy = x->rect[MIN_Y]; fy < x->rect[MAX_Y]; fy += dy) 01478 { 01479 for (i = 0; i < x->unitcount; i++) 01480 ft[i] = x->texrect[i * 2 + MIN_T]; 01481 01482 for (fx = x->rect[MIN_X]; fx < x->rect[MAX_X]; fx += dx) 01483 { 01484 x->elements++; 01485 01486 // LL 01487 for (i = 0; i < x->unitcount; i++) 01488 { 01489 *(pt[i]) = fs[i]; pt[i]++; 01490 *(pt[i]) = ft[i]; pt[i]++; 01491 } 01492 *pv = fx; pv++; 01493 *pv = fy; pv++; 01494 *pv = x->depth; pv++; 01495 01496 // LR 01497 for (i = 0; i < x->unitcount; i++) 01498 { 01499 *(pt[i]) = fs[i]; pt[i]++; 01500 *(pt[i]) = ft[i] + dt[i]; pt[i]++; 01501 } 01502 *pv = fx; pv++; 01503 *pv = fy + dy; pv++; 01504 *pv = x->depth; pv++; 01505 01506 // UR 01507 for (i = 0; i < x->unitcount; i++) 01508 { 01509 *(pt[i]) = fs[i] + ds[i]; pt[i]++; 01510 *(pt[i]) = ft[i] + dt[i]; pt[i]++; 01511 } 01512 *pv = fx + dx; pv++; 01513 *pv = fy + dy; pv++; 01514 *pv = x->depth; pv++; 01515 01516 // UL 01517 for (i = 0; i < x->unitcount; i++) 01518 { 01519 *(pt[i]) = fs[i] + ds[i]; pt[i]++; 01520 *(pt[i]) = ft[i]; pt[i]++; 01521 } 01522 *pv = fx + dx; pv++; 01523 *pv = fy; pv++; 01524 *pv = x->depth; pv++; 01525 01526 for (i = 0; i < x->unitcount; i++) 01527 fs[i] += ds[i]; 01528 } 01529 for (i = 0; i < x->unitcount; i++) 01530 ft[i] += dt[i]; 01531 } 01532 } 01533 else if(x->shape == ps_jit_gl_slab_shape_edges) 01534 { 01535 x->geomtype = GL_LINES; 01536 01537 // calc pixel dim 01538 pw = (x->rect[MAX_X] - x->rect[MIN_X]) / (x->texrect[0 * 2 + MAX_S] - x->texrect[0 * 2 + MIN_S]); 01539 ph = (x->rect[MAX_Y] - x->rect[MIN_Y]) / (x->texrect[0 * 2 + MAX_T] - x->texrect[0 * 2 + MIN_T]); 01540 01541 // calc texel dim 01542 tw = (x->texrect[0 * 2 + MAX_S] - x->texrect[0 * 2 + MIN_S]) / x->dim[0]; 01543 th = (x->texrect[0 * 2 + MAX_T] - x->texrect[0 * 2 + MIN_T]) / x->dim[1]; 01544 01545 // left boundary 01546 it = 0; 01547 if (x->edges[EDGE_L]) 01548 { 01549 for (i = 0; i < x->unitcount; i++) 01550 { 01551 if(i == 0) 01552 { 01553 // base texture 01554 *(pt[i]) = x->texrect[0 * 2 + MIN_S]; pt[i]++; 01555 *(pt[i]) = x->texrect[0 * 2 + MIN_T]; pt[i]++; 01556 *(pt[i]) = x->texrect[0 * 2 + MIN_S]; pt[i]++; 01557 *(pt[i]) = x->texrect[0 * 2 + MAX_T]; pt[i]++; 01558 } 01559 else 01560 { 01561 // edge texture 01562 *(pt[i]) = x->wrap[EDGE_L] ? x->dim[0] - 2 * tw : tw; pt[i]++; 01563 *(pt[i]) = 0; pt[i]++; 01564 } 01565 } 01566 01567 *pv = x->rect[MIN_X]; pv++; 01568 *pv = x->rect[MIN_Y]; pv++; 01569 *pv = x->depth; pv++; 01570 01571 *pv = x->rect[MIN_X]; pv++; 01572 *pv = x->rect[MAX_Y]; pv++; 01573 *pv = x->depth; pv++; 01574 } 01575 01576 // right boundary 01577 if (x->edges[EDGE_R]) 01578 { 01579 for (i = 0; i < x->unitcount; i++) 01580 { 01581 if(i == 0) 01582 { 01583 // base texture 01584 *(pt[i]) = x->texrect[0 * 2 + MAX_S] - tw; pt[i]++; 01585 *(pt[i]) = x->texrect[0 * 2 + MIN_T]; pt[i]++; 01586 *(pt[i]) = x->texrect[0 * 2 + MAX_S] - tw; pt[i]++; 01587 *(pt[i]) = x->texrect[0 * 2 + MAX_T]; pt[i]++; 01588 } 01589 else 01590 { 01591 // edge texture 01592 *(pt[i]) = x->wrap[EDGE_R] ? x->dim[0] + 2 * tw : -tw; pt[i]++; 01593 *(pt[i]) = 0; pt[i]++; 01594 } 01595 } 01596 01597 *pv = x->rect[MAX_X] - pw; pv++; 01598 *pv = x->rect[MIN_Y]; pv++; 01599 *pv = x->depth; pv++; 01600 01601 *pv = x->rect[MAX_X] - pw; pv++; 01602 *pv = x->rect[MAX_Y]; pv++; 01603 *pv = x->depth; pv++; 01604 } 01605 01606 // bottom boundary 01607 if (x->edges[EDGE_B]) 01608 { 01609 for (i = 0; i < x->unitcount; i++) 01610 { 01611 if(i == 0) 01612 { 01613 // base texture 01614 *(pt[i]) = x->texrect[0 * 2 + MIN_S]; pt[i]++; 01615 *(pt[i]) = x->texrect[0 * 2 + MIN_T]; pt[i]++; 01616 *(pt[i]) = x->texrect[0 * 2 + MAX_S]; pt[i]++; 01617 *(pt[i]) = x->texrect[0 * 2 + MIN_T]; pt[i]++; 01618 } 01619 else 01620 { 01621 // edge texture 01622 *(pt[i]) = 0; pt[i]++; 01623 *(pt[i]) = x->wrap[EDGE_B] ? x->dim[1] - 2 * th : th; pt[i]++; 01624 } 01625 } 01626 01627 *pv = x->rect[MIN_X]; pv++; 01628 *pv = x->rect[MIN_Y] + 0.5f * ph; pv++; 01629 *pv = x->depth; pv++; 01630 01631 *pv = x->rect[MAX_X] - pw; pv++; 01632 *pv = x->rect[MAX_Y] + 0.5f * ph; pv++; 01633 *pv = x->depth; pv++; 01634 } 01635 01636 // top boundary 01637 if (x->edges[EDGE_T]) 01638 { 01639 for (i = 0; i < x->unitcount; i++) 01640 { 01641 if(i == 0) 01642 { 01643 // base texture 01644 *(pt[i]) = x->texrect[0 * 2 + MIN_S]; pt[i]++; 01645 *(pt[i]) = x->texrect[0 * 2 + MAX_T] - th; pt[i]++; 01646 *(pt[i]) = x->texrect[0 * 2 + MAX_S]; pt[i]++; 01647 *(pt[i]) = x->texrect[0 * 2 + MAX_T] - th; pt[i]++; 01648 } 01649 else 01650 { 01651 // edge texture 01652 *(pt[i]) = 0; pt[i]++; 01653 *(pt[i]) = x->wrap[EDGE_T] ? x->dim[1] + 2 * th : -th; pt[i]++; 01654 } 01655 } 01656 01657 *pv = x->rect[MIN_X]; pv++; 01658 *pv = x->rect[MAX_Y] - 0.5f * ph; pv++; 01659 *pv = x->depth; pv++; 01660 01661 *pv = x->rect[MAX_X]; pv++; 01662 *pv = x->rect[MAX_Y] - 0.5f * ph; pv++; 01663 *pv = x->depth; pv++; 01664 } 01665 } 01666 return JIT_ERR_NONE; 01667 }
Copyright © 2008, Cycling '74