Original Post
Hi guys, How do I find the nearest selected primitive drawn from my hit records? Thanks for any help given!
std::vector<selectStruct> Select(int x, int y, int w, int h){ std::vector<selectStruct> hitList; GLuint buffer[512] = {0}; // Set Up A Selection Buffer GLint hits = 0; // The Number Of Objects That We Selected // The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is <length>, [3] Is <width> GLint viewport[4] = {0}; // This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(512, buffer); // Tell OpenGL To Use Our Array For Selection // Puts OpenGL In Selection Mode. Nothing Will Be Drawn. Object ID's and Extents Are Stored In The Buffer. glRenderMode(GL_SELECT); glInitNames(); // Initializes The Name Stack glPushName(0); // Push 0 (At Least One Entry) Onto The Stack glMatrixMode(GL_PROJECTION); // Selects The Projection Matrix glPushMatrix(); // Push The Projection Matrix glLoadIdentity(); // Resets The Matrix // This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is. double cx = x + ((double)w / 2.0f); double cy = (viewport[3] - y) - ((double)h / 2.0f); gluPickMatrix(cx, cy, (double)w, (double)h, viewport); // Apply The Perspective Matrix gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View // Draw only objects that can be selected here, will // need to a nice little system to do that, do not hard code it in a game DrawBox(true); glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPopMatrix(); // Pop The Projection Matrix glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix hits = glRenderMode(GL_RENDER); // Switch To Render Mode, Find Out How Many // Objects Were Drawn Where The Mouse Was if (hits > 0) { GLdouble modelMatrix[16]; glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); GLdouble projMatrix[16]; glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); int viewport2[4]; glGetIntegerv(GL_VIEWPORT, viewport2); double objX, objY, objZ; for (int loop = 0; loop < hits; loop++) { gluUnProject(cx, cy, .5, modelMatrix, projMatrix, viewport2, &objX, &objY, &objZ); hitList.push_back(selectStruct(buffer[(loop * 4) + 3], objX, objY, objZ)); } // Sort the list so the closest object is the first item std::sort(hitList.begin(), hitList.end()); } return hitList;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more