예제 #1
0
//transform the x, y position on the screen into the corresponding 3D world position
void produceRay(int x_I, int y_I, Vec3Df * origin, Vec3Df * dest)
{
		int viewport[4];
		double modelview[16];
		double projection[16];
		glGetDoublev(GL_MODELVIEW_MATRIX, modelview); //recuperer matrices
		glGetDoublev(GL_PROJECTION_MATRIX, projection); //recuperer matrices
		glGetIntegerv(GL_VIEWPORT, viewport);//viewport
		int y_new = viewport[3] - y_I;

		double x, y, z;
		
		gluUnProject(x_I, y_new, 0, modelview, projection, viewport, &x, &y, &z);
		origin->p[0]=float(x);
		origin->p[1]=float(y);
		origin->p[2]=float(z);
		gluUnProject(x_I, y_new, 1, modelview, projection, viewport, &x, &y, &z);
		dest->p[0]=float(x);
		dest->p[1]=float(y);
		dest->p[2]=float(z);
}
예제 #2
0
ofVec3f ofkUnProjectionHelper::getProjectionPoint(float ofScreenPosX, float ofScreenPosY, const GLdouble *model, const GLdouble *proj, const GLint *viewPort)
{
    ofVec3f res;
    
    // ---------------- SETTING MATRIX  ----------------  //
    glViewport(viewPort[0], viewPort[1], viewPort[2], viewPort[3]);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadMatrixd(proj);
    
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadMatrixd(model);

    // ---------------- DRAW and pickup points ---------------  //
    //Enable Depth
    glEnable(GL_DEPTH_TEST);
    
    //DEpth Buffer Clear
    glClear(GL_DEPTH_BUFFER_BIT);
    
    //Once Render one Plane to Draw Depth Buffer
    ofEnableAlphaBlending();
    ofSetColor(255, 255, 255, 0);
    ofFill();
    ofRect(-10000, -10000, 20000, 20000);

    //-> FUNC
    double objX;
	double objY;
	double objZ;
    float z;
    
    //-> FUNC
    glReadPixels(ofScreenPosX, viewPort[3] - ofScreenPosY ,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
    gluUnProject(ofScreenPosX, viewPort[3] - ofScreenPosY ,z,model, proj, viewPort,&objX,&objY,&objZ);
    
    res.x = objX;
    res.y = objY;
    res.z = 0.0;  // Actually , we'll get small number in objZ , I guess it's computation Error of double
    
    glClear(GL_DEPTH_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    
    return res;
}
예제 #3
0
파일: nRenderer.cpp 프로젝트: gan74/nGine
nVec3 nRenderer::getRay(nVec2 screenPos) {
	double x = screenPos.x;
	double y = screenPos.y;
	GLint viewport[4];
	GLfloat winX, winY;
	GLdouble posX, posY, posZ;
	glGetIntegerv(GL_VIEWPORT, viewport);
	winX = x;
	winY = y;
	gluUnProject(winX, winY, 1, (/*objectMatrix * */viewMatrix).matrix, projectionMatrix.matrix, viewport, &posX, &posY, &posZ);
	return nVec3(posX, posY, posZ).normalize();
}
예제 #4
0
// get intersection with plane for "trackball" style rotation
static vec planar_coords(GLdouble mx, GLdouble my)
{
  GLdouble ax,ay,az;

  gluUnProject(mx,my,0,ab_glm,ab_glp,ab_glv,&ax,&ay,&az);
  vec m = vec((float)ax,(float)ay,(float)az) - ab_eye;
  // intersect the point with the trackball plane
  GLfloat t = (ab_planedist - ab_zoom) / (ab_eyedir * m);
  vec d = ab_eye + m*t;

  return vec(d*ab_up,d*ab_out,0.0);
}
예제 #5
0
// Uses the last GL matrices set in set_perspective to project a point from
// screen coordinates to the agent's region.
void LLViewerCamera::projectScreenToPosAgent(const S32 screen_x, const S32 screen_y, LLVector3* pos_agent) const
{

	GLdouble x, y, z;
	gluUnProject(
		GLdouble(screen_x), GLdouble(screen_y), 0.0,
		gGLModelView, gGLProjection, (GLint*)gGLViewport,
		&x,
		&y,
		&z );
	pos_agent->setVec( (F32)x, (F32)y, (F32)z );
}
예제 #6
0
void motion(int x, int y) {
    if(mudaCurva) {
        realy=matrizViewport[3] - (GLint) y - 1;
        gluUnProject((GLdouble) x, (GLdouble) realy, winz,
                     matrizModelview, matrizProjecao, matrizViewport,
                     &wx, &wy, &wz);
        vertices[vcx][vcy][0]=wx;
        vertices[vcx][vcy][1]=wy;
        vertices[vcx][vcy][2]=wz;
        glutPostRedisplay();
    }
}
예제 #7
0
void DEMGenerator::convert(int screenX, int screenY, int* indexX, int* worldY, int* indexZ) {

	GLint viewport[4];						// Where The Viewport Values Will Be Stored
	GLdouble projection[16];				// Where The 16 Doubles Of The Projection Matrix Are To Be Stored
	GLdouble modelview[16];					// Where The 16 Doubles Of The Modelview Matrix Are To Be Stored
	GLfloat windowX, windowY;

	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
	glGetDoublev(GL_PROJECTION_MATRIX, projection);

	windowX = screenX;
	windowY = viewport[3] - screenY;

	GLdouble pos1X, pos1Y, pos1Z;
	GLdouble pos2X, pos2Y, pos2Z;

	gluUnProject( windowX, windowY, 0.0, modelview, projection, viewport, &pos1X, &pos1Y, &pos1Z);
	gluUnProject( windowX, windowY, 1.0, modelview, projection, viewport, &pos2X, &pos2Y, &pos2Z);

	Vec3f directionVec(pos1X - pos2X, pos1Y - pos2Y, pos1Z - pos2Z);
	Vec3f directionOrigin(pos1X, pos1Y, pos1Z);

	float t = -directionOrigin.y / directionVec.y;

	float x = directionOrigin.x + (t * directionVec.x);
	float z = directionOrigin.z + (t * directionVec.z);

	float offset = this->gridWidth * this->cellSize / 2;

	x += offset;
	z += offset;

	x /= this->cellSize;
	z /= this->cellSize;

	(*indexX) = x;
	(*worldY) = 0;
	(*indexZ) = z;
}
예제 #8
0
void	transpose(void)
{
	GLdouble	model[16];
	GLdouble	proj[16];
	GLint		view[4];
	GLdouble	posz0[3];
	GLdouble	posz1[3];

	glGetDoublev(GL_MODELVIEW_MATRIX, model);
	glGetDoublev(GL_PROJECTION_MATRIX, proj);
	glGetIntegerv(GL_VIEWPORT, view);
	gluUnProject(0, 0, 0, model, proj, view,
				posz0 + 0, posz0 + 1, posz0 + 2);
	gluUnProject(0, 0, 1, model, proj, view, posz1 + 0, posz1 + 1, posz1 + 2);
	map_intersection(g_env->realpos00, posz0, posz1);
	gluUnProject(0, g_env->winy, 0, model, proj, view,
				posz0 + 0, posz0 + 1, posz0 + 2);
	gluUnProject(0, g_env->winy, 1, model, proj, view,
				posz1 + 0, posz1 + 1, posz1 + 2);
	map_intersection(g_env->realpos0y, posz0, posz1);
	gluUnProject(g_env->winx, g_env->winy, 0, model, proj, view,
				posz0 + 0, posz0 + 1, posz0 + 2);
	gluUnProject(g_env->winx, g_env->winy, 1, model, proj, view,
				posz1 + 0, posz1 + 1, posz1 + 2);
	map_intersection(g_env->realposxy, posz0, posz1);
}
예제 #9
0
void G308_Geometry::isOnClick(GLfloat winX, GLfloat winY){
	GLint viewport[4];
	GLdouble modelview[16];
	GLdouble projection[16];
	GLfloat winZ = 0;

	//printf(" ( %f %f ) ", winX, winY);
	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
	glGetDoublev( GL_PROJECTION_MATRIX, projection );
	glGetIntegerv( GL_VIEWPORT, viewport );

	winX = (float)winX;
	winY = (float)viewport[3] - (float)winY;
	winZ = selectBuf[1]/4294967295.0;

	printf(" (( %f )) ", winZ);

	GLdouble worldX, worldY, worldZ;

	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
	//printf(" ( %f %f %f ) ", worldX, worldY, worldZ);

	//if (sel_vrt>)
	GLfloat distance, min_distance;
	// number of the closest vertex
	int cnt=1;
	// distance between the pressed point and the first vertex of selected triangle
	min_distance = (m_pVertexArray[m_pTriangles[sel_vrt].v1].x-worldX)*(m_pVertexArray[m_pTriangles[sel_vrt].v1].x-worldX) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v1].y-worldY)*(m_pVertexArray[m_pTriangles[sel_vrt].v1].y-worldY) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v1].z-worldZ)*(m_pVertexArray[m_pTriangles[sel_vrt].v1].z-worldZ);
	// distance to the second vertex
	distance = (m_pVertexArray[m_pTriangles[sel_vrt].v2].x-worldX)*(m_pVertexArray[m_pTriangles[sel_vrt].v2].x-worldX) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v2].y-worldY)*(m_pVertexArray[m_pTriangles[sel_vrt].v2].y-worldY) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v2].z-worldZ)*(m_pVertexArray[m_pTriangles[sel_vrt].v2].z-worldZ);
	if (distance>min_distance) {
		min_distance = distance;
		cnt=2;
	}
	// third vertex of a triangle
	distance = (m_pVertexArray[m_pTriangles[sel_vrt].v3].x-worldX)*(m_pVertexArray[m_pTriangles[sel_vrt].v3].x-worldX) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v3].y-worldY)*(m_pVertexArray[m_pTriangles[sel_vrt].v3].y-worldY) +
		(m_pVertexArray[m_pTriangles[sel_vrt].v3].z-worldZ)*(m_pVertexArray[m_pTriangles[sel_vrt].v3].z-worldZ);
	if (distance>min_distance) cnt=3;
	sel_cnt = cnt;

	int num_vrt;
	if (sel_cnt==1) num_vrt=m_pTriangles[sel_vrt].v1;
	else if (sel_cnt==2) num_vrt=m_pTriangles[sel_vrt].v2;
	else if (sel_cnt==3) num_vrt=m_pTriangles[sel_vrt].v3;
	printf(" {{ %d }} ", num_vrt);

}
예제 #10
0
static void unproject (double x2, double y2, double *x3, double *y3, double *z3)
{
  double mv[16];
  double pm[16];
  int    vp[4];

  glGetDoublev (GL_MODELVIEW_MATRIX,  mv);
  glGetDoublev (GL_PROJECTION_MATRIX, pm);
  glGetIntegerv (GL_VIEWPORT, vp);
  gluUnProject (x2, y2, 0.0,
                mv, pm, vp,
                x3, y3, z3);
}
예제 #11
0
void worldSpaceCoords(double& x, double& y, double z)
{
    GLdouble modelMatrix[16];
    GLdouble projMatrix[16];
    GLint viewport[4];
    
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
    
    GLdouble nx, ny, nz;
    gluUnProject(x, viewport[1]+viewport[3]-y, 0, modelMatrix, projMatrix, viewport, &nx, &ny, &nz);
    
    GLdouble fx, fy, fz;
    gluUnProject(x, viewport[1]+viewport[3]-y, 1, modelMatrix, projMatrix, viewport, &fx, &fy, &fz);
    
    if(nz == fz) return;
    
    GLfloat t = (nz - z) / (nz - fz);
    x = nx + (fx - nx) * t;
    y = ny + (fy - ny) * t;
}
예제 #12
0
float2 MouseToSpace2(int x, int y, int windowResY, float zoom, float2 camPos) //return float2(-scn.camPos.x+ (tmx/scn.zoom), -scn.camPos.y+ (-tmy/scn.zoom));
{
	GLdouble model[16], proj[16];
	GLint view[4];
	glGetDoublev(GL_MODELVIEW_MATRIX, model);
	glGetDoublev(GL_PROJECTION_MATRIX, proj);
	glGetIntegerv(GL_VIEWPORT, view);
	GLdouble tmx, tmy, tmz;

	gluUnProject(x, windowResY - y, 0, model, proj, view, &tmx, &tmy, &tmz);

	return float2(-camPos.x + (tmx/zoom), -camPos.y + (-tmy/zoom));
};
예제 #13
0
파일: ex7.c 프로젝트: JamesWP/UNI-Y1-2
void mouse (int x, int y) {
  /* Maps a mouse (x,y) back to world coordinates */
  GLdouble projection[16], modelview[16];
  GLint viewport[4];

  glLoadIdentity();
  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  glGetDoublev(GL_PROJECTION_MATRIX, projection);
  glGetIntegerv(GL_VIEWPORT, viewport);
  gluUnProject ((double)x, (double)500-y-1, (double)0.0, 
                modelview, projection, viewport, 
                &tx, &ty, &tz);
}
예제 #14
0
void
motion(int x, int y)
{
  GLdouble objx, objy, objz;

  gluUnProject(x, height - y, 0.95,
    modelMatrix, projMatrix, viewport,
    &objx, &objy, &objz);
  overlaySphere.x = objx;
  overlaySphere.y = objy;
  overlaySphere.z = objz;
  glutPostOverlayRedisplay();
}
예제 #15
0
파일: BoizStone.cpp 프로젝트: EwanMcA/Cards
float * getRayFromMouse(int x, int y)
{

	GLint viewport[4];
	GLdouble modelview[16];
	GLdouble projection[16];
	GLfloat winX, winY;
	GLdouble posXs, posYs, posZs;
	GLdouble posXe, posYe, posZe;

	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
	glGetDoublev(GL_PROJECTION_MATRIX, projection);
	glGetIntegerv(GL_VIEWPORT, viewport);

	winX = (float)x;
	winY = (float)viewport[3] - (float)y;
	//glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
	gluUnProject(winX, winY, 0.0f, modelview, projection, viewport, &posXs, &posYs, &posZs);
	gluUnProject(winX, winY, 1.0f, modelview, projection, viewport, &posXe, &posYe, &posZe);
	float coords[6] = { posXs, posYs, posZs, posXe, posYe, posZe };
	return coords;
}
예제 #16
0
	////////////////////////////////////////////////////////////////////////////
	///
	/// @fn bool Vue::convertirClotureAVirtuelle(int x, int y, const math::Plan3D& plan, glm::dvec3& point) const
	///
	/// Cette fonction permet de transformer un point (donné en coordonnées
	/// d'affichage) en coordonnées virtuelles étant donné un certain plan sur
	/// lequel doit se trouver le point.  Elle utilise les fonction d'OpenGL
	/// donc cette fonction s'applique peu importe la position de la caméra.
	///
	/// @param[in]      x     : La position @a X du point en coordonnée
	///                         d'affichage.
	/// @param[in]      y     : La position @a Y du point en coordonnée
	///                         d'affichage.
	/// @param[in]      plan  : Le plan sur lequel on veut trouver la position
	///                         correspondante en virtuel.
	/// @param[in, out] point : point transformé @a (x,y) le @a z est le même
	///                         que le plan
	///
	/// @return Faux s'il est impossible de convertir le point en virtuel
	///         puisque le plan est parallèle à la direction de la caméra, vrai
	///         autrement.
	///
	////////////////////////////////////////////////////////////////////////////
	bool Vue::convertirClotureAVirtuelle(int x, int y, const math::Plan3D& plan, glm::dvec3& point) const
	{
		//Initialisation de variables.
		const GLdouble MinZ{ 0.0 };
		const GLdouble MaxZ{ 1.0 };

		// Lire la matrice de modélisation et de visualisation.
		glm::dmat4 matriceModelisation;
		glGetDoublev(GL_MODELVIEW_MATRIX, glm::value_ptr(matriceModelisation));

		// Lire la matrice de projection.
		glm::dmat4 matriceProjection;
		glGetDoublev(GL_PROJECTION_MATRIX, glm::value_ptr(matriceProjection));

		// Lire la clôture.
		glm::ivec4 cloture;
		glGetIntegerv(GL_VIEWPORT, glm::value_ptr(cloture));

		// Premier point.
		glm::dvec3 point1;
		gluUnProject(
			x, cloture[3] - y, MinZ,
			glm::value_ptr(matriceModelisation), glm::value_ptr(matriceProjection), glm::value_ptr(cloture),
			&point1[0], &point1[1], &point1[2]
			);

		// Deuxième point.
		glm::dvec3 point2;
		gluUnProject(
			x, cloture[3] - y, MaxZ,
			glm::value_ptr(matriceModelisation), glm::value_ptr(matriceProjection), glm::value_ptr(cloture),
			&point2[0], &point2[1], &point2[2]
			);

		// On construit la droite.
		math::Droite3D DroiteCoupe{ point1, point2 };

		return DroiteCoupe.intersection(plan, point);
	}
