Forum: JRuby Calling java method with output parameter

Posted by neo chuky (neochuky)
on 2012-09-20 21:17
Hello,

I am testing some code with jruby and I use the opengl method
glGenTextures(GLsizei n, GLuint *textures) that I have previously
imported with java_import. The second parameter is a vector where the
generated textures will be stored.

 @textures = [-1]
 glGenTextures(1, @textures.to_java(:int), 0)

The thing is the code works fine, but the value of the elements in
@textures should change but it does not. Any ideas of what I am doing
wrong?



Thanks
Posted by Keith B. (keith_b)
on 2012-09-20 21:47
(Received via mailing list)
I don't know anything about the internals, but my guess is that the 
to_java is a one way thing.

That is, to_java would create an object that Java would understand, but 
there is no ability for that Java object's modifications to be written 
back to the original Ruby object.

Just a guess, though.

Maybe you could create the to_java object before the call, and then load 
the results back after the call, something like this, if the Java object 
is an array?:

java_array = [].to_java
glGenTextures(1, java_array, 0)
@ruby_array = java_array.inject([]) { |ra, x| ra << x }

or if it's a list:

java_list = java.util.ArrayList.new
glGenTextures(1, java_list, 0)
@ruby_array = java_list.inject([]) { |ra, x| ra << x }

or if it's a Vector, just replace 'ArrayList' with 'Vector'.

- Keith
Posted by Charles Nutter (headius)
on 2012-09-21 00:30
(Received via mailing list)
On Thu, Sep 20, 2012 at 12:17 PM, neo chuky <lists@ruby-forum.com> 
wrote:
> The thing is the code works fine, but the value of the elements in
> @textures should change but it does not. Any ideas of what I am doing
> wrong?

The array you pass in is only used for the call and never used again.

to_java on an Array creates a *new* Java array with the same contents.
So what you really want is:

@textures = [-1].to_java(:int)
glGenTexture(1, @textures, 0)

And it should contain the results.

If you don't need to pass any "in" values, you can also do:

@textures = Java::int[1].new

- Charlie
Posted by neo chuky (neochuky)
on 2012-09-22 17:51
> @textures = [-1].to_java(:int)
> glGenTexture(1, @textures, 0)

Works as expected, thanks for such a quick and precise answers.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.