使用鼠标在openGL中移动图形

Deadalus

我试图在按住鼠标左键的同时在openGL中移动图像。我不是要拖动对象,而只是移动整个图片。它是一个分形的2D图,有人告诉我可以使用gluortho2d,但找不到任何信息或类似的尝试来做。我假设像

void mouse_callback_func(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    gluOrtho2D(x-250.0, x+250.0, y-250.0,y+250.);
glutPostRedisplay();
}  

500x500的窗口,但无法正常工作。我离开的那一刻,窗口空白。有任何想法吗?

jozxyqk

gluOrtho2D修改当前矩阵。它设计用于与glMatrixMode(GL_PROJECTION),例如:

glMatrixMode(GL_PROJECTION); //start editing the projection matrix
glLoadIdentity(); //remove current projection
gluOrtho2D(...); //create new one
glMatrixMode(GL_MODELVIEW); //back to editing the modelview matrix

设置相机概念可能更简单。

float cameraX, cameraY;
int lastMouseX, lastMouseY;

void mouse_callback_func(int button, int state, int x, int y)
{
    int dx = x - lastMouseX;
    int dy = y - lastMouseY;
    const float speed = 0.1f;
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        cameraX += dx * speed; //or -=, depending on which direction feels more natural to you
        cameraY -= dy * speed; //-= as mouse origin is top left, so +y is moving down
        glutPostRedisplay();
    }
    lastMouseX = x;
    lastMouseX = y;
}

void display()
{
    glLoadIdentity(); //remove transforms from previous display() call
    glTranslatef(-cameraX, -cameraY, 0.0f); //move objects negative = move camera positive
    ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用键盘箭头和鼠标在Java中移动图形

来自分类Dev

使用鼠标在画布中移动图像

来自分类Dev

C ++使用SetCursorPos在Windows中移动鼠标

来自分类Dev

C ++使用SetCursorPos在Windows中移动鼠标

来自分类Dev

使用鼠标在Threejs场景中移动对象

来自分类Dev

使用鼠标在角度Google地图中移动标记

来自分类Dev

在CasperJS中移动鼠标以触发悬停事件

来自分类Dev

如何从用户代码中移动鼠标光标?

来自分类Dev

xdotool不能在Firefox中移动鼠标

来自分类Dev

如何从用户代码中移动鼠标光标?

来自分类Dev

从Windows服务中移动鼠标光标

来自分类Dev

通过单击鼠标在PuTTY中移动光标?

来自分类Dev

在Windows窗体中移动鼠标光标

来自分类Dev

在CasperJS中移动鼠标以触发悬停事件

来自分类Dev

无法在Windows子窗体C#中使用鼠标在文本框中移动光标

来自分类Dev

无法在Windows子窗体C#中使用鼠标在文本框中移动光标

来自分类Dev

OpenGL在场景中移动表格

来自分类Dev

在OpenGL中移动3d形状

来自分类Dev

需要帮助在OpenGL中移动相机

来自分类Dev

使用C ++在OpenGL中移动自动旋转的3d多边形

来自分类Dev

在使用C ++的OpenGL中移动形状时按时间自动旋转形状

来自分类Dev

使用鼠标单击与鼠标光标移动的回调时的OpenGL未处理的异常

来自分类Dev

如何在概览窗格中移动(拖动)缩放的图形?

来自分类Dev

在游戏中移动鼠标的正确方法是什么?

来自分类Dev

鼠标在HTML画布中移动时图像闪烁

来自分类Dev

您可以从工作中移动鼠标位置吗?

来自分类Dev

在查看鼠标位置的同时在世界空间中移动

来自分类Dev

在Opengl中通过鼠标移动形状

来自分类Dev

在Opengl中通过鼠标移动形状

Related 相关文章

热门标签

归档