Sign in to follow this  
z42

Unable to get continuous mouse movement updates.

Recommended Posts

Hi, this is my code:
// header file for the GLUT library.
#include <GL/glut.h>
// header file for the Glu library.
#include <GL/glu.h>
// header file for the OpenGL library.
#include <GL/gl.h>

#include <pthread.h>
#include <iostream>
#include <cstdlib>

#include "theMachine.h"

int main(int argc, char ** argv)
{
	// initialize GLUT state - glut will take any command line arguments that 
	//	pertain to it or X Windows - this can be found in the documentation: 
	//	http://reality.sgi.com/mjk/spec3/spec3.html
	glutInit( & argc, argv);

	// select type of display mode.
	//	* Double buffer.
	//	* RGBA color.
	//	* Alpha components supported.
	//	* Depth buffer.
	// At the moment, these are all of the possible values that we'll ever 
	//	need and use. These values are enough for just about any situation.
	//glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

	// get a 640 x 480 window.
	glutInitWindowSize(1200, 600);  

	// the window starts at the upper left corner of the screen.
	glutInitWindowPosition(0, 0);

	// create a top-level window. This window returns a unique identifier for 
	//	your window. Also, if applicable, the string that it takes as an input 
	//	parameter becomes the title of the window. Nothing is drawn on the 
	//	window until the glutMainLoop method is called, so don't draw/render 
	//	on it until then. The window that is created is based on the 
	//	parameters that were set prior to the call of this method.
	window = glutCreateWindow("The Machine");

	// register the function that will do all of our OpenGL drawing whenever 
	//	the contents of the window require to be redrawn.
	glutDisplayFunc(DrawGLScene);

	// go fullscreen.
	//glutFullScreen();

	// even if there are no events, re-draw our gl scene. In this case, it is 
	//	uncommented since it would eat up too much valuable CPU.
	//glutIdleFunc(DrawGLScene);

	// register the function called when our window is resized.
	glutReshapeFunc(ReSizeGLScene);

	// register the function called when the keyboard is pressed. This 
	//	function takes the input in the form of a char and 2 ints. The char is 
	//	an ASCII character that value represents the pressed key.
	glutKeyboardFunc(keyPressed);

	// register the function called when special keys (arrows, page down, 
	//	etc.) are pressed.
	glutSpecialFunc(specialKeyPressed);

	// register the function called when there is input from the mouse.
	glutMouseFunc(mouse);

	// initialize our window. This function is a custom function that we will 
	//	use to do a few things to our window before enterring the message loop.
	InitGL(1200, 600);
  
	// start the messaging loop.
	glutMainLoop();

	return 0;
}

void InitGL(GLsizei Width, GLsizei Height)
{
	// this will clear the background color to black. This method can be used 
	//	at any one point in order to clear the screen to a particular color. 
	//	The different parameters are (red, green, blue, alpha.)
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	// enables the clearing of the depth buffer. The 1.0 means that one should 
	//	clear the entire buffer. It's clamped to that degree.
	glClearDepth(1.0);
	// the type of depth test to do. The different depth function calls (or 
	//	function calls that relate to depth) are saying that in order for the 
	//	object to be displayed, its z value must be less than that of the 
	//	viewpoint, which means that the viewer can see the object.
	glDepthFunc(GL_LESS);
	// enables depth testing. This will enable the comparisons of depth and 
	//	updates to the depth buffer.
	glEnable(GL_DEPTH_TEST);

	// enables smooth color shading. This paints all of the objects in shades 
	//	of different colors, as opposed to a single color cover all of the 
	//	objects (which is what GL_FLAT would do.)
	glShadeModel(GL_SMOOTH);

	// the current matrix that will be used will be the projection matrix. All 
	//	subsequent calls made will affect the projection matrix. In this case, 
	//	the project matrix will be modified. What this means is that the 
	//	camera through which the user is looking is changed, but not the 
	//	actual objects.
	glMatrixMode(GL_PROJECTION);
	// reset the projection matrix. This initializes the projection matrix so 
	//	that only the specified projection transformation has an effect.
	glLoadIdentity();

	// calculate the aspect ratio of the window. All that this function does 
	//	is simply set the camera through which we are viewing the drawn 
	//	picture.
	gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);

	// the current matrix that will be used will be the projection matrix. All 
	//	subsequent calls made will affect the projection matrix. In this case, 
	//	the model matrix will be modified. What this means is that the camera 
	//	through which the user is looking is unchanged, but the actual objects 
	//	are altered. If this is not set to GL_MODELVIEW at the end of this 
	//	function call, then nothing will be drawn on the screen (OpenGL will 
	//	still think that we are still dealing with the projection matrix and 
	//	not the model matrix.)
	glMatrixMode(GL_MODELVIEW);
}

void ReSizeGLScene(int Width, int Height)
{
	// prevent a divide by zero if the window is too small.
	if(Height == 0)
	{
		Height = 1;
	}

	// reset the current viewport and perspective transformation.
	glViewport(0, 0, Width, Height);

	// the current matrix that will be used will be the projection matrix. All 
	//	subsequent calls made will affect the projection matrix. In this case, 
	//	the project matrix will be modified. What this means is that the 
	//	camera through which the user is looking is changed, but not the 
	//	actual objects.
	glMatrixMode(GL_PROJECTION);
	// reset the projection matrix. This initializes the projection matrix so 
	//	that only the specified projection transformation has an effect.
	glLoadIdentity();

	// calculate the aspect ratio of the window. All that this function does 
	//	is simply set the camera through which we are viewing the drawn 
	//	picture.
	gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);

	// the current matrix that will be used will be the projection matrix. All 
	//	subsequent calls made will affect the projection matrix. In this case, 
	//	the model matrix will be modified. What this means is that the camera 
	//	through which the user is looking is unchanged, but the actual objects 
	//	are altered. If this is not set to GL_MODELVIEW at the end of this 
	//	function call, then nothing will be drawn on the screen (OpenGL will 
	//	still think that we are still dealing with the projection matrix and 
	//	not the model matrix.)
	glMatrixMode(GL_MODELVIEW);
}

