使用GLFW和OpenGL在鼠标单击上画点?

这是我用来获取鼠标单击位置的功能:

Call BindComboBox(ComboBox1,"select stage from sample where stage like '%" + ComboBox1.Text + "%'","stage","stage")
Call BindComboBox(cboCompanies,"SELECT CompanyID,CompanyName,FROM Companies","CompanyID","CompanyName")

完整代码:

void Window::mouseButtonCallback(GLFWwindow * WindowObject,int button,int action,int mods)
{
 double xpos,ypos;

    if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
    {
        glfwgetcursorPos(WindowObject,&xpos,&ypos);
        cout << " XPOS : " << xpos << endl;
        cout << " YPOS : " << ypos << endl;

    }
    if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
    {
        cout << "Mouse Button Released" << endl;
    }
}
glfwSetMouseButtonCallback(this->WindowObject,mouseButtonCallback);
simplehzj 回答:使用GLFW和OpenGL在鼠标单击上画点?

使用glfwSetWindowUserPointer将指向Window对象(this)的指针与GLFWindow关联:

void Window::CreateWindow()
{
    this->WindowObject = glfwCreateWindow(this->screenWidth,this->screenHeight,this->WindowName,NULL,NULL );

    glfwSetWindowUserPointer( this->WindowObject,this ); // <----

    // [...]
}

使用glfwGetWindowUserPointer检索静态鼠标按钮回调中的关联指针,并将回调委托给Window方法:
(遗憾的是,您必须将类型void*的返回值转换为Window*,无法解决。)

class Window
{
    // [...]

    void mouseButtonCallback( int button,int action,int mods );
    static void mouseButtonCallback( GLFWwindow* WindowObject,int button,int mods );
};
void Window::mouseButtonCallback( GLFWwindow * WindowObject,int mods )
{
    Window *window = (Window*)glfwGetWindowUserPointer( WindowObject );
    window->mouseButtonCallback( button,action,mods );
}

void Window::mouseButtonCallback( int button,int mods )
{
    // [...]
}

在该方法中,可以访问窗口宽度(this->screenWidth)和高度(this->screenHeight)的属性,并计算与鼠标位置对应的归一化设备坐标。现在可以更改顶点坐标(vertex):

void Window::mouseButtonCallback( int button,int mods )
{
    double xpos,ypos;

    if( button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS )
    {
        glfwGetCursorPos( WindowObject,&xpos,&ypos );
        cout << " XPOS : " << xpos << endl;
        cout << " YPOS : " << ypos << endl;

        vertex[0] = (double)xpos / this->screenWidth * 2.0 - 1.0;
        vertex[1] = 1.0 - (double)ypos / this->screenHeight * 2.0;
    }
    if( button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE )
    {
        cout << "Mouse Button Released" << endl;
    }
}
本文链接:https://www.f2er.com/3078736.html

大家都在问