controlling mouse co-ordinate OPENGL

Hi, I am trying to make this program in open gl using GLUT.
What I want to do is colour my circles from a pop up menu.
Options are in the menu and when the user clicks it should change.

there are 40 circles.
SO i am having trouble getting the co-ordinates where the mouse right clicks.


Also, could you also tell me how to make options and when the user clicks it go to a certain part. Like when user says PLay Game. Program should go to the game page.


Any help would be greatly appreciated.

thanks!
glutMouseFunc sets the mouse callback for the current window.
void glutMouseFunc( void(*func)(int button, int state, int x, int y) );
¿What problem are you having?
Problem, finding logic :p
I have these functions:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//------------------------------------------------------------------------------------------------------------------
void handlemouse(int button, int state, int x, int y)
{
     
}
//------------------------------------------------------------------------------------------------------------------

void createPopupMenus()
{     
       int mainMenu = glutCreateMenu(processMainMenu);       
       glutAddMenuEntry("Blue",1);
       glutAddMenuEntry("Green",2);
       glutAddMenuEntry("Light Blue",3);
       glutAddMenuEntry("Yellow",4);
       glutAddMenuEntry("Red",5);
       glutAddMenuEntry("White",6);
       glutAttachMenu(GLUT_RIGHT_BUTTON);     
         
}
//-----------------------------------------------------------------------------------------------------------

void processMainMenu(int option) 
{
     if (option ==1)
     {
                                
     }
     else if (option==2)
     {
     }
     else if (option==3)
     {
     }
     else if (option==4)
     {
     }
     else if (option==5)
     {
     }
     else if (option==6)
     {
     }

}

I have declared glutMouseFunc(handlemouse); in main.
Dont know where to go now.
COuld you please tell me what a callback is? I know what it is but so confused
I can change the colour of all but need to change colour of the individual
It seems that when you attach a menu to you mouse button, you loose that callback.
It really doesn't make sense.

You could change a little the interface in order to 'make it work'. By instance requiring the user to select the circle.
Then the pop-up menu will be applied to that circle, without taking into account where the menu was invoqued.

That behaviour is weird. I mean, it seems that the purpose of that function is to provide a contextual menu. ¿but how can you have a contextual menu if you don't know the context?
Here is what I have about mouse in main:

1
2
glutMouseFunc(mouseButton); 
 glutPassiveMotionFunc(handlemouse);



here is my handlemouse function :

1
2
3
4
5
void handlemouse( int x, int y)
{     
     mouse_x = x;
     mouse_y = y;      
}



here is my mousebutton function:

1
2
3
4
5
6
7
void mouseButton(int button, int state, int x, int y) 
{
      if (button == GLUT_RIGHT_BUTTON)
    {
         handlemouse(x,y);       
    }
}


this is where I am calling it :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void createPopupMenus()
{     
       int mainMenu = glutCreateMenu(processMainMenu);       
       glutAddMenuEntry("Blue",1);
       glutAddMenuEntry("Green",2);
       glutAddMenuEntry("Light Blue",3);
       glutAddMenuEntry("Yellow",4);
       glutAddMenuEntry("Red",5);
       glutAddMenuEntry("White",6);
       float y = 3.8;
        for (int m = 0; m < 10; m++)
     {         
         for ( float x = -6.5; x <= -3.5; x++)
         { 
           mouseButton(GLUT_RIGHT_BUTTON, GLUT_DOWN, x, y);
         }
         y = y - 0.7;
     } 
       glutAttachMenu(GLUT_RIGHT_BUTTON);     
         
}



this is my popup menu function if you need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void processMainMenu(int option) 
{
         
     if (option ==1)
     {red = 0;  green = 0; blue = 255;  // red green blue are float variables                               
     }
     else if (option==2)
     {red = 0;  green = 100; blue = 0; 
     }
     else if (option==3)
     {red = 0;  green = 255; blue = 255; 
     }
     else if (option==4)
     {red = 265;  green = 140; blue = 0; 
     }
     else if (option==5)
     {red = 1;  green = 0; blue = 0; 
     }
     else if (option==6)
     {red = 1;  green = 1; blue = 1; 
     }
}
Last edited on
ok Now i will try getting the user to select the circle. And then will call the menu from that function and see where it goes.
which function lets the user select something?
Last edited on
In general you shouldn't be calling at the callbacks yourself. They will be triggered by the associated event.

