I dont know what goes wrong in this code...
Help me here
I simply want to save the initial and final positions after dragging the mouse...

   #include"iostream"
    #include<glut.h>

    class point1
    {
    public:
        point1()
        {
            x=0;
            y=0;
        }
        int x;
        int y;
    }p[2];

    int flag=0;

    void processMouse(int button, int state, int x, int y);
    void actmot(int x, int y);

    int main()
    {
        //glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
        glutMouseFunc(processMouse);
        glutMotionFunc(actmot);
        glutMainLoop();
        return 0;
    }


    void processMouse(int button, int state, int x, int y)
    {
        if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
        {
            if(flag!=1)
                {
                    flag=1;
                    p[0].x=x;
                    p[0].y=y;
                }
        }
        else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
        {
            if (flag==1)
            {
                p[1].x=x;
                p[1].y=y;
                flag=0;
            }
        }
    }

    void actmot(int x,int y)
    {
    }
Re: Mouse events in opengl 80 80

You are checing whether the mouse is moving or not? Thus you need to do something like (PSuedo code):

int X = 0, Y = 0;
int X2 = 0, Y2 = 0;

bool MouseDrag = false;


if (MOUSE_LEFTButton == DOWN)
{   
    GetMousePos(&X, &Y);
    MouseDrag = true;
}


if ((MOUSE_LEFTButton == UP) && MouseDrag)
{
    MouseDrag = false;
    GetMousePos(&X2, &Y2);
}

It's just PSuedo code since I'm not familiar with GLUT. If you need a WinAPI solution, I can show you that and maybe you can do that in glut. But it should be done like the above.

Re: Mouse events in opengl 80 80

Mouse events in opengl

Should've been "mouse events in GLUT". OpenGL does not deal with Window management.
Anyway, coming back to your question, the logic seems to be fine. What exactly is the problem?

Re: Mouse events in opengl 80 80

It was a silly mistake... I had not created the window for GL... hahaha :) Thanks btw.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.