c++ - 返回不准确的鼠标坐标

标签 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;
 }
}

最佳答案

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

关于c++ - 返回不准确的鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3754640/

相关文章:

c++ - QComboBox 不调用委托(delegate)方法

c++ - 段错误 : 11 ? C++

c++ - 使用顶点数组对象渲染三角形什么都不显示(OpenGL)

c++ - 在 Windows API 中重置 OpenGL 扩展指针

java - 制服中的平移/旋转/缩放和矩阵

c++ - 如何检查 sc_buffer 和 sc_signal 之间的区别?

java - 如何为 Java 应用程序调试 C++ dll

ios - UIVisualEffectView setHidden=YES 非常慢 - 奇怪的错误?

bash --debugger 在安装 bashdb 后无法在没有参数的脚本上使用 bash 4.3

jquery - 如何在 Firefox 30 上调试 Greasemonkey 脚本?