Program is blocked while holding mouse button

I’ve recently started studying OpenGL, I’m trying to make a camera fly around, I managed to get it to work however when I tried to move the camera around only while holding a mouse button it blocked the whole program as soon as I hold the button then back to running when I release, it wouldn’t access the mouseMotion function while the button is held down for some reason.

I’ve looked everywhere, most tutorials show almost the exact same code as mine yet it doesn’t work for me for some reason, any help is greatly appreciate!

This is my code if it helps:

void mouse(int button, int state, int x, int y) {
	mouseState = state;
	mouseButton = button;

void mouseMotion(int x, int y) {
	cameraXRotation += x - mouseX;
	cameraYRotation += y - mouseY;

	mouseX = x;
	mouseY = y;

void draw()
	glTranslatef(camx, camy, camz);

	glRotatef(cameraYRotation * cameraRotationSpeed, 1, 0, 0);
	glRotatef(cameraXRotation * cameraRotationSpeed, 0, 1, 0);

int main(int argc, char *argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(500, 500);

	return 0;

Just my guess (since I don’t have complete code of your draw function), by try calling glutPostRedisplay() at the end of your mouseMotion function.

Thanks for your reply, I’ve tried that already but it didn’t solve my problem

You need to use glutMotionFunc() to register a callback which is invoked when the mouse is moved with at least one button pressed. The callback registered with glutPassiveMotionFunc() is only invoked when the mouse is moved without any buttons pressed (hence the “passive” in the name).

How did I not pay attention to that haha, guess that’s what happens when you copy-paste code :joy:
Thank you so much you saved my day!!!