Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 problem?  (Read 1275 times)
0 Members and 1 Guest are viewing this topic.
Offline mikeseven

Senior Newbie




Java games rock!


« Posted 2003-08-15 19:34:59 »

Dear All,

While debugging some picking code, I decided to try the picksquare sample in OpenGL red book.

The code doesn't work and I'm not sure why, can anyone help? Huh

Thanks,

Here is the sample ported to JOGL:

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  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import net.java.games.jogl.Animator;
import net.java.games.jogl.GL;
import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLDrawableFactory;
import net.java.games.jogl.GLEventListener;
import net.java.games.jogl.GLU;

public class PickSquare {
     
      public static void main(String[] args) {
        Frame frame = new Frame("PickSquare Demo");
        GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

        canvas.addGLEventListener(new PickSquareSample());
        frame.add(canvas);
        frame.setSize(300, 300);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  animator.stop();
                  System.exit(0);
              }
            });
        frame.show();
        animator.start();
      }
     
      static class PickSquareSample implements GLEventListener, MouseListener {
     
            GL myGL;
            GLU myGLU;
     
            private int board[][] = new int[3][3]; /* amount of color for each square */
     
            private void init () {
                  int i, j;
                  for (i = 0; i < 3; i++) {
                        for (j = 0; j < 3; j++) {
                        board [i][j] = 0;
                        }
                  }
                  myGL.glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            }
     
            private void drawSquares (int mode) {
                  int i, j;
                  for (i = 0; i < 3; i++) {
                        if (mode == GL.GL_SELECT) {
                              myGL.glLoadName (i);
                        }
                        for (j = 0; j < 3; j ++) {
                              if (mode == GL.GL_SELECT) {
                                    myGL.glPushName (j);
                              }
                              myGL.glColor3f ((float)(i/3.0),(float)(j/3.0),(float)(board[i][j]/3.0));
                              myGL.glRecti (i, j, i+1, j+1);
                              if (mode == GL.GL_SELECT) {
                                    myGL.glPopName ();
                              }
                        }
                  }
            }
     
            private void processHits (int hits, int buffer []) {
                  int i, j;
                  int ii = 0, jj = 0, names, ptr;
           
                  System.out.println ("hits = " + hits);
                  ptr = 0;
                  for (i = 0; i < hits; i++) {        /*  for each hit  */
                        names = buffer [ptr];
                        System.out.println (" number of names for hit = " + names); ptr++;
                        System.out.print ("  z1 is " + buffer [ptr] + ";"); ptr++;
                        System.out.println (" z2 is " + buffer [ptr]); ptr++;
                        System.out.print ("   the name is ");
                        for (j = 0; j < names; j++) {   /*  for each name */
                              System.out.print (buffer [ptr] + " ");
                              if (j == 0) { /*  set row and column */
                                    ii = buffer [ptr];
                              } else {
                                    jj = buffer [ptr];
                              }
                              ptr++;
                        }
                        System.out.println ();
                        board [ii][jj] = (board [ii][jj] + 1) % 3;
                  }
            }
     
            private static final int BUFSIZE = 512;
     
            private void pickSquares (MouseEvent e) {
                  int selectBuf [] = new int [BUFSIZE];
                  int hits;
                  int viewport [] = new int [4];
                  int x, y;
           
                  x = e.getX ();
                  y = e.getY ();
                 
                  myGL.glGetIntegerv (GL.GL_VIEWPORT, viewport);
                 
                  myGL.glSelectBuffer (BUFSIZE, selectBuf);
                  myGL.glRenderMode (GL.GL_SELECT);
           
                  myGL.glInitNames ();
                  myGL.glPushName (-1);
           
                  myGL.glMatrixMode (GL.GL_PROJECTION);
                  myGL.glPushMatrix ();
                  myGL.glLoadIdentity ();
                 
                  /*  create 5x5 pixel picking region near cursor location        */
                  myGLU.gluPickMatrix((double)x,(double)(viewport[3]-y),5.0,5.0,viewport);
                  myGLU.gluOrtho2D (0.0, 3.0, 0.0, 3.0);
                  drawSquares (GL.GL_SELECT);
                  myGL.glPopMatrix ();
                  myGL.glFlush ();
           
                  hits = myGL.glRenderMode (GL.GL_RENDER);
                  processHits (hits, selectBuf);
            }
     
            public void mousePressed(MouseEvent e) {
                  // process which button by myself
                  if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) { // left
                        pickSquares (e);
                        //display(null);
                        e.consume ();
                  }
            }
     
            public void display (GLDrawable d) {
                  myGL.glClear (GL.GL_COLOR_BUFFER_BIT);
                  drawSquares (GL.GL_RENDER);
                  myGL.glFlush ();
            }
     
            public void reshape(GLDrawable drawable, int x, int y, int w, int h) {
                  myGL.glViewport (0, 0, w, h);
                  myGL.glMatrixMode (GL.GL_PROJECTION);
                  myGL.glLoadIdentity ();
                  myGLU.gluOrtho2D (0.0, 3.0, 0.0, 3.0);
                  myGL.glMatrixMode (GL.GL_MODELVIEW);
                  myGL.glLoadIdentity ();
            }
     
            public void init (GLDrawable drawable) {
                  myGL=drawable.getGL();
                  myGLU=drawable.getGLU();
                  drawable.addMouseListener(this);
                 
                  init ();
            }

            // unimplemented interface methods
            public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2) {}
            public void mouseClicked(MouseEvent e) {}
            public void mouseEntered(MouseEvent arg0) {}
            public void mouseExited(MouseEvent arg0) {}
            public void mouseReleased(MouseEvent arg0) {}
      }
}
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!