问题描述
遇到问题时,我试图在PyOpenGL中构造多维数据集。 PyOpenGL仅支持1.10、1.20、1.30、1.00 ES和3.00 ES。我至少需要版本1.40,因为在1.40中引入了layout
,并且我需要使用layout
来获取纹理坐标。我正在尝试使用数组纹理对立方体的每一侧进行不同的纹理处理。这是我的一些代码片段(如果需要的话)。
加载数组纹理:
def load_texture_array(path,width,height):
teximg = pygame.image.load(path)
texels = teximg.get_buffer().raw
texture = gluint(0)
layerCount = 6
mipLevelCount = 1
glGenTextures(1,texture)
glBindTexture(GL_TEXTURE_2D_ARRAY,texture)
glTexStorage3D(GL_TEXTURE_2D_ARRAY,mipLevelCount,GL_RGBA8,height,layerCount)
glTexSubImage3D(GL_TEXTURE_2D_ARRAY,layerCount,GL_RGBA,GL_UNSIGNED_BYTE,texels)
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE)
我的顶点着色器:
#version 130
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 color;
out vec2 TexCoord;
void main() {
gl_Position = vec4(aPos,1.0);
color = aColor;
TexCoord = aTexCoord;
}
我的片段着色器:
#version 130
out vec4 FragColor;
in vec3 color;
in vec2 TexCoord;
uniform sampler2D texture;
void main()
{
FragColor = texture(texture,TexCoord);
}
解决方法
PyOpenGL仅支持1.10、1.20、1.30、1.00 ES和3.00 ES [...]
不。 PyOpenGL支持1.0到3.1的OpenGL ES版本以及1.1到4.4的所有桌面OpenGL版本(请参见About PyOpenGL)。您混淆了(桌面)OpenGL和OpneGL ES。比较OpenGL specification - Khronos OpenGL registry和OpenGL ES Specification - Khronos OpenGL ES Registry。
无论如何,“受支持”版本仅指OpenGL API。 OpenGL context版本仅取决于图形卡。您可以使用当前OpenGL上下文支持的任何GLSL版本。 PyOpenGL保证OpenGL API的实现版本最高为4.4,但是您也可以使用更高的版本。这只是意味着PyOpenGL API可能会丢失以后添加的OpenGL函数。
创建OpenGL窗口并使上下文为当前状态后记录当前版本:
print(glGetString(GL_VENDOR))
print(glGetString(GL_RENDERER))
print(glGetString(GL_VERSION))
print(glGetString(GL_SHADING_LANGUAGE_VERSION))
Layout Qualifier不支持顶点着色器输入OpenGL Shading Language 1.30。您必须切换到OpenGL Shading Language 1.40:
#version 130
#version 140
无论如何,您在问题中提到-“我至少需要1.40版本,因为在1.40版中引入了[...]”
glsl采样器类型必须匹配纹理目标。 (浮点数)GL_TEXTURE_2D_ARRAY
的正确采样器类型为sampler2Darray
(请参见Sampler types):
uniform sampler2D texture;
uniform sampler2Darray texture;
对于2维数组纹理,需要3维纹理坐标(请参见texture
):
FragColor = texture(texture,TexCoord);
FragColor = texture(texture,vec3(TexCoord.st,index));
index
是数组中已寻址纹理的索引。您可以通过Uniform变量或纹理坐标属性中的第三个组件来提供索引。