我试图在按住鼠标左键的同时在 openGL 中移动图像。
我不想拖动物体,只是移动整个图片。它是分形的二维绘图,我被告知我可以使用 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 窗口,但它不起作用。当我点击左键时,窗口变成空白。
有任何想法吗?
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
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)