PDA

View Full Version : Menu Trouble



Kris2011
01-16-2011, 11:18 AM
Hey, im having trouble with a menu I created for my app. I have created the menu and it displays the options I want, yet I cant get them to do anything.

Im trying to get it so the options in the menu change the background colour.

I cant spot anything wrong with my code and neither can visual studio. Can anyone help me?



#include <stdio.h> // for using "printf"
#include <glut.h>

//////////////////////////////////////////////////////
void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
glMatrixMode(GL_MODELVIEW);
}

void init(void)
{
printf("\n----- init() Started \t");
glClearColor(0,0,0,1); // Background color (RGB)- Black
glColor3f(1.0,1.0,1.0); // Drawing color (RGB) - White
glLineWidth(3); // Width of the drawing line
glShadeModel (GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, .1, 100);
printf("--- init() Done ---- \n");

}

////////////////////////////////////////////////////////////////

void mykey(unsigned char key, int x, int y)
{

printf(" > Keyboard pressed\n");
switch(key) {
// Drawing color
case 'r': glColor3f(1.0,0.0,0.0);
break;
case 'g': glColor3f(0.0,1.0,0.0);
break;
case 'b': glColor3f(0.0,0.0,1.0);
break;
case 'q': exit(1);
break;
}
glutPostRedisplay(); // Forces the redrawing {call display()}
}

void HandleSelectedMenuItem (int id)
{
switch (id)
{
case '1': glClearColor(1.0,0.0,0.0,0.0); //Red
break;
case '2': glClearColor(0.0,1.0,0.0,0.0); //Green
break;
case '3': glClearColor(0.0,0.0,1.0,0.0); //Blue
break;
case '4': exit(1); //Exit
break;
}
// Almost any menu selection requires a redraw
glutPostRedisplay();
}

//////////////////////////////////////////////////////
void display()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2,2,3,0,0,0,0,1,0);
printf("\n--- display() started ---- \t");
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINE_LOOP);
glVertex3f( -0.5, -0.5, -0.5);
glVertex3f( -0.5, -0.5, 0.5);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f( 0.5, -0.5, -0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( -0.5, 0.5, -0.5);
glVertex3f( -0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);
glVertex3f( 0.5, 0.5, -0.5);
glEnd();
glBegin(GL_LINES);
glVertex3f( -0.5, -0.5, -0.5);
glVertex3f( -0.5, 0.5, -0.5);
glEnd();
glBegin(GL_LINES);
glVertex3f( -0.5, -0.5, 0.5);
glVertex3f( -0.5, 0.5, 0.5);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.5, -0.5, 0.5);
glVertex3f( 0.5, 0.5, 0.5);
glEnd();
glBegin(GL_LINES);
glVertex3f( 0.5, -0.5, -0.5);
glVertex3f( 0.5, 0.5, -0.5);
glEnd();

glFlush(); // Finish the drawing
printf("--- display() Done ---- \n");
}

//////////////////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv); // GLUT Initialization
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); // Initializing the Display mode
glutInitWindowPosition(100,100);
glutInitWindowSize(300,300); // Define the window size
glutCreateWindow("Hello OpenGL"); // Create the window, with caption.
printf("\n========== Window Created ========= \n");
init(); // All OpenGL initialization

int menu;
menu = glutCreateMenu(HandleSelectedMenuItem); //create & define handler
glutAddMenuEntry ("Red", 1); //Menu items
glutAddMenuEntry ("Green", 2);
glutAddMenuEntry ("Blue", 3);
glutAddMenuEntry ("Exit", 4);
glutAttachMenu (GLUT_RIGHT_BUTTON); // Attach menu to Right-Button of the mouse

//-- Callback functions ---------------------
glutDisplayFunc(display);
glutKeyboardFunc(mykey);

glutMainLoop(); // Loop waiting for event
}

Alfonse Reinheart
01-16-2011, 11:28 AM
switch (id)
{
case '1': glClearColor(1.0,0.0,0.0,0.0); //Red
break;
case '2': glClearColor(0.0,1.0,0.0,0.0); //Green
break;
case '3': glClearColor(0.0,0.0,1.0,0.0); //Blue
break;
case '4': exit(1); //Exit
break;
}


The ids you gave are numbers. The id's you're comparing to are characters. The number 1 is not the same as the ASCII character '1'.

ugluk
01-16-2011, 01:11 PM
If you want GUI + GL, a better choice would be Qt, WxWidgets, ... or even Java. Much easier for GUI.