其他分享
首页 > 其他分享> > 纹理

纹理

作者:互联网

纹理坐标

float texCoords[] = {
		1.0f, 1.0f, //右上
		1.0f, 0.0f, //右下
		0.0f, 1.0f, //左上
		0.0f, 0.0f  //左下
	};

纹理环绕方式

把纹理坐标设置在(0,0)到(1,1)之外,会产生循环铺满

纹理过滤

邻近过滤:选择离纹理坐标最近的像素(默认)
线性过滤:线性插值

加载纹理

下载stb_image之后放到项目文件夹里,然后头文件

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

加载图片:

int width, height, nrChannels;
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);

生成纹理

纹理也是ID引用的

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

然后在顶点上面加纹理坐标,加到最后之后

glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);

在顶点shader里面,加第三个layout

#version 330 core
layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aColor;
layout (location=2) in vec2 aTexCoord;

out vec4 color;
out vec2 TexCoord;

void main()
{
	gl_Position = vec4(aPos, 1.0f);
	color = vec4(aColor, 1.0f);
	TexCoord = aTexCoord;
}

片段着色器加uniform采样器

#version 330 core
in vec4 color;
in vec2 TexCoord;
out vec4 fragColor;
uniform sampler2D ourtexture;
void main()
{
	fragColor = texture(ourtexture, TexCoord) * color;
}

注意顶点坐标一定要仔细检查对应对,要不纹理会乱

标签:layout,color,TexCoord,纹理,vec4,1.0
来源: https://www.cnblogs.com/IamIron-Man/p/16613408.html