Drawing for Picking
Hello,
When I draw my objects (class Triangle) with glRecti() picking with gluPickMatrix() works fine. But when I draw them with glBegin(GL_TRIANGLES)glVertex()...glEnd(), it seems like _all_ the objects that are drawn are selected independently from where I click on the screen (slightly modified example 13-3 red book).
Why cant I pick the things drawn with glBegin()... correctly?
What does glRecti do that the the other drawing routine does not?
Thank you for your time and answers.
I am sorry if the example is to long, I could not shorten it more. The following example code compiles and works. It stops working if you enable the other drawing routine.
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);
glRecti (m_pos, m_pos, m_pos + 10, m_pos + 10); //works
/*
glBegin(GL_TRIANGLES); //does not work:
glVertex3f(9 + m_pos, 10,0);
glVertex3f(14 + m_pos,10,0);
glVertex3f(11 + m_pos,5,0);
glEnd();
//glFlush();
*/
}
private:
GLuint m_name;
int m_pos;
};
std::vector <triangle> triangles;
const int HITBUFSIZE = 512;
void process_Hits( GLint hits, GLuint buffer[] ){
std::cerr << "hits: " << hits << "\n";
GLuint *ptr = (GLuint*)buffer;
for (GLint i = 0; i < hits; i++) {
GLuint names = *ptr;
std::cerr << "number of names for this hit = " << names << "\n";
ptr++;
std::cerr << "z1: " << (*ptr/0x7fffffff);
ptr++;
std::cerr << " z2: " << (*ptr/0x7fffffff);
ptr++;
std::cerr << "\nnames: ";
for( GLuint j = 0; j < names; j++){
std::cerr << (*ptr) << " ";
ptr++;
}
std::cerr << "\n";
}
std::cerr << "\n\n";
}
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);
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);
glMatrixMode(GL_MODELVIEW);
drawObjects();
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
GLint hits = glRenderMode (GL_RENDER);
process_Hits (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;
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement