Problem with Picking

Hello,

I have a problem with picking. I tired to follow example 13-3 from the
red book.
My problem is that I always get 4 hits wherever I click on the window,
as if the
picking matrix would cover the whole window, and not 5x5 pixels.
(the z values are also reported wrongly)

I am sorry if the example is to long, I could not shorten it more. The
important
functions are selectObjects and proces****s.

Thank you for your time and answers.

peter

#include <gl\glut.h>
#include <vector>
#include <iostream>

class Triangle{
public:
Triangle( GLuint name, int pos ):m_name(name),m_pos(pos){}
void draw()const{
glLoadName(m_name);
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(9 + m_pos, 10,4);
glVertex3f(14 + m_pos,10,4);
glVertex3f(11 + m_pos,5,4);
glEnd();
}
private:
GLuint m_name;
int m_pos;
};

std::vector <Triangle> triangles;
const int HITBUFSIZE = 512;

void proces****s( GLint hits, GLuint buffer[] ){
std::cerr << "hits: " << hits << "
";
GLuint ptr = (GLuint)buffer;
for (GLint i = 0; i < hits; i++) {
GLuint names = *ptr;
std::cerr << "number of names for this hit = " << names <<
"
";
ptr++;
std::cerr << "z1: " << (*ptr/0x7fffffff);
ptr++;
std::cerr << " z2: " << (*ptr/0x7fffffff);
ptr++;
std::cerr << "
names: ";
for( GLuint j = 0; j < names; j++){
std::cerr << (*ptr) << " ";
ptr++;
}
std::cerr << "
";
}
std::cerr << "

";
}

void drawObjects(){
for( std::vector<Triangle>::const_iterator ti = triangles.begin();
ti != triangles.end(); ++ti){
(*ti).draw();
}
}

void selectObjects(int button, int state, int x, int y){
if (button != GLUT_LEFT_BUTTON | | state != GLUT_DOWN){
return;
}
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
//std::cerr << “x: " << x << " y: " << y << " vp1: " <<
viewport[0] << " vp2: " << viewport[1] << " vp3: " << viewport[2] << "
vp4: " << viewport[3] <<”
";
GLuint selectBuf[HITBUFSIZE];
glSelectBuffer (HITBUFSIZE, selectBuf);

glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);

glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
gluPickMatrix ((GLdouble) x, (GLdouble)(viewport[3]-y),5, 5,

viewport);
glOrtho (-50.0, 50.0, -50.0, 50.0, 20.0, -20.0);
drawObjects();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();

glFlush ();
GLint hits = glRenderMode (GL_RENDER);
proces****s (hits, selectBuf);
glutPostRedisplay();

}

void display(){
glClear (GL_COLOR_BUFFER_BIT);
drawObjects();
glFlush ();
}

void reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (-50.0, 50.0, -50.0, 50.0, 20.0, -20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

void init(void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
triangles.push_back(Triangle(1,10));
triangles.push_back(Triangle(2,20));
triangles.push_back(Triangle(3,30));
triangles.push_back(Triangle(4,40));
init ();
glutMouseFunc (selectObjects);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

I now know that the problem was the drawing code for the triangle. When I replace it with:
glRecti (m_pos, m_pos, m_pos + 10, m_pos + 10);

it works correctly.

If somebody could explain this to me please?