The XOR Texture 异或纹理

说明

最近在做纹理合成和传输的相关研究。本文是我在淘纹理合成相关材料时偶然得到,觉得这个纹理合成过程挺有意思,做了一些简单的翻译。因为是本人的第一篇随笔,所以贴出来献丑了,有失误的地方请大家见谅。该文的链接如下:

http://lodev.org/cgtutor/xortexture.html

 

Introduction 简介

The XOR texture is a very easy to generate texture that looks fine. However, it's so overused that it's not a good choice to use in in a demo or intro release. It isn't useful for games either, unless you want some fancy floor tiles. What it's useful for, is for testing a texture mapper you just wrote, in case you want to quickly test out a pattern without having to load an image file or write more complex texture generation code.

      异或纹理是一种很容易生成出看起来不错的纹理。然而,它被过度使用以至于它应用在demo或介绍发布中并不是最好的选择。同样,它在游戏中也不实用,除非你想要一些高档的地板砖。它有用的地方是,测试一个你刚刚写好的纹理映射器,假设你希望快速的测试一个样例而无需加载一副图像文件或者写更复杂的纹理生成代码。

This is an extremely small article, but the XOR Texture just couldn't be left out in a series of texture generation articles.

      这一个非常简短的文章,但是异或纹理无法被排除在纹理生成系列文章之外。

The XOR Texture 异或纹理

The XOR texture is simply generated by xor-ing the x and y coordinate of the current pixel. The '^' operator in C++ is the XOR operator.

      异或纹理通过当前像素的横坐标和纵坐标异或得到。C++中^为异或操作符。

 1 int main(int argc, char *argv[]) 
 2 { 
 3     screen(256, 256, 0, "The XOR Texture"); 
 4    
 5     for(int x = 0; x < w; x++) 
 6     for(int y = 0; y < h; y++) 
 7     {    
 8          Uint8 c = x ^ y; 
 9          pset(x, y, ColorRGB(c, c, c)); 
10     } 
11    
12     redraw(); 
13     sleep(); 
14     return 0; 
15 }

That's it, if you run it, you see the XOR texture:

      跑出来的异或纹理如下:

 

There are 3 things you should keep in mind though:

      有三件事你需要记住:

1) The sizes of the texture should be a power of two, if they aren't, the texture doesn't look as good:

      纹理的尺寸应该为2的幂,如果不是的话纹理看起来不会很好; 

2) Color component values range from 0 to 255. The maximum color value generated by the XOR operation is the same as the dimensions of the texture if it's size is a power of two. So if the size of your XOR pattern is smaller than 256, for example only 64, it'll be too dark (image on the left). Multiply the color with 4 to make it bright again (image on the right):

      颜色的值范围应该是[0, 255]。如果纹理的尺寸是2的幂,异或操作生成的颜色最大值应该和纹理的维度相同。所以,如果你的异或样式尺寸小于256,例如只有64,会看起来太暗(左图)。颜色值乘以4让它看亮起来(右图)。 

3) On the other hand, if the size is larger than 256, for example 512, you have to make sure the color is limited to a maximum value of 256. You can either modulo divide it through 256, but then it isn't a real XOR pattern anymore. Better is to divide it through 2. In any case, using a XOR texture larger than 256x256 doesn't increase the quality because there aren't enough distinct color values, unless you're using a color mode that allows more bits per channel. But who'd want to generate a 1024x1024 XOR texture anyway.

      另一方面,如果尺寸大于256,例如512,你不得不确保颜色被限制在最大值为256。你可以既可以将它模256计算,但它不再是一个异或样式了。更好的办法是将它除以2。无论如何,使用一个大于256*256的异或纹理无法提升质量,因为没有足够的颜色值来区分,除非你使用一个颜色模式允许每个通道有更多位(bits)。但是总之,没有人想生成一个1024*1024的异或纹理。

