如何使用鼠标拖动事件OpenGL(带透视投影)添加场景平移?

问题描述 投票:1回答:1

是否可以通过简单的鼠标事件进行场景平移(类似于我们在gmap中获得的场景?目前我有两个鼠标事件:

  1. 通过更改cRadius中的glTranslatef进行缩放的鼠标轮
  2. 通过更改xrot中的yrotglRotatef拖动LMB进行旋转。

    下面是功能:

float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, zrot = 0, cRadius = 30.0f, lastx, lasty, lastz;

void mouseMovement(int x, int y)
{
    int diffx = x - lastx; 
    int diffy = y - lasty; 
    lastx = x;             
    lasty = y;  

    xrot += (float)diffy;  
    yrot += (float)diffx; 
}

和下面的鼠标按键初始化功能

void mouseFunc(int button, int state, int x, int y) 
{
    lastx = x;
    lasty = y;
}

是否可以使用与旋转相同的逻辑来启用摇摄功能,例如用xrot中的其他变量替换yrotglTranslatef(如果不应该在已应用翻译)?以下是我的显示功能以及重塑功能。我正在使用glPerspective而不是glLookat

void display(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);                   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -cRadius);
    glRotatef(xrot, 1.0, 0.0, 0.0);
    glRotatef(yrot, 0.0, 1.0, 0.0); 
    glBegin(GL_LINES);      
      ------------
      ------
    glTranslated(-xpos, 0.0f, zpos);
    glutSwapBuffers();              
    glEnd();
}

鼠标功能已在主循环中调用

int OpenGL(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Window");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMouseFunc(mouseFunc);
    glutMotionFunc(mouseMovement);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

//重塑

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 0.1, 500.0);
    glMatrixMode(GL_MODELVIEW);
}
opengl visual-c++ glut freeglut glu
1个回答
0
投票

在透视投影时,平移取决于对象的深度。 (请参见Qmatrix4x4 translate does not take any effect。幸运的是,您知道几何的部门。它在视图空间中由cRadius定义。

视图空间中的投影区域与视图空间的Z坐标之间的关系是线性的。它取决于视角和纵横比。(另请参见Field of view + Aspect Ratio + View Matrix from Projection Matrix (HMD OST Calibration)

标准化设备空间中的预计大小可以通过以下方式转换为视图空间中的大小:

aspect = w / h
tanFov = tan(fov_y * 2.0) * 2.0;

size_x = ndx_size_x * z_eye * tanFov * aspect;
size_y = ndx_size_y * z_eye * tanFov;

将其应用于您的代码:

#define _USE_MATH_DEFINES
#include <math.h>

float cRadius = 10.0f;
float fov_y = 60.0f;
float nearp = 0.1f;
float farp = 500.0f;
float width = 500.0f;
float height = 500.0f;

void mouseMovement(int x, int y)
{
    int diffx = x - lastx; 
    int diffy = y - lasty; 
    lastx = x;
    lasty = y;

    float ndc_x = diffx * 2.0f / width;
    float ndc_y = diffy * 2.0f / height;

    float aspect = width / height;
    float fov_rad = fov_y * M_PI / 180.0;
    float tanFov = tan(fov_rad / 2.0);

    xtrans += ndc_x * cRadius * tanFov * aspect;
    ytrans -= ndc_y * cRadius * tanFov;
}

void mouseFunc( int button, int state, int x, int y )
{
  lastx = x;
  lasty = y;
}
void reshape(int w, int h)
{
    width  = (float)w;
    height = (float)h;
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov_y, width / height, nearp, farp);
    glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
    // [...]

    glLoadIdentity();
    glTranslatef(xtrans, ytrans, -cRadius);
    glRotatef(xrot, 1.0, 0.0, 0.0);
    glRotatef(yrot, 0.0, 1.0, 0.0);
    // [...]
    glEnd();

    glutSwapBuffers();
}
© www.soinside.com 2019 - 2024. All rights reserved.