Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trouble with gluUnproject  (Read 3635 times)
0 Members and 1 Guest are viewing this topic.
Offline cborders

Junior Member





« Posted 2005-11-10 19:00:07 »

I am drawing a flat image in an XY plane.  In the example that I posted z[0] is the z value of that plane.  Here is my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
projectionBuff.rewind();
modelBuff.rewind();
viewportBuff.rewind();

GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionBuff);
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelBuff);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewportBuff);

modelBuff.get(modelArray[0]);
modelBuff.get(modelArray[1]);
modelBuff.get(modelArray[2]);
modelBuff.get(modelArray[3]);

projectionBuff.get(projectionArray[0]);
projectionBuff.get(projectionArray[1]);
projectionBuff.get(projectionArray[2]);
projectionBuff.get(projectionArray[3]);

viewportBuff.get(viewportArray);
GLU.gluUnProject((float)mouseX, (float)mouseY, z[0], modelArray, projectionArray, viewportArray, oldResult);
System.out.println("("+oldResult[0]+","+oldResult[1]+")");

The trouble is that I am always getting really small results usually between 1.001E-6 and 7.5E-5.  What am I doing wrong?
Offline cborders

Junior Member





« Reply #1 - Posted 2005-11-14 18:12:56 »

I have tried using a z of zero, -300, +300 and always I get the same values!!!  Does anyone have an example of this actually working that they could post?
Offline anarchotron

Junior Member




...precious bodily fluids.


« Reply #2 - Posted 2005-11-15 05:00:49 »

How is your projection matrix setup?  Is your nearplane or farplane zero?   Can you paste the code where you setup your projection matrix?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cborders

Junior Member





« Reply #3 - Posted 2005-11-15 16:07:42 »

Here is the code in my initGL() method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
private void initGL()
 {
     try
     {
          setVSyncEnabled(true);
     }
     catch (LWJGLException e1)
     {
          System.err.println("Unable to enable VSync: " + e1);
     }
     GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
    GL11.glClearDepth(1.0); // Depth Buffer Setup

     GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
    GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

     GL11.glEnable(GL11.GL_CULL_FACE);

     // Really Nice Perspective Calculations
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_FASTEST);
     GL11.glEnable(GL11.GL_LINE_SMOOTH);
     GL11.glLineWidth(1.0f);

     GL11.glEnable(GL11.GL_BLEND);
     GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

     viewportInit();
}

And here is where I initialize the viewport, there is a bit of weirdness due to te fact that I have multiple viewports to create a Picture-in-a-Picture effect.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
private void viewportInit()
{
     GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
    GL11.glLoadIdentity(); // Reset The Projection Matrix
    // Calculate The Aspect Ratio Of The Window
    width = getWidth();
     height = getHeight();

     viewMaxX = (int)(width*0.75f);
     viewMinX = (int)(width*0.05f);
     viewMaxY = (int)(height*0.75f);
     viewMinY = (int)(height*0.05f);

     for(int i = 0; i < 7; i++)
     {
          if(viewWidth[i] == 0) viewWidth[i] = width/5;
          if(viewHeight[i] == 0) viewHeight[i] = height/5;
     }

     GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 2000.0f);
     GL11.glViewport(0, 0, width, height);
     GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
    GL11.glLoadIdentity();
}
Offline cborders

Junior Member





« Reply #4 - Posted 2005-11-15 19:58:07 »

Ok, I have found out that I am an idiot!  I just read in the Red Book that the winZ value must be between 0 and 1, I was using the actual dpeth that I was drawing!  So now I am trying to read the depth from the DepthBuffer using  glReadPixels(), but I am not understanding it either!  Here is what I am doing:
1  
2  
3  
4  
5  
6  
7  
private IntBuffer winZ = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
...
winZ.rewind();
GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_INT, winZ);
GLU.gluUnProject((float)mouseX, (float)mouseY, winZ.get(), modelArray, projectionArray, viewportArray, oldResult);
 winZ.rewind();
System.out.println("Z="+winZ.get()+"\t("+oldResult[0]+","+oldResult[1]+")");

and it is returning 2147483647, which is most definately not between 0 and 1!   Huh
Offline Spasi
« Reply #5 - Posted 2005-11-16 12:45:06 »

Depth values lie in the [0, 1] range, but are represented in fixed-point (see 2.11.1, DepthRange in the OpenGL spec for details). You may use GL_FLOAT in glReadPixels to get the [0, 1] value(s), the conversion will be done automatically.
Offline cborders

Junior Member





« Reply #6 - Posted 2005-11-16 15:17:00 »

Just to be sure that I understand, I should use GL_FLOAT and then pass it a ByteBuffer?  Because according to the lwjgl javadoc, my only options are a ByteBuffer, and IntBuffer and a ShortBuffer.

-=EDIT=-
It can't be ByteBuffer, it complied fine, but it always returns 0.
Offline Spasi
« Reply #7 - Posted 2005-11-16 15:38:31 »

Yes, you could do that. In LWJGL cvs there're also the Float & Double versions, so in the next release you will be able to use the Float one instead.
Offline Spasi
« Reply #8 - Posted 2005-11-16 15:42:43 »

-=EDIT=-
It can't be ByteBuffer, it complied fine, but it always returns 0.

It works for me here. Make sure you're passing a direct ByteBuffer with native order and using buffer.getFloat().
Offline cborders

Junior Member





« Reply #9 - Posted 2005-11-16 16:37:07 »

Ahhh, I was just using get()!   Thank you!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!