Jump to content
  • Advertisement
Sign in to follow this  
Jianxing

problem about picking

This topic is 3176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently I am working on the picking function. I tried out some of the codes from the book but it did not worked out for me. The problem here is that I have loaded an object from an object file. next thing I would like to do is to perform picking on the faces of this object. To start things off I only tried to pick the entire object but it also does not work. Can I get any help?

Share this post


Link to post
Share on other sites
Advertisement
Sure you can, if you tell us which 'book', and which picking method. (I guess the openGL picking feature). If so, post the picking code!

Share this post


Link to post
Share on other sites
The book I'm reading on is the one that says the offical guide to learning opengl (Version 2).

Here is my code.


private void selectObject(GL gl,GLAutoDrawable drawable)
{
int[] selectBuf = new int[buf_size];
IntBuffer selectBuffer = BufferUtil.newIntBuffer(buf_size);
int hits;
int viewport[] = new int[4];
// int x, y;

gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);

gl.glSelectBuffer(buf_size, selectBuffer);
gl.glRenderMode(GL.GL_SELECT);

gl.glInitNames();
gl.glPushName(-1);

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
/* create 5x5 pixel picking region near cursor location */
//this.calObjectCoor(gl,mouse_x,mouse_y,0.0f);
glu.gluPickMatrix(mouse_x,
viewport[3] - mouse_y, //
5.0, 5.0, viewport, 0);
gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
drawModel(gl,drawable,gl.GL_SELECT);
gl.glPopMatrix();
gl.glFlush();

hits = gl.glRenderMode(GL.GL_RENDER);
selectBuffer.get(selectBuf);
processHits(hits, selectBuf);
}


private void processHits(int hits, int buffer[])
{
int names, ptr = 0;

System.out.println("hits = " + hits);
// ptr = (GLuint *) buffer;
for (int i = 0; i < hits; i++)
{ /* for each hit */
names = buffer[ptr];
System.out.println(" number of names for hit = " + names);
ptr++;
System.out.println(" z1 is " + buffer[ptr]);
ptr++;
System.out.println(" z2 is " + buffer[ptr]);
ptr++;
System.out.print("\n the name is ");
for (int j = 0; j < names; j++)
{ /* for each name */
System.out.println("" + buffer[ptr]);
ptr++;
}
System.out.println();
}


public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();

gl.glColor3f(1.0f, 0.0f, 0.0f);
this.calObjectCoor(gl,mouse_x,mouse_y,0.0f);

//Do not clear screen when drawing the stroke, clear otherwise
if (clearBuf == true) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
}
//Only initialized the values of the mouse x, y coordindate upon user's mouse click
// where tmpX and tmpY are temporary holding ground for starting valus X,Y of the stroke
if (initStartCoord == true) {
tmpX = (float) wcoord[0];
tmpY = (float) wcoord[1];
initStartCoord = false;
}
//set up camera position and looking direction
setCamera(gl, glu, 4);

// Scene
gl.glPushMatrix();
if (todraw == true) {
gl.glDisable(gl.GL_DEPTH_TEST);
this.drawStroke(gl);
gl.glEnable(gl.GL_DEPTH_TEST);
}
gl.glPopMatrix();

//draw the model with or without the axis
drawModel(gl,drawable,gl.GL_RENDER);

if(displayonce){
selectObject(gl,drawable);
displayonce=false;
}
// Flush OpenGL pipeline
gl.glFlush();
}


Also.. how do I do picking on faces rather than the whole object?

Share this post


Link to post
Share on other sites
post the drawModel. I used this method long ago. Maybe you don't use the name stack properly, which is inside the drawModel.

Share this post


Link to post
Share on other sites
my drawmodel only has this:


public void drawModel(GL gl, GLAutoDrawable drawable,int mode) {
if (mode == gl.GL_SELECT) gl.glLoadName(1);

//Sets color to green and draw object
gl.glColor3f(0.0f, 1.0f, 0.0f);
model.draw(gl);
}


Could the problem lies in gl.glortho??
As I do not know what vales to set for its parameters.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!