Texture

Reference

纹理 - LearnOpenGL CN (learnopengl-cn.github.io)

练习1:

修改了片元着色器的UV。

1
2
3
4
void main()  
{
FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(1-TexCoord.r, TexCoord.g)), 0.2);
}

练习2:

修改纹理坐标和纹理环绕方式:

1
2
3
4
5
6
7
8
9
10
11
// 设置顶点数据  
float vertices[] = {
//---- 位置 ---- ---- 颜色 ---- - 纹理坐标 - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 2.0f, 2.0f, // 右上
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 2.0f, 0.0f, // 右下
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 2.0f // 左上
};
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

练习3:

GL_NEAREST(也叫邻近过滤,Nearest Neighbor Filtering)

GL_LINEAR(也叫线性过滤,(Bi)linear Filtering)

1
2
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

练习4:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
float tempMixVal = 0.5f;
while(...) {
...
if (glfwGetKey(window,GLFW_KEY_UP))
{
tempMixVal = max(tempMixVal + 0.1f, 1.0f);
}
if (glfwGetKey(window,GLFW_KEY_DOWN))
{
tempMixVal = max(tempMixVal - 0.1f, 0.0f);
}

int mixValue = glGetUniformLocation(shader->ID, "mixValue");
glUniform1f(mixValue, tempMixVal);
...
}