OpenGL:GLSL中的访问数组纹理 [英] OpenGL: Access Array Texture in GLSL

查看:155
本文介绍了OpenGL:GLSL中的访问数组纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题时,我试图在PyOpenGL中构造多维数据集. PyOpenGL仅支持1.10、1.20、1.30、1.00 ES和3.00 ES.我至少需要版本1.40,因为在1.40中引入了layout,并且我需要使用layout来获取纹理坐标.我正在尝试使用数组纹理对立方体的每一侧进行不同的纹理处理.这是我的一些代码片段(如果需要的话).

I was trying to texture a cube in PyOpenGL when I ran into an issue. PyOpenGL only supports 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES. I need at least version 1.40 because in 1.40 layout was introduced and I need to use layout to get the texture coordinates. I'm trying to texture each side of a cube differently using array textures. Here are some of my code snippets if they are required.

加载数组纹理:

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, width, height, layerCount)
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, 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_LINEAR)
    glTexParameteri(GL_TEXTURE_2D_ARRAY, 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);
}

如果您需要更多信息,请在评论中提问,我会添加它.

If you need any more information, just ask in the comments and I will add it.

推荐答案

PyOpenGL仅支持1.10、1.20、1.30、1.00 ES和3.00 ES [...]

PyOpenGL only supports 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES [...]

不. PyOpenGL支持从1.0到3.1的OpenGL ES版本以及从1.1到4.4的所有桌面OpenGL版本(请参阅关于PyOpenGL ) .您混淆了(桌面)OpenGL和OpneGL ES.比较 OpenGL规范-Khronos OpenGL注册表 OpenGL上下文版本仅取决于图形卡.您可以使用当前OpenGL上下文支持的任何GLSL版本. PyOpenGL保证OpenGL API的实现版本最高为4.4,但是您也可以使用更高的版本.这只是意味着PyOpenGL API可能会丢失以后添加的OpenGL函数.
创建OpenGL窗口并使上下文为当前状态后记录当前版本:

No. PyOpenGL supports OpenGL ES versions from 1.0 to 3.1 and all desktop OpenGL version from 1.1 to 4.4 (See About PyOpenGL). You confused (desktop) OpenGL and OpneGL ES. Compare OpenGL specification - Khronos OpenGL registry and OpenGL ES Specification - Khronos OpenGL ES Registry.
In any case, the "supported" version only refers to OpenGL API. The OpenGL context version only depends on the graphics card. You can use any GLSL version which is supported by the current OpenGL context. PyOpenGL guarantees, that the OpenGL API is implemented up to version 4.4, but you can also use a higher version. It just means that OpenGL functions added later may be missing from the PyOpenGL API.
Log current version after creating the OpenGL window and making the context current:

print(glGetString(GL_VENDOR))
print(glGetString(GL_RENDERER))
print(glGetString(GL_VERSION))
print(glGetString(GL_SHADING_LANGUAGE_VERSION))


顶点着色器输入布局限定符 ="https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.1.30.pdf" rel ="nofollow noreferrer"> OpenGL着色语言1.30 .您必须切换到 OpenGL阴影语言1.40 :


Vertex shader input Layout Qualifier are not supported in OpenGL Shading Language 1.30. You have to switch to OpenGL Shading Language 1.40:

#version 130

#version 140

无论如何,您在问题中提到-我至少需要1.40版,因为在1.40版中引入了[...]"

Anyway you've mentioned that in your question - "I need at least version 1.40 because in 1.40 layout was introduced [...]"

glsl采样器类型必须匹配纹理目标. (浮点)GL_TEXTURE_2D_ARRAY的正确采样器类型是sampler2Darray(请参见采样器类型):

The glsl sampler type has to match the texture target. The proper sampler type for (floating point) GL_TEXTURE_2D_ARRAY is sampler2Darray(See Sampler types):

uniform sampler2D texture;

uniform sampler2Darray texture;

对于2维数组纹理,需要3维纹理坐标(请参见

For 2 dimensional array textures, 3 dimensional texture coordinates are required (See texture):

FragColor = texture(texture, TexCoord);

FragColor = texture(texture, vec3(TexCoord.st, index));

index是数组中已寻址纹理的索引.您可以通过 Uniform 变量甚至第3个组件来提供索引在纹理坐标属性中.

index is the index of the addressed texture in the array. You can provide the index by a Uniform variable or even by a 3rd component in the texture coordinate attribute.

这篇关于OpenGL:GLSL中的访问数组纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