gpt4 book ai didi

opengl - 使用鼠标在 openGL 中移动绘图

转载 作者:行者123 更新时间:2023-12-02 01:56:04 24 4
gpt4 key购买 nike

我试图在按住鼠标左键的同时在 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
...

关于opengl - 使用鼠标在 openGL 中移动绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com