动画问题

编程入门 行业动态 更新时间:2024-10-09 00:40:41
本文介绍了动画问题-翻译,投影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.

更多推荐

动画问题

本文发布于:2023-05-28 00:48:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!