2
$\begingroup$

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

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.

$\endgroup$

1 Answer 1

1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.