Forums > Dev

using jitter gl objects in c

March 11, 2007 | 6:37 pm

Hi, I’m trying to combine some gl objects in my c code but I can’t figure
out how to instantiate or draw them. I have the following in my externals
new function:

if(!(x->jit_gl_mesh = jit_object_new(gensym("jit_gl_mesh"),
gensym("ctxt1"))))
{
error("graph2D: couldn’t instantiate jit_gl_mesh");
return NULL;
}

and later on:

jit_atom_setsym(&atom, x->mod.mx_name);
object_attr_setvalueof(x->jit_gl_mesh, gensym("vertex_matrix"), 1,
&atom);

The object seems to be valid, and I’m sure about the matrix data, but I
can’t get it to show up in my rendering window.

If I try to do the same with jit_gl_gridshape or jit_gl_nurbs, they won’t
even instantiate using the jit_object_new method. I then tried jit_ob3d_new
instead, and the objects are instantiated, but they don’t show up either and
I get zgetfn errors about corrupt objects.

I’m clearly missing something here. Any clues?

cheers, -thijs


March 11, 2007 | 6:48 pm

Hi Thijs,
Your code is mostly correct except that vertex_matrix is not an
attribute but a method. I believe this should work.

jit_atom_setsym(&atom, x->mod.mx_name);
jit_object_method(x->jit_gl_mesh, gensym("vertex_matrix"), 1, &atom);

wes

On 3/11/07, Thijs Koerselman

wrote:
> Hi, I’m trying to combine some gl objects in my c code but I can’t figure
> out how to instantiate or draw them. I have the following in my externals
> new function:
>
> if(!(x->jit_gl_mesh =
> jit_object_new(gensym("jit_gl_mesh"), gensym("ctxt1"))))
> {
> error("graph2D: couldn’t instantiate jit_gl_mesh");
> return NULL;
> }
>
> and later on:
>
> jit_atom_setsym(&atom, x->mod.mx_name);
> object_attr_setvalueof(x->jit_gl_mesh,
> gensym("vertex_matrix"), 1, &atom);
>
>
> The object seems to be valid, and I’m sure about the matrix data, but I
> can’t get it to show up in my rendering window.
>
> If I try to do the same with jit_gl_gridshape or jit_gl_nurbs, they won’t
> even instantiate using the jit_object_new method. I then tried jit_ob3d_new
> instead, and the objects are instantiated, but they don’t show up either and
> I get zgetfn errors about corrupt objects.
>
> I’m clearly missing something here. Any clues?
>
> cheers, -thijs
>
>
>
>
>


March 11, 2007 | 7:45 pm

On 3/11/07, Wesley Smith wrote:
>
> Hi Thijs,
> Your code is mostly correct except that vertex_matrix is not an
> attribute but a method. I believe this should work.
>
> jit_atom_setsym(&atom, x->mod.mx_name);
> jit_object_method(x->jit_gl_mesh, gensym("vertex_matrix"), 1, &atom);

Thanks Wes, that was obviously a mistake. However its still not working. I
get the following error

"jit.gl.mesh: couldn’t get matrix object!"

I’m sure I pass it the atom with the correct symbol. I can view the content
of the same named matrix from within my patch, so I know it is valid /
existing. I have no idea why this isn’t working.

Also something like this:

jit_object_new(gensym("jit_gl_text2d"), ctxt)

returns null for gl.text2d, gl.nurbs, and gl.gridshape. So far gl.mesh seems
to be the only one that is compatible.

-thijs


March 11, 2007 | 9:02 pm

Hi Thijs,
Here’s what’s happening. For the mesh matrix methods, you have to do
the following:

t_atom a[1];
jit_atom_setsym(a, gensym("verts"));
jit_object_method(x->mesh, gensym("vertex_matrix"),
gensym("vertex_matrix"), 1, a);

One of the vertex_matrix symbols will be consumed by the message
passing infrastructure of max and one will be passed on to the mesh
function.

As for the other objects that won’t load, you have to explicitly load
their bundle from disk. The reason mesh works is that it is compiled
into jitlib and is loaded when jitlib is loaded. The others are only
externals and if the object hasn’t yet been used in a Max session,
when you go to create it from within your object, max has no idea
about it yet. So, you need to explicitly load the object (basically
call its init() method). This is one way to do that:

void load_bundle(t_symbol *realname, t_symbol *diskname)
{
void *c, *p;

c = (void *)class_findbyname(gensym("jitter"),realname);

if (!c) {
if (p=newinstance(diskname,0,NULL)) {
c = (void *)class_findbyname(gensym("jitter"),realname);
freeobject(p);
p = NULL;
}
}
}

load_bundle(gensym("jit_gl_nurbs"), gensym("jit.gl.nurbs"));
void *ob = jit_object_new(gensym("jit_gl_nurbs"),jit_attr_getsym(x, ps_drawto));

wes


March 11, 2007 | 11:40 pm

