所以我一直在单独跟随learnopengl.com书并且一直无法让纹理单元工作。起初我在主循环中调用glGetUniform1i,就像他在书中所做的那样,但是建议(通过#learnprogramming IRC)将任何纹理操作限制在主循环之前。使用本书的代码时,问题是纹理不会像片段着色器中那样正确混合,只会渲染绑定到GL_TEXTURE_2D的第二个纹理(在本例中为“faceTexture”)。翻转渲染顺序可以渲染出先前使用的纹理。
但是,在主循环之前放置纹理单元代码会导致glUniform1i调用上的无效操作:
File "C:/Users/1117254/Documents/Python Experiments/OpenGL/OpenGL Testing/main.py", line 171, in main
glUniform1i(wood_texture_loc, 0)
File "C:\Users\1117254\AppData\Local\Programs\Python\Python36\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__
return self( *args, **named )
File "errorchecker.pyx", line 53, in
OpenGL_accelerate.errorchecker._ErrorChecker.glCheckError (src\errorchecker.c:1218)
OpenGL.error.GLError: GLError(
err = 1282,
description = b'invalid operation',
baseOperation = glUniform1i,
cArguments = (23724032, 0)
)
我的错误似乎与frag着色器中统一变量的位置有关,但我不确定glGetUniformLocation给出的位置(似乎是23724032)是否有效。
以下相关来源以及顶点和片段着色器:
# Shader
shader = shader_loader.Shader("vertex.vs", "fragment.fs")
# Vertex Buffering
vao = GLuint(0)
vbo = GLuint(0)
ebo = GLuint(0)
glGenVertexArrays(1, vao)
glGenBuffers(1, vbo)
glGenBuffers(1, ebo)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, 4 * len(vertices), vertices, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * len(indices), indices, GL_STATIC_DRAW)
# Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * 4, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# Color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * 4, ctypes.c_void_p(3 * 4))
glEnableVertexAttribArray(1)
# Texture attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * 4, ctypes.c_void_p(6 * 4))
glEnableVertexAttribArray(2)
# Unbinding buffers
glBindVertexArray(0)
# Texturing
wood = Image.open("resources/wood.jpg")
wood.load()
face = Image.open("resources/awesomeface.jpg")
face.load()
# face.transpose(Image.FLIP_LEFT_RIGHT)
wood_texture_loc = glGetUniformLocation(shader.shader_program, "woodTexture")
face_texture_loc = glGetUniformLocation(shader.shader_program, "faceTexture")
wood_data = list(wood.getdata())
wood_data = numpy.array(wood_data, dtype=numpy.uint8)
face_data = list(face.getdata())
face_data = numpy.array(face_data, dtype=numpy.uint8)
# Wood texture
texture1 = GLuint(0)
glGenTextures(1, texture1)
glBindTexture(GL_TEXTURE_2D, texture1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
wood.size[0], wood.size[1],
0,
GL_RGB,
GL_UNSIGNED_BYTE,
wood_data)
glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
# Face texture
texture2 = GLuint(0)
glGenTextures(1, texture2)
glBindTexture(GL_TEXTURE_2D, texture2)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
face.size[0], face.size[1],
0,
GL_RGB,
GL_UNSIGNED_BYTE,
face_data)
glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture1)
glUniform1i(wood_texture_loc, 0)
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, texture2)
glUniform1i(face_texture_loc, 1)
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
glClearColor(0.4667, 0.7373, 1., 1.0)
glClear(GL_COLOR_BUFFER_BIT)
shader.use()
glBindVertexArray(vao)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
glBindVertexArray(0)
pygame.display.flip()
clock.tick(fps)
顶点着色器:
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoords;
out vec3 ourColor;
out vec2 texCoordsOut;
void main()
{
gl_Position = vec4(position, 1.0f);
ourColor = color;
texCoordsOut = vec2(texCoords);
}
Fragment Shader:
#version 330
in vec3 ourColor;
in vec2 texCoordsOut;
out vec4 color;
uniform sampler2D woodTexture;
uniform sampler2D faceTexture;
void main()
{
color = mix(texture(woodTexture, texCoordsOut), texture(faceTexture,
texCoordsOut), 0.2);
}
非常感谢任何见解。
答案 0 :(得分:5)
glUniform*
不会修改程序对象。因此,它修改了当前使用的程序。由于当前没有使用任何程序,因此会出现错误。
在调用此函数之前使用该程序。