OpenGL: Access Array Texture in GLSL
Asked Answered
K

1

0

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.

Loading the array texture:

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)

My vertex shader:

#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;
}

my fragment shader:

#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.

Kilimanjaro answered 18/9, 2020 at 17:16 Comment(0)
P
1

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

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))

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

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


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;

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 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.

Proprietary answered 18/9, 2020 at 17:30 Comment(2)
@Kilimanjaro OpenGL with PyOpenGLProprietary
Let us continue this discussion in chat.Kilimanjaro

© 2022 - 2024 — McMap. All rights reserved.