예제 #17
0
void 
CClipVolume::_Create()
{
	glPushAttrib(GL_CURRENT_BIT);

						// specify the clip planes
	for(int cp = 0; cp < iNrOfClipPlanes; cp++)
		glClipPlane(GL_CLIP_PLANE0 + cp, ppdClipPlanes[cp]);

	static double pdModelview[16];
	static double pdProjection[16];
	static int piViewport[4];

	glGetDoublev(GL_MODELVIEW_MATRIX, pdModelview);
	glGetDoublev(GL_PROJECTION_MATRIX, pdProjection);
	glGetIntegerv(GL_VIEWPORT, piViewport);

	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fid);
	glClear(GL_COLOR_BUFFER_BIT);

	glUseProgramObjectARB(pidClipVolume);

	SET_1F_VALUE_BY_NAME(pidClipVolume, "fWindowWidth", (float)piViewport[2]);
	SET_1F_VALUE_BY_NAME(pidClipVolume, "fWindowHeight", (float)piViewport[3]);
	SET_1I_VALUE_BY_NAME(pidClipVolume, "iNrOfClipPlanes", iNrOfClipPlanes);

	double dX, dY, dZ;
	double dW = (double)piViewport[2];
	double dH = (double)piViewport[3];
	glBegin(GL_QUADS);
		gluUnProject(0.0, 0.0, 0.5, pdModelview, pdProjection, piViewport, &dX, &dY, &dZ);	glVertex3d(dX, dY, dZ);
		gluUnProject(dW,  0.0, 0.5, pdModelview, pdProjection, piViewport, &dX, &dY, &dZ);	glVertex3d(dX, dY, dZ);
		gluUnProject(dW,  dH,  0.5, pdModelview, pdProjection, piViewport, &dX, &dY, &dZ);	glVertex3d(dX, dY, dZ);
		gluUnProject(0.0, dH,  0.5, pdModelview, pdProjection, piViewport, &dX, &dY, &dZ);	glVertex3d(dX, dY, dZ);
	glEnd();

