PDA

View Full Version : mouse event problem



m0j1_2010
05-21-2010, 09:21 AM
hi everyone . this is my first post here . I have wrote a code to respond to mouse click . it's supposed to some points where the mouse's been clicked . but when I click the mouse , instead of drawing points , it blacks my screen and I don't have any idea why?
can anyone help me with this?
tnx
-------------------------------------------------

#include <windows.h> // use as needed for your system
#include <gl/Gl.h>
#include <gl/glut.h>

int flag=0;
int rx,ry;
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0.0, 640.0, 0.0, 480.0);
// glViewport(-320.0, 320.0, -240.0, 240.0);
// gluOrtho2D(-160.0, 160.0, -120.0, 120.0);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{

if (flag==1)
{
glBegin(GL_POINTS);
glVertex2i(rx, ry); // draw three points
glEnd();
}
glFlush(); // send all output to display */
}

void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
flag=1;
rx=x;
ry=480-y;
}
glutPostRedisplay();
}

/*void myMouseMotion(int x, int y)
{
flag=1;
rx=x;
ry=480-y;
glutPostRedisplay();
}*/

/*void myPassiveMotion(int x, int y)
{
flag=1;
rx=x;
ry=480-y;
glutPostRedisplay();
}
*/
/*void myKeyboard(unsigned char ch, int x, int y)
{

switch (ch)
{
case 'e':
exit (-1);
case 'a':
flag=0;
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
break;
}

glutPostRedisplay();
}*/

/*void mySpecialKeyboard(int ch, int x, int y)
{

switch (ch)
{
case GLUT_KEY_LEFT:
flag=0;
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
break;
}

glutPostRedisplay();
}*/
//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&amp;argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("my first attempt"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMouseFunc(myMouse);
//glutKeyboardFunc(myKeyboard);
//glutSpecialFunc (mySpecialKeyboard);
//glutMotionFunc (myMouseMotion);
// glutPassiveMotionFunc(myPassiveMotion);
myInit();
glutMainLoop(); // go into a perpetual loop
}

RobertEd
05-21-2010, 12:22 PM
For starters, you don't need all those re-displays or gl.h.
Your problem is you might in ortho mode, you need projection mode.
Check out the lighthouse tutorial, partner! Go snowman.

m0j1_2010
05-21-2010, 01:18 PM
thanks , I solved that problem(by rewriting it, I don't know why it got fixed!) . the code is as below . it draws a point where the mouse is clicked . but when I click again it erases the previous point and draws a new point . can anyone tell me what can I do to keep the previously drawn points .
tnx
here's the code:
-----------------------------------------------------
#include <windows.h> // use as needed for your system
#include <gl/Gl.h>
#include <gl/glut.h>
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
int rx,ry;
int flag=0;
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
if(flag==1){
glBegin(GL_POINTS);
glVertex2i(rx, ry);
glEnd();
}
glFlush(); // send all output to display
}

void myMouse(int button,int state,int x,int y){
if((button==GLUT_LEFT_BUTTON)&amp;&amp;(state==GLUT_DOWN)) {
flag=1;
rx=x;
ry=480-y;
}
glutPostRedisplay();
}


//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&amp;argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("my first attempt"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMouseFunc(myMouse);
myInit();
glutMainLoop(); // go into a perpetual loop
}

mhagain
05-21-2010, 01:29 PM
Each frame exists in isolation and does not carry over anything from the previous frame unless you store it out yourself.

In this case your call to glClear is causing everything from the previous frame to be wiped, so you'll only ever get a single point drawn. You could remove the glClear call, but I don't think that's what you want to do as it will cause you trouble if you ever add any more geometry to the scene. It will also potentially cause trouble on some cards/drivers depending on their page flipping policy.

What you want is to store all clicked points in a big array and re-render them each frame. Use something like:

glBegin (GL_POINTS);

for (int i = 0; i < numpoints; i++)
glVertex2iv (clickedpoints[i]);

glEnd ();

This is of course going to impose a max on the number of points you can store, but depending on what it is you're trying to achieve (it sounds like some kind of drawing program) the max needn't be that big a problem.

zerothrillz
05-21-2010, 01:32 PM
store created points in some structure, then when you redraw the the scene you'd have to iterate through all the points to draw them.

typedef point2d float[2];
vector<point2d> mypoints;
point2d temp;
on_mouse_click:
temp[0] = mouse_x;
temp[1] = mouse_y;
mypoints.push_back(temp);