setMouseCallback and OpenGL

asked 2015-05-14 08:39:17 -0600

LBerger gravatar image

updated 2015-05-14 08:55:35 -0600

Hi,

I want to use a callback function for mouse inside an openGL windows. My program looks like :

static void onMouse(int event, int x, int y, int, void*)
{
    if (event==EVENT_LBUTTONDOWN)
    {
        prevX=x;
        prevY=y;
    }
    if (event == EVENT_LBUTTONUP)
    {    
        prevX=-1;
        prevY=-1;
    }
    if (prevX!=-1)
    {
        obsX += x - prevX;
        obsY += y - prevY;
    }
    cout << event << "\t" << x << "\t" << y << "\t" << prevX << "\t" << prevY << "\n";


}

void DrawOpenGLMSER(Mat img, Mat result)
{
    Mat imgGray;
    if (img.type() != CV_8UC1)
        cvtColor(img, imgGray, COLOR_BGR2GRAY);
    else
        imgGray = img;
    namedWindow("OpenGL", WINDOW_OPENGL);
    //resizeWindow("OpenGL", win_width, win_height);
    setMouseCallback("OpenGL", onMouse, NULL);.......

About Opengl everything works fine but mouse inside OpenGL windows does not work. I can see event but mouse position is always 0,0. (when I run opencv/samples/cpp/ffilldemo.cpp I can see mouse coordinates)

Help would be appreciate thanks.

PS in file window_w32.cpp at line 1617

        window->on_mouse( event, pt.x*size.cx/MAX(rect.right - rect.left,1),
                                 pt.y*size.cy/MAX(rect.bottom - rect.top,1), flags,
                                 window->on_mouse_param );

Size is always 0,0

edit retag flag offensive close merge delete

Comments

it's either bad luck, - or you must have a special talent for running into weird bugs ;)

berak gravatar imageberak ( 2015-05-14 09:32:40 -0600 )edit

So if it's a bug I solve my problem :

        if (window->flags &cv::WINDOW_OPENGL==0)
        {
            icvGetBitmapData( window, &size, 0, 0 );

            window->on_mouse( event, pt.x*size.cx/MAX(rect.right - rect.left,1),
                                     pt.y*size.cy/MAX(rect.bottom - rect.top,1), flags,
                                     window->on_mouse_param );
        }
        else
            window->on_mouse(event, pt.x,pt.y, flags,window->on_mouse_param);

But I don't know if it is a good idea! (Bug #4338) It is easyer to find bug than write code

LBerger gravatar imageLBerger ( 2015-05-14 09:51:17 -0600 )edit

it is partially fixed

namedWindow(window_name, WINDOW_AUTOSIZE || WINDOW_OPENGL ); //works without problem.
namedWindow(window_name, WINDOW_OPENGL ); //Mouse call back gives x and y coordinates wrong if window resized
sturkmen gravatar imagesturkmen ( 2015-08-21 02:51:58 -0600 )edit