<p>After a lot of research, i finally found the problem. glTexImage2D expects the data to be padded. I had to use <code>glPixelStorei(GL_UNPACK_ALIGNMENT, 1)</code>
to ask opengl to not interpret any padding</p>

CC BY-SA 4.0
2022-12-28T13:23:06.427Z
2022-12-28T13:23:06.427Z
ihsan
<p>This code displays a white texture always except when the dimensions are 383 x 512</p>
<pre><code>    img.size.height = 512;
    img.size.width = 383;
    img.data = new uint8_t[img.size.width * img.size.height*3];
    memset(img.data, 'a', img.size.width * img.size.height*3);
    if (!glfwInit())
    {
        error(&quot;Failed to initialize glfw\n&quot;);
    }

    GLFWwindow* window = glfwCreateWindow(983, 720, &quot;Hello&quot;, NULL, NULL);
    screenRatio = 983.0 / 720.0;
    textureRatio = (float)img.size.width / (float)img.size.height;
    calculate_quad_dimensions();
    if (!window)
        error(&quot;Failed to create GLFWwindow&quot;);
    

    glfwMakeContextCurrent(window);
    int err = glewInit();
    if (err != GLEW_OK)
    {

        error(&quot;Failed to initialize GLEW.&quot;);
    }

    GLCall(glClearColor(0.20, 0.20, 0.20, 1));
    glEnable(GL_TEXTURE_2D);

    glfwSetWindowSizeCallback(window, size_callback);
    glfwSetKeyCallback(window, key_callback);
    glfwSwapInterval(1);

    GLuint texture;
    GLCall(glGenTextures(1, &amp;texture));
    GLCall(glBindTexture(GL_TEXTURE_2D, texture));

    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));

    GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, img.size.width, img.size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data));
    glGenerateMipmap(GL_TEXTURE_2D);
    delete[] img.data;


    while (!glfwWindowShouldClose(window))
    {

            glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glTexCoord2f(offsetx + 0.0, offsety + 0.0); glVertex2f(-quadWidth, -quadHeight); // Lower left
    glTexCoord2f(offsetx + 1, offsety + 0.0);  glVertex2f(quadWidth, -quadHeight);  // Lower right
    glTexCoord2f(offsetx + 1, offsety + 1);     glVertex2f(quadWidth, quadHeight);   // Upper right
    glTexCoord2f(offsetx + 0.0, offsety + 1);  glVertex2f(-quadWidth, quadHeight);  // upper left
    glEnd();



    glfwSwapBuffers(window);
    glfwPollEvents();
    }
    
    glfwDestroyWindow(window);
    glfwTerminate();
</code></pre>
<p>when the dimensions are 383 x 512, the image has glitched random pixels at the top row:<a href="https://i.sstatic.net/TJd8U.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TJd8U.png" alt="enter image description here" /></a></p>
<p>any other dimension i tried, the image is pure white. This is a problem because im making a photo viewer app and the thumbnail is the dimension 383 x 512, and the garbage pixels at the top row are pushing the image down skewing it.</p>

<opengl><texture>
Opengl Texture top row pixels are garbage
ihsan
# Opengl Texture top row pixels are garbage

(Asked by: username_0 on 2022-12-21)

This code displays a white texture always except when the dimensions are 383 x 512

```
    img.size.height = 512;
    img.size.width = 383;
    img.data = new uint8_t[img.size.width * img.size.height*3];
    memset(img.data, 'a', img.size.width * img.size.height*3);
    if (!glfwInit())
    {
        error("Failed to initialize glfw\n");
    }

    GLFWwindow* window = glfwCreateWindow(983, 720, "Hello", NULL, NULL);
    screenRatio = 983.0 / 720.0;
    textureRatio = (float)img.size.width / (float)img.size.height;
    calculate_quad_dimensions();
    if (!window)
        error("Failed to create GLFWwindow");
    

    glfwMakeContextCurrent(window);
    int err = glewInit();
    if (err != GLEW_OK)
    {

        error("Failed to initialize GLEW.");
    }

    GLCall(glClearColor(0.20, 0.20, 0.20, 1));
    glEnable(GL_TEXTURE_2D);

    glfwSetWindowSizeCallback(window, size_callback);
    glfwSetKeyCallback(window, key_callback);
    glfwSwapInterval(1);

    GLuint texture;
    GLCall(glGenTextures(1, &texture));
    GLCall(glBindTexture(GL_TEXTURE_2D, texture));

    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
    GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));

    GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, img.size.width, img.size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data));
    glGenerateMipmap(GL_TEXTURE_2D);
    delete[] img.data;

    while (!glfwWindowShouldClose(window))
    {

            glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_QUADS);
    glTexCoord2f(offsetx + 0.0, offsety + 0.0); glVertex2f(-quadWidth, -quadHeight); // Lower left
    glTexCoord2f(offsetx + 1, offsety + 0.0);  glVertex2f(quadWidth, -quadHeight);  // Lower right
    glTexCoord2f(offsetx + 1, offsety + 1);     glVertex2f(quadWidth, quadHeight);   // Upper right
    glTexCoord2f(offsetx + 0.0, offsety + 1);  glVertex2f(-quadWidth, quadHeight);  // upper left
    glEnd();

    glfwSwapBuffers(window);
    glfwPollEvents();
    }
    
    glfwDestroyWindow(window);
    glfwTerminate();

```

when the dimensions are 383 x 512, the image has glitched random pixels at the top row:[![enter image description here](https://i.sstatic.net/TJd8U.png)](https://i.sstatic.net/TJd8U.png)

any other dimension i tried, the image is pure white. This is a problem because im making a photo viewer app and the thumbnail is the dimension 383 x 512, and the garbage pixels at the top row are pushing the image down skewing it.

**Tags:** opengl, texture


---

## 1 answers

### Answer by username_0 on 2022-12-28. Score: 1 | Accepted answer

After a lot of research, i finally found the problem. glTexImage2D expects the data to be padded. I had to use `glPixelStorei(GL_UNPACK_ALIGNMENT, 1)`
to ask opengl to not interpret any padding


---

