Exemple #1
0
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;
}
Exemple #2
0
/* 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;
}
Exemple #3
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();
}
/*******************************************************
   Set up the camera
   Optionally creating a small viewport about
   the mouse click point for object selection
********************************************************/
void MakeCamera(int pickmode,int x,int y)

{
   static double theta = 0;
   GLint viewport[4];

   /* Camera setup */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (pickmode == TRUE) {
      glGetIntegerv(GL_VIEWPORT,viewport); /* Get the viewport bounds */
      gluPickMatrix(x,viewport[3]-y,3.0,3.0,viewport);
   }
   gluPerspective(70.0,          /* Field of view */
                   1.0,          /* aspect ratio  */
                   0.1,1000.0);  /* near and far  */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt(300*cos(theta*PI/180)*sin(updownrotate*PI/180),
             300*cos(updownrotate*PI/180),
             300*sin(theta*PI/180)*sin(updownrotate*PI/180),
             0.0,0.0,0.0,                                   /* Focus    */
             0.0,1.0,0.0);                                  /* Up       */
   if (spincamera)
      theta += (cameradirection * 0.2);
}
Exemple #5
0
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();
}
Exemple #6
0
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 PerspectiveCamera::setCameraForSelection(int x,int y,int selectionWidth,int selectionHeight,float width,float height,float ratio)
{
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT,viewport);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
/*
	glColor3ub(255,0,0);
	glBegin(GL_QUADS);
	glVertex2f(-1,-1);
glVertex2f(-1,1);
glVertex2f(1,1);
glVertex2f(1,-1);
	glEnd();
*/


	gluPickMatrix(x+selectionWidth*0.5f,viewport[3]-y-selectionHeight*0.5f,selectionWidth,selectionHeight,viewport);

	gluPerspective(fov(), ratio, nearPlane(), farPlane());
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(from()[0],from()[1],from()[2] ,to()[0],to()[1], to()[2],up()[0],up()[1],up()[2]);
}
Exemple #8
0
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];
}
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;
}
void GameBackground::SelectionMode(int x, int y)
{
	static GLuint selectBuffer[64];
	float aspect;
	GLint hits, viewport[4];

	glSelectBuffer(64,selectBuffer);
	glGetIntegerv(GL_VIEWPORT,viewport);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();

	glRenderMode(GL_SELECT);
	glLoadIdentity();
	gluPickMatrix(x,viewport[3]-y,2,2,viewport);

	_select = true;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	Render(_engine_instance->GetSecondsElapsed());
	_select = false;

	hits = glRenderMode(GL_RENDER);

	for (int i=0;i<hits;i++)
	{
		printf("Hits %u : %d\n",hits,selectBuffer[3+4*i]);
	}
	printf("\n");

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glMatrixMode(GL_MODELVIEW);
}
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);
}
Exemple #13
0
// Setup viewport to a 1 pixel window where the user clicked.
// Set the "pickMode" flag and call draw to find out what piece of the grid was drawn.
bool MapDraw::pickPoint(int x, int y, Camera &cam)
{
  // This flag makes it so "startCheck" and "endCheck" are called
  // before and after every polygon is drawn
  pickMode = true;


  int viewport[]={0,0,255,191}; // used later for gluPickMatrix()
  //change ortho vs perspective
  glViewport(0,192,0,192); // set the viewport to fullscreen
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPickMatrix(x,(191-y),4,4,viewport); // render only what is below the cursor
  cam.Perspective();
  glMatrixMode(GL_MODELVIEW);

  // opaque polygons, no culling, this fixes my problems.
  glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);

  // Un-mark the last one.
  if (picked)
    picked->picked = false;

  // pick mode flags will do the magic & return picked patch.
  picked = draw();

  // Mark the new one (which may still be the last one btw)
  if (picked)
    picked->picked = true;

  // Turn off pick mode.
  pickMode = false;

  return endCheck();
}
Exemple #14
0
void 
ViewCameraGL::beginSelectGL(const QRect& region)
{
  GEOM_GL_ERROR;

  GLint viewport[4];
  int x = region.center().x();
  int y = region.center().y();
  // printf("%i %i %i %i\n",x,y,region.width(),region.height());

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();

  glGetIntegerv(GL_VIEWPORT, viewport);
  gluPickMatrix((GLdouble)x,(GLdouble)viewport[3]-y,
#ifdef _MSC_VER
  (GLdouble)max(region.width(),2),
  (GLdouble)max(region.height(),2),viewport);
#else
  (GLdouble)std::max(region.width(),2),
  (GLdouble)std::max(region.height(),2),viewport);
