void selectObject(x, y) { GLuint selectBuffer[BUFSIZE]; GLint hitsNumber; GLint viewPort[4]; glGetIntegerv(GL_VIEWPORT, viewPort); glSelectBuffer(BUFSIZE, selectBuffer); (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glGetIntegerv(GL_VIEWPORT, viewPort); gluPickMatrix(x, y, 1.0, 1.0, viewPort); gluPerspective(45.0, 1.0, 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); glutSwapBuffers(); display(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hitsNumber = glRenderMode(GL_RENDER); glFlush(); glutPostRedisplay(); processSelectedObject(hitsNumber, selectBuffer); }
void pickSquares(int button, int state, int x, int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluOrtho2D (0.0, 3.0, 0.0, 3.0); drawSquares (GL_SELECT); glMatrixMode (GL_PROJECTION); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); glutPostRedisplay(); }
void handleSelection(int x, int y) { glInitNames(); GLuint hitBuffer[64] = {0}; glSelectBuffer(64, hitBuffer); glRenderMode(GL_SELECT); GLint vp[4]; glGetIntegerv(GL_VIEWPORT,vp); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(x,currentRes[1]-y,5.0,5.0,vp); gluPerspective(45.0,RESOLUTION_X/RESOLUTION_Y, 0.5, 50.0); glScalef((float)RESOLUTION_X/currentRes[0], (float)RESOLUTION_Y/currentRes[1], 1.0); glMatrixMode(GL_MODELVIEW); renderScene(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glFlush(); GLint hitCount = glRenderMode(GL_RENDER); listSelected(hitCount, hitBuffer); glMatrixMode(GL_MODELVIEW); }
void gl_select(int x, int y){ GLuint buff[64] = {0}; GLint hits, view[4]; glSelectBuffer(64, buff); glGetIntegerv(GL_VIEWPORT, view); glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(x,y,1.0,1.0,view); gluPerspective(45,1.0,0.0001,1000.0); glMatrixMode(GL_MODELVIEW); glutSwapBuffers(); draw(); glMatrixMode(GL_PROJECTION); glPopMatrix(); hits = glRenderMode(GL_RENDER); list_hits(hits,buff); glMatrixMode(GL_MODELVIEW); }
/****************************************************** Handle mouse events *******************************************************/ void HandleMouse(int button,int state,int x,int y) { int i,maxselect = 100,nhits = 0; GLuint selectlist[100]; if (state == GLUT_DOWN) { glSelectBuffer(maxselect,selectlist); glRenderMode(GL_SELECT); glInitNames(); glPushName(-1); glPushMatrix(); MakeCamera(TRUE,x,y); MakeGeometry(); glPopMatrix(); nhits = glRenderMode(GL_RENDER); if (button == GLUT_LEFT_BUTTON) { } else if (button == GLUT_MIDDLE_BUTTON) { } /* Right button events are passed to menu handlers */ if (nhits == -1) fprintf(stderr,"Select buffer overflow\n"); if (nhits > 0) { fprintf(stderr,"\tPicked %d objects: ",nhits); for (i=0;i<nhits;i++) fprintf(stderr,"%d ",selectlist[4*i+3]); fprintf(stderr,"\n"); } } }
/// returns the selected face, NULL if nothing selected view::face* view::get_selected_face(int x, int y) { GLuint select_buf[SELECT_BUF_SIZE]; glSelectBuffer(SELECT_BUF_SIZE, select_buf); glRenderMode(GL_SELECT); glInitNames(); GLdouble projection_matrix[16]; glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); glMatrixMode (GL_PROJECTION); glLoadIdentity (); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); gluPickMatrix (GLdouble(x), GLdouble(viewport[3] - y), SELECT_TOL, SELECT_TOL, viewport); glMultMatrixd(projection_matrix); basemesh_drawable->render_faces(); glMatrixMode(GL_PROJECTION); glLoadMatrixd(projection_matrix); glFlush(); GLint hits = glRenderMode(GL_RENDER); if (hits > 0) { int selected_face = select_buf[3]; int depth = select_buf[1]; for (int i = 1; i < hits; i++) { if (select_buf[i*4+1] < GLuint(depth)) { selected_face = select_buf[i*4+3]; depth = select_buf[i*4+1]; } } return (face*)selected_face; } return NULL; }
std::vector<GUIGlID> GUISUMOAbstractView::getObjectsInBoundary(const Boundary& bound) { const int NB_HITS_MAX = 1024 * 1024; // Prepare the selection mode static GUIGlID hits[NB_HITS_MAX]; static GLint nb_hits = 0; glSelectBuffer(NB_HITS_MAX, hits); glInitNames(); Boundary oldViewPort = myChanger->getViewport(false); // backup the actual viewPort myChanger->setViewport(bound); applyGLTransform(false); // paint in select mode int hits2 = doPaintGL(GL_SELECT, bound); // Get the results nb_hits = glRenderMode(GL_RENDER); if (nb_hits == -1) { myApp->setStatusBarText("Selection in boundary failed. Try to select fewer than " + toString(hits2) + " items"); } std::vector<GUIGlID> result; for (int i = 0; i < nb_hits; ++i) { assert(i * 4 + 3 < NB_HITS_MAX); result.push_back(hits[i * 4 + 3]); } // switch viewport back to normal myChanger->setViewport(oldViewPort); return result; }
/////////////////////////////////////////////////////////// // Render the torus and sphere void DrawObjects(void) { // Save the matrix state and do the rotations glMatrixMode(GL_MODELVIEW); glPushMatrix(); // Translate the whole scene out and into view glTranslatef(-0.75f, 0.0f, -2.5f); // Initialize the names stack glInitNames(); glPushName(0); // Set material color, Yellow // torus glColor3f(1.0f, 1.0f, 0.0f); glLoadName(TORUS); glPassThrough((GLfloat)TORUS); DrawTorus(40, 20); // Draw Sphere glColor3f(0.5f, 0.0f, 0.0f); glTranslatef(1.5f, 0.0f, 0.0f); glLoadName(SPHERE); glPassThrough((GLfloat)SPHERE); DrawSphere(0.5f); // Restore the matrix state glPopMatrix(); // Modelview matrix }
static gboolean on_motion_notify(GritsOpenGL *opengl, GdkEventMotion *event, gpointer _) { gdouble height = GTK_WIDGET(opengl)->allocation.height; gdouble gl_x = event->x; gdouble gl_y = height - event->y; /* Configure view */ gint viewport[4]; gdouble projection[16]; glGetIntegerv(GL_VIEWPORT, viewport); glGetDoublev(GL_PROJECTION_MATRIX, projection); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(gl_x, gl_y, 2, 2, viewport); glMultMatrixd(projection); /* Prepare for picking */ guint buffer[100][4] = {}; glSelectBuffer(G_N_ELEMENTS(buffer), (guint*)buffer); glRenderMode(GL_SELECT); glInitNames(); /* Run picking */ g_mutex_lock(opengl->objects_lock); GPtrArray *objects = _objects_to_array(opengl); for (guint i = 0; i < objects->len; i++) { glPushName(i); GritsObject *object = objects->pdata[i]; object->state.picked = FALSE; grits_object_pick(object, opengl); glPopName(); } int hits = glRenderMode(GL_RENDER); g_debug("GritsOpenGL: on_motion_notify - hits=%d ev=%.0lf,%.0lf", hits, gl_x, gl_y); for (int i = 0; i < hits; i++) { //g_debug("\tHit: %d", i); //g_debug("\t\tcount: %d", buffer[i][0]); //g_debug("\t\tz1: %f", (float)buffer[i][1]/0x7fffffff); //g_debug("\t\tz2: %f", (float)buffer[i][2]/0x7fffffff); //g_debug("\t\tname: %p", (gpointer)buffer[i][3]); guint index = buffer[i][3]; GritsObject *object = objects->pdata[index]; object->state.picked = TRUE; } for (guint i = 0; i < objects->len; i++) { GritsObject *object = objects->pdata[i]; grits_object_set_pointer(object, object->state.picked); } g_ptr_array_free(objects, TRUE); g_mutex_unlock(opengl->objects_lock); /* Cleanup */ glMatrixMode(GL_PROJECTION); glPopMatrix(); return FALSE; }
/* ARGSUSED */ void locate(int value) { GLint viewport[4]; GLint hits; if (locating) { if (mouse_state == GLUT_ENTERED) { (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); viewport[0] = 0; viewport[1] = 0; viewport[2] = W; viewport[3] = H; gluPickMatrix(x, H - y, 5.0, 5.0, viewport); myortho(); glMatrixMode(GL_MODELVIEW); draw(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits = glRenderMode(GL_RENDER); } else { hits = 0; } processHits(hits, selectBuf); } locating = 0; }
void mouse(int button, int state, int x, int y) { if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; GLuint select_buf[BUFSIZE]; GLint hits; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFSIZE, select_buf); (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix((GLdouble) x, (GLdouble) viewport[3] - y, 20.0, 20.0, viewport); gluPerspective(45.0, width / (double) height, .2, 200.0); scene.render(true); glMatrixMode(GL_PROJECTION); glPopMatrix(); glFlush(); hits = glRenderMode(GL_RENDER); if (hits != 0) processHits(hits, select_buf); glutPostRedisplay(); }
void Engine::processMouseButtonPressed(sf::Event &event) { if (event.MouseButton.Button == sf::Mouse::Left) { glRenderMode(GL_SELECT); glInitNames(); glPushName(cfg::emptySelectName); display(true); GLint nHits = glRenderMode(GL_RENDER); cout << "nhits: " << nHits << endl; GLint selectedName = -1; GLuint lowestZMin = 0xFFFFFFFF; for (int i = 0, index = 0; i < nHits; ++i) { GLint nItems = selectBuffer[index++]; GLuint zMin = selectBuffer[index++]; //GLuint zMax = selectBuffer[index++]; ++index; if (zMin < lowestZMin) { selectedName = selectBuffer[index+nItems-1]; lowestZMin = zMin; } index += nItems; } model->select(selectedName); transformingObject = true; } else if (event.MouseButton.Button == sf::Mouse::Right) rotatingCamera = true; else if (event.MouseButton.Button == sf::Mouse::Middle) positioningCamera = true; }
void DrawS() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(0.0f,0.0f,-35.0f); glPushMatrix(); // NEW: Prepare Dynamic Transform glMultMatrixf(Transform.M); // NEW: Apply Dynamic Transform glColor3f(0.20f,1.00f,0.3f); glInitNames(); if(recyclingMode) { for(int i=bridge->nedges; i--;) { Edge& e=bridge->edges[i]; // glPopName(); glPopName(); glPopName(); glPopName(); glPushName((unsigned int) e.from->b.x + 24); glPushName((unsigned int) e.from->b.y + 13); glPushName((unsigned int) e.to->b.x + 24); glPushName((unsigned int) e.to->b.y + 13); glColor3f(0.6f,0.6f,0.6f); Objects::timber4(e.width, e.from->b.x, e.from->b.y, e.to->b.x, e.to->b.y); glPopName(); glPopName(); glPopName(); glPopName(); } // glPopName(); glPopName(); glPopName(); glPopName(); } else { Objects::gridS(); } Objects::square(2.7,0,-13); Objects::upButton(0.8,22,-11,1); Objects::upButton(0.8,22,-13,-1); glPopMatrix(); // NEW: Unapply Dynamic Transform glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 7.0 glFlush(); }
void Setup3dSelect ( Viewer3dParam *par, GLuint *buffer, int cursorX, int cursorY ) { GLint viewport[4]; glSelectBuffer(SELECT_BUFSIZE, buffer); glRenderMode(GL_SELECT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glGetIntegerv(GL_VIEWPORT,viewport); gluPickMatrix(cursorX,viewport[3]-cursorY, 5,5,viewport); float ratio = 1.386; gluPerspective(par->fov, ratio, par->zNear, par->zFar); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(par->eye[0],par->eye[1],par->eye[2], par->unit[0],par->unit[1],par->unit[2], par->up[0],par->up[1],par->up[2]); glInitNames(); }
void selectionDetection(int button, int state, int x, int y) { GLuint selectBuf[ BUFSIZE ] ; GLint viewport[4]; if (state !=GLUT_DOWN) return; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFSIZE, selectBuf); (void) glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluPerspective(45.0, (GLfloat) ww/(GLfloat) wh, 1.0, 50.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); draw(GL_SELECT); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glFlush(); glMatrixMode(GL_MODELVIEW); processHits( glRenderMode(GL_RENDER),selectBuf, button); }
bool Renderer::offsetOnPoint(int x, int y, int& out) { GLuint buf[256]; glSelectBuffer(256, buf); glRenderMode(GL_SELECT); setProjectionMatrix(true, x, y); glInitNames(); drawScene(true); GLint hits = glRenderMode(GL_RENDER); setProjectionMatrix(); GLuint *p = buf; GLuint minZ = INFINITE; GLuint selectedName = 0; for (int i=0; i < hits; i++){ GLuint names = *p; p++; GLuint z = *p; p++; p++; if ((names > 1) && (z < minZ) && (*p == SELECTION_COVERS)){ minZ = z; selectedName = *(p+1); } p += names; } if (minZ != INFINITE){ out = (selectedName - SELECTION_CENTER); return true; } else { return false; } }
TokenId KPboardView::Selection(const Camera &camera, int x, int y) const { GLuint buffer[BUF_SIZE]; // Set Up A Selection Buffer auto choose = TokenId::EMPTY; glSelectBuffer(BUF_SIZE, buffer); glRenderMode(GL_SELECT); // Change to Selection mode glInitNames(); glPushName(0); // Push 0 (At Least One Entry) Onto The Stack camera.Draw(x, y); Draw(true); // Draw the tokens (not the board) auto hits = glRenderMode(GL_RENDER); // Change back to render mode if (hits > 0) { choose = static_cast<TokenId>(buffer[3]); auto depth = buffer[1]; for (auto loop = 1; loop < hits; loop++) { if (buffer[loop * 4 + 1] < GLuint(depth)) { // store the closest object choose = static_cast<TokenId>(buffer[loop * 4 + 3]); depth = buffer[loop * 4 + 1]; } } } return choose; }
void pickRects(AUX_EVENTREC *event) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; int x, y; x = event->data[AUX_MOUSEX]; y = event->data[AUX_MOUSEY]; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); glOrtho (0.0, 8.0, 0.0, 8.0, -0.5, 2.5); drawRects (GL_SELECT); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); }
bool C3dView::BeginSelect ( int x, int y ) { if (m_bDrawing ) return false; m_bDrawing = true; m_bOverlayMode = false; m_bForcedDrawing = false; GLint viewport[4] = {0}; memset(m_uiSelectBuf,0,sizeof(GLuint)*_SEL_BUFFER_SIZE); glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(_SEL_BUFFER_SIZE, m_uiSelectBuf); glRenderMode(GL_SELECT); glInitNames(); glPushName(0xffffffff); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y),2.0, 2.0, viewport); gluPerspective (m_fov,m_fAspect,m_xNearZ,m_xFarZ); glMatrixMode(GL_MODELVIEW); glPushMatrix(); SetInitalMatrix(); return true; }
Vector<int> GLCtrl::GLPicking::Pick(int x, int y, Callback resizeCallback, Callback paintCallback) { GLuint buffer[_bufferSize]; _pickPoint = Point(x, y); glSelectBuffer(_bufferSize, buffer); glRenderMode(GL_SELECT); _isPicking = true; resizeCallback(); glInitNames(); paintCallback(); _isPicking = false; resizeCallback(); // returning to normal rendering mode int hits = glRenderMode(GL_RENDER); if (hits == 0) return Vector<int>(); else return ParseHits(buffer, hits); }
static GLint DoSelect(GLint x, GLint y) { GLint hits; glSelectBuffer(MAXSELECT, selectBuf); glRenderMode(GL_SELECT); glInitNames(); glPushName(~0); glPushMatrix(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPickMatrix(x, windH - y, 4, 4, vp); gluOrtho2D(-175, 175, -175, 175); glMatrixMode(GL_MODELVIEW); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glScalef(zoom, zoom, zoom); glRotatef(zRotation, 0, 0, 1); Render(GL_SELECT); glPopMatrix(); hits = glRenderMode(GL_RENDER); if (hits <= 0) { return -1; } return selectBuf[(hits - 1) * 4 + 3]; }
// Render the cube and sphere void DrawObjects(void) { // Save the matrix state and do the rotations glMatrixMode(GL_MODELVIEW); glPushMatrix(); // Translate the whole scene out and into view glTranslatef(-80.0f, 0.0f, -300.0f); // Initialize the names stack glInitNames(); glPushName(0); // Set material color, Yellow // Cube glRGB(255, 255, 0); glLoadName(CUBE); glPassThrough((GLfloat)CUBE); glutSolidCube(75.0f); // Draw Sphere glRGB(128,0,0); glTranslatef(130.0f, 0.0f, 0.0f); glLoadName(SPHERE); glPassThrough((GLfloat)SPHERE); glutSolidSphere(50.0f, 15, 15); // Restore the matrix state glPopMatrix(); // Modelview matrix }
static void selectObjects(void) { GLuint selectBuf[BUFSIZE]; GLint hits; glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(0); glPushMatrix (); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho (0.0, 5.0, 0.0, 5.0, 0.0, 10.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glLoadName(1); drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0); glLoadName(2); drawTriangle (2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0); glLoadName(3); drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0); drawTriangle (2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf); }
/* This routine is just OpenGL, no WX (except wxGetApp) */ void DisplayWindow::render() { ParticleSystems *ps = &(wxGetApp().particlesystems); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glInitNames(); for (unsigned int j = 0; j < ps->size(); j++) { glPushName(j); for(unsigned int i = 0; i < (*ps)[j]->particles.size(); i++) { glPushName(i); (*ps)[j]->particles[i]->draw(); glPopName(); } glPopName(); } glFlush(); SwapBuffers(); //added for GL canvas capturing -ms if(wxGetApp().captureCanvasFlag) this->captureCanvas(); ps->animation(); if(wxGetApp().captureSVGflag) this->captureSVG(); }
void picking(int iXPos, int iYPos) { float fRatio; GLint viewport[4]; for (int i = 0; i < 512; i++) { g_uiBuffer[i] = 0; } glSelectBuffer(512, g_uiBuffer); glGetIntegerv(GL_VIEWPORT, viewport); glRenderMode(GL_SELECT); glInitNames(); g_bPicking = true; glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); int delX = 16; int delY = 16; gluPickMatrix(iXPos, viewport[3] - iYPos, delX, delY, viewport); fRatio = ((float) viewport[2] / (float) viewport[3]); gluPerspective(30.0f, fRatio, 0.1f, 50000.0f); glMatrixMode(GL_MODELVIEW); }
int SubdivScene::select(int x, int y, int width, int height, float fovy, Face* f) { GLuint buffer[SELECT_BUFFER_SIZE]; glSelectBuffer(SELECT_BUFFER_SIZE, buffer); glRenderMode(GL_SELECT); glInitNames(); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, (GLint*) &viewport); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(x,y, 2, 2, (GLint*) &viewport); gluPerspective(fovy, (GLfloat) width/height, 0.01, 100.0); glMatrixMode(GL_MODELVIEW); glPushName(0); //draw the scene we will select from if (f) drawQuads(f); else drawHDS(true); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); const GLuint hitlist = glRenderMode(GL_RENDER); return processHits(hitlist, buffer); }
int Tetrahedron::faceAtPosition(const QPoint &pos) { const int MaxSize = 512; GLuint buffer[MaxSize]; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(MaxSize, buffer); glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(GLdouble(pos.x()), GLdouble(viewport[3] - pos.y()), 5.0, 5.0, viewport); GLfloat x = GLfloat(width()) / height(); glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0); draw(); glMatrixMode(GL_PROJECTION); glPopMatrix(); if (!glRenderMode(GL_RENDER)) return -1; return buffer[3]; }
/////////////////////////////////////////////////////////// // Render the torus and sphere void DrawObjects(void) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(-0.75f, 0.0f, -2.5f); //初始化名称栈 glInitNames(); glPushName(0); //圆环 glColor3f(1.0f, 1.0f, 0.0f); //圆环的名称 glLoadName(TORUS); //设置一个自定义的过渡标记,与圆环的名称相同,以便后面的解析判断 glPassThrough((GLfloat)TORUS); DrawTorus(40, 20); //球体 glColor3f(0.5f, 0.0f, 0.0f); glTranslatef(1.5f, 0.0f, 0.0f); //球体的名称 glLoadName(SPHERE); //球体的过渡标记 glPassThrough((GLfloat)SPHERE); DrawSphere(0.5f); glPopMatrix(); }
point mouse_pointer::draw_grid() { GLint x; GLint y; GLint block; GLint hits; // Set up a selection buffer GLuint buffer[512]; gl_vector3 v1; gl_vector3 v2; gl_vector3 v3; gl_vector3 v4; point cell; ini_manager ini_mgr; GLint pt_size = ini_mgr.get_int("Pointer Settings", "pt_size"); memset(buffer, 0, sizeof(buffer)); // Tell OpenGL to use our array for selection glSelectBuffer(512, buffer); // Put OpenGL in selection mode. glRenderMode(GL_SELECT); glInitNames(); glPushName(0); block = 0; glDisable(GL_CULL_FACE); for(y = 0; y < map_->get_size(); y += pt_size) { for(x = 0; x < map_->get_size(); x += pt_size) { block = x + (y * map_->get_size()); glLoadName(block); v1 = map_->get_position(x, y); v2 = map_->get_position(x, y + pt_size); v3 = map_->get_position(x + pt_size, y + pt_size); v4 = map_->get_position(x + pt_size, y); glBegin(GL_QUADS); glVertex3fv(v1.get_data()); glVertex3fv(v2.get_data()); glVertex3fv(v3.get_data()); glVertex3fv(v4.get_data()); glEnd(); } } hits = glRenderMode(GL_RENDER); cell.set_y(-1); cell.set_x(-1); if(hits > 0) { block = buffer[3]; cell.set_x((block % map_->get_size()) + (pt_size / 2)); cell.set_y(((block - cell.get_x()) / map_->get_size()) + (pt_size / 2)); } return cell; }
void MyRenderThread::gl_select(int posx, int posy) { std::cout << "gl_select \n"; // we need to lock the rendering context //lockGLContext(); // this is the same as in every OpenGL picking example const int MaxSize = 512; // see below for an explanation on the buffer content GLuint buffer[MaxSize]; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(MaxSize, buffer); // enter select mode glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix((GLdouble)posx, (GLdouble)(viewport[3] - posy), 5.0, 5.0, viewport); GLfloat x = (GLfloat)GLFrame->width() / GLFrame->height(); glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); paintGL(); glMatrixMode(GL_PROJECTION); glPopMatrix(); // finally release the rendering context again // if (!glRenderMode(GL_RENDER)) // { // std::cout << "rendermode !GL_RENDER \n"; // //unlockGLContext(); // return ; // } //unlockGLContext(); // Each hit takes 4 items in the buffer. // The first item is the number of names on the name stack when the hit occured. // The second item is the minimum z value of all the verticies that intersected // the viewing area at the time of the hit. The third item is the maximum z value // of all the vertices that intersected the viewing area at the time of the hit // and the last item is the content of the name stack at the time of the hit // (name of the object). We are only interested in the object name // (number of the surface). // return the name of the clicked surface std::cout << sizeof(buffer)<< "face: " << buffer[3] <<"\n"; return ; }