Buffer offsets from Sys.getDirectBufferAddress();

Say I want to use the following method:

unProject(double winx, double winy, double winz, int modelMatrix, int projMatrix, int viewport, int objx, int objy, int objz);

The last 3 parameters and pointers to doubles. One way to use this method is to create 3 different one double long buffers, get their addresses and give them to unProject. Is there a way to use just one? For example, do something like this:

DoubleBuffer xyz = ByteBuffer.allocateDirect(3 * 8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
int xyzP = Sys.getDirectBufferAddress(xyz);

and then:

unProject(winx, winy, winz, modelMatrix, projMatrix, viewport, xyzP, xyzP + 2, xyzP + 4);

where xyzP, the address of the first double
where xyzP + 2, the address of the second double
where xyzP + 4, the address of the third double

I have tested this and of course it didn’t work. Actually, it run, but it didn’t work correctly. Could something like this work, use one buffer’s starting address to get offsets on the same buffer? Am I correct to assume that the second double is 2 addresses (2 * 32 bits = 64 bits) away from the first one? I’m asking this because I’m afraid of the overhead of many Buffers (should I?) and besides my code gets really ugly. It would be a lot easier and cleaner to use xyz.get(double_array[3]) than have three different buffers and use buffer.get() three times. It would be faster too. Any suggestions?

[quote]Am I correct to assume that the second double is 2 addresses (2 * 32 bits = 64 bits)
[/quote]
Odds are I think that the addresses are going to be either byte or 16-bit word addressable, so your offsets of +2 and +4 should be doubled at least. I was using something similar for gluProject a little while ago, but i can’t remember whether I got it working or not (or even if its cross-platform friendly…).

Yes, and it’s a necessary evil to be sure. Offsets are in bytes so a double would need +8, +16 etc.

Cas :slight_smile: