我在Visual中有一个程序,它正确地呈现一个正在缓慢旋转的三维立方体。我有一个工作的FillTriangle()函数,它用我输入的十六进制代码作为参数填充立方体的正面(例如,紫色的0x00ae00ff )。我已经将每个脸的颜色设置为红色(0xF000000),然后在main()中有一个while循环来更新场景并绘制每个帧的新像素。我还有一个Timer类,它处理所有与时间相关的事情,包括更新()方法,它更新每一个帧的内容。我想通过彩虹的每一种颜色,使脸的颜色从一种颜色平稳地过渡到另一种颜色,我希望它循环起来,只要程序运行就行。现在,它正在平稳地过渡到几种颜色之间,然后突然跳到另一种颜色。例如,它可能顺利地从黄色过渡到橙色到红色,但随后突然跳到绿色。下面是正在执行此操作的代码:
...
main()
{
...
float d = 0.0f; //float for the timer to increment
//screenPixels is the array of all pixels on the screen, numOfPixels is the number of pixels being displayed
while(Update(screenPixels, numOfPixels))
{
...
timer.Signal(); //change in time between the last 2 signals
d += timer.Delta(); //timer.Delta() is the average current time
if(d > (1/30)) // 1 divided by number of frames
{
//Reset timer
d = 0.0f;
//Add to current pixel color being displayed
pixelColor += 0x010101FF;
}
...
}
...
}
有没有更好的方法来解决这个问题?添加到当前像素颜色是我脑海中的第一件事,它有点工作,但由于某种原因,它一直跳过颜色。
发布于 2022-02-19 18:30:10
这个常量会随着每一个加法溢出。不仅是一个整数,而且横跨光谱的每个分量: R、G和B。
您需要将您的pixelColor分解为单独的红色、绿色和蓝色颜色,并对每个字节分别进行计算。将阿尔法固定在255 (完全不透明)。检查沿途是否有溢流/下溢。当您到达溢流或下溢时刻时,只需将方向从增量改为递减。
而且,我不会在每个步骤中增加每个组件相同的值(1)。在R,G和B上有相同的增量,您只需在颜色中添加“更多的白色”。如果您想要一个更自然的彩虹环,我们可以这样做:
改变这一点:
pixelColor += 0x010101FF;
对此:
// I'm assuming pixelColor is RGBA
int r = (pixelColor >> 24) & 0x0ff;
int g = (pixelColor >> 16) & 0x0ff;
int b = (pixelColor >> 8) & 0x0ff;
r = Increment(r, &redInc);
r = Increment(g, &greenInc);
g = Increment(g, &blueInc);
pixelColor = (r << 24) | (g << 16) | (b << 8) | 0x0ff;
在主while循环之外定义和初始化redInc、greenInc和blueInc,如下所示:
int redInc = -1;
int greenInc = 2;
int blueInc = 4;
增量函数是这样的:
void Increment(int color, int* increment) {
color += *increment;
if (color < 0) {
color = 0;
*increment = (rand() % 4 + 1);
} else if (color > 255) {
color = 255;
*increment = -(rand() % 4 + 1);
}
}
这应该是以一种更自然的方式(从更暗到更亮,再到更暗)以一种随机性的方式来循环,所以它不会有两次相同的图案。您可以通过在初始化时调整初始colorInc常数以及如何在Increment
函数中更新*increment
值来处理随机性。
如果您看到任何奇怪的颜色闪烁,很可能是您的阿尔法字节在错误的位置。可能是高字节,而不是低字节。类似地,一些系统将整数中的颜色排序为RGBA。其他人做ARGB。很有可能RGB被BGR翻转了。
https://stackoverflow.com/questions/71190760
复制