void DrawGLScene()
{
	// clear the screen and the depth buffer. This will set the screen to the 
	//	color of what we painted the screen to be in the InitGL method 
	//	(glClearColor(0.0f, 0.0f, 0.0f, 0.0f);) Without executing this method, 
	//	one will have to draw over the previous picture (which is not cleared) 
	//	and will get a picture over picture result.
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// reset the view. This sets the currently modifiable matrix to the 4 x 4 
	//	identity matrix. This is the matrix representation of the screen that 
	//	will be drawn.
	glLoadIdentity();

	std::cout << "DrawGLScene method." << std::endl;

	// move into the screen 6.0 units.
	glTranslatef(0.0f, 0.0f, -6.0f);

	// draw a triangle on the screen.
	glBegin(GL_TRIANGLES);
		// set the color to red.
		glColor3f(1.0f, 0.0f, 0.0f);
		// set the point of drawing a point of a triangle.
		glVertex3f(0.0, 1., 0.0);
		// set the color to green.
		glColor3f(1.0f, 1.0f, 0.0f);
		// set the point of drawing a point of a triangle.
		glVertex3f(-0.5, 0.0, 0.0);
		// set the color to blue.
		glColor3f(0.0f, 0.0f, 1.0f);
		// set the point of drawing a point of a triangle.
		glVertex3f(0.5, 0.0, 0.0);
	glEnd();

	// since this is double buffered, swap the buffers to display what just 
	//	got drawn. The current buffer goes into the background so that new 
	//	stuff can be drawn into it again. If the buffers are not swapped in 
	//	this case, then that will create a program that uses only one buffer, 
	//	making the picture begins to flicker.
	glutSwapBuffers();
}

void keyPressed(unsigned char key, int x, int y)
{
	nap();

	switch(key)
	{
	// kill everything.
	case ESCAPE:
	case 'Q':
	case 'q':
		// shut down our window.
		glutDestroyWindow(window); 

		// exit the program.
		exit(1);

		break;

    default:
		std::cout << "Key " << key << " pressed. No action there yet." 
			<< std::endl;

		break;
	}
}

void specialKeyPressed(int key, int x, int y)
{
	nap();

	switch(key)
	{
	// process the up key.
	case GLUT_KEY_PAGE_UP:

		break;

	// process the down key.
	case GLUT_KEY_PAGE_DOWN:

		break;

    default:
		break;
	}	
}

void mouse(int button, int state, int x, int y)
{
	//nap();

	// location of the mouse as it's moving.
	std::cout << "X: " << x << " Y: " << y << std::endl;

	switch(button)
	{
	// process the left mouse button.
	case GLUT_LEFT_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Left mouse button down." << std::endl;
			// print out the co-ordinates of where the left mouse button was 
			//	clicked on the console.
			//std::cout << "X: " << x << " Y: " << y << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Left mouse button up." << std::endl;
		}

		break;

	// process the middle mouse button.
	case GLUT_MIDDLE_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Middle mouse button down." << std::endl;
			// print out the co-ordinates of where the middle mouse button was 
			//	clicked on the console.
			//std::cout << "X: " << x << " Y: " << y << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Middle mouse button up." << std::endl;
		}

		break;

	// process the right mouse button.
	case GLUT_RIGHT_BUTTON:
		if(GLUT_DOWN == state)
		{
			std::cout << "Right mouse button down." << std::endl;
			// print out the co-ordinates of where the right mouse button was 
			//	clicked on the console.
			//std::cout << "X: " << x << " Y: " << y << std::endl;
		}
		else if(GLUT_UP == state)
		{
			std::cout << "Right mouse button up." << std::endl;
		}

		break;

	default:
		break;
	}
}

void nap()
{
	// avoid thrashing this procedure. This will happen if you press and hold 
	//	the escape key down.
	const struct timespec ts = {0, 800};
	struct timespec remainder;

	nanosleep( & ts, & remainder);
}
My problem is that I would like the mouse function to print out to the console the co-ordinates of the mouse pointer whenever I move it. However, it doesn't do that and behaves exactly the opposite from what I would expect. Maybe I need to put the cout statement elsewhere? I'm not sure.

Share this post


Link to post
Share on other sites
To be precise, it doesn't print out the mouse movements to the console whenever I move it on the OpenGL screen, not on my desktop overall.

Share this post


Link to post
Share on other sites
Holy mountains-of-irrelevant-code, Batman! The only code we needed to see was this:
// register the function called when there is input from the mouse.
glutMouseFunc(mouse);

...

void mouse(int button, int state, int x, int y)
{
//nap();

// location of the mouse as it's moving.
std::cout << "X: " << x << " Y: " << y << std::endl;

...
}

Also, when you want to paste a huge amount of source code, use the [source] tag, not [code].


Now, the problem is that glutMouseFunc only generates callbacks for mouse button presses. You need glutMotionFunc and glutPassiveMotionFunc, which generate callbacks whenever the mouse moves while any button(s) is depressed or no buttons are depressed, respectively.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this