You will need to make you own select function. By instance, using the mouse callback you get the coordinates of the clicked point.
Then you search for the object that 'owns' that point. (there is an interesting trick with an auxiliar buffer and colormaps)
could you please give me the algorithm for this?
Then you search for the object that 'owns' that point. (there is an interesting trick with an auxiliar buffer and colormaps)
I think what you want is "Picking".
Search "picking in opengl" in google.
yes thanks!!
but does picking work for circle? There is no predefined function so I had to us a algorithm to make them.
Picking is for every geometry, but for circle, you may not find a source code on internet. (I didn't search that).
I think, it's better to understand fundametals of picking and implement your own function for that purpose.
ok I am trying. And now I need to attach my popupmenu to the selected object. I think I did something wrong. could you look at it. below are my functions.
1) Mouse function:
1
2
3
4
5
6
7
8
void mouseButton(int button, int state, int x, int y) 
{
    if 	((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
     {
                handlemouse(x, y, button);
     }
    
}

2) Second Mouse Function
1
2
3
4
5
6
7
void handlemouse( int x, int y, int button)
{
       
        printf("Mouse button %d pressed at %d %d\n", button, x, y);
        gl_select(x,y);       
         
}

3) Select Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void gl_select(int x, int y)
{
    GLuint buff[64] = {0};
    GLint hits, view[4];
    int id;
    glSelectBuffer(64, buff);
    glGetIntegerv(GL_VIEWPORT, view);
    glRenderMode(GL_SELECT);
    glInitNames();
    glPushName(1);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
   	gluPickMatrix(x, y, 1.0, 1.0, view);
	gluPerspective(45,1,1,100);
	glMatrixMode(GL_MODELVIEW);
	glutSwapBuffers();
	background();
    createPopupMenus();	
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	hits = glRenderMode(GL_RENDER);			
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4) Background(My Rendering)
void background()
{
  glLoadIdentity();  
  draw_background();
  draw_hline();
  draw_vline();
  draw_colorcircles(-6.5, -3.2, 0, 0 , 255);
  draw_colorcircles(-5.5, -3.2, 0, 100 , 0);
  draw_colorcircles(-4.5, -3.2, 0, 255 , 255);
  draw_colorcircles(-3.5, -3.2, 265, 140 , 0);
  draw_colorcircles(-2.5, -3.2, 1, 0 , 0);
  draw_colorcircles(-1.5, -3.2, 1, 1 , 1);
  glutPrint(4, 7, GLUT_BITMAP_TIMES_ROMAN_24,  "MASTERMIND", 1.0, 1.0, 1.0,1.0);
  glutPrint(1, 6.5, GLUT_BITMAP_HELVETICA_18,  "MASTERMIND is a game of thinking and guessing. Guess the computer`s code in 10 tries and beat the game", 1.0, 1.0, 1.0,1.0);
  glutPrint(1, 6.2, GLUT_BITMAP_HELVETICA_18,  " and beat the game. You can also enjoy the multiplayer!! Its a bonus feauture!!", 1.0, 1.0, 1.0,1.0);   
  
  glutSwapBuffers();
} 

6) Pop up menus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void createPopupMenus()
{     
       int mainMenu = glutCreateMenu(processMainMenu);       
       glutAddMenuEntry("Blue",1);
       glutAddMenuEntry("Green",2);
       glutAddMenuEntry("Light Blue",3);
       glutAddMenuEntry("Yellow",4);
       glutAddMenuEntry("Red",5);
       glutAddMenuEntry("White",6);
       glutAttachMenu(GLUT_RIGHT_BUTTON);     
         
}
//-----------------------------------------------------------------------------------------------------------

void processMainMenu(int option) 
{         
     if (option ==1)
     {red = 0;  green = 0; blue = 255;                                                                
     }
     else if (option==2)
     {red = 0;  green = 100; blue = 0; 
     }
     else if (option==3)
     {red = 0;  green = 255; blue = 255; 
     }
     else if (option==4)
     {red = 265;  green = 140; blue = 0; 
     }
     else if (option==5)
     {red = 1;  green = 0; blue = 0; 
     }
     else if (option==6)
     {red = 1;  green = 1; blue = 1; 
     }
}


Did I do anything wrong here? And could you tell me how to attach the pop up menu to the selected object? I have 40 circles. I want the user to select each and change the color using pop up menu.
and o Also:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
float y = 3.8;
    int name=1;
    for (int m = 0; m < 10; m++)
     {                      
         for ( float x = -6.5; x <= -3.5; x++)
         {
             
             glPushName(name);
             name++;                                                            
             drawcircle (x,y);                  
             
         }         
         y = y - 0.7;
         
     }                    
}

is my pushname right? Because I highly doubt it
Last edited on
Topic archived. No new replies allowed.