Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
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  
  Picking Utility  (Read 2359 times)
0 Members and 1 Guest are viewing this topic.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 799
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 Member




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.

UprightPath (12 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

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

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

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

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

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

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

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

Longarmx (45 views)
2014-09-07 01:11:22
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!