I know that I have to do call one of the following before each call to glVertex:
我知道我必须在每次调用glVertex之前调用其中一个:
glTexCoord(0,0);
glTexCoord(0,1);
glTexCoord(1,1);
glTexCoord(1,0);
But I have no idea what they mean. I know, however, that if I multiply (or is that divide?) the right side (or is it all the ones?) by two, my texture expands, and if I do the opposite, my texture repeats twice. I've managed to code a texture atlas by applying operations until it worked. But I have proper idea of what's going on. Why does dividing these affect the image, and why does reversing them mirror it? How do texture coordinates work?
但我不知道他们是什么意思。但是,我知道,如果我乘以(或者是这个除以?)右边(或者是所有的),我的纹理会扩大,如果我做相反的事情,我的纹理会重复两次。我已经通过应用操作来编写纹理地图集,直到它生效。但是我对发生的事情有一个正确的认识。为什么划分这些会影响图像,为什么倒转它们会镜像呢?材质如何协调工作?
4 个解决方案
#1
96
Texture coordinates specify the point in the texture image that will correspond to the vertex you are specifying them for. Think of a rectangular rubber sheet with your texture image printed on it, where the length of each side is normalized to the range 0-1. Now let's say you wanted to draw a triangle using that texture. You'd take 3 pins and place them in the rubber sheet in the positions of each of your desired texture coordinates. (Say [0, 0], [1, 0] and [1, 1]) then move those pins (without taking them out) to your desired vertex coordinates (Say [0, 0], [0.5, 0] and [1, 1]), so that the rubber sheet is stretched out and the image is distorted. That's basically how texture coordinates work.
纹理坐标指定纹理图像中的点,它将对应于指定它们的顶点。想象一个长方形的橡胶板,上面印着你的纹理图像,每边的长度被标准化到0-1。现在我们假设你想画一个三角形使用这个纹理。你会拿3个针,把它们放在每个你想要的纹理坐标位置的橡胶板上。(比如[0,0],[1,0]和[1,1])然后将这些引脚(不带出来)移动到你想要的顶点坐标(比如[0,0],[0.5,0]和[1,1]),这样橡皮布就会拉长,图像被扭曲。这就是纹理坐标的工作原理。
If you use texture coordinates greater than 1 and your texture is set to repeat, then it's as if the rubber sheet was infinite in size and the texture was tiled across it. Therefore if your texture coordinates for two vertices were 0, 0 and 4, 0, then the image would have to be repeated 4 times between those vertices.
如果你使用的纹理坐标大于1,你的纹理就会重复,那么就好像橡胶片的大小是无限的,纹理就会被平铺在上面。因此,如果两个顶点的纹理坐标为0、0和4,0,那么图像就必须在这些顶点之间重复4次。
@b1nary.atr0phy Image for all you visual thinkers!
@b1nary。为您所有视觉思考者的图像!
#2
8
OpenGL uses inverse texturing. It takes coordinates from world space (X,Y,Z) to texture space (X,Y) to discrete space(U,V), where the discrete space is in the [0,1] domain.
OpenGL使用逆纹理。它从世界空间(X,Y,Z)到纹理空间(X,Y)到离散空间(U,V)的坐标,其中离散空间在[0,1]域中。
Take a polygon, think of it as a sheet of paper. With this:
取一个多边形,把它想象成一张纸。用这个:
glTexCoord(0,0);
glTexCoord(0,1);
glTexCoord(1,1);
glTexCoord(1,0);
You tell OpenGL to draw on the whole sheet of paper. When you apply modifications your texturing space modifies accordingly to the give coordinates. That is why for example when you divide you get the same texture twice, you tell OpenGL to map half of your sheet, instead of the whole sheet of paper.
你让OpenGL在整张纸上画画。当你应用修改的时候,你的纹理空间会相应调整到给出的坐标。这就是为什么当你分割你得到相同的纹理两次,你告诉OpenGL映射你的纸的一半,而不是整张纸。
#3
6
Chapter 9 of the Red Book explains this in detail and is available for free online.
《红皮书》第9章详细解释了这一点,并在网上免费提供。
http://www.glprogramming.com/red/chapter09.html
http://www.glprogramming.com/red/chapter09.html
Texture coordinates map x,y to the space 0-1 in width and height texture space. This is then stretched like a rubber sheet over the triangles. It is best explained with pictures and the Red Book does this.
纹理坐标映射x,y到空间0-1的宽度和高度纹理空间。然后就像橡皮布一样在三角形上展开。最好用图片和红皮书来解释。
#4
3
For 2D image textures, 0,0 in texture coordinates corresponds to the bottom left corner of the image, and 1,1 in texture coordinates corresponds to the top right corner of the image. Note that "bottom left corner of the image" is not at the center of the bottom left pixel, but at the edge of the pixel.
对于二维图像纹理,纹理坐标的0、0对应于图像的左下角,而纹理坐标中的1、1对应于图像的右上角。请注意,“图像的左下角”不是在左下角的像素中心,而是在像素的边缘。
Also interesting when uploading images:
上传图片也很有趣:
8.5.3 Texture Image Structure
8.5.3纹理图像结构
The texture image itself (referred to by data) is a sequence of groups of values. The first group is the lower left back corner of the texture image. Subsequent groups fill out rows of width width from left to right; height rows are stacked from bottom to top forming a single two-dimensional image slice; and depth slices are stacked from back to front.
纹理图像本身(由数据引用)是一系列的值。第一组是纹理图像的左下角。后续组从左到右填充行宽度;高度行从下到上叠加形成一个二维图像切片;深度片从后到前堆积。
Note that most image formats have the data start at the top, not at the bottom row.
注意,大多数图像格式都是从顶部开始的,而不是在底层。
#1
96
Texture coordinates specify the point in the texture image that will correspond to the vertex you are specifying them for. Think of a rectangular rubber sheet with your texture image printed on it, where the length of each side is normalized to the range 0-1. Now let's say you wanted to draw a triangle using that texture. You'd take 3 pins and place them in the rubber sheet in the positions of each of your desired texture coordinates. (Say [0, 0], [1, 0] and [1, 1]) then move those pins (without taking them out) to your desired vertex coordinates (Say [0, 0], [0.5, 0] and [1, 1]), so that the rubber sheet is stretched out and the image is distorted. That's basically how texture coordinates work.
纹理坐标指定纹理图像中的点,它将对应于指定它们的顶点。想象一个长方形的橡胶板,上面印着你的纹理图像,每边的长度被标准化到0-1。现在我们假设你想画一个三角形使用这个纹理。你会拿3个针,把它们放在每个你想要的纹理坐标位置的橡胶板上。(比如[0,0],[1,0]和[1,1])然后将这些引脚(不带出来)移动到你想要的顶点坐标(比如[0,0],[0.5,0]和[1,1]),这样橡皮布就会拉长,图像被扭曲。这就是纹理坐标的工作原理。
If you use texture coordinates greater than 1 and your texture is set to repeat, then it's as if the rubber sheet was infinite in size and the texture was tiled across it. Therefore if your texture coordinates for two vertices were 0, 0 and 4, 0, then the image would have to be repeated 4 times between those vertices.
如果你使用的纹理坐标大于1,你的纹理就会重复,那么就好像橡胶片的大小是无限的,纹理就会被平铺在上面。因此,如果两个顶点的纹理坐标为0、0和4,0,那么图像就必须在这些顶点之间重复4次。
@b1nary.atr0phy Image for all you visual thinkers!
@b1nary。为您所有视觉思考者的图像!
#2
8
OpenGL uses inverse texturing. It takes coordinates from world space (X,Y,Z) to texture space (X,Y) to discrete space(U,V), where the discrete space is in the [0,1] domain.
OpenGL使用逆纹理。它从世界空间(X,Y,Z)到纹理空间(X,Y)到离散空间(U,V)的坐标,其中离散空间在[0,1]域中。
Take a polygon, think of it as a sheet of paper. With this:
取一个多边形,把它想象成一张纸。用这个:
glTexCoord(0,0);
glTexCoord(0,1);
glTexCoord(1,1);
glTexCoord(1,0);
You tell OpenGL to draw on the whole sheet of paper. When you apply modifications your texturing space modifies accordingly to the give coordinates. That is why for example when you divide you get the same texture twice, you tell OpenGL to map half of your sheet, instead of the whole sheet of paper.
你让OpenGL在整张纸上画画。当你应用修改的时候,你的纹理空间会相应调整到给出的坐标。这就是为什么当你分割你得到相同的纹理两次,你告诉OpenGL映射你的纸的一半,而不是整张纸。
#3
6
Chapter 9 of the Red Book explains this in detail and is available for free online.
《红皮书》第9章详细解释了这一点,并在网上免费提供。
http://www.glprogramming.com/red/chapter09.html
http://www.glprogramming.com/red/chapter09.html
Texture coordinates map x,y to the space 0-1 in width and height texture space. This is then stretched like a rubber sheet over the triangles. It is best explained with pictures and the Red Book does this.
纹理坐标映射x,y到空间0-1的宽度和高度纹理空间。然后就像橡皮布一样在三角形上展开。最好用图片和红皮书来解释。
#4
3
For 2D image textures, 0,0 in texture coordinates corresponds to the bottom left corner of the image, and 1,1 in texture coordinates corresponds to the top right corner of the image. Note that "bottom left corner of the image" is not at the center of the bottom left pixel, but at the edge of the pixel.
对于二维图像纹理,纹理坐标的0、0对应于图像的左下角,而纹理坐标中的1、1对应于图像的右上角。请注意,“图像的左下角”不是在左下角的像素中心,而是在像素的边缘。
Also interesting when uploading images:
上传图片也很有趣:
8.5.3 Texture Image Structure
8.5.3纹理图像结构
The texture image itself (referred to by data) is a sequence of groups of values. The first group is the lower left back corner of the texture image. Subsequent groups fill out rows of width width from left to right; height rows are stacked from bottom to top forming a single two-dimensional image slice; and depth slices are stacked from back to front.
纹理图像本身(由数据引用)是一系列的值。第一组是纹理图像的左下角。后续组从左到右填充行宽度;高度行从下到上叠加形成一个二维图像切片;深度片从后到前堆积。
Note that most image formats have the data start at the top, not at the bottom row.
注意,大多数图像格式都是从顶部开始的,而不是在底层。