gluProject

Started by Optus, January 13, 2004, 17:48:49

Previous topic - Next topic

Optus

Hi.  I have a game I'm working that uses both perspective and ortho modes.  At certain times I will want to display text above a specific character, for example. I'm supposed to use gluProject for this, I think, but can't get it to work.
DoubleBuffer modelview = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
DoubleBuffer projection = ByteBuffer.allocateDirect(16*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
IntBuffer viewport = ByteBuffer.allocateDirect(4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
DoubleBuffer result = ByteBuffer.allocateDirect(3*8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
GL.glGetDouble(GL.GL_MODELVIEW_MATRIX, modelview);
GL.glGetDouble(GL.GL_PROJECTION_MATRIX, projection);
GL.glGetInteger(GL.GL_VIEWPORT, viewport);

modelview.flip();
projection.flip();
viewport.flip();

GLU.gluProject(plyr.pos.X, plyr.pos.Y, plyr.pos.Z, modelview, projection, viewport, result);

result.rewind();
float x = (float)result.get();
float y = (float)result.get();
float z = (float)result.get();

System.out.println("Located to: "+x+", "+y+", "+z);


Am I flipping the buffers incorrectly, I still am not always sure how to pass buffers to lwjgl?  I currently am running my game in a 1024x768 window, but I get values like 1663, 330 which are off the screen.

Fool Running

I noticed your post is old, but if you still are having problems with it.   :)

I have had no luck with the flip() method and have used rewind() for all calls to LWJGL and it has worked for me, so my suggestion is to try do a rewind instead of a flip().

Don't know it it will work without trying it though  :wink:
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

princec

You only use flip() after you've used put() to put stuff into the buffers. The GL methods never change the position of the buffers, so the code above would set limit() to 0 and you'd effectively have a problem.

The new version of LWJGL in the works, 0.9, has checking code in all buffer operations to find this sort of mistake. Hurrah!

Cas :)

Optus

Well thanks guys, for reviving this one.  I didn't expect to get a response after so long.  Unfortunately, I've tried many different combinations, including flipping, rewinding, flipping/rewinding, or doing nothing to the buffers.  Still I get nonsensical results :(  Does anyone have working code that utilizes gluProject?

Fool Running

I think I found the answer to your problem (now that I've gotten a chance to try it  :lol: )

change
GLU.gluProject(plyr.pos.X, plyr.pos.Y, plyr.pos.Z, modelview, projection, viewport, result);


to

GLU.gluProject(0.0, 0.0, 0.0, modelview, projection, viewport, result);


The reason is that you are trying to find a point (plyr.pos) from the current modelview, but you have already set the modelview to that pos.
:wink:

I tried this code fix in a temp project and it gave results I was expecting, except the y pos was reversed (you'll figure it out  :)  )
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

princec

Oh and by the way - there's a nifty easy technique to do this that doesn't involve gluProject - it's called billboarding. A quick google turns up all sorts of useful tutorials.

In fact, surprise surprise, the first link returned by google is this tutorial which is excellent :)

Cas :)

Optus

Ahh, Fool Running thanks!  You were right.  I should have thought of this, good thing I have you guys around! ;)  Also thanks for that link cas, I think I will be employing that for some other things... is it a practical technique to be used with particle systems (1000s of particles)? Right now, my particle systems are only drawn in the y plane, thus massively simplifying the sorting process, because you only have to test their y values.  When I tried billboarding the particles, they would get ordered incorrectly (AFAIK, because of float imprecisison), and it would result in nasty squarish overlapping.  I guess my question is:  What is the best way to sort all of these particles? Using a Comparator that calculated the distance to 2 particles from the camera was not cutting it.

princec

Generally speaking I'd say that if they can't be drawn to the screen in any old order, they're not really "particles", they're more like "entities". You could have thousands of particles on the screen and sorting them all would be a bit slow.

There are some tests and blending modes you can use to make the particles render correctly in any order at a tiny loss in quality. The alpha test is used for example to billboard trees - the edges look a teeny bit ragged but as you're drawing an entire forest usually it's not a problem.

Cas :)