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

问题描述 投票:0回答:1

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

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);

完整代码:

#include <GL/glew.h>
#include <glfw/glfw3.h>
#include <iostream>

using namespace std;

class Window
{
private:
    int screenWidth, screenHeight;
    const char* WindowName;
    GLFWwindow* WindowObject;
public:
    Window();
    void CreateWindow();
    void SetViewPort( int, int, const char* );
    void SetBGColor( float, float, float, float );
    void SetWindowActive();
    bool IsWindowActive();
    static void mouseButtonCallback( GLFWwindow* WindowObject, int button, int action, int mods );
};

GLdouble vertex[] = { 0.0f,0.0f };
GLuint VBO;

void DrawPoints()
{
    //generate vbo
    glGenBuffers( 1, &VBO );

    //binding vbo
    glBindBuffer( GL_ARRAY_BUFFER, VBO );

    //generating data in vbo
    glBufferData( GL_ARRAY_BUFFER, sizeof( vertex ), vertex, GL_STATIC_DRAW );

    //retrieving data
    glVertexAttribPointer( 0, 3, GL_DOUBLE, GL_FALSE, 0, 0 );

    //enable data for drawing
    glEnableVertexAttribArray( 0 );

    //drawing a point
    glDrawArrays( GL_POINTS, 0, 1 );

    //to increase the size of point
    glPointSize( 5 );

    //to change the color of the point
    glColor3f( 1, 0, 0 );

    //disable data after using
    glDisableVertexAttribArray( 0 );
}

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;
        //glBegin(GL_POINTS);
        //glPointSize(5);
        //glColor4f(1, 0, 0, 1);
        //glVertex2f(xpos,ypos);
        //glEnd();
    }
    if( button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE )
    {
        cout << "Mouse Button Released" << endl;
    }
}

Window::Window()
{
    this->screenWidth = 640;
    this->screenHeight = 480;
    this->WindowName = "New Window";
    glfwInit();
}

void Window::SetViewPort( int screenWidth, int screenHeight, const char* WindowName )
{
    this->screenHeight = screenHeight;
    this->screenWidth = screenWidth;
    this->WindowName = WindowName;
}

void Window::CreateWindow()
{
    this->WindowObject = glfwCreateWindow( this->screenWidth, this->screenHeight, this->WindowName, NULL, NULL );
    // To get co-ordinates of cursor
    //glfwSetCursorPosCallback(this->WindowObject, cursorPosCallback);
    glfwSetMouseButtonCallback( this->WindowObject, mouseButtonCallback );
    if( this->WindowObject == NULL )
    {
        cout << stderr << "Failed to Load the Window";
        glfwTerminate();
    }
    glfwMakeContextCurrent( this->WindowObject );
    glewInit();
}

void Window::SetBGColor( float R, float G, float B, float A )
{
    glClear( GL_COLOR_BUFFER_BIT );
    glClearColor( R, G, B, A );
}

void Window::SetWindowActive()
{
    glfwSwapBuffers( this->WindowObject );
    glfwPollEvents();
}

bool Window::IsWindowActive()
{
    if( glfwGetKey( this->WindowObject, GLFW_KEY_ESCAPE ) == GLFW_PRESS )
    {
        return false;
    }
    if( GLFW_PRESS && glfwWindowShouldClose( this->WindowObject ) != 0 )
    {
        return false;
    }
    return true;
}

int main()
{
    Window MyGameWindow;
    MyGameWindow.SetViewPort( 1024, 768, "My Game Window" );
    MyGameWindow.CreateWindow();

    do
    {
        MyGameWindow.SetBGColor( 0.128f, 0.128f, 0.128f, 0.0f );
        DrawPoints();
        MyGameWindow.SetWindowActive();
    }
    while( MyGameWindow.IsWindowActive() );
    return 0;
}
c++ opengl glfw mouseclick-event
1个回答
0
投票

使用glfwSetWindowUserPointer将指向glfwSetWindowUserPointer对象的指针与Window关联:

GLFWindow

使用void Window::CreateWindow() { this->WindowObject = glfwCreateWindow(this->screenWidth, this->screenHeight, this->WindowName, NULL, NULL ); glfwSetWindowUserPointer( this->WindowObject, this ); // <---- // [...] } 检索静态鼠标按钮回调中的关联指针,并将回调委托给glfwGetWindowUserPointer方法:(遗憾的是,您必须将类型为glfwGetWindowUserPointer的返回值转换为Window,无法解决。)

void*
Window*

在该方法中,可以访问窗口宽度(class Window { // [...] void mouseButtonCallback( int button, int action, int mods ); static void mouseButtonCallback( GLFWwindow* WindowObject, int button, int action, int mods ); }; )和高度(void Window::mouseButtonCallback( GLFWwindow * WindowObject, int button, int action, int mods ) { Window *window = (Window*)glfwGetWindowUserPointer( WindowObject ); window->mouseButtonCallback( button, action, mods ); } void Window::mouseButtonCallback( int button, int action, int mods ) { // [...] } )的属性,并计算与鼠标位置对应的归一化设备坐标。现在可以更改顶点坐标(this->screenWidth):

this->screenHeight
© www.soinside.com 2019 - 2024. All rights reserved.