#endif
  glPushProjectionMatrix();

  glInitModelViewMatrix();

  GEOM_GL_ERROR;

}
Exemple #15
0
Block *
click(int x, int y, int *face)
{
	GLuint  buffer[BUFFER_SIZE];
	GLint	viewport[4];
	GLint  hits;

	glGetIntegerv(GL_VIEWPORT, viewport);
	glSelectBuffer(BUFFER_SIZE, buffer);

	glRenderMode(GL_SELECT);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3]-y), 1.0f, 1.0f, viewport);

	gluPerspective(FOV, (GLfloat) (WINDOW_WIDTH/WINDOW_HEIGHT), Z_NEAR, Z_FAR);
	glMatrixMode(GL_MODELVIEW);
	redraw_world();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();

	glMatrixMode(GL_MODELVIEW);
	hits=glRenderMode(GL_RENDER);

	return processHits(hits, buffer, face);
}
Exemple #16
0
/// 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;
}
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);
} 
Exemple #18
0
void ProcessSelection(int xPos, int yPos)
{

  static GLuint selectBuff[BUFFER_LENGTH];
  GLint hits, viewport[4];
  glSelectBuffer(BUFFER_LENGTH, selectBuff);
  glGetIntegerv(GL_VIEWPORT, viewport);
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glRenderMode(GL_SELECT);
  glLoadIdentity();
  gluPickMatrix(xPos, viewport[3] - yPos + viewport[1], 2,2, viewport);
  gluPerspective(60.0f, fAspect, 1.0, 1000.0);
  DrawObjects();
  hits = glRenderMode(GL_RENDER);
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  if(hits == 1)
    {
      MakeSelection(selectBuff[3]);
      if(selectedObject == selectBuff[3])
	selectedObject = 0;
      else
	selectedObject = selectBuff[3];
    }

  printf("holaaaaa donde estoy en %d , %d\n", xPos, yPos);

  glutPostRedisplay();
}
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);
}
Exemple #20
0
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();
} 
Exemple #21
0
/* PickMatrix( x, y, deltax, deltay, viewportArray) -> none */
static int luaglu_pick_matrix(lua_State *L)
{
  GLint *vp;
  int num_args;

  if (!( lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isnumber(L, 3)
    && lua_isnumber(L, 4)))
  {
    luaL_error(L, "incorrect argument to function 'glu.PickMatrix'");
    return 0;
  }

  if(!lua_istable(L, 5) || (num_args = luagl_get_arrayi(L, 5, &vp)) < 4)
  {
    luaL_error(L, "incorrect argument to function 'glu.PickMatrix'");
    return 0;
  }

  gluPickMatrix(
    (GLdouble)lua_tonumber(L, 1), (GLdouble)lua_tonumber(L, 2), (GLdouble)lua_tonumber(L, 3),
    (GLdouble)lua_tonumber(L, 4), vp);

  LUAGL_DELETE_ARRAY(vp);

  return 0;
}
void SelectionRecorder::enterSelectionMode(int x, int y)
{
    // Pre-multiply the projection matrix with a pick matrix, which expands
    // the viewport to fill the 1x1 square under the mouse pointer at (x, y).
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPickMatrix((double)x, (double)(viewport[3] - y - 1), 1, 1, viewport);
    glMultMatrixd(projectionMatrix);
    glMatrixMode(GL_MODELVIEW);

    // restrict the viewport to [0,1]x[0,1]
    glEnable(GL_SCISSOR_BOX);
    glScissor(0, 0, 1, 1);
    glViewport(0, 0, 1, 1);

    // set an invalid object index
    double clearColor[4];
    glGetDoublev(GL_COLOR_CLEAR_VALUE, clearColor);
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
    depthTest = glIsEnabled(GL_DEPTH_TEST);
    glEnable(GL_DEPTH_TEST);
    setObjectIndex(-1);
}
/*******************************************************
   Set up the camera
   Optionally creating a small viewport about
   the mouse click point for object selection.
   Note:
   1. change the camera E distance from the origin of
      the world coordinate system, by incease rho,
      the default set rho = 400, need to change to
      interactive user defined way.
********************************************************/
void MakeCamera(int pickmode,int x,int y)

{
   #define rho  200      //Eye distance from the origin of the world coordinate

   static double theta = 0;
   GLint viewport[4];

   /* Camera setup */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (pickmode == TRUE) {
      glGetIntegerv(GL_VIEWPORT,viewport); /* Get the viewport bounds */
      gluPickMatrix(x,viewport[3]-y,3.0,3.0,viewport);
   }
   gluPerspective(70.0,          /* Field of view */
                   1.0,          /* aspect ratio  */
                   0.1,1000.0);  /* near and far  */

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
/*
   Note the rho is defined to give the eye location
*/
   gluLookAt(rho*cos(theta*PI/180)*sin(updownrotate*PI/180), //control the E distance
             rho*cos(updownrotate*PI/180),
             rho*sin(theta*PI/180)*sin(updownrotate*PI/180),
             0.0,0.0,0.0,                                   /* Focus    */
             0.0,1.0,0.0);                                  /* Up       */
   if (spincamera)
      theta += (cameradirection * 0.2);
}
    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);
    }
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];
}
Exemple #26
0
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);
}
Exemple #27
0
/**
 * GLUT callback to handle mouse.
 */
