c - 在 OpenGL 中读取鼠标坐标单击

标签 c opengl math coordinates dev-c++

当我点击屏幕上的一个点并用一个点标记它时,我试图读取鼠标坐标,但它不起作用。

     float cx = 0, cy = 0;

 glPushMatrix(); 
    glPointSize(6.0f);
    //Draw the points
    glBegin(GL_POINTS);
        cx = (((x * (maxx + minx))) / width) + minx;
        cy = (((-1 * pow((y - height), width) * (maxy - miny)) / height ) + miny;

        glVertex2f(cx , cy);
    glEnd();


glPopMatrix();

这是一个大学练习,从屏幕上获取坐标的公式是:

Formula to get the coordinate from screen

Px 和 Py 是 mouseFunc 传递给这个函数的坐标。 w 和 h 是屏幕的宽度和高度(我从 reshape 中得到它) maxx, maxy, minx, miny...为正坐标

那么,我的代码有什么问题吗?

鼠标功能(我已经测试过鼠标点击是否有效,确实如此):

void mouse(int button, int state, int x, int y){
 switch(button){
       case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN)
                      exerciseThree(x, y);
       break;         
 }    

 glutPostRedisplay();
}

最佳答案

您没有正确执行您引用的公式。尝试

cx = x * (maxx - minx) / width + minx;
cy = (height - y) * (maxy - miny) / height + miny;

两个显着差异是第一个表达式中的 (maxx - minx),以及您误读第二行并认为上面一行的除数 w 是一种强大的力量对于第二行。

关于c - 在 OpenGL 中读取鼠标坐标单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784192/

相关文章:

国家分组 c

c - 如何在 UNIX 上将 PID 写入文件

c - 在 FPU 和 SSE 发明之前, float 转换是如何处理的?

c++ - 如何通过缓冲区纹理正确地将 16 位整数发送到 GLSL 着色器

objective-c - 我在 Objective-C 中的矩阵方面遇到问题

c - OMP 2.0 嵌套 For 循环

c - sdl_gl_setattribute 上的 sdl 应用程序段错误

swift - GLKit 与 Metal 透视矩阵的区别

javascript - Javascript 中的太阳弧路径

math - 从点计算文档的倾斜度?