如何在顶点着色器中使用计时器来为OpenGL中的点大小设置动画 [英] How to use a timer inside a vertex shader to animate point sizes in OpenGL

查看:238
本文介绍了如何在顶点着色器中使用计时器来为OpenGL中的点大小设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个点云,在该点云中,不同点的大小将根据与之相关的不确定性值而变化.假设,如果该值为零,则大小应该是恒定的,如果接近1,则这些点的半径应该会越来越大.首先,这些点的大小应增加,达到最大值后,它们应减小到最小值,依此类推. 描述此现象的函数可能是:

I'm trying to implement a point cloud where the different points shall vary in size based on an uncertainty value associated with them. Let's say, if this value is zero, the size should be constant, if it's approaching 1, the radius of those points should vary more and more. First, the points should gain in size and after reaching a maximum, they should decrease until a minimum, and so on. A function to describe this phenomenon could be:

pointSize = x +/- c * pointUncertainty, where x = standard point size 
                                              c = scaling constant

根据我的阅读,可以通过将统一的计时器变量传递给我的顶点着色器并计算其中的点大小来实现.但是,所有点应同时变化,这意味着不确定性为1的点和不确定性为0.5的点应同时达到pointSize的最小值和最大值.此外,整个过程不应该依赖于帧速率.

From what I've read, this could potentially be achieved by passing a uniform timer variable to my vertex shader and compute the point size in there. However, all the points should vary in the same time, meaning that a point that has an uncertainty of 1 and a point that has an uncertainty of 0.5 should reach their minimum and maximum value of pointSize at the same time. Additionally, the whole process shouldn't be dependent on the frame rate.

我不确定完成此操作的最佳方法是什么,如何最好地实现增减增幅模式以及在哪里放置必要的OpenGL(4.2)命令.

I'm not sure what's the best way of getting this done, how the increase-decrease-increase-pattern could be implemented best and where to put the necessary OpenGL (4.2) commands.

我仍然希望得到这个问题的答案,因为我不清楚如何实现这种动画效果的整个过程.

I still hope to get an answer to this question, since The whole process of how such an animation effect could be achieved is unclear to me.

推荐答案

首先...感谢您的回答.我仅使用顶点着色器解决了问题.我想这就是我希望得到的解决方案.也许有相同问题的人们可以从中受益:

First of all...thanks for your answer. I've worked things out using just a vertex shader. I guess this was the solution I was hoping to get. Maybe folks with the same problem can benefit from this:

我的顶点着色器的一部分看起来像这样:

Part of my vertex shader looks like this:

 ... 
 "  float standardPointSize = 100.0;"
 "  float timeScale = standardPointSize / my_loopDuration;"
 "  float currentTime = mod(my_time, my_loopDuration);"
 "  float offset = currentTime * timeScale * in_uncertainty;"
 "  if(currentTime < my_loopDuration / 4)"
 "  {"
 "      gl_PointSize = standardPointSize - 4 * offset;"
 "  }"
 "  else if(currentTime >= my_loopDuration / 4 && currentTime < my_loopDuration / 2)"
 "  {"
 "      gl_PointSize = standardPointSize - standardPointSize * in_uncertainty + 4 * offset - 1 * in_uncertainty * standardPointSize;"
 "  }"
 "  else if(currentTime >= my_loopDuration / 2 && currentTime < 3 * my_loopDuration / 4)"
 "  {"
 "      gl_PointSize = standardPointSize + 4 * offset - 2 * in_uncertainty * standardPointSize;"
 "  }"
 "  else if(currentTime >= 3 * my_loopDuration / 4 && currentTime <= my_loopDuration)"
 "  {"
 "      gl_PointSize = standardPointSize + standardPointSize * in_uncertainty - 4 * offset + 3 * in_uncertainty * standardPointSize;"
 "  }"
 ...

my_time和my_loopDuration是统一变量.第一个设置为glutGet(GLUT_ELASED_TIME),第二个设置为任意数字.

my_time and my_loopDuration are uniform variables. The first one is set with glutGet(GLUT_ELASED_TIME) and the second one is an arbitrary number.

这篇关于如何在顶点着色器中使用计时器来为OpenGL中的点大小设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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