void
oth_mouse(int button, int state, int x, int y)
{
        int r, f;
        GLint hits;
        GLuint name_stack[100]; // [FIXME] unnecesserily big ?
        GLint viewport[4];
        Board* board = current_board;
        Camera* camera = current_camera;

        if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
                return;

        glGetIntegerv(GL_VIEWPORT, viewport);

        /* Set up viewing for selection mode */
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        gluPickMatrix(x, viewport[3] - (y - viewport[1] * 2), 2, 2,
                      viewport);
        gluPerspective(camera->frustum.fov, 1.0, camera->frustum.close,
                       camera->frustum.distant);
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();

        /* Initialize name stack and enter selection mode */
        glInitNames();
        glSelectBuffer(100, name_stack);        // [FIXME]
        glRenderMode(GL_SELECT);

        /* Force redraw */
        oth_display();

        /* Restore viewing conditions */
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
        glFlush();

        /* Validate choice */
        hits = glRenderMode(GL_RENDER);
        if (hits != 1)
                return;
        r = rank(board, name_stack[3]);
        f = file(board, name_stack[3]);
        if (!oth_shift_valid(board, r, f))
        {
                r = f = -1;     /* invalid choice */
                return;
        }

        /* Process valid choice */
        oth_shift_update(board, r, f);
        glutPostRedisplay();
}
 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 ;
 }
Exemple #29
0
int Core::getSelectedId(int x, int y)
{
  GLuint buff[64] = {0};
  GLint hits, view[4];
  int id;
  
  //This chooses the buffer where to store the values for the selection data
  glSelectBuffer(64, buff);
  
  //This retrieves info about the viewport
  glGetIntegerv(GL_VIEWPORT, view);

  //Switching in selecton mode
  glRenderMode(GL_SELECT);
  
  // Clearing the names' stack
  // This stack contains all the info about the objects
  glInitNames();
  
  // Now fill the stack with one element (or glLoadName will generate an error)
  glPushName(0);

  // Now modify the viewing volume, restricting selection area around the cursor
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();

  // Restrict the draw to an area around the cursor
  //printf("XY %d %d %d %d %d %d %f\n",x, y, view[0], view[1], view[2], view[3], (float)width/height);
  gluPickMatrix(x, view[3] - y, 1, 1, view);
  gluPerspective(45, (float)width/height, .1, 100);

  // Draw the objects onto the screen
  glMatrixMode(GL_MODELVIEW);
  // Draw only the names in the stack, and fill the array
  draw();
  
  // Do you remeber? We do pushMatrix in PROJECTION mode
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  // Get number of objects drawed in that area
  // and return to render mode
  hits = glRenderMode(GL_RENDER);
  
  // Print a list of the objects
  //list_hits(hits, buff);
  
  // uncomment this to show the whole buffer
  //gl_selall(hits, buff);
  
  glMatrixMode(GL_MODELVIEW);

  if(hits > 0)
    return buff[3];
  else
    return 0;
}
Exemple #30
0
//---------------------------------------------------------------------------
void CFabAtHomeView::Pick(UINT nFlags, CPoint point)
//---------------------------------------------------------------------------
{// find objects in selction zone

	if(!this->bShowModel) return;
	#define BUFSIZE 1000

	GLuint selectBuf[BUFSIZE];
	GLint hits=0, viewport[4];

	glGetIntegerv(GL_VIEWPORT, viewport);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	gluPickMatrix(point.x, viewport[3]-point.y, 5, 5, viewport);
	SetViewTransformation();
    glMatrixMode(GL_MODELVIEW);

	glSelectBuffer(BUFSIZE, selectBuf);
	glRenderMode(GL_SELECT);
	glInitNames();
    DrawScene();
	hits = glRenderMode(GL_RENDER);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	SetViewTransformation();
    glMatrixMode(GL_MODELVIEW);

	// deselect all if necessary

	CFabAtHomeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if ((nFlags & MK_SHIFT) == 0) {
		pDoc->model.SelectAll(false);
	}

	// parse hit buffer and select items

	int idx=0,v;
	for (int hit = 0; hit<hits; hit++) {
  		int names = selectBuf[idx];
		if (names == 2) {
			switch (selectBuf[idx+3]) {
			case NAMESTACK_CHUNK:
				v = selectBuf[idx+4];
				ASSERT(v >= 0 && v <= pDoc->model.chunk.GetSize());
				pDoc->model.SelectOneChunk(v);
				pDoc->UpdateAllViews(0);
				return;
				break;
			}
		}
		idx += 3+names;
	}

	pDoc->UpdateAllViews(0);
}