Jump to content
  • Advertisement
Sign in to follow this  
reisrocks

Zooming to arbitrary point

This topic is 3699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to zoom to where the mouse pointer is, I can zoom to an arbitrary point, but I just can't seem to get this to work with the mouse pointer:
void Interface::DrawGLScene()
{
	glClearColor(0.4,0.4,0.4,1.0);
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	
	glTranslatef(m_offset, m_winSizeY/2.0, 0);
	glScalef(m_xScale, m_winSizeY/2.0, 1);

       // drawing stuff goes here

	glutSwapBuffers();
}

void Interface::processButtonEvent(int button, int state, int x, int y)
{
	if (state == GLUT_DOWN)
	{
		if (button == GLUT_LEFT_BUTTON)
		{
			gl_select(x,m_winSizeY-y);
		}
	}
	if (state == GLUT_UP)
	{
		if (button == GLUT_WHEEL_UP || button == GLUT_WHEEL_DOWN)
		{
			float dir = button == GLUT_WHEEL_UP ? 1 : -1;
			float oldScaleX = m_xScale;
			m_xScale += SCALE_INCREMENT*dir;
			float newX = 100;
			m_offset += (newX * oldScaleX) - (newX * m_xScale);
		}
	}
	DrawGLScene();
}

This code will zoom no problem towards pixel location 100 on the x axis, however I'm trying to get this working with the mouse. All help appreciated.

Share this post


Link to post
Share on other sites
Advertisement

if (state == GLUT_UP)
{
if (button == GLUT_WHEEL_UP || button == GLUT_WHEEL_DOWN)
{
float dir = button == GLUT_WHEEL_UP ? 1 : -1;
float oldScaleX = m_xScale;
m_xScale += SCALE_INCREMENT*dir;
float newX = (x-m_offset)/oldScaleX;
m_offset += (newX * oldScaleX) - (newX * m_xScale);
}
}



OK got it, through good old algebra..


float newX = (x-m_offset)/oldScaleX;



This line basically undoes what was done by the translating and scaling done in the draw.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!