LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Optus on January 13, 2004, 17:48:49

Title: gluProject
Post by: Optus on January 13, 2004, 17:48:49
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.
Title: Hi
Post by: Fool Running on February 06, 2004, 16:28:18
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:
Title: gluProject
Post by: princec on February 06, 2004, 19:25:35
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 :)
Title: gluProject
Post by: Optus on February 06, 2004, 20:15:23
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?
Title: The answer
Post by: Fool Running on February 12, 2004, 01:51:05
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  :)  )
Title: gluProject
Post by: princec on February 12, 2004, 09:00:47
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 (http://www.lighthouse3d.com/opengl/billboarding/) which is excellent :)

Cas :)
Title: gluProject
Post by: Optus on February 13, 2004, 04:47:42
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.
Title: gluProject
Post by: princec on February 13, 2004, 10:15:44
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 :)