[Help] Opengl 2d texture array is not displayed correctly, what is wrong ?

Started by
4 comments, last by Tom Sloper 4 years, 5 months ago

While studying opengl I managed to draw a square on the screen. Now I have a 2D texture array. and I want to display it on the screen. This is one of the images for example.

vertex.shader

#version 430 core
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 texCoord;

out vec2 uv;
void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
    uv = texCoord;
}

fragment.shader

#version 430 core
out vec4 color;

in vec2 uv;

layout (binding=0) uniform sampler2DArray textureArray;
layout (location=1) uniform int layer;

void main()
{
    color = texture(textureArray, vec3(uv.x,uv.y, layer));
}

I compile them without errors. Then I draw two triangles, a square. Then I make the index buffer. At this point I got a black square on the screen. Then I create a texture.

    // Generate an array texture
    let mut texture = 0;
    gl::GenTextures(1, &mut texture);
    gl::ActiveTexture(gl::TEXTURE0);
    gl::BindTexture(gl::TEXTURE_2D_ARRAY, texture);

    // Create storage for the texture. (1 layer of 150x150 texels)
    gl::TexStorage3D(
        gl::TEXTURE_2D_ARRAY,
        1,        // no mipmaps
        gl::RGB8, // internal format
        150, // width
        150, // height
        1,   // Number of layers
    );
    // Add texture
    let img = image::open(&Path::new("./xx.png")).expect("Failed to load texture");
    let data = img.raw_pixels();
    gl::TexSubImage3D(
        gl::TEXTURE_2D_ARRAY,
        0, // Mipmap number
        0, // xoffset
        0, // yoffset
        0, // zoffset
        150, // width
        150, // height
        1, // depth
        gl::RGBA, // format
        gl::UNSIGNED_BYTE, // type
        &data[0] as *const u8 as *const c_void, // pointer to data
    );
    
    gl::GenerateMipmap(gl::TEXTURE_2D_ARRAY);

    gl::TexParameteri(
        gl::TEXTURE_2D_ARRAY,
        gl::TEXTURE_MIN_FILTER,
        gl::LINEAR as i32,
    );
    gl::TexParameteri(
        gl::TEXTURE_2D_ARRAY,
        gl::TEXTURE_MAG_FILTER,
        gl::LINEAR as i32,
    );
    gl::TexParameteri(
        gl::TEXTURE_2D_ARRAY,
        gl::TEXTURE_WRAP_S,
        gl::CLAMP_TO_EDGE as i32,
    );
    gl::TexParameteri(
        gl::TEXTURE_2D_ARRAY,
        gl::TEXTURE_WRAP_T,
        gl::CLAMP_TO_EDGE as i32,
    );

And then.

// ..
    gl::ClearColor(0.2, 0.3, 0.3, 1.0);
    gl::Clear(gl::COLOR_BUFFER_BIT);

    gl::UseProgram(shaderProgram);
    gl::Uniform1i(1, 0); //Sampler refers to texture unit 0

    gl::BindVertexArray(VAO);

    gl::DrawElements(gl::TRIANGLE_FAN, 200, gl::UNSIGNED_INT, ptr::null());
// ..

And this is what I got

What should I fix. I googled but couldn’t find so many results about a 2D texture array ?

The coordinates of the texture

[
[0.0, 1.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0]
]
Advertisement

Solved https://www.reddit.com/r/rustgamedev/comments/e1w352/helpopengl_2d_texture_array_is_not_displayed/

btw., a bedug context or questioning for errors would have told that ...

I must correct, in results in a segmentation fault, probably because more data is written than has memory been asked for.

Strange that it runs at all ... maybe undefined behaviour behind the scenes ?

This is not a Writing question. Moving to an appropriate forum.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement