Appleshell
Appleshell

Reputation: 7388

glTexSubImage leads to black texture, while glTexImage works fine

When I bind my image using glTexImage2D, it renders fine.

First, the code in the fragment shader:

uniform sampler2D tex;
in vec2 tex_coord;
// main:
fragment_out = mix(
                   texture(tex, tex_coord),
                   vec4(tex_coord.x, tex_coord.y, 0.0, 1.0),
                   0.5
                   );

Using this code, it draws the image:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 
             0, GL_RGBA, GL_UNSIGNED_BYTE, image);

working fine

But if I bind it via glTexSubImage2D, the texture is drawn just black.

glTexStorage2D(GL_TEXTURE_2D,
               1,
               GL_RGBA,
               width, height);
glTexSubImage2D(GL_TEXTURE_2D,
                0,
                0, 0,
                width, height,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                image);

not working at all

When I replace GL_RGBA with GL_RGBA8 or GL_RGBA16 in the second code, the rendering is distorted.

distorted

This is the whole texture loading and binding code:

GLuint texture; 
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
unsigned char *image = SOIL_load_image(Resource::getPath("Images/2hehwdv.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGBA);
std::cout << SOIL_last_result() << std::endl;
std::cout << width << "x" << height << std::endl;
glTexStorage2D(GL_TEXTURE_2D,
               1,
               GL_RGBA,
               width, height);
glTexSubImage2D(GL_TEXTURE_2D,
                0,
                0, 0,
                width, height,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                image);

//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glGenerateMipmap(GL_TEXTURE_2D);

Can someone explain this behavior to me?

Upvotes: 2

Views: 1942

Answers (1)

genpfault
genpfault

Reputation: 52084

glTexStorage2D( GL_TEXTURE_2D, 1, GL_RGBA, width, height );
                                  ^^^^^^^ not sized

GL_RGBA is an unsized format.

glTexStorage2D()'s internalFormat parameter must be passed a sized internal format.

A glGetError() call after glTexStorage2D() would have returned GL_INVALID_ENUM:

GL_INVALID_ENUM is generated if internalformat is not a valid sized internal format.

Try a sized format from Table 1 like GL_RGBA8.

Upvotes: 3

Related Questions