PDA

View Full Version : clicking a square mesh and printing the value of the square



iris_raj
09-21-2004, 02:08 AM
Dear All,
I have one more doubt,

When I click the mouse on a square(say 3rd square on the 5th row), in the mesh it should print
the print as row = 5 and colum = 3)

I used glutMouseFunc(mouse) and wrote the following code for mouse call back. But it is not printing the exact row column value.




#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

void make_mesh()
{
float x, y;
glBegin(GL_LINES);
for (x = -5; x <= 5; x += 1.0f) // x += 1.0f Stands For 1 Meter Of Space In This Example
{
glVertex3f(x, 5.0, 0);
glVertex3f(x, -5.0, 0);
}
// Draw The Horizontal Lines
for (y = -5; y <= 5; y += 1.0f) // y += 1.0f Stands For 1 Meter Of Space In This Example
{
glVertex3f( 5.0, y, 0);
glVertex3f( -5.0, y, 0);
}
glEnd();
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity();
make_mesh();
glFlush();
}


void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
}

void mouse(int button, int state, int x, int y)
{
int mx = x * 10/500;
int my = y * 10/500;
printf("Value of x = %d\n", x);
printf("Value of y = %d\n", y);

}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

-NiCo-
09-21-2004, 02:44 AM
I think the problem is that you're using integer values for your calculations.

in

if x*10/500 is evaluated as x*(10/500) then 10/500 in integer calculus is 0.

Try this:


void mouse(int button, int state, int x, int y)

{

int mx = int(x*10.0/500.0);

int my = int(y*10.0/500.0);

printf("Value of x = %d\n", x);

printf("Value of y = %d\n", y);


}P.S. I believe in glut y=0 is the top of the screen and not the bottom like in OpenGL.

Nico

09-21-2004, 08:08 PM
Yeah I changed, But it is still not working

09-21-2004, 09:29 PM
hey iris_raj, try this:


void mouse(int button, int state, int x, int y)
{
int mx = (x * 10)/screenWidth;
int my = (y * 10)/screenHeight;

// you had a typo here, mx not x, my not y!
printf("Value of x = %d\n", mx);
printf("Value of y = %d\n", my);
}