Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  LWJGL: Getting mouse's 3D location  (Read 1861 times)
0 Members and 1 Guest are viewing this topic.
Offline Alectriciti

Junior Newbie





« Posted 2013-02-08 08:54:45 »

I have a scene set up in a 3D viewport. The game is 2.5D, so for the most part, the game will be composed of sprites.
Until I started using glRotated(0.15, 1, 0, 0); to rotate the entire view of the game, I had no problem getting the world location of the mouse. Now, obviously, the input methods I use for Mouse's X and Mouse's Y on the world need updating, or I need new methods.

So my question is, is there any method to getting the physical X, Y, and Z location of the mouse, taking in the input of the mouse's X and Y? If OpenGL doesn't support the mouse's location with drawn objects or anything like that, could anyone help me with the math?

I gladly thank anyone who takes the time to read this. Smiley
Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Reply #1 - Posted 2013-02-08 08:56:49 »

1  
int x = Mouse.getX()

Update every frame

Smiley
Offline Alectriciti

Junior Newbie





« Reply #2 - Posted 2013-02-08 09:00:09 »

Thanks for the effort... but I'm asking for the Mouse's X, Y, and Z coordinates in the 3D space, not the 2D window.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Reply #3 - Posted 2013-02-08 09:02:57 »

Thanks for the effort... but I'm asking for the Mouse's X, Y, and Z coordinates in the 3D space, not the 2D window.
1. The mouse is a 2D interface, not a 3D one.
2. You can do by
1  
2  
3  
int x = Mouse.getX()
int y = Mouse.getY()
// there is no z function

Smiley
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2013-02-08 09:05:26 »

I think what he/she means is that a X/Y position from the mouse on the screen can be many positions in 3D space - are you looking to project it onto an object? If so, you want to look up generating rays into the screen based on your current projection matrix and collide those with the shapes within your scene.

Cheers,

Kev

Offline masteryoom

JGO Coder


Medals: 5
Projects: 3


If you look closely, you might see it turning...


« Reply #5 - Posted 2013-02-08 09:06:40 »

Sorry for the misunderstanding

Smiley
Offline Alectriciti

Junior Newbie





« Reply #6 - Posted 2013-02-08 09:30:34 »

I think what he/she means is that a X/Y position from the mouse on the screen can be many positions in 3D space - are you looking to project it onto an object? If so, you want to look up generating rays into the screen based on your current projection matrix and collide those with the shapes within your scene.

Cheers,

Kev

Exactly this. Hrmm. So I assume OpenGL Utilities don't really support anythinwg like this, so I'll have to create it myself.
I'm thinking about how I would create rays. Perhaps I should read up on it. It doesn't seem like the easiest task, especially taking into account Field of View, and rotation at 15 Degrees.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2013-02-08 09:33:00 »

Typically you simply read the value of the depth buffer at the 2D coordinate of your wish.

That depth value is then unprojected through the projection view, and you end up with 3D coordinates in modelview space.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 6
Exp: 8 years



« Reply #8 - Posted 2013-02-08 18:34:14 »

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  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
   static IntBuffer viewport = BufferUtils.createIntBuffer(16);
   static FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
   static FloatBuffer projection = BufferUtils.createFloatBuffer(16);
   static FloatBuffer winZ = BufferUtils.createFloatBuffer(20);
   static FloatBuffer position = BufferUtils.createFloatBuffer(3);

   static public Vector3f getMousePositionIn3dCoords(int mouseX, int mouseY)
   {

      viewport.clear();
      modelview.clear();
      projection.clear();
      winZ.clear();;
      position.clear();
      float winX, winY;


      GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview );
      GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection );
      GL11.glGetInteger( GL11.GL_VIEWPORT, viewport );

      winX = (float)mouseX;
      winY = /* (float)viewport.get(3) -  */  //Uncomment this if you invert Y
        (float)mouseY;

      GL11.glReadPixels(mouseX, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);

      float zz = winZ.get();

      GLU.gluUnProject(winX, winY, zz, modelview, projection, viewport, position);



      Vector3f v = new Vector3f (position.get(0),position.get(1),position.get(2));


      return v ;
   }


I won' try to explain what is happening, since I myself forgot exactly how all this stuff works after writing it. In short is what Riven said.

Note that you have to use this method after your 3d rendering is done, otherwise the depth buffer will be empty and will not yield the result you're expecting.

Offline theagentd
« Reply #9 - Posted 2013-02-08 22:05:58 »

The best way is to do a ray intersection test against the world. That way you can avoid doing an expensive readback from the GPU.

Myomyomyo.
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 (53 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (210 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!