Problems With Very Basic Right-Click Popup Menu

  • Follow


Apologies beforehand for being a newb!

I am writing a program that draws points on the screen wherever the
user clicks.  There's also a right-click menu that changes the point
size and color.  Unfortunately I've run into a couple of issues.

First, in the AddMenu() function, I have to add a third call to
glutCreateMenu().  If I remove this line, the three color options get
added to the "main" right-click menu rather than the 'Point Color'
submenu.

Second, right after the program is loaded, the first time a menu
option is used, not only does the option not take effect, but the
entire screen is cleared.  Thereafter the menu options work fine,
mysteriously.

Does anyone know why I am seeing these problems?

Thanks!

#include <GL/glut.h>

GLsizei winWidth = 400, winHeight = 300; // Initial display-window
size.

void Initialize(void)
{
	glClearColor(0.0, 0.0, 1.0, 1.0); // Set display-window color to
blue.
	glMatrixMode(GL_PROJECTION);
	gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}

void DisplayFcn(void)
{
	glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
	glColor3f(1.0, 0.0, 0.0); // Set point color to red.
	glPointSize(3.0); // Set point size to 3.0.
}

void WinReshapeFcn(GLint newWidth, GLint newHeight)
{
	/* Reset viewport and projection parameters */
	glViewport(0, 0, newWidth, newHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));

	/* Reset display-window size parameters. */
	winWidth = newWidth;
	winHeight = newHeight;
}

void PlotPoint(GLint x, GLint y)
{
	glBegin(GL_POINTS);
	glVertex2i(x, y);
	glEnd();
}

void MousePtPlot(GLint button, GLint action, GLint xMouse, GLint
yMouse)
{
	if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
		PlotPoint(xMouse, winHeight - yMouse);

	glFlush();
}

void KeyPress(unsigned char key, int x, int y)
{
	if (key == 27)
	exit(0);
}

void SelectSize(int iCommand)
{

	switch(iCommand)
	{
		case 1:
			glPointSize(3.0);
			break;
		case 2:
			glPointSize(6.0);
			break;
		case 3:
			glPointSize(12.0);
			break;
		default:
			break;
	}

}

void SelectColor(int iCommand)
{

	switch(iCommand)
	{
		case 1:

			// Red
			glColor3f(1.0, 0.0, 0.0);
			break;
		case 2:

			// Green
			glColor3f(0.0, 1.0, 0.0);
			break;
		case 3:

			// White
			glColor3f(1.0, 1.0, 1.0);
			break;
		default:
			break;
	}

}

void AddMenu()
{
	int iSizeMenu = glutCreateMenu(SelectSize);

	glutAddMenuEntry("Small", 1);
	glutAddMenuEntry("Medium", 2);
	glutAddMenuEntry("Large", 3);

	int iColorMenu = glutCreateMenu(SelectColor);

	glutAddMenuEntry("Red", 1);
	glutAddMenuEntry("Green", 2);
	glutAddMenuEntry("White", 3);

	// Why is this needed?
	glutCreateMenu(SelectSize);

	glutAddSubMenu("Point Size", iSizeMenu);
	glutAddSubMenu("Point Color", iColorMenu);

	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(winWidth, winHeight);
	glutCreateWindow("Mouse Plot Points");
	Initialize();
	glutDisplayFunc(DisplayFcn);
	glutReshapeFunc(WinReshapeFcn);
	glutMouseFunc(MousePtPlot);
	glutKeyboardFunc(KeyPress);

	AddMenu();

	glutMainLoop();
}

0
Reply s_valmont (8) 2/7/2007 4:36:13 AM

On Feb 7, 5:36 am, s_valm...@telus.net wrote:
> Apologies beforehand for being a newb!
>
> I am writing a program that draws points on the screen wherever the
> user clicks.  There's also a right-click menu that changes the point
> size and color.  Unfortunately I've run into a couple of issues.
>
> First, in the AddMenu() function, I have to add a third call to
> glutCreateMenu().  If I remove this line, the three color options get
> added to the "main" right-click menu rather than the 'Point Color'
> submenu.

  In fact if you comment only that line (glutCreateMenu()) and leave
