Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
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 Depth Values  (Read 695 times)
0 Members and 1 Guest are viewing this topic.
Offline causs

Senior Newbie




Java games rock!


« Posted 2004-03-12 15:19:53 »

Hi There,

First,
Any idea regarding how to get this code working with model-dialog is deeply appreciated.

Second,
OpenGL Programming Guide, plainly tells that window-coordinate max and min Z-Values of all vertices of the primitives intersected in viewing volume are given in the int[] buffer.
In the sample the cube is beyond the sphere, but the z-values of sphere are (absolute value) greater than those of the cube. I think closer object to window screen should have smaller z-values (absolute value).
Since the sphere is closer it is supposed to have smaller z-values(absolute value). It seems it is not the case in sample.

Thanks..
/////////////////////////////////////////////
I modified sample code posted here before.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class JoglTest
   implements GLEventListener, MouseListener, MouseMotionListener, KeyListener {
 static private Frame frame;
 //static private Animator animator;
 static private GLCanvas canvas;
 private float prevMouseX, prevMouseY;
 private float mouseX, mouseY;
 private float transX = 0f, transY = 0f, transZ = -4.0f;
 private boolean picked;
 private int pickedName;
 private float view_rotX_1 = 0.0f, view_rotY_1 = 0.0f, view_rotZ = 0.0f;
 private float view_rotX_2 = 0.0f, view_rotY_2 = 0.0f;
 private final int test = 1;
 private float fAspect;
 float lambr = 0.0f;
 float lambg = 0.0f;
 float lambb = 0.0f;
 float ldifr = 1.0f;
 float ldifg = 1.0f;
 float ldifb = 1.0f;
 float lsper = 1.0f;
 float lspeg = 1.0f;
 float lspeb = 1.0f;
 float lposx = 0.8f;
 float lposy = 0.8f;
 float lposz = 0.8f;
 float light_ambient[] = {lambr, lambg, lambb, 1.0f};
 float light_diffuse[] = {ldifr, ldifg, ldifb, 1.0f};
 float light_specular[] = {lsper, lspeg, lspeb, 1.0f};
 float light_position[] = {lposx, lposy, lposz};
 float global_ambient[] = {0.75f, 0.75f, 0.75f, 1.0f};
 float mambr = 0.25f;
 float mambg = 0.2f;
 float mambb = 0.074f;
 float mdifr = 0.57f;
 float mdifg = 0.14f;
 float mdifb = 0.17f;
 float msper = 0.8f;
 float mspeg = 0.65f;
 float mspeb = 0.25f;
 float ambient[] = {mambr, mambg, mambb, 1.0f};
 float diffuse[] = {mdifr, mdifg, mdifb, 1.0f};
 float specular[] = {msper, mspeg, mspeb, 1.0f};
 float shininess = 20.0f;
 JDialog d = new JDialog();

 private JoglTest() {
   System.out.println("JoglTest instance created");
   // Canvas setup
   GLCapabilities caps = new GLCapabilities();
   caps.setDoubleBuffered(true);
   caps.setAlphaBits(Cool;
   caps.setStencilBits(Cool;
   canvas = GLDrawableFactory.getFactory().createGLCanvas(caps);
   canvas.addGLEventListener(this);
   canvas.setGL(new DebugGL(canvas.getGL()));
   canvas.addMouseListener(this);
   canvas.addMouseMotionListener(this);
   canvas.addKeyListener(this);
   // Frame setup
   frame = new Frame("Vectrix");
   frame.add(canvas);
   frame.setSize(800, 600);
   d.setUndecorated(true);
   frame.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
       System.exit(0);
     }
   });
 }

 public static void main(String[] args) {
   System.out.println("Vectrix starting...");
   JoglTest app = new JoglTest();
   //show frame
   frame.show();
 }

 // GL Event Listener methods
 // Called when GL has been initialised for the first time.
 public void init(GLDrawable drawable) {
   GL gl = drawable.getGL();
   // light properties
   gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light_ambient);
   gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diffuse);
   gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_specular);
   gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
   gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, global_ambient);
   // material properties
   gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, ambient);
   gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, diffuse);
   gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, specular);
   gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, shininess);
   // various OpenGL states
   gl.glFrontFace(GL.GL_CW);
   gl.glEnable(GL.GL_LIGHTING);
   gl.glEnable(GL.GL_LIGHT0);
   gl.glEnable(GL.GL_AUTO_NORMAL);
   gl.glEnable(GL.GL_NORMALIZE);
   gl.glDepthFunc(GL.GL_LESS);
   gl.glEnable(GL.GL_DEPTH_TEST);
   gl.glMatrixMode(GL.GL_PROJECTION);
   gl.glLoadIdentity();
   gl.glMatrixMode(GL.GL_TEXTURE);
   gl.glLoadIdentity();
   gl.glMatrixMode(GL.GL_MODELVIEW);
   gl.glLoadIdentity();
 }

 public void display(GLDrawable drawable) {

   if (picked) {
     processSelection(drawable, mouseX, mouseY);
   }else
   {draw(drawable);}
 }

 public void draw(GLDrawable drawable) {
   GL gl = drawable.getGL();
   GLU glu = drawable.getGLU();
   GLUT glut = new GLUT();

   gl.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
   gl.glClear(
       GL.GL_COLOR_BUFFER_BIT
       | GL.GL_DEPTH_BUFFER_BIT
       | GL.GL_STENCIL_BUFFER_BIT);
   float ambient[] = {0.0f, 0.7f, 0.9f, 1.0f};
   gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, ambient);
   gl.glInitNames();
   gl.glPushName(0);
   gl.glPushMatrix();
   gl.glTranslatef(transX - 0.5f, transY, transZ);
   gl.glRotatef(view_rotX_1, 1.0f, 0.0f, 0.0f);
   gl.glRotatef(view_rotY_1, 0.0f, 1.0f, 0.0f);
   gl.glRotatef(view_rotZ, 0.0f, 0.0f, 1.0f);
   gl.glLoadName(1);
   glut.glutWireSphere(glu, 0.5, 100, 100);
   gl.glPopMatrix();

   float ambient_1[] = {0.9f, 0.7f, 0.0f, 1.0f};
   gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, ambient_1);
   gl.glPushMatrix();
   gl.glTranslatef(transX, transY, -6);
   gl.glRotatef(view_rotX_2, 1.0f, 0.0f, 0.0f);
   gl.glRotatef(view_rotY_2, 0.0f, 1.0f, 0.0f);
   gl.glRotatef(view_rotZ, 0.0f, 0.0f, 1.0f);
   gl.glLoadName(2);
   glut.glutSolidCube(gl, 1.1f);
   gl.glPopMatrix();

   gl.glFlush();
 }

 public void reshape(
     GLDrawable drawable,
     int x,
     int y,
     int width,
     int height) {
   GL gl = drawable.getGL();
   GLU glu = drawable.getGLU();
   fAspect = (float) width / (float) height;
   gl.glMatrixMode(GL.GL_PROJECTION);
   gl.glLoadIdentity();
   glu.gluPerspective(30.0, (float) width / (float) height, 1.0, 50);
   gl.glMatrixMode(GL.GL_MODELVIEW);
   gl.glLoadIdentity();
 }

 public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2) {
 }

 public void processSelection(GLDrawable drawable, float x, float y) {
   GL gl = drawable.getGL();
   GLU glu = drawable.getGLU();
   int[] selectBuff = new int[64];
   int hits = 0;
   int[] viewport = new int[4];

   gl.glSelectBuffer(64, selectBuff);
   gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
   gl.glMatrixMode(GL.GL_PROJECTION);
   gl.glPushMatrix();

   gl.glRenderMode(GL.GL_SELECT);
   gl.glLoadIdentity();
   glu.gluPickMatrix(x, viewport[3] - y, 50, 50, viewport);
   glu.gluPerspective(30.0f, fAspect, 1.0, 50);
   draw(drawable);
   hits = gl.glRenderMode(GL.GL_RENDER);
   if (hits > 0) {
     System.out.println("hits " + hits);
     int count = selectBuff[0];
     pickedName = selectBuff[3];
     d = new JDialog();
     //d.setModal(true);
     d.setUndecorated(true);
     d.getContentPane().setLayout(new BorderLayout());
     d.setSize(new Dimension(100, 100));
     d.setLocation( (int) x, (int) y);
     JPanel p = new JPanel(new GridLayout(hits * 4, 1));
     for (int i = 0; i < hits * 4; i++) {
       String name = String.valueOf(selectBuff);
       JLabel l = new JLabel(String.valueOf(name));
       p.add(l);
     }
     d.getContentPane().add(p, BorderLayout.CENTER);
     d.setVisible(true);
   }
   else {
     System.out.println("no hit");
     pickedName = 0;
     d.dispose();
   }
   gl.glMatrixMode(GL.GL_PROJECTION);
   gl.glPopMatrix();
   gl.glMatrixMode(GL.GL_MODELVIEW);
   picked = false;
 }

 public void processKey(KeyEvent event) {
   int key = event.getKeyCode();
   if (event.isControlDown() && key == KeyEvent.VK_DOWN) {
     transY -= 0.1f;
   }
   else if (event.isControlDown() && key == KeyEvent.VK_UP) {
     transY += 0.1f;
   }
   else if (key == KeyEvent.VK_DOWN) {
     transZ += 0.1f;
   }
   else if (key == KeyEvent.VK_UP) {
     transZ -= 0.1f;
   }
   else if (key == KeyEvent.VK_LEFT) {
     transX -= 0.1f;
   }
   else if (key == KeyEvent.VK_RIGHT) {
     transX += 0.1f;
   }
 }

 public void mouseEntered(MouseEvent e) {
 }

 public void mouseExited(MouseEvent e) {
 }

 public void mousePressed(MouseEvent e) {
   if (d.isVisible()) {
     d.dispose();
   }
   prevMouseX = e.getX();
   prevMouseY = e.getY();
   mouseX = e.getX();
   mouseY = e.getY();
   picked = true;
 }

 public void mouseReleased(MouseEvent e) {
   canvas.repaint();
 }
 public void mouseClicked(MouseEvent e) {
 }

 public void mouseDragged(MouseEvent e) {
   int x = e.getX();
   int y = e.getY();
   Dimension size = e.getComponent().getSize();
   float thetaY = 360.0f * (float) (x - prevMouseX) / (float) size.width;
   float thetaX = 360.0f * (float) (prevMouseY - y) / (float) size.height;
   prevMouseX = x;
   prevMouseY = y;
   switch (pickedName) {
     case 1:
       view_rotX_1 += thetaX;
       view_rotY_1 += thetaY;
       break;
     case 2:
       view_rotX_2 += thetaX;
       view_rotY_2 += thetaY;
       break;
   }
   canvas.repaint();
 }
 public void mouseMoved(MouseEvent e) {
 }

 public void keyPressed(KeyEvent e) {
   processKey(e);
 }
 public void keyReleased(KeyEvent e) {
 }
 public void keyTyped(KeyEvent e) {
 }
}
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.

theagentd (20 views)
2014-10-25 15:46:29

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

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

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

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

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

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

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

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

BurntPizza (47 views)
2014-10-11 23:10:45
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!