Can't get picking to work with 3d model | Solved

It is on the display function i am stuck
import javax.media.opengl.;
import javax.media.opengl.glu.
;
import com.sun.opengl.utils.; //BufferUtils
import java.io.
;
import java.nio.;
import javax.imageio.
;
import java.awt.;
import java.net.
;
import java.util.;
import java.awt.image.
;
import java.awt.event.;
import najgl.model.
;
import najgl.model.ms3d.*;
public class JoglHandler implements GLEventListener, KeyListener, MouseListener, MouseMotionListener
{
private GLCanvas canvas = null;
private GLU glu;
private GLUT glut;
private int mouse_x = 0;
private int mouse_y = 0;
private boolean mouse_down = false;
private Model model;
private boolean changeColor = false;
//Filnavnet på modellen
private String model_filename = “data/run.txt”;
private float trans = 0.0f;
private float rot = 0.0f;
private int width, height;
public JoglHandler(GLCanvas canvas)
{
this.canvas = canvas;
}
public void displayChanged(GLAutoDrawable glDrawable,boolean modeChanged, boolean deviceChanged)
{
}
public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height)
{
GL gl = glDrawable.getGL();
this.height = height;
this.width = width;
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable glDrawable)
{
GL gl = glDrawable.getGL();
gl.glClearColor(0.0f,0.0f,0.8f,0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glOrtho(0.0f,5.0f,0.0f,5.0f,0.0f,10.0f);
//glu.gluLookAt(0.0f,0.0f,10.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
gl.glPushMatrix();
//Creating a floor
gl.glColor3f(1.0f,1.0f,1.0f);
gl.glTranslatef(0.0f,-10.0f,-20.0f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-30.0f,0.0f,-30.0f);
gl.glVertex3f(30.0f,0.0f,-30.0f);
gl.glVertex3f(30.0f,0.0f,30.0f);
gl.glVertex3f(-30.0f,0.0f,30.0f);
gl.glEnd();
gl.glPopMatrix();
gl.glPushMatrix();
if(changeColor)
gl.glColor3f(1.0f,0.0f,0.0f);
else
gl.glColor3f(1.0f,1.0f,1.0f);
gl.glTranslatef(0.0f,-0.0f,trans);
gl.glRotatef(rot, 0.0f,1.0f,0.0f);
gl.glScalef(0.1f,0.1f,0.1f);
if(mouse_down) //THis is where i am stuck
{
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float)width / (float)height;
int buffsize = 512;
double x = (double) mouse_x, y = (double) mouse_y;
IntBuffer viewPort = BufferUtils.newIntBuffer(4);
IntBuffer selectBuffer = BufferUtils.newIntBuffer(buffsize);
int hits = 0;
gl.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
gl.glSelectBuffer(buffsize, selectBuffer);
gl.glRenderMode(GL.GL_SELECT);
gl.glInitNames();
gl.glPushName(0);
gl.glPushMatrix();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 100.0);
glu.gluPickMatrix(x, (double) viewPort.get(3) - y, 5.0d, 5.0d, viewPort);
gl.glOrtho(0.0f,5.0f,0.0f,5.0f,0.0f,10.0f);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glLoadName(1);
model.draw(); //This is where the model is drawn, using a loader for ms3d ascii models
/gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-10.0f,-10.0f,-5.0f);
gl.glVertex3f(10.0f,-10.0f,-5.0f);
gl.glVertex3f(10.0f,10.0f,-5.0f);
gl.glVertex3f(-10.0f,10.0f,-5.0f);
gl.glEnd();
/
gl.glPopMatrix();
gl.glFlush();
hits = gl.glRenderMode(GL.GL_RENDER);
System.out.println("Hits: "+hits);
if(hits == 1)
changeColor = !changeColor;
mouse_down = false;
gl.glMatrixMode(GL.GL_MODELVIEW);
}
else
{
/*gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-10.0f,-10.0f,-5.0f);
gl.glVertex3f(10.0f,-10.0f,-5.0f);
gl.glVertex3f(10.0f,10.0f,-5.0f);
gl.glVertex3f(-10.0f,10.0f,-5.0f);
gl.glEnd(); */
model.draw();
}
gl.glPopMatrix();
gl.glFlush();
}
private void clearScreen(GLAutoDrawable glDrawable)
{
GL gl = glDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
}
public void mousePressed(MouseEvent e)
{
//cmd = SELECT;
mouse_x = e.getX();
mouse_y = e.getY();
mouse_down = true;
//System.out.println(e.getX());
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void keyPressed(KeyEvent e)
{
	switch(e.getKeyCode())
	{
		case KeyEvent.VK_UP:
			trans += 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_DOWN:
			trans -= 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_RIGHT:
			rot -= 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_LEFT:
			rot += 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_Q: //Slakker ned på hvor fort 
			model.slow_anim(); //animasjonen
			break;	//skal kjøres
		case KeyEvent.VK_W:
			model.fast_anim();
			break;
		case KeyEvent.VK_V:
			break;
		case KeyEvent.VK_C:
			changeColor = !changeColor;
			break;
	}
}
public void init(GLAutoDrawable glDrawable)
{
    
	GL gl = glDrawable.getGL();
	glDrawable.addMouseListener(this);
	glDrawable.addMouseMotionListener(this);
	glu = new GLU();
	glut = new GLUT();
	gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE_MINUS_SRC_ALPHA);
	gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
	//gl.glEnable(GL.GL_CULL_FACE);
	gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    // Black Background
	gl.glClearDepth(1.0f);                      // Depth Buffer Setup
	gl.glEnable(GL.GL_DEPTH_TEST);
	gl.glEnable(GL.GL_NORMALIZE);
	// Enables Depth Testing
	gl.glDepthFunc(GL.GL_LEQUAL);                             // The Type Of Depth Testing To Do
	gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);   // Really Nice Perspective Calculations
	//Oppretter modell objektet, parameterne er som følger:
	//Første parameter er om den inneholder animasjon,
	//det andre parameteret er om det skal være wiredframed, 
	//og det siste er GLAutoDrawable objektet som blir brukt
	//av modell loaderen.
	model = new MilkshapeModel(true,false,glDrawable);
	//Leser inn modellen
	model.load(model_filename);
	//float light_position[] = { 0.0f,30.0f, 5.0f,1.0f}; 
	//gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
	float fog_color[] = {1.0f,1.0f,1.0f,1.0f};
	//Setter hvor sterk tåken skal være
	gl.glFogf(GL.GL_FOG_DENSITY,0.02f);
	//Har 3 forskjellige typer tåke, 
	//De forskjellige er GL_LINEAR OG GL_EXP OG GL_EXP2
	gl.glFogi(GL.GL_FOG_MODE,GL.GL_EXP);
	//Setter at tåken skal være penest mulig renderert
	gl.glHint(GL.GL_FOG_HINT,GL.GL_NICEST);
	//Setter start og slutt verdier på tåken
	gl.glFogf(GL.GL_FOG_START,1.0f);
	gl.glFogf(GL.GL_FOG_END,100.0f);
	//Setter fargen
	gl.glFogf(GL.GL_FOG_COLOR,0.5f);
	gl.glEnable(GL.GL_FOG);
	//Enabler en sol
	gl.glEnable(GL.GL_LIGHTING);
	gl.glEnable(GL.GL_LIGHT0);

	gl.glColorMaterial(GL.GL_FRONT,GL.GL_DIFFUSE);
	gl.glEnable(GL.GL_COLOR_MATERIAL);
}

}

