Skip to main content
gamedev.net
🔒 Locked

2D mouse mapping

Started by dnaxx Apr 12, 2006 at 1:21 PM 2 replies 450+ views
Original Post
dnaxx
dnaxx
Hello! I am using GLUT (and it's mouse callback functions) and now I want to map the coordinates from mouse clicks to world coordinates. This must only be done in 2D (the clicks are used for 2D controlls like checkboxes and buttons). How can I do this mapping? thank you,
dnaxx
dnaxx
	printf("Mouse x: %i, y: %i\n",x,y);	GLint viewport[4];	GLdouble mvmatrix[16], projmatrix[16];	GLint realy;	GLdouble wx,wy,wz;		glGetIntegerv(GL_VIEWPORT,viewport);	glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);	glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);	realy = viewport[3] - (GLint) y - 1;	gluUnProject((GLdouble)x, (GLdouble) realy, 0.0, mvmatrix,projmatrix,viewport,&wx,&wy,&wz);	printf("World x: %f, y: %f\n",wx,wy);


I used this code from the Red Book and it return reasonable results for 3D scenes. But for my 2D elements hte world x,y values are "strange" (x ~= 0-1, y ~= 4-5).

Here is also the code after which 2D elements are drawn:
			glMatrixMode(GL_PROJECTION);			glPushMatrix();							glLoadIdentity();						gluOrtho2D (0, 800, 0, 600);			//gluOrtho2D (0, 800, 0, 600);			glMatrixMode(GL_MODELVIEW);				// Draw 2D elements here
dnaxx
dnaxx
Just another question: Are the OpenGL picking functions ("glSelectBuffer()"...) "good"?
Fahrenheit451
Fahrenheit451
I answered the question of mouse coordinate conversion quite recently in this thread. See if that helps any. You have the general idea though. The oddity might be the orientation of your viewport not being taken into consideration in the conversion calcs - (0,0) being lower left or top left.

F451

[Edited by - Fahrenheit451 on April 12, 2006 4:46:46 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.