This is the function I used to get the mouse click position:
(这是我用来获取鼠标单击位置的功能:)
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);
Full code:
(完整代码:)
#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;
}
ask by saran gunasekaran translate from so