Got it to work, Pasting the code, maybe it can help someone else :smiley:
import javax.media.opengl.;
import javax.media.opengl.glu.
;
import com.sun.opengl.utils.; //BufferUtils
import java.io.
;
import java.nio.;
import javax.imageio.
;
import java.awt.;
import java.net.
;
import java.util.;
import java.awt.image.
;
import java.awt.event.;
import najgl.model.
;
import najgl.model.ms3d.*;
public class JoglHandler implements GLEventListener, KeyListener, MouseListener, MouseMotionListener
{
private GLCanvas canvas = null;
private GLU glu;
private GLUT glut;
private int mouse_x = 0;
private int mouse_y = 0;
private boolean mouse_down = false;
private Model model;
private boolean changeColor = false;
//Filnavnet på modellen
private String model_filename = “data/run.txt”;
private float trans = 0.0f;
private float rot = 0.0f;
private int width, height;
public JoglHandler(GLCanvas canvas)
{
this.canvas = canvas;
}
public void displayChanged(GLAutoDrawable glDrawable,boolean modeChanged, boolean deviceChanged)
{
}
public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height)
{
GL gl = glDrawable.getGL();
this.height = height;
this.width = width;
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void display(GLAutoDrawable glDrawable)
{
GL gl = glDrawable.getGL();
gl.glClearColor(0.0f,0.0f,0.8f,0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
//gl.glOrtho(0.0f,5.0f,0.0f,5.0f,0.0f,10.0f);
glu.gluLookAt(0.0f,0.0f,10.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
gl.glPushMatrix();
//Creating a floor
gl.glColor3f(1.0f,1.0f,1.0f);
gl.glTranslatef(0.0f,-10.0f,-20.0f);
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-30.0f,0.0f,-30.0f);
gl.glVertex3f(30.0f,0.0f,-30.0f);
gl.glVertex3f(30.0f,0.0f,30.0f);
gl.glVertex3f(-30.0f,0.0f,30.0f);
gl.glEnd();
gl.glPopMatrix();
gl.glPushMatrix();
if(changeColor)
gl.glColor3f(1.0f,0.0f,0.0f);
else
gl.glColor3f(1.0f,1.0f,1.0f);
gl.glTranslatef(0.0f,-5.0f,trans);
gl.glRotatef(rot, 0.0f,1.0f,0.0f);
gl.glScalef(0.1f,0.1f,0.1f);
if(mouse_down) //THis is where i am stuck
{
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float)width / (float)height;
int buffsize = 512;
double x = (double) mouse_x, y = (double) mouse_y;
IntBuffer viewPort = BufferUtils.newIntBuffer(4);
IntBuffer selectBuffer = BufferUtils.newIntBuffer(buffsize);
int hits = 0;
gl.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
gl.glSelectBuffer(buffsize, selectBuffer);

		gl.glRenderMode(GL.GL_SELECT);

		gl.glInitNames();                       // clear name stack

		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glPushMatrix();                      // save current proj. matrix

		gl.glLoadIdentity();                    // reset the proj. matrix

		glu.gluPickMatrix(x, (double) viewPort.get(3) - y, 5.0d, 5.0d, viewPort);          // restrict drawing to around
		// the cursor

		glu.gluPerspective(45, h,1, 200);   // set up view frustrum. Make sure that
		// that you set it up the same way
		// as you set up the frustum in GL_RENDER 
		// mode!

		gl.glMatrixMode(GL.GL_MODELVIEW);       // now back to model view so that we
		// can transform our objects

		gl.glPushName(20);                      // assign '20' to the quad
		model.draw();


		gl.glMatrixMode(GL.GL_PROJECTION);      
		gl.glPopMatrix();                       // restore the previous proj. matrix

		gl.glMatrixMode(GL.GL_MODELVIEW);       // back again in default mode

		hits = gl.glRenderMode(GL.GL_RENDER);   // back to GL_RENDER mode


		System.out.println("Hits: "+hits);
		if(hits == 1)
			changeColor = !changeColor;
		mouse_down = false;
		gl.glMatrixMode(GL.GL_MODELVIEW);
	}
	else
	{
		model.draw();
	}
	gl.glPopMatrix();
	gl.glFlush();
}
private void clearScreen(GLAutoDrawable glDrawable)
{
	GL gl = glDrawable.getGL();
	gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
	gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
}
public void mousePressed(MouseEvent e) 
{
	//cmd = SELECT;
	mouse_x = e.getX();
	mouse_y = e.getY();
	mouse_down = true;
	//System.out.println(e.getX());
}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void keyPressed(KeyEvent e)
{
	switch(e.getKeyCode())
	{
		case KeyEvent.VK_UP:
			trans += 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_DOWN:
			trans -= 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_RIGHT:
			rot -= 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_LEFT:
			rot += 10.0f;
			//canvas.repaint();
			break;
		case KeyEvent.VK_Q: //Slakker ned på hvor fort 
			model.slow_anim(); //animasjonen
			break;	//skal kjøres
		case KeyEvent.VK_W:
			model.fast_anim();
			break;
		case KeyEvent.VK_V:
			break;
		case KeyEvent.VK_C:
			changeColor = !changeColor;
			break;
	}
}
public void init(GLAutoDrawable glDrawable)
{

	GL gl = glDrawable.getGL();
	glDrawable.addMouseListener(this);
	glDrawable.addMouseMotionListener(this);
	glu = new GLU();
	glut = new GLUT();
	gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE_MINUS_SRC_ALPHA);
	gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
	//gl.glEnable(GL.GL_CULL_FACE);
	gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    // Black Background
	gl.glClearDepth(1.0f);                      // Depth Buffer Setup
	gl.glEnable(GL.GL_DEPTH_TEST);
	gl.glEnable(GL.GL_NORMALIZE);
	// Enables Depth Testing
	gl.glDepthFunc(GL.GL_LEQUAL);                             // The Type Of Depth Testing To Do
	gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);   // Really Nice Perspective Calculations
	//Oppretter modell objektet, parameterne er som følger:
	//Første parameter er om den inneholder animasjon,
	//det andre parameteret er om det skal være wiredframed, 
	//og det siste er GLAutoDrawable objektet som blir brukt
	//av modell loaderen.
	model = new MilkshapeModel(true,false,glDrawable);
	//Leser inn modellen
	model.load(model_filename);
	//float light_position[] = { 0.0f,30.0f, 5.0f,1.0f}; 
	//gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
	float fog_color[] = {1.0f,1.0f,1.0f,1.0f};
	//Setter hvor sterk tåken skal være
	gl.glFogf(GL.GL_FOG_DENSITY,0.02f);
	//Har 3 forskjellige typer tåke, 
	//De forskjellige er GL_LINEAR OG GL_EXP OG GL_EXP2
	gl.glFogi(GL.GL_FOG_MODE,GL.GL_EXP);
	//Setter at tåken skal være penest mulig renderert
	gl.glHint(GL.GL_FOG_HINT,GL.GL_NICEST);
	//Setter start og slutt verdier på tåken
	gl.glFogf(GL.GL_FOG_START,1.0f);
	gl.glFogf(GL.GL_FOG_END,100.0f);
	//Setter fargen
	gl.glFogf(GL.GL_FOG_COLOR,0.5f);
	gl.glEnable(GL.GL_FOG);
	//Enabler en sol
	gl.glEnable(GL.GL_LIGHTING);
	gl.glEnable(GL.GL_LIGHT0);

	gl.glColorMaterial(GL.GL_FRONT,GL.GL_DIFFUSE);
	gl.glEnable(GL.GL_COLOR_MATERIAL);
}

}

hi…
Can u please tell me… how did you import this javax.media.opengl package? Which jar file you are using? Actully i have imported JSR Beta 4 version but i didn’t see this pkg in it. I am stuck with my code. I am getting error while importing this package in my java file.
Can you suggests me anything.?
Do you have jar file having this pkg…

thanks for bothering you…
but it will really help me…

thanks,
mont