Calling java method with output parameter

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

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

On Thu, Sep 20, 2012 at 12:17 PM, neo chuky [email protected]
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

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

Works as expected, thanks for such a quick and precise answers.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs