Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Finding 2D point on screen from 3D perspective  (Read 1288 times)
0 Members and 1 Guest are viewing this topic.
Offline PaidGEEK

Senior Member


Medals: 5
Projects: 3


twitter.com/PaidGEEK


« Posted 2012-07-27 18:30:02 »

Well, the title explains what I need to know  Smiley
So if I have a basic lwjgl/opengl 3D world with some entities which can be some NPC's or other game stuff. And lets say I want to make floating names, test if object is seen by camera etc. I need to know exact 2D point coordinates when I have 3D point location data.

This is how I did 3D picking (needs to be "reversed"):
Please tell me if there's a better way.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public static Vector3f getMousePosition(int mouseX, int mouseY) {
      viewport = BufferUtils.createIntBuffer(16);
      modelview = BufferUtils.createFloatBuffer(16);
      projection = BufferUtils.createFloatBuffer(16);
      winZ = BufferUtils.createFloatBuffer(1);
      position = BufferUtils.createFloatBuffer(3);
      GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
      GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
      GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
      GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT,
            GL11.GL_FLOAT, winZ);

      if (winZ.get(0) == 1) {
         return null;
      }
      GLU.gluUnProject(mouseX, mouseY, winZ.get(), modelview, projection,
            viewport, position);
     
      return new Vector3f(position.get(0), position.get(1), position.get(2));
   }


So I was thinking...
By using this (found on stackoverflow):
1  
screen_coordinates = projection_matrix * modelview_matrix * world_coordinates

Could I somehow get position this way? I need help Smiley

Improvisational programmer.
Offline Roquen
« Reply #1 - Posted 2012-07-27 19:00:23 »

There's an infinite set of points that all map to a given screen position and they form a line. Taking the point on the near clipping plane and any 'forward' from that (say the far clipping plane) will give you two points on the line in the direction into the scene.  Cast the ray thorough the scene to check stuff.  There should be plenty of reference code available to do this.
Offline theagentd
« Reply #2 - Posted 2012-07-29 23:24:49 »

For floating name tags:

1. Calculate the world space position of the name tag from the object's position.
2. Manually do the matrix transformations on the CPU.
3. You now have a (normalized) screen coordinate and a depth for the name tag.
4. Draw text centered at those screen coordinates at the calculated depth with both the modelview and projection matrices set to identity (our coordinates are already transformed).

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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!