On 3/11/07, Wesley Smith wrote:
>
> Hi Thijs,
> Here’s what’s happening. For the mesh matrix methods, you have to do
> the following:
>
> t_atom a[1];
> jit_atom_setsym(a, gensym("verts"));
> jit_object_method(x->mesh, gensym("vertex_matrix"),
> gensym("vertex_matrix"), 1, a);
>
> One of the vertex_matrix symbols will be consumed by the message
> passing infrastructure of max and one will be passed on to the mesh
> function.
>
> As for the other objects that won’t load, you have to explicitly load
> their bundle from disk. The reason mesh works is that it is compiled
> into jitlib and is loaded when jitlib is loaded. The others are only
> externals and if the object hasn’t yet been used in a Max session,
> when you go to create it from within your object, max has no idea
> about it yet. So, you need to explicitly load the object (basically
> call its init() method). This is one way to do that:
>
> void load_bundle(t_symbol *realname, t_symbol *diskname)
> {
> void *c, *p;
>
> c = (void *)class_findbyname(gensym("jitter"),realname);
>
> if (!c) {
> if (p=newinstance(diskname,0,NULL)) {
> c = (void
> *)class_findbyname(gensym("jitter"),realname);
> freeobject(p);
> p = NULL;
> }
> }
> }
>
> load_bundle(gensym("jit_gl_nurbs"), gensym("jit.gl.nurbs"));
> void *ob = jit_object_new(gensym("jit_gl_nurbs"),jit_attr_getsym(x,
> ps_drawto));
>
>
Awesome thanks a lot! I got it all working now. I’m wondering what is
exactly the reason for the double vertex_matrix message? Just curious in
case it applies to more situations I’ll come across.

Im porting some js to c and there are still 2 function calls that I don’t
know how to approach in my code. Hope you can help me with these as well ;-)

how to process 2 input matrices to one output like in javascript:
repos.matrixcalc([mx_row, mx_repos], mx_tmp2);

how to use exprfill:
mx_repos.exprfill("norm[0]*dim[0]"); // all planes

or
mx_color.exprfill(3, "norm[0]+0.1"); // specified plane

I tried:

jit_object_method(x->mx_repos, gensym("exprfill"), gensym("norm[0]*dim[0]"),
0L);

….but that doesn’t seem to do anything. Hope I’m not asking too much. I
find these things very hard to figure out, as there is little or no
documentation on this. Anyway thanks again for the help so far.

best, -thijs


March 11, 2007 | 11:50 pm

> how to process 2 input matrices to one output like in javascript:
> repos.matrixcalc([mx_row, mx_repos], mx_tmp2);

jit_object_method(repos, gensym("matrix_calc"), input_list, output_list);

where input_list and output_list are linklists like
list = jit_object_new(_jit_sym_jit_linklist);

You can add elements to a link list as follows
jit_object_method(list, _jit_sym_append, matrix);

In the case of the expr_fill methods, check out the JitterSDK online docs:

http://cycling74.com/download/JitterAPIReference/

The Matrix Module should show you how to use exprfill.

t_jit_err jit_matrix_exprfill ( t_jit_matrix * x,
t_symbol * s,
long argc,
t_atom * argv
)

Fills cells according to the jit.expr expression provided.

See Jitter user documentation for more information.

Parameters:
x t_jit_matrix object pointer
s message symbol pointer
argc argument count
argv argument vector

Returns:
t_jit_err error code

Warning:
This function is not exported, but is provided for reference when
calling via jit_object_method on an intance of t_jit_matrix.

wes


March 12, 2007 | 1:22 pm

On 3/11/07, Wesley Smith wrote:
>
> > how to process 2 input matrices to one output like in javascript:
> > repos.matrixcalc([mx_row, mx_repos], mx_tmp2);
>
> jit_object_method(repos, gensym("matrix_calc"), input_list, output_list);
>
> where input_list and output_list are linklists like
> list = jit_object_new(_jit_sym_jit_linklist);
>
> You can add elements to a link list as follows
> jit_object_method(list, _jit_sym_append, matrix);

Thanks I would have never guessed that :-) Afaik the only place in the docs
that talks about linked lists for matrix_calc is in the API linked_list
module, which I never read before.

> In the case of the expr_fill methods, check out the JitterSDK online docs:
> http://cycling74.com/download/JitterAPIReference/
>
> The Matrix Module should show you how to use exprfill.
>
> t_jit_err jit_matrix_exprfill ( t_jit_matrix * x,
> t_symbol * s,
> long argc,
> t_atom * argv
> )

I did read the API of course but still didn’t know what to do. I now
understand the same message passing principle applies as with my
vertex_matrix problem. So just for the archives, exprfill "norm[0]" is done
like this

jit_atom_setsym(&atom, gensym("norm[0]"));
jit_object_method(x->matrix_obj, gensym("exprfill"), gensym("exprfill"), 1,
&atom);

cheers, -thijs


March 12, 2007 | 3:54 pm

The docs may be a bit terse but they do have alot of info in them once
you start to find your way around. Also, you may want to cache
commonly used gensym() results like so:

static t_symbol *ps_myobject_stuff;

//in init function

init()
{
ps_myobject_stuff = gensym("stuff");
}

It will save you some performance in sections where gensym() is used
in draw loops of the like.

wes


March 13, 2007 | 11:51 am

On 3/12/07, Wesley Smith wrote:
>
> The docs may be a bit terse but they do have alot of info in them once
> you start to find your way around. Also, you may want to cache
> commonly used gensym() results like so:
>
> static t_symbol *ps_myobject_stuff;
>
> //in init function
>
> init()
> {
> ps_myobject_stuff = gensym("stuff");
> }
>
>
> It will save you some performance in sections where gensym() is used
> in draw loops of the like.

I’m already caching symbols global to the library that my objects are part
of, but local caching object symbol caching for performance loops is a good
idea. Thanks.

-thijs


Viewing 9 posts - 1 through 9 (of 9 total)