Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Picking Utility  (Read 2391 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2005-05-23 10:24:58 »

For newbies who have a hardtime getting picking to work.. Smiley

Usage:
1  
2  
3  
4  
5  
6  
7  
// rendering
drawScene();

// picking
Picking.begin();
drawScene();
List<Integer> hits = Picking.end();


Class sourcecode:
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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
import static org.lwjgl.opengl.GL11.*;

import java.util.List;
import java.nio.IntBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.glu.GLU;

public class Picking
{
   private static final IntBuffer hitBuffer = BufferUtils.createIntBuffer(128);

   private static final FloatBuffer matrix = BufferUtils.createFloatBuffer(4 * 4);



   /**
    * BEGIN
    */


   public static final void begin()
   {
      glSelectBuffer(hitBuffer);
      glRenderMode(GL_SELECT);

      // modify proj-mat
      glMatrixMode(GL_PROJECTION);
      glGetFloat(GL_PROJECTION_MATRIX, matrix);
      glLoadIdentity();
      GLU.gluPickMatrix(Mouse.getX(), Mouse.getY(), 1, 1, yourViewportValuesArrayHere);
      glMultMatrix(matrix);
      glMatrixMode(GL_MODELVIEW);
   }



   /**
    * END
    */


   public static final List<Integer> end()
   {
      int hits = glRenderMode(GL_RENDER);

      // restore proj-mat
      glMatrixMode(GL_PROJECTION);
      glLoadMatrix(matrix);
      glMatrixMode(GL_MODELVIEW);

      // process
      return Picking.getNearestNames(hits);
   }



   /**
    * NEAREST NAMES
    */


   private static final List<Integer> getNearestNames(int hits)
   {
      List<Integer> names = new ArrayList<Integer>();

      int absolute_min = Integer.MAX_VALUE;

      for (int i = 0; i < hits; i++)
      {
         // how many names for this hit
         int count = hitBuffer.get();

         // store min distance
         int min = hitBuffer.get();

         // ignore max distance
         hitBuffer.get();

         // found hit that was nearer than previous
         if (min < absolute_min)
         {
            absolute_min = min;

            // remove all previous names
            names.clear();
         }

         // skip: min > absolute_min
         // allow: min == absolute-min
         if (min > absolute_min)
         {
            // skip hit-names if not nearest hit
            hitBuffer.position(hitBuffer.position() + count);

            continue;
         }

         // add names
         for (int j = 0; j < count; j++)
         {
            // read name
            int name = hitBuffer.get();

            // add to names
            names.add(new Integer(name));
         }
      }

      // reset hit-buffer
      hitBuffer.clear();

      return names;
   }
}


I hope I help out at least 1 person with this Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Schabby

Junior Devvie




The Receding Brow Worm will eat your code!


« Reply #1 - Posted 2005-08-13 05:13:06 »

I wrote a picking class myself as well. It's not as neat as the above, but it's always good to have a second example Smiley

http://user.cs.tu-berlin.de/~schabby/PickingExample.java

Offline Cyberchrist

Innocent Bystander





« Reply #2 - Posted 2005-09-27 20:32:39 »

That's cool from you, very useful for a name based picking.
Thanks.

I see everywhere name and color picking, but I cant find a "calculated ray picking exemple".
Please, does anyone have a working exemple for ray picking ?
I mean : getting a ray with 2 gluUnProjects and calculating the intersection (x,y,z world coordinates) between an object (ie. the y=0 plane) and the ray comming from the eye (gluLookAt).

Any feedback and/or source input much appreciated Wink
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.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (62 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!