the others,
program will crash whenever you click the right-button. When you
create menu
in GLUT (using glutCreateMenu())  it become the current, means that
every call
of glutAddMenuEntry () or glutAddSubMenu () will add items/submenus to
it. That's
why your color items have been added to main menu, because it was the
last created.
And the call of glutAddSubMenu () just added extra submenu item (with
the point size).
I would suggest you to read the GLUT documentation ;)

>
> Second, right after the program is loaded, the first time a menu
> option is used, not only does the option not take effect, but the
> entire screen is cleared.  Thereafter the menu options work fine,
> mysteriously.

   well, here you did something very strange :) I'm surprised how did
you even
have seen other point colors or sizes except the red of size 3. The
GLUT calls
function DisplayFcn () each time it needs to refresh or repaint the
context (window).
In your case you continuously set the color and size to their initial
values.  Here
is the modified code:

#include <GL/glut.h>
#include <iostream>

GLsizei winWidth = 400, winHeight = 300; // Initial display-window
size.
bool draw = false;
GLint xgMouse, ygMouse;


void Initialize(void)
{
	glClearColor(0.0, 0.0, 1.0, 1.0); // Set display-window color to
blue.
	glColor3f (1.0f, 0.0f, 0.0f);
	glPointSize (3.0);
}


void WinReshapeFcn(GLint newWidth, GLint newHeight)
{
	/* Reset viewport and projection parameters */
	glViewport(0, 0, newWidth, newHeight);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));

	/* Reset display-window size parameters. */
	winWidth = newWidth;
	winHeight = newHeight;
}

void PlotPoint(GLint x, GLint y)
{
	glBegin(GL_POINTS);
	glVertex2i(x, y);
	glEnd();

}

void DisplayFcn(void)
{
	glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
	if (draw)
		PlotPoint(xgMouse, winHeight - ygMouse);

	glFlush ();
}

void MousePtPlot(GLint button, GLint action, GLint xMouse, GLint
yMouse)
{
	xgMouse = xMouse;
	ygMouse = yMouse;

	if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
		draw = true;
	else if (button == GLUT_LEFT_BUTTON && action == GLUT_UP)
		draw = false;

	glutPostRedisplay ();
}

void KeyPress(unsigned char key, int x, int y)
{
	if (key == 27)
		exit(0);

}

void SelectSize(int iCommand)
{

	switch(iCommand)
	{
	case 1:
		glPointSize(3.0);
		break;
	case 2:
		glPointSize(6.0);
		break;
	case 3:
		glPointSize(12.0);
		break;
	default:
		break;
	}
}

void SelectColor(int iCommand)
{

	switch(iCommand)
	{
	case 1:

		// Red
		glColor3f(1.0, 0.0, 0.0);
		break;
	case 2:

		// Green
		glColor3f(0.0, 1.0, 0.0);
		break;
	case 3:

		// White
		glColor3f(1.0, 1.0, 1.0);
		break;
	default:
		break;
	}

}

void AddMenu()
{
	int iSizeMenu = glutCreateMenu(SelectSize);

	glutAddMenuEntry("Small", 1);
	glutAddMenuEntry("Medium", 2);
	glutAddMenuEntry("Large", 3);

	int iColorMenu = glutCreateMenu(SelectColor);

	glutAddMenuEntry("Red", 1);
	glutAddMenuEntry("Green", 2);
	glutAddMenuEntry("White", 3);

	// Why is this needed?
	glutCreateMenu(SelectSize);

	glutAddSubMenu("Point Size", iSizeMenu);
	glutAddSubMenu("Point Color", iColorMenu);

	glutAttachMenu(GLUT_RIGHT_BUTTON);

}

void main(int argc, char** argv)
{
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(winWidth, winHeight);
	glutCreateWindow("Mouse Plot Points");
	Initialize();

	glutDisplayFunc(DisplayFcn);
	glutReshapeFunc(WinReshapeFcn);
	glutMouseFunc(MousePtPlot);
	glutKeyboardFunc(KeyPress);

	AddMenu();

	glutMainLoop();
}

regards,

Idd

0
Reply ivan 2/14/2007 8:30:17 PM


1 Replies
495 Views

(page loaded in 0.089 seconds)

Similiar Articles:













7/18/2012 10:57:19 PM


Reply: