Show and Move mouse pointer

Hello

How do you move the mouse. I have a camera looking straight down from the top and only zoom in/out is supported (2D game).

This is how I setup the environment:


	// Set viewport
	glViewport( 0, 0, width, height );

	// Clear color and depht buffers
	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// Set up projection matrix
	glMatrixMode( GL_PROJECTION );    // Select projection matrix
	glLoadIdentity();                 // Start with an identity matrix
	gluPerspective(                   // Set perspective view
		64.0,						  // 65.0 Field of view = 65 degrees
		(double)width/(double)height, // Window aspect (assumes square pixels)
		1.0,                          // Near Z clipping plane
		400.0                         // Far Z clippling plane
	);

	// Set up modelview matrix
	glMatrixMode( GL_MODELVIEW );     // Select modelview matrix
	glLoadIdentity();                 // Start with an identity matrix
	gluLookAt(                        // Set camera position and orientation
		0.0, 0.0, 10.0,               // Camera position (x,y,z)
		0.0, 0.0, 0.0,                // View point (x,y,z)
		0.0, 1.0, 0.0                 // Up-vector (x,y,z)
	);

Now every time I draw, I also call a function to draw the mouse pointer. This function looks like this:


void DrawMouse(
	)
{
	int x, y;

	glfwGetMousePos(&x, &y);

	// Now draw the quad
	glBegin( GL_LINES );
	
	glColor3f( 1.0f, 0.431f, 0.647f );
	glVertex3f( x/1680.0, y/-1050.0, 0.0f );

	glColor3f( 1.0f, 0.431f, 0.647f );
	glVertex3f( (x/1680.0)+0.5f, (y/-1050.0)+0.5f, 0.0f );

	glEnd();
}

Now, my question is:

  1. Is this the right way to introduce mouse support? (If not how)
  2. If this is the way to go, how do I solve the zoom problem. The further away I am zoomed out, the slower the mouse moves. What operations do I have to do to adjust for the zoom? (I am not that great with matrices)

Tnx

-vance

There is no best way to manage viewing in a 3D scene, it depends on your needs. Some would use gluLookAt, some would not.

The further away I am zoomed out, the slower the mouse moves

If you compute the viewing vector as: target - camera_position, your problem is normal. The closer you are from your target the smallest the viewing vector will be and its length will tend to zero. The solution is to move the target with the camera to keep a constant zoom speed.

If you compute the viewing vector as: target - camera_position, your problem is normal. The closer you are from your target the smallest the viewing vector will be and its length will tend to zero. The solution is to move the target with the camera to keep a constant zoom speed.

How exactly do you do this? The target is at 0,0,0 and the camera is at 0,0,200. The zoom does glTranslatef with variable Z before glBegin

Tnx!

If your camera is at (0,0,0) and the target at (0,0,200), the view vector is (0,0,1) after normalization.

Now, to keep a constant zoom speed you translate the camera position and target position vectors by the view vector (0,0,1) using glTranslate function or your equivalent custom one.

So if you translate by (0,0,1) exactly, you have (0,0,1) and (0,0,201) as new camera and target positions. Your view direction is still the same and you never reach the target position.