动画问题-翻译,投影OpenGL/C ++ [英] Issues with animation - translation, projection OpenGL/C++

查看:56
本文介绍了动画问题-翻译,投影OpenGL/C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我有这是我要实现的动画的gif .

Recently I had this issue. Now that I've fixed that with given solution, I ran into some other issues. Here is a gif of the animation I have to achieve.

我现在遇到的问题是:动画中的球看起来像在.gif开头时一样前后移动.我确实认为这与Ortho有关,但我不知道如何解决.

Issues I have now are: the ball in my animation doesn't look like it's moving forwards and backwards like it is at the beginning of the .gif. I do believe this has something to do with Ortho but I don't know how to fix this.

此外,在某个点上,当它完全向右移动时,移动的球和圆环就会被吞下",仅在起点处可见,而沿z轴平移时它会慢慢消失.这是我的代码.

Also, at some point, when it's moving completely to the right, the moving ball and torus just get "swallowed", it's visible only at the starting point and slowly disappears when translating along z-axis. Here is the code I have.

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glColor3f(0.26f, 0.74f, 0.73f);
    glutWireTorus(0.2, 0.85, 17, 30);

    glPushMatrix();
    glTranslatef(0.0, 0.0, tra);
    glColor3f(0.9f, 0.5f, 0.3f);
    glutWireSphere(0.5, 17, 15);

    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(ex+0.0, ey+0.0, ez+10.0, cx, cy, cz, 0.0, 1.0, 0.0);
    glFlush();
    }

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float sx = w / 70.0f;
    float sy = h / 70.0f;
    glOrtho(-sx/2.0f, sx/2.0f, -sy/2.0f, sy/2.0f, 1.0f, 600.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

我相信这部分内有一些错误.

I believe there are some mistakes inside this part.

推荐答案

如果要进行透视投影,其中对象的大小与屏幕分辨率有关,并且与窗口的大小无关,则必须计算相对于窗口高度的视角场:

If you want a perspective projection, where the size of the objects is relative to screen resolution and independent on the the size of the window, then you have to compute the filed of view angle in relation to the height of the window:

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float angle_rad = atan((float)h / 600.0f) * 2.0f;
    float angle_deg = angle_rad * 180.0f / M_PI;
    gluPerspective(angle_deg, (GLfloat)w / (GLfloat)h, 1.0, 80.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

视角与物体尺寸之间的关系是 tan(fov/2)* 2 ,因此 fov = atan(size)* 2 ,其中 size 取决于窗口的大小((float)h/600.0f ).
您必须根据需要调整分频器( 600.0f ).

The relation between the field of view angle and the size of an object is tan(fov / 2) * 2., thus the fov = atan(size) * 2, where size depends on the size of the window ((float)h / 600.0f).
You have to adjust the divider (600.0f) for your needs.

这篇关于动画问题-翻译,投影OpenGL/C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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