Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actual___

Posted 23 April 2013 - 03:17 PM



t3dpoint __fastcall TModelGroupHandler::GetPositionFromSpaceDepth(int X, int Y, HWND hwnd)
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, int( DISPLAY_WIDTH), int(DISPLAY_HEIGHT));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspectiveA(90.0, DISPLAY_WIDTH/DISPLAY_HEIGHT,0.10f, 1000.0f * 1000.0f * 1000.0f);
  glMatrixMode(GL_MODELVIEW);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glPushMatrix();
glLoadIdentity();

gluLookAt(base->eyepos.x,base->eyepos.y,base->eyepos.z,
base->eyepointingpos.x,base->eyepointingpos.y,base->eyepointingpos.z,base->upvector.x,
base->upvector.y,base->upvector.z);



base->DrawDepthBillboard(last_depth); //default 100 meters away


TPoint mouse;
GetCursorPos(&mouse);
ScreenToClient(hwnd, &mouse);
t3dpoint depth_result;
depth_result = base->Reproduce_Mouse_coordinates(mouse.x,mouse.y);


glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glPopMatrix();
return depth_result;
}

t3dpoint __fastcall ModelGroupEditor::Reproduce_Mouse_coordinates(int x, int y)
{
t3dpoint res;
glGetDoublev( GL_PROJECTION_MATRIX, mprojection );
glGetDoublev( GL_MODELVIEW_MATRIX, mmodelview );
glGetIntegerv( GL_VIEWPORT, mviewport );

				  int wy = mviewport[3] -y;
	  double nx,ny,nz;      //  double fx,fy,fz;
  double	winX = double(x);
   double	winY = double(mviewport[3]) - double(y);
double winZ;
			  // glReadBuffer(GL_DEPTH_BUFFER_BIT);
float * pdata = new float;
float pk;



	glReadPixels(x,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,pdata);
	pk = (*pdata);
delete pdata;	
winZ = double(pk);


gluUnProject(winX, winY, winZ, mmodelview, mprojection, mviewport, &nx, &ny, &nz);

res.x = float(nx);
res.y = float(ny);
res.z = float(nz);

 return res;
}


 

if your code looks like this and you still have no results check your drawing code / or you just have float / double problem anyway this code works in C++


#1___

Posted 23 April 2013 - 03:13 PM

t3dpoint __fastcall TModelGroupHandler::GetPositionFromSpaceDepth(int X, int Y, HWND hwnd)
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, int( DISPLAY_WIDTH), int(DISPLAY_HEIGHT));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspectiveA(90.0, DISPLAY_WIDTH/DISPLAY_HEIGHT,0.10f, 1000.0f * 1000.0f * 1000.0f);
  glMatrixMode(GL_MODELVIEW);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glPushMatrix();
glLoadIdentity();

gluLookAt(base->eyepos.x,base->eyepos.y,base->eyepos.z,
base->eyepointingpos.x,base->eyepointingpos.y,base->eyepointingpos.z,base->upvector.x,
base->upvector.y,base->upvector.z);



base->DrawDepthBillboard(last_depth); //default 100 meters away


TPoint mouse;
GetCursorPos(&mouse);
ScreenToClient(hwnd, &mouse);
t3dpoint depth_result;
depth_result = base->Reproduce_Mouse_coordinates(mouse.x,mouse.y);


glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glPopMatrix();
return depth_result;
}

t3dpoint __fastcall ModelGroupEditor::Reproduce_Mouse_coordinates(int x, int y)
{
t3dpoint res;
glGetDoublev( GL_PROJECTION_MATRIX, mprojection );
glGetDoublev( GL_MODELVIEW_MATRIX, mmodelview );
glGetIntegerv( GL_VIEWPORT, mviewport );

				  int wy = mviewport[3] -y;
	  double nx,ny,nz;      //  double fx,fy,fz;
  double	winX = double(x);
   double	winY = double(mviewport[3]) - double(y);
double winZ;
			  // glReadBuffer(GL_DEPTH_BUFFER_BIT);
float * pdata = new float;
float pk;



	glReadPixels(x,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,pdata);
	pk = (*pdata);
delete pdata;	
winZ = double(pk);


gluUnProject(winX, winY, winZ, mmodelview, mprojection, mviewport, &nx, &ny, &nz);

res.x = float(nx);
res.y = float(ny);
res.z = float(nz);

 return res;
}


PARTNERS