Ok, I'm not an expert on picking, but I'll try to explain it.
void Draw_Map(){ int x, z; for (z=0;z<size;z++) { for (x=0;x<size;x++) { // Add this line glLoadName(z+x*size); glBegin(GL_QUADS); glVertex3f(Map.tile[x][z].x, Map.tile[x][z].y, Map.tile[x][z].z); glVertex3f(Map.tile[x+1][z].x, Map.tile[x+1][z].y, Map.tile[x+1][z].z); glVertex3f(Map.tile[x+1][z+1].x, Map.tile[x+1][z+1].y, Map.tile[x+1][z+1].z); glVertex3f(Map.tile[x][z+1].x, Map.tile[x][z+1].y, Map.tile[x][z+1].z); glEnd(); } }}
|
That's only required for the actual naming of objects.
Note that OpenGL is limited to 64 objects when picking,
so
size can't be larger than 8
You'll also need a function that tests to see if a tile is
selected. This function takes 2 parameters, the mouse x and y values
bool PickTile(int x, int y){ // check to see if x,y are even pointing at the map // if not it's faster if we don't even test them for picking GLuint Buffer[512]; GLint Hits; GLint Viewport[4]; glGetIntegerv(GL_VIEWPORT, Viewport); glSelectBuffer(512, Buffer); glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glLoadIdentity();// Note change y to Window.Height-y if picking seems upside down gluPickMatrix(x, y, 1.0f, 1.0f, Viewport); gluPerspective(45.0f, f_Ratio, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); // Note that this does NOT actually draw the map, you must //do that elsewhere, because we are in GL_SELECT mode Draw_Map(); glMatrixMode(GL_MODELVIEW); Hits = glRenderMode(GL_RENDER); // Returns how many objects // are drawn at [x,y] if (Hits > 0) { GLuint Picked = Buffer[3]; GLuint Depth = Buffer[1]; for (int i = 1; i < Hits; i++) { // Check if this hit is over the previous one if ((Buffer[i*4+1] < Depth)) { Picked = Buffer[i*4+3]; Depth = Buffer[i*4+1]; } } } else { // Nothing was picked return false; } // Ok, now you can get the value of which tile was selected like this int MapX = Picked % size; // size is same as in Draw_Map() int MapY = Picked / size; return true;}
|
This isn't perfect, and can be improved greatly. If you don't understand something, I'll try to help, but I may not be able to beyond this, because picking isn't my forte.
Feel free to
email me.
Edited by - Lord Karnus on September 19, 2001 12:45:33 AM
Edited by - Lord Karnus on September 19, 2001 12:46:28 AM