用鼠标在 openGL 中移动绘图 [英] Moving a drawing around in openGL with mouse

查看:37
本文介绍了用鼠标在 openGL 中移动绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to move an image around in openGL while holding left mouse button. i am NOT trying to drag an object around, just move the whole picture. Its a 2d drawing of a fractal and i was told that i can use gluortho2d but i can't find any info or similar tries on how to do it. I am assuming something like

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 的窗口,但它不起作用.我左键单击窗口的那一刻变为空白.有什么想法吗?

for a 500x500 window,but it's not working. The moment i left click the window goes blank. Any ideas?

推荐答案

gluOrtho2D 修改当前矩阵.它旨在与 glMatrixMode(GL_PROJECTION) 一起使用,例如:

gluOrtho2D modifies the current matrix. It's designed to be used with glMatrixMode(GL_PROJECTION), for example:

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

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

It might be more simple to set up a camera concept...

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
    ...

这篇关于用鼠标在 openGL 中移动绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