Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 2368 times)
0 Members and 1 Guest are viewing this topic.
Offline PaidGEEK

Senior Devvie

Medals: 5
Projects: 3

« 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.
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):
screen_coordinates = projection_matrix * modelview_matrix * world_coordinates

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

Improvisational programmer.
Offline Roquen

JGO Kernel

Medals: 517

« 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).

Pages: [1]
  ignore  |  Print  

EgonOlsen (74 views)
2018-06-10 19:43:48

EgonOlsen (55 views)
2018-06-10 19:43:44

EgonOlsen (74 views)
2018-06-10 19:43:20

DesertCoockie (254 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!