//	glUseProgramObjectARB(0);
	glPopAttrib();	// glPushAttrib(GL_CURRENT_BIT);
}
예제 #18
0
void recoverClick(int iX, int iY, double &oX, double &oY){
  // http://www.3dbuzz.com/forum/threads/191296-OpenGL-gluUnProject-ScreenCoords-to-WorldCoords-problem
  GLdouble posX1, posY1, posZ1, posX2, posY2, posZ2, modelview[16], projection[16];
  GLint viewport[4];

  // Get matrices
  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  glGetDoublev(GL_PROJECTION_MATRIX, projection);
  glGetIntegerv(GL_VIEWPORT, viewport);
 
  // Create ray
  gluUnProject(iX, viewport[1] + viewport[3] - iY, 0, modelview, projection, viewport, &posX1, &posY1, &posZ1);  // Near plane
  gluUnProject(iX, viewport[1] + viewport[3] - iY, 1, modelview, projection, viewport, &posX2, &posY2, &posZ2);  // Far plane

  // This is a little bit hacky. The top of the discs is .5 above the board. 
  //This offset keeps the mouse in the plane of the top of the disc.
  GLfloat t = (posZ1 - z_distance - 0.5) / (posZ1 - posZ2);

  // so here are the desired (x, y) coordinates
  oX = (posX1 + (posX2 - posX1) * t) / scale;
  oY = (posY1 + (posY2 - posY1) * t) / scale;
}
예제 #19
0
void GetMouseRay(sVector3 &pos1, sVector3  &pos2, sVector3 &dir) {
	int myX, myY;
	glfwGetMousePos(&myX,&myY);
	
	gCamera.Render();
	
	double mvmatrix[16];
	double projmatrix[16];
	int viewport[4];
	double dx,dy,dz;
	
	
	glGetIntegerv(GL_VIEWPORT, viewport);
	glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
	
	double mouseY = viewport[3] - myY;
	
	gluUnProject((double)myX, (double)mouseY, 0.0, mvmatrix, projmatrix, viewport, &dx, &dy, &dz);
	pos1 = sVector3((float)dx,(float)dy,(float)dz);
	
	gluUnProject((double)myX, (double)mouseY, 1.0, mvmatrix, projmatrix, viewport, &dx, &dy, &dz);
	pos2  = sVector3((float)dx,(float)dy,(float)dz);
	
	float x,y,z, mag;
	
	x = pos2.x - pos1.x;
	y = pos2.y - pos1.y;
	z = pos2.z - pos1.z;
	
	mag = sqrt(x*x + y*y + z * z);
	
	x/= mag;
	y/= mag;
	z/= mag;
	dir = sVector3(x,y,z);
	
	
}
예제 #20
0
파일: OpenGL.cpp 프로젝트: mjs513/cs4620-1
Point unproject(const Point &point, const GLMatrix &modelview, const GLMatrix &projection)
{
	GLint viewport[4];
	
	glGetIntegerv(GL_VIEWPORT,viewport);
	
	Point unproj;
	
	// Invert y when unprojecting
	gluUnProject(point.x,viewport[3] - point.y,point.z,modelview.v,projection.v,viewport,&unproj.x,&unproj.y,&unproj.z);
	
	return unproj;
}
예제 #21
0
void PickableObject::mousePress(tgt::MouseEvent* mouseEve, tgt::Camera*  sceneCamera){
    isClicked_ = true;

    pressedButton_ = mouseEve->button();

    oldScreenPosition_ = tgt::ivec2(mouseEve->x(), mouseEve->viewport().y - mouseEve->y());

    newScreenPosition_ = tgt::ivec2(mouseEve->x(), mouseEve->viewport().y - mouseEve->y());

    //GLint deltaX, deltaY;

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLdouble posX, posY, posZ;

    //deltaX = newScreenPosition_.x - oldScreenPosition_.x;
    //deltaY = newScreenPosition_.y - oldScreenPosition_.y;

    tgt::mat4 projection_tgt = sceneCamera->getProjectionMatrix(mouseEve->viewport());
    tgt::mat4 modelview_tgt = sceneCamera->getViewMatrix();

    for (int i = 0; i < 4; ++i) {
        modelview[i+0]   = modelview_tgt[i].x;
        modelview[i+4]   = modelview_tgt[i].y;
        modelview[i+8]   = modelview_tgt[i].z;
        modelview[i+12]  = modelview_tgt[i].w;
        projection[i+0]  = projection_tgt[i].x;
        projection[i+4]  = projection_tgt[i].y;
        projection[i+8]  = projection_tgt[i].z;
        projection[i+12] = projection_tgt[i].w;
    }

    viewport[0] = 0;
    viewport[1] = 0;
    viewport[2] = static_cast<GLint>(renderTargetDimensions_.x);
    viewport[3] = static_cast<GLint>(renderTargetDimensions_.y);

    GLfloat WindowPosZ;

    glReadPixels(oldScreenPosition_.x, oldScreenPosition_.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &WindowPosZ);

    LGL_ERROR;
    gluUnProject(oldScreenPosition_.x, oldScreenPosition_.y, WindowPosZ, modelview, projection, viewport, &posX, &posY, &posZ);
    LGL_ERROR;

    oldWorldPosition_ = tgt::vec3(static_cast<float>(posX), static_cast<float>(posY), static_cast<float>(posZ));
    newWorldPosition_ = tgt::vec3(static_cast<float>(posX), static_cast<float>(posY), static_cast<float>(posZ));

    mouseEve->accept();
}
예제 #22
0
Vector3<> SimObjectRenderer::projectClick(int x, int y)
{
  GLdouble mvmatrix[16];
  GLdouble projmatrix[16];
  for(int i = 0; i < 16; ++i)
  {
    mvmatrix[i] = (GLdouble) cameraTransformation[i];
    projmatrix[i] = (GLdouble) projection[i];
  }

  GLdouble tx, ty, tz;
  gluUnProject((GLdouble)(x),(GLdouble)(height - y), 1.0, mvmatrix, projmatrix, viewport, &tx, &ty, &tz);
  return Vector3<>(float(tx), float(ty), float(tz));
}
예제 #23
0
void ViewUnProject(int xi, int yi, float depth, point * v)
{
	GLint viewPort[4];
	GLdouble modelMatrix[16];
	GLdouble projMatrix[16];
	glGetIntegerv(GL_VIEWPORT, viewPort);
	glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
	yi = viewPort[3] - yi - 1;
	GLdouble wx, wy, wz;
	gluUnProject((GLdouble) xi, (GLdouble) yi, (GLdouble) depth,
                 modelMatrix, projMatrix, viewPort, &wx, &wy, &wz);
    pMAKE(wx, wy, wz, *v);
}
예제 #24
0
파일: input.cpp 프로젝트: mtokarski/Fizyka
void MouseRayTestData::CalculateRay(const Camera &cam)
{
	double matModelView[16], matProjection[16];
	int viewport[4];

	glGetDoublev( GL_MODELVIEW_MATRIX, matModelView );
	glGetDoublev( GL_PROJECTION_MATRIX, matProjection );
	glGetIntegerv( GL_VIEWPORT, viewport );

	double winX = (double)cam.m_mouseX;
	double winY = viewport[3] - (double)cam.m_mouseY;

	gluUnProject(winX, winY, 0.0, matModelView, matProjection, viewport, &m_start.x, &m_start.y, &m_start.z);
	gluUnProject(winX, winY, 1.0, matModelView, matProjection, viewport, &m_end.x, &m_end.y, &m_end.z);

	Vec3d v2 = m_end - m_start;
	Vec3d pt2 = m_start + v2*m_lastT;

	m_point = pt2;

	m_dir = m_end - m_start;
	m_dir.Normalize();
}
예제 #25
0
void PickableObject::mouseMove(tgt::MouseEvent* mouseEve, tgt::Camera* sceneCamera){
    newScreenPosition_ = tgt::ivec2(mouseEve->x(), mouseEve->viewport().y - mouseEve->y());

    GLint deltaX, deltaY;

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLdouble winX, winY, winZ;
    GLdouble posX, posY, posZ;

    deltaX = newScreenPosition_.x - oldScreenPosition_.x;
    deltaY = newScreenPosition_.y - oldScreenPosition_.y;

    tgt::mat4 projection_tgt = sceneCamera->getProjectionMatrix(mouseEve->viewport());
    tgt::mat4 modelview_tgt = sceneCamera->getViewMatrix();

    for (int i = 0; i < 4; ++i) {
        modelview[i+0]   = modelview_tgt[i].x;
        modelview[i+4]   = modelview_tgt[i].y;
        modelview[i+8]   = modelview_tgt[i].z;
        modelview[i+12]  = modelview_tgt[i].w;
        projection[i+0]  = projection_tgt[i].x;
        projection[i+4]  = projection_tgt[i].y;
        projection[i+8]  = projection_tgt[i].z;
        projection[i+12] = projection_tgt[i].w;
    }

    viewport[0] = 0;
    viewport[1] = 0;
    viewport[2] = static_cast<GLint>(renderTargetDimensions_.x);
    viewport[3] = static_cast<GLint>(renderTargetDimensions_.y);

    posX = oldWorldPosition_.x;
    posY = oldWorldPosition_.y;
    posZ = oldWorldPosition_.z;

    LGL_ERROR;
    gluProject(posX, posY, posZ, modelview, projection, viewport, &winX, &winY, &winZ);

    winX = winX + deltaX;
    winY = winY + deltaY;

    LGL_ERROR;
    gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    LGL_ERROR;

    newWorldPosition_ = tgt::vec3(static_cast<float>(posX), static_cast<float>(posY), static_cast<float>(posZ));
    mouseEve->accept();
}
예제 #26
0
bool
HdxIntersector::Result::_ResolveHit(int index, int x, int y, float z,
                                    HdxIntersector::Hit* hit) const
{
    unsigned char const* primIds = _primIds.get();
    unsigned char const* instanceIds = _instanceIds.get();
    unsigned char const* elementIds = _elementIds.get();

    GfVec3d hitPoint(0,0,0);
    gluUnProject(x, y, z,
                 _params.viewMatrix.GetArray(),
                 _params.projectionMatrix.GetArray(),
                 &_viewport[0],
                 &((hitPoint)[0]),
                 &((hitPoint)[1]),
                 &((hitPoint)[2]));

    int idIndex = index*4;

    int primId = HdxRenderSetupTask::DecodeIDRenderColor(&primIds[idIndex]);
    hit->objectId = _index->GetRprimPathFromPrimId(primId);

    if (!hit->IsValid()) {
        return false;
    }

    int instanceIndex = HdxRenderSetupTask::DecodeIDRenderColor(
            &instanceIds[idIndex]);
    int elementIndex = HdxRenderSetupTask::DecodeIDRenderColor(
            &elementIds[idIndex]);

    bool rprimValid = _index->GetSceneDelegateAndInstancerIds(hit->objectId,
                                                           &(hit->delegateId),
                                                           &(hit->instancerId));

    if (!TF_VERIFY(rprimValid, "%s\n", hit->objectId.GetText())) {
        return false;
    }

    hit->worldSpaceHitPoint = GfVec3f(hitPoint);
    hit->ndcDepth = float(z);
    hit->instanceIndex = instanceIndex;
    hit->elementIndex = elementIndex; 

    if (TfDebug::IsEnabled(HDX_INTERSECT)) {
        std::cout << *hit << std::endl;
    }

    return true;
}
예제 #27
0
void MyTorus::worldCoord(int x, int y, int z, Vector3d &v) {
    #if RETINA_DISPLAY
        x *= 2;
        y *= 2;
    #endif
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT,viewport);
    std::cout << "x: "<< x << " - y: " << y << " - z: " << z<< std::endl;
    std::cout << viewport[0] << " " << viewport[1] << " " << viewport[2] << " " << viewport[3] << std::endl;
	GLdouble M[16], P[16];
	glGetDoublev(GL_PROJECTION_MATRIX,P);
	glGetDoublev(GL_MODELVIEW_MATRIX,M);
	gluUnProject(x,viewport[3]-1-y,z,M,P,viewport,&v[0],&v[1],&v[2]);
}
예제 #28
0
void
motion(int x, int y)
{
  GLdouble objx, objy, objz;

  if (selectedPoint != ~0) {
    gluUnProject(x, winHeight - y, 0.95,
      modelMatrix, projMatrix, viewport,
      &objx, &objy, &objz);
    grid[selectedPoint * 3 + 0] = objx;
    grid[selectedPoint * 3 + 1] = objy;
    glutPostRedisplay();
  }
}
예제 #29
0
파일: nRenderer.cpp 프로젝트: gan74/nGine
nVec3 nRenderer::pick(nVec2 screenPos) {
	double x = screenPos.x;
	double y = screenPos.y;
	GLint viewport[4];
	GLfloat winX, winY, winZ;
	GLdouble posX, posY, posZ;
	glGetIntegerv(GL_VIEWPORT, viewport);
	winX = x;
	winY = y;//(double)viewport[3] - y;
	glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
	//cout<<"depth = "<<winZ<<endl;
	gluUnProject(winX, winY, winZ, (/*objectMatrix * */viewMatrix).matrix, projectionMatrix.matrix, viewport, &posX, &posY, &posZ);
	return nVec3(posX, posY, posZ);
}
예제 #30
0
void mouse(int button, int state, int x, int y) {
    switch (button) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) {
            realy = matrizViewport[3] - (GLint) y - 1;
            gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
                          matrizModelview, matrizProjecao, matrizViewport,
                          &wx0, &wy0, &wz0);
            gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
                          matrizModelview, matrizProjecao, matrizViewport,
                          &wx1, &wy1, &wz1);
            vx=wx1-wx0;
            vy=wy1-wy0;
            vz=wz1-wz0;
            proximidade();
            glutPostRedisplay();
        }
        else {
            mudaCurva=0;
        }
        break;
    }
}