The XOR operator takes the binary values of both integers, and does a binary XOR on every two corresponding bits. XOR or eXclusive OR returns 1 if both bits are different, and returns 0 if both bits are the same: "Bit a is 1 OR bit 2 is 1, but not both". In other words, it applies the following truth table to every two corresponding bits:

      异或运算符需要两个整数的二进制值,和一个在每两个对应位上的二进制异或值。如果两位值不同,异或返回1;如果两位值相同,返回0。

XOR

Bit_a

Bit_b

Result

0

0

0

0

1

1

1

0

1

1

1

0


This is done on every bit of the integer, creating the many possible resulting values.

For example, 5 XOR 13 = 8, because in binary 0101 XOR 1101 = 1000.

Colors 颜色

You can also try the XOR texture with different colors, by using different value for R, G and B. For example:

      你可以尝试异或纹理在不同的颜色,通过使用不同的R、G、B值:

 1 int main(int argc, char *argv[])
 2 {
 3     screen(256, 256, 0, "The XOR Texture");
 4   
 5     ColorRGB color;
 6     
 7     for(int x = 0; x < w; x++)
 8     for(int y = 0; y < h; y++)
 9     {   
10          Uint8 c = (x ^ y);
11          color.r = 255 - c;
12          color.g = c;
13          color.b = c % 128;
14          pset(x, y, color);
15     }
16       
17     redraw();
18     sleep();
19     return 0;
20 }

You can even use the xor value as hue for the HSVtoRGB function...

      你甚至可以使用异或值作为HSV中的H值

 1 int main(int argc, char *argv[])
 2 {
 3     screen(256, 256, 0, "The XOR Texture");
 4   
 5     ColorRGB color; 
 6       
 7     for(int x = 0; x < w; x++)
 8     for(int y = 0; y < h; y++)
 9     {   
10          Uint8 c = (x ^ y);
11          color = HSVtoRGB(ColorHSV(c, 255, 255));
12          pset(x, y, color);
13     }
14   
15     redraw();
16     sleep();
17     return 0;
18 }

AND and OR 与和或操作

The AND and the OR operator also generate a similar texture.

The XOR operator returns 1 if both bits are different:

 

XOR

Bit_a

Bit_b

Result

0

0

0

0

1

1

1

0

1

1

1

0

The AND operator, only returns 1 if both bits are 1 (bit a AND bit b are true)

AND

Bit_a

Bit_b

Result

0

0

0

0

1

0

1

0

0

1

1

1

The OR operator returns 1 if any or both of the bits are 1 (bit a OR bit b is true)

OR

Bit_a

Bit_b

Result

0

0

0

0

1

1

1

0

1

1

1

1

The AND operator is denoted '&' in C++, and the OR operator '|', replace the '^' operator with those to use the new operators. Here's the result of XOR, AND and OR respectively:  

        

 

                异或运算结果                                          与运算结果                                        或运算结果

It makes sense that the AND texture is darker, because it returns 1 only in a single case. The OR texture is brighter, because it returns 1 very often. The sum of the XOR texture and the AND texture is the OR texture.

      与纹理更暗,因为只有在单一情况下才会返回1。或纹理更亮,因为它经常会返回1。异或纹理和与纹理的和为或纹理。

Conclusion 总结

It was shown how easy it is to create a XOR texture, which makes the XOR texture useful to test if a texture renderer is working. However, it's not suitable for applications such as art or games.

      异或纹理很容易生成,所以异或纹理在测试一个纹理渲染器是否在工作是很有用的。然而,在一些例如艺术或游戏中并不适用。

Here, the XOR pattern was used as a 3D texture (x ^ y ^ z) to test if a planet texture renderer was working correctly:

      在此,异或样式被用在3D纹理上来测试一个星球纹理渲染器是否作用正确。

 

posted on 2015-11-27 09:18  T_F_Hou  阅读(321)  评论(0)    收藏  举报