返回不准确的鼠标坐标

时间:2010-09-20 19:07:58

标签: c++ debugging opengl glut

问题是鼠标点击距离左上角原点(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;
 }
}

3 个答案:

答案 0 :(得分:1)

你的glOrtho()电话几乎肯定是错的。您传递窗口大小,而不是窗口客户区大小。区别在于边框的大小和标题的高度。

答案 1 :(得分:0)

请注意,您的实际窗口大小可能与您在初始时询问的内容略有不同。

简单地说,实施一个glutReshapeFunc()

答案 2 :(得分:0)

我正在Parallels VM v.6中编译和运行我的代码。这是导致返回不准确的鼠标坐标的问题。我在OS X上编译并运行完全相同的代码没有问题。