Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualluckyyyyyy

Posted 13 October 2012 - 07:13 AM

I want to get the mouse click position in 3D. Below is my code, its simple, I am not doing wrong in it but I don't know why I am always getting X and Y values are wrong. I think Maximum X = 0.072345664 and Y = 0.04124355 . something like this. where I am doing wrong.. ? Why I am not getting the exact values of x and y ? Z value is OK. Only issue with X and Y. any idea ? Thanks


void glPerspective()
{
	glViewport(0, 0, WINDOW_SIZE_W, WINDOW_SIZE_H);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (GLdouble)WINDOW_SIZE_W / (GLdouble)WINDOW_SIZE_H, 0.1, 100000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, m_zoom,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();  
	  glPerspective();	 // perspective view
	  Some_rotation_and_translation();
	  Render_Triangular_model();
	glPopMatrix();
	swapbuffer();
}
GLpoint GetOGLMousePos(GLint x, GLint y)
{
	GLdouble winX = 0.0, winY = 0.0, winZ = 0.0;
	GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
	GLint OGLMviewport[4];							  //stores viewport information
	GLdouble OGLMmodelview[16];
	GLdouble OGLMprojection[16];
	glGetDoublev( GL_PROJECTION_MATRIX, OGLMprojection );
	glGetDoublev( GL_MODELVIEW_MATRIX, OGLMmodelview );
	glGetIntegerv( GL_VIEWPORT, OGLMviewport );		 //transfers viewport info into viewport array.
	winX = (float)x;
	winY = (float)OGLMviewport[3] - (float)y;		   // invert winY so that down lowers value
	glReadPixels( x, GLint(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
	gluUnProject( winX, winY, winZ, OGLMmodelview, OGLMprojection, OGLMviewport, &posX, &posY, &posZ);
	return GLpoint(posX, posY, posZ);
}

Output 3D coordinates (top right section of the OpenGL display screen):

-0.000810418,  0.0011706,  99.9
0.00288149,  0.00126065,  99.9
0.00981506,  0.00144074,  99.9
0.0157581,  0.00171088,  99.9
0.0249429,  0.00171088,  99.9
0.0367389,  0.00180093,  99.9
0.0468241,  0.00180093,  99.9
0.0557387,  0.00225116,  99.9
0.065914,  0.00243125,  99.9
-0.000990511,  0.0105354,  99.9
-0.0013507,  0.017379,  99.9
-0.0013507,  0.0225116,  99.9
-0.00171088,  0.0268338,  99.9
-0.00171088,  0.0313362,  99.9
-0.00207107,  0.0380896,  99.9
0.0724874,  0.0404308,  99.9
0.0744684,  0.0411512,  99.9 // top right corner

Think of Design with Dimensions
http://real3d.pk


#1luckyyyyyy

Posted 07 October 2012 - 06:52 PM

I want to get the mouse click position in 3D. Below is my code, its simple, I am not doing wrong in it but I don't know why I am always getting X and Y values are wrong. I think Maximum X = 0.072345664 and Y = 0.04124355 . something like this. where I am doing wrong.. ? Why I am not getting the exact values of x and y ? Z value is OK. Only issue with X and Y. any idea ? Thanks


void glPerspective()
{
	glViewport(0, 0, WINDOW_SIZE_W, WINDOW_SIZE_H);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (GLdouble)WINDOW_SIZE_W / (GLdouble)WINDOW_SIZE_H, 0.1, 100000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, m_zoom,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();  
	  glPerspective();	 // perspective view
	  Some_rotation_and_translation();
	  Render_Triangular_model();
	glPopMatrix();
	swapbuffer();
}
GLpoint GetOGLMousePos(GLint x, GLint y)
{
	GLdouble winX = 0.0, winY = 0.0, winZ = 0.0;
	GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
	GLint OGLMviewport[4];							  //stores viewport information
	GLdouble OGLMmodelview[16];
	GLdouble OGLMprojection[16];
	glGetDoublev( GL_PROJECTION_MATRIX, OGLMprojection );
	glGetDoublev( GL_MODELVIEW_MATRIX, OGLMmodelview );
	glGetIntegerv( GL_VIEWPORT, OGLMviewport );		 //transfers viewport info into viewport array.
	winX = (float)x;
	winY = (float)OGLMviewport[3] - (float)y;		   // invert winY so that down lowers value
	glReadPixels( x, GLint(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
	gluUnProject( winX, winY, winZ, OGLMmodelview, OGLMprojection, OGLMviewport, &posX, &posY, &posZ);
	return GLpoint(posX, posY, posZ);
}

Output 3D coordinates (top right section of the OpenGL display screen):

-0.000810418,  0.0011706,  99.9
0.00288149,  0.00126065,  99.9
0.00981506,  0.00144074,  99.9
0.0157581,  0.00171088,  99.9
0.0249429,  0.00171088,  99.9
0.0367389,  0.00180093,  99.9
0.0468241,  0.00180093,  99.9
0.0557387,  0.00225116,  99.9
0.065914,  0.00243125,  99.9
-0.000990511,  0.0105354,  99.9
-0.0013507,  0.017379,  99.9
-0.0013507,  0.0225116,  99.9
-0.00171088,  0.0268338,  99.9
-0.00171088,  0.0313362,  99.9
-0.00207107,  0.0380896,  99.9
0.0724874,  0.0404308,  99.9
0.0744684,  0.0411512,  99.9 // top right corner

PARTNERS