一、目的
1、二维纹理映射学习,画一个使用多个纹理单元混合的立方体;
二、程序运行结果
三、使用多个纹理单元
一个纹理单元能支持多个纹理绑定到不同的目标,一个程序中也可以使用多个纹理单元加载多个2D纹理。
使用多个纹理单元的代码如下:
// 使用0号纹理单元
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texid1)
glUniform1i(glGetUniformLocation(shader.programId, "tex1"), 0)
// 使用1号纹理单元
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, texid2)
glUniform1i(glGetUniformLocation(shader.programId, "tex2"), 1)
在着色器中,对两个纹理的颜色进行混合:
in vec3 v_color;
in vec2 v_texture;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform float mixValue;
out vec4 out_color;
void main()
{
vec4 color1 = texture(tex1, v_texture);
vec4 color2 = texture(tex2, vec2(v_texture.s, 1.0 - v_texture.t));
out_color = mix(color1, color2, mixValue);
}
其中mix函数完成颜色插值,函数原型为:
API genType mix( genType x,genType y,genType a);
最终值得计算方法为:x×(1−a)+y×a。
mixValue通过程序传递,可以通过键盘上的A和S键,调整纹理混合值,改变混合效果。
四、图像倒立的原因及解决办法
画面中图像是倒立的,主要原因是加载图片时,图片的(0,0)位置一般在左上角,而OpenGL纹理坐标的(0,0)在左下角,这样y轴顺序相反。有的图片加载库提供了相应的选项用来翻转y轴,有的没有这个选项。我们可以修改顶点数据中的纹理坐标来达到目的,或者对于我们这里的简单情况使用如下代码实现y轴的翻转:
vec4 color2 = texture(tex2, vec2(v_texture.s, 1.0 - v_texture.t));
五、glTexImage2D函数
glTexImage2D函数定义纹理图像的格式,宽度和高度等信息,具体参数如下
1、函数原型:
GL_APICALL void GL_APIENTRY glTexImage2D(GLenum target, GLint level, GLenum internalformat,GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* pixels);
2、参数说明:
- target 指定目标纹理,这个值必须是GL_TEXTURE_2D。
- level 执行细节级别。0是最基本的图像级别,n表示第N级贴图细化级别。
- internalformat 指定纹理中的颜色组件。可选的值有GL_ALPHA,GL_RGB,GL_RGBA,GL_LUMINANCE, GL_LUMINANCE_ALPHA 等几种。
- width 指定纹理图像的宽度,必须是2的n次方。纹理图片至少要支持64个材质元素的宽度
- height 指定纹理图像的高度,必须是2的m次方。纹理图片至少要支持64个材质元素的高度
- border 指定边框的宽度。必须为0。
- format 像素数据的颜色格式, 不需要和internalformatt取值必须相同。可选的值参考internalformat。
- type 指定像素数据的数据类型。可以使用的值有GL_UNSIGNED_BYTE,GL_UNSIGNED_SHORT_5_6_5,GL_UNSIGNED_SHORT_4_4_4_4,GL_UNSIGNED_SHORT_5_5_5_1。
- pixels 指定内存中指向图像数据的指针
六、源代码
"""
程序名称:GL_textures03.py
编程: dalong10
功能: 使用多个纹理单元的实现
参考资料: https://blog.csdn.net/wangdingqiaoit/article/details/51457675
"""
import myGL_Funcs #实用程序 myGL_Funcs.py (见https://www.jianshu.com/p/49dec482a291)
import glfw
from OpenGL.GL import *
from OpenGL.GL.shaders import compileProgram, compileShader
import numpy as np
import pyrr
from PIL import Image
vertex_src = """
# version 330
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_color;
layout(location = 2) in vec2 a_texture;
uniform mat4 rotation;
out vec3 v_color;
out vec2 v_texture;
void main()
{
gl_Position = rotation * vec4(a_position, 1.0);
v_color = a_color;
v_texture = a_texture;
//v_texture = 1 - a_texture; // Flips the texture vertically and horizontally
//v_texture = vec2(a_texture.s, 1 - a_texture.t); // Flips the texture vertically
}
"""
fragment_src = """
# version 330
in vec3 v_color;
in vec2 v_texture;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform float mixValue;
out vec4 out_color;
void main()
{
vec4 color1 = texture(tex1, v_texture);
vec4 color2 = texture(tex2, vec2(v_texture.s, 1.0 - v_texture.t));
out_color = mix(color1, color2, mixValue);
}
"""
def on_key(window, key, scancode, action, mods):
global mixValue
# 键盘控制, A -混合系数减; S -混合系数增
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
glfw.set_window_should_close(window,1)
elif key == glfw.KEY_S and action == glfw.PRESS:
mixValue += 0.05
if (mixValue >1.0):
mixValue =1.0
elif key == glfw.KEY_A and action == glfw.PRESS:
mixValue -= 0.05
if (mixValue <0.0):
mixValue =0.0
# glfw callback functions
def window_resize(window, width, height):
glViewport(0, 0, width, height)
# initializing glfw library
if not glfw.init():
raise Exception("glfw can not be initialized!")
# creating the window
window = glfw.create_window(400, 400, "My OpenGL window", None, None)
# check if window was created
if not window:
glfw.terminate()
raise Exception("glfw window can not be created!")
# set window's position
glfw.set_window_pos(window, 100, 100)
# set the callback function for window resize
glfw.set_window_size_callback(window, window_resize)
# Install a key handler
glfw.set_key_callback(window, on_key)
# make the context current
glfw.make_context_current(window)
vertices = [-0.5, -0.5, 0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
-0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0]
indices = [0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20]
vertices = np.array(vertices, dtype=np.float32)
indices = np.array(indices, dtype=np.uint32)
shader = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER), compileShader(fragment_src, GL_FRAGMENT_SHADER))
# Step2: 创建并绑定VBO 对象 传送数据
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
# Step3: 创建并绑定EBO 对象 传送数据
EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
# Step4: 指定解析方式 并启用顶点属性
# 顶点位置属性
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertices.itemsize * 8, ctypes.c_void_p(0))
# 顶点颜色属性
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertices.itemsize * 8, ctypes.c_void_p(12))
# 顶点纹理属性
glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertices.itemsize * 8, ctypes.c_void_p(24))
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
texid1 = myGL_Funcs.loadTexture("metal.png")
texid2 = myGL_Funcs.loadTexture("wall.png")
glUseProgram(shader)
glClearColor(0, 0.1, 0.1, 1)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
model = pyrr.matrix44.create_from_translation(pyrr.Vector3([0.0, 0.0, 0]))
rotation_loc = glGetUniformLocation(shader, "rotation")
mixValue = 0.4
# the main application loop
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
#// 启用多个纹理单元 绑定纹理对象
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texid1);
glUniform1i(glGetUniformLocation(shader, "tex1"), 0); #// 设置纹理单元为0号
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texid2);
glUniform1i(glGetUniformLocation(shader, "tex2"), 1); #// 设置纹理单元为1号
glUniform1f(glGetUniformLocation(shader, "mixValue"), mixValue); #// 设置纹理混合参数
# 绘制第一个矩形
glUniformMatrix4fv(rotation_loc, 1, GL_FALSE, model)
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)
glfw.swap_buffers(window)
# terminate glfw, free up allocated resources
glfw.terminate()
七、参考资料
1、大龙10的简书:https://www.jianshu.com/p/49dec482a291
2、The fool的博客:https://blog.csdn.net/wangdingqiaoit/article/details/51457675