簡體   English   中英

我怎樣才能讓我的立方體的面在彩虹的所有 colors 之間平滑過渡?

[英]How can I make the faces of my cube smoothly transition between all colors of the rainbow?

我在 Visual Studio 中有一個程序可以正確呈現一個緩慢旋轉的 3D 立方體。 我有一個有效的 FillTriangle() function,它用我輸入的十六進制代碼作為參數的任何顏色填充立方體的面(例如,紫色為 0x00ae00ff)。 我已將每張臉的顏色設置為從紅色 (0xFF000000) 開始,然后我在 main() 中有一個 while 循環來更新場景並在每一幀繪制新像素。 我還有一個計時器 class,它可以處理各種與時間相關的事情,包括每幀更新事物的 Update() 方法。 我想讓 colors 的臉從一種顏色平滑過渡到另一種顏色,通過彩虹的每種顏色,我希望它循環並在程序運行時執行它。 現在,它在幾個 colors 之間平穩過渡,然后突然跳到另一種顏色。 例如,它可能從黃色平滑地過渡到橙色再到紅色,但隨后突然跳到綠色。 這是現在正在執行的代碼:

...
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;
   }
   ...
 }
 ...
}

有沒有更好的方法來解決這個問題? 添加到當前像素顏色是我想到的第一件事,它有點工作,但由於某種原因它一直跳過 colors。

該常量將隨着每次添加而溢出。 不僅僅是一個整數,而是整個色譜的每個組成部分:R、G 和 B。

您需要將 pixelColor 分成單獨的紅色、綠色和藍色 colors 並獨立地對每個字節進行數學計算。 並將 Alpha 固定為 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;

其中 redInc、greenInc 和 blueInc 在主 while 循環之外定義和初始化,如下所示:

int redInc = -1;
int greenInc = 2;
int blueInc = 4;

增量 function 是這樣的:

 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);
     }
 }

這應該以更自然的方式(從更暗到更亮再到更暗)循環通過 colors 並帶有一點隨機性,因此它永遠不會出現兩次相同的模式。 您可以通過在初始化時調整初始 colorInc 常量以及如何在Increment function 中更新*increment值來玩弄隨機性。

如果您看到任何奇怪的顏色閃爍,很可能您的 alpha 字節是錯誤的 position。它可能是高字節,而不是低字節。 類似地,一些系統將integer中的colors排序為RGBA。 其他人做ARGB。 很可能 RGB 被 BGR 翻轉了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM