返回的鼠标坐标不准确
问题是,鼠标单击距离左上角原点 (0,0) 越远,绘制顶点时的高度不准确度就越大。有什么想法吗?
int WindowWidth = 19;
int WindowHeight = 13;
int mouseClickCount = 0;
int rectPlotted;
GLint x1;
GLint y1;
GLint x2;
GLint y2;
//Declare our functions with prototypes:
void display(void);
void init (void);
void processNormalKeys(unsigned char key, int x, int y);
void on_vertex_selected(GLint x, GLint y);
void on_mouse_event(int button, int state, int x, int y);
/////////////////////////////////// MAIN ////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
//start calculation of max window size available at 19:13 aspect ratio
int screenWidth = glutGet(GLUT_SCREEN_WIDTH);
int screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
screenWidth = screenWidth/WindowWidth;
WindowWidth = WindowWidth*screenWidth;
screenHeight = screenHeight/WindowHeight;
WindowHeight = screenHeight*WindowHeight;
//end calculation
glutInitWindowSize (WindowWidth, WindowHeight);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Plot a rectangle!");
glutDisplayFunc(display);
glutKeyboardFunc(processNormalKeys);
glutMouseFunc(on_mouse_event);
init();
glutMainLoop();
return 0;
}
/////////////////////////////////////////////////////////////////////////
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); //clear all pixels
glFlush();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (1.0, 1.0, 1.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WindowWidth, 0, WindowHeight, -1.0, 1.0);
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27) //esc
exit(0);
//Allows color change of most recently plotted rectangle
if (key == 98){ //b
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
if (key == 114){ //r
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
if (key == 103){ //g
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
}
void on_vertex_selected(GLint x, GLint y){
if(mouseClickCount == 0){
x1 = x;
y1 = y;
glColor3f(0.0, 0.0, 0.0);
glEnable(GL_POINT_SMOOTH);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glEnd();
glFlush();
}
else{
x2 = x;
y2 = y;
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(x1,y1); //"clears" previous point to make way for the rectangle
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
}
void on_mouse_event(int button, int state, int x, int y){
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && mouseClickCount == 0){
//y = y+20; //adjusts for VM mouse tracking error
on_vertex_selected(x, WindowHeight - y);
rectPlotted = 0;
}
if(button==GLUT_LEFT_BUTTON && state ==GLUT_UP && mouseClickCount == 0){
if(rectPlotted == 1){
return;
}
else{
mouseClickCount++;
}
}
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && mouseClickCount == 1){
//y = y+20; //adjusts for VM mouse tracking error
on_vertex_selected(x, WindowHeight - y);
mouseClickCount = 0;
rectPlotted = 1;
}
}
The issue is that the further the mouse click is from the top left origin (0,0) the greater the height inaccuracy when the vertex is plotted. Any ideas?
int WindowWidth = 19;
int WindowHeight = 13;
int mouseClickCount = 0;
int rectPlotted;
GLint x1;
GLint y1;
GLint x2;
GLint y2;
//Declare our functions with prototypes:
void display(void);
void init (void);
void processNormalKeys(unsigned char key, int x, int y);
void on_vertex_selected(GLint x, GLint y);
void on_mouse_event(int button, int state, int x, int y);
/////////////////////////////////// MAIN ////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
//start calculation of max window size available at 19:13 aspect ratio
int screenWidth = glutGet(GLUT_SCREEN_WIDTH);
int screenHeight = glutGet(GLUT_SCREEN_HEIGHT);
screenWidth = screenWidth/WindowWidth;
WindowWidth = WindowWidth*screenWidth;
screenHeight = screenHeight/WindowHeight;
WindowHeight = screenHeight*WindowHeight;
//end calculation
glutInitWindowSize (WindowWidth, WindowHeight);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Plot a rectangle!");
glutDisplayFunc(display);
glutKeyboardFunc(processNormalKeys);
glutMouseFunc(on_mouse_event);
init();
glutMainLoop();
return 0;
}
/////////////////////////////////////////////////////////////////////////
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); //clear all pixels
glFlush();
}
void init (void)
{
/* select clearing (background) color */
glClearColor (1.0, 1.0, 1.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WindowWidth, 0, WindowHeight, -1.0, 1.0);
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27) //esc
exit(0);
//Allows color change of most recently plotted rectangle
if (key == 98){ //b
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
if (key == 114){ //r
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
if (key == 103){ //g
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
}
void on_vertex_selected(GLint x, GLint y){
if(mouseClickCount == 0){
x1 = x;
y1 = y;
glColor3f(0.0, 0.0, 0.0);
glEnable(GL_POINT_SMOOTH);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glEnd();
glFlush();
}
else{
x2 = x;
y2 = y;
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(x1,y1); //"clears" previous point to make way for the rectangle
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
}
void on_mouse_event(int button, int state, int x, int y){
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && mouseClickCount == 0){
//y = y+20; //adjusts for VM mouse tracking error
on_vertex_selected(x, WindowHeight - y);
rectPlotted = 0;
}
if(button==GLUT_LEFT_BUTTON && state ==GLUT_UP && mouseClickCount == 0){
if(rectPlotted == 1){
return;
}
else{
mouseClickCount++;
}
}
if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && mouseClickCount == 1){
//y = y+20; //adjusts for VM mouse tracking error
on_vertex_selected(x, WindowHeight - y);
mouseClickCount = 0;
rectPlotted = 1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 glOrtho() 调用几乎肯定是错误的。您传递的是窗口大小,而不是窗口客户区大小。区别在于边框的大小和标题的高度。
Your glOrtho() call is almost certainly wrong. You pass the window size, not the window client area size. The difference is the size of the borders and the height of the caption.
请注意,您的实际窗口大小很可能与您在初始化时要求的大小略有不同。
简单地说,实现一个 glutReshapeFunc()
Be aware that your actual window size will most probably be slightly different than what you asked at init time.
Simply said, implement a glutReshapeFunc()
我在 Parallels VM v.6 中编译并运行我的代码。这是导致返回不准确的鼠标坐标的问题。我在 OS X 上编译并运行了完全相同的代码,没有出现任何问题。
I was compiling and running my code within Parallels VM v.6. This was the issue leading to inaccurate mouse coordinates being returned. I compiled and ran the exact same code on OS X without problem.