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纹理上来测试一个星球纹理渲染器是否作用正确。

转载于:https://www.cnblogs.com/houfeng-Ted/p/4998566.html

【转】The XOR Texture 异或纹理(Lode's Computer Graphics Tutorial)相关推荐

  1. Lode's Computer Graphics Tutorial Image Filtering

    http://lodev.org/cgtutor/filtering.html 转载于:https://www.cnblogs.com/guochen/p/8919144.html

  2. 提取图片纹理_Fundamentals Of Computer Graphics 第十一章 纹理映射(中)

    本文翻译虎书第十一章纹理映射的11.3部分 11.3 抗锯齿纹理查询 纹理映射的第二个基本问题是抗锯齿.渲染纹理映射的图像是一个采样过程:将纹理映射到表面上,然后将表面投影到图像中,将在整个图像平面上 ...

  3. ML之NN:利用神经网络的BP算法解决XOR类(异或非)问题(BP solve XOR Problem)

    ML之NN:利用神经网络的BP算法解决XOR类(异或非)问题(BP solve XOR Problem) 目录 输出结果 实现代码 输出结果 实现代码 #BP solve XOR Problem im ...

  4. OpenGL Texture Coordinate Wrapping纹理坐标包装的实例

    OpenGL Texture Coordinate Wrapping纹理坐标包装 先上图,再解答. 完整主要的源代码 源代码剖析 先上图,再解答. 完整主要的源代码 #include <verm ...

  5. C++xor cipher异或密码算法(附完整源码)

    xor cipher异或密码的算法 xor cipher异或密码的完整源码(定义,实现,main函数测试) xor cipher异或密码的完整源码(定义,实现,main函数测试) #include & ...

  6. Vertex Texture Fetch 顶点纹理拾取

    « 水效果Ⅱ - 涟漪学一学,VBO » Vertex Texture Fetch 顶点纹理拾取 2009-8-19 22:50:32 | 发布:zwqxin Vertex Texture Fetch ...

  7. unity texture贴图纹理

    文章内一些内容引用自作者:Aimar_Johnny http://blog.csdn.net/lzhq1982/article/details/75045358 导入png图片,默认显示如下 Text ...

  8. 线段树 ---- 牛客多校4 ETree Xor 区间异或分段

    题目链接 题目大意: 就是给你nnn个节点的树,树上每个节点都有一个权值wi∈[li,ri]w_i\in[l_i,r_i]wi​∈[li​,ri​],以及相邻(u,v)(u,v)(u,v)的异或值wu ...

  9. 2021牛客暑期多校训练营4 E-Tree Xor(异或+思维+区间交 or Trie树)

    E-Tree Xor 首先不考虑区间限制条件,我们给定其中一个点的权值后,那么其他点的权值也就确定.比如 val1=0\text{val}_1=0val1​=0,即可通过变得限制求出其他点valu\t ...

最新文章

  1. eosjs v20 中文文档
  2. python类的继承与多态_python类的继承和多态
  3. 浅析ASP.NET页面缓存的几点体会
  4. Unity AssetBundles and Resources指引 (三) AssetBundle基础
  5. 程序员之道——编程也是一门艺术
  6. python seo百度_Python与seo,百度关键词相关搜索关键词采集源码
  7. hexo sever端口占用,localhost:4000无响应
  8. Mac版WebStorm破解方案
  9. 黑客帝国般的Linux屏保cmatrix的安装和使用
  10. 麦子学院demo(html+css)
  11. Caltech-UCSD Birds 200 (CUB) 数据库预处理
  12. 一文带你了解什么是CDN
  13. 社区英雄榜:谁是真正的技术英雄?
  14. 跨境电子商务行业进入发展快车道
  15. 宝塔设置A站点SSL,同服务器下其他未设SSL站点访问HTTPS默认会打开A站点
  16. cs起源本地服务器无响应,CS起源上为什么我无法进入有反作
  17. python中怎么创建一个词典_如何在Python中创建字典词典
  18. VC++检测当前网络状态
  19. 题目1022:游船出租 2007年浙江大学计算机及软件工程研究生机试真题
  20. 延长数据中心使用年限的低成本方法

热门文章

  1. 编写一个函数,输入整数m和n,计算m的n次幂,用c语言实现
  2. 2021-11-05前端技术栈
  3. v50.03 鸿蒙内核源码分析(编译环境) | 编译鸿蒙防掉坑指南 | 百篇博客分析HarmonyOS源码
  4. Docker更换容器存储路径Root-dir
  5. 【Vue实用功能】elementUI 自定义表单模板组件
  6. 航向角,横摆角,车辆质心侧偏角,前轮侧偏角(这又可以分为在轮胎坐标系下和车辆坐标系下的前轮侧偏角哦),前轮转角
  7. html制作龙卷风,DIY人造龙卷风原理 教你如何制作微型水龙卷
  8. 『摄影欣赏』20幅精美的秋天落叶风景欣赏【组图】
  9. 功能核磁共振影像分析AFNI教程(2)
  10. 4点意见,从今天在京东和亚马逊的购物体验谈用户体验设计