c++openglglslshaderrender-to-texture

Texture does not fill the figure - OpenGL


I'm trying to display an image on the whole window. For that, I only draw a rectangle which take the whole window and apply it a texture.

The problem is that the image does not fill the whole screen at all: Output image

The image isn't too small since it is 5600 x 3000. Also, I don't want the image to be repeat. I don't what i've missed, why it does't fill the screen and why it's only on the top left corner. I already try this solution and it didn't work: Texture does not fit in the square - OpenGL

Here's how I create and bind my shaders:

Shader::Shader(const char* vertSrcFile, const char* fragSrcFile, const char* texSrcFile) {
    GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

    GLint vlen;
    GLint flen;
    char* source = loadFile(vertSrcFile, vlen);
    char* fragment = loadFile(fragSrcFile, flen);

    glShaderSource(vertShader, 1, &source, &vlen);
    glShaderSource(fragShader, 1, &fragment, &flen);

    GLint compiled = 0;
    glCompileShader(vertShader);
    glGetShaderiv(vertShader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        GLint maxLength = 0;
        glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetShaderInfoLog(vertShader, maxLength, &maxLength, &infoLog[0]);

        // We don't need the shader anymore.
        glDeleteShader(vertShader);

        Logger::GetSystemLogger()->error("Vertex shader compilation error");
        Logger::GetSystemLogger()->error("{}", infoLog.data());

        return;
    }

    glCompileShader(fragShader);
    glGetShaderiv(fragShader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        GLint maxLength = 0;
        glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetShaderInfoLog(fragShader, maxLength, &maxLength, &infoLog[0]);

        // We don't need the shader anymore.
        glDeleteShader(fragShader);

        Logger::GetSystemLogger()->error("Fragment shader compilation error");
        Logger::GetSystemLogger()->error("{}", infoLog.data());

        return;
    }

    prog = glCreateProgram();

    glAttachShader(prog, vertShader);
    glAttachShader(prog, fragShader);

    glLinkProgram(prog);
    GLint isLinked = 0;
    glGetProgramiv(prog, GL_LINK_STATUS, (int*)&isLinked);
    if (isLinked == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &maxLength);

        // The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetProgramInfoLog(prog, maxLength, &maxLength, &infoLog[0]);

        // We don't need the program anymore.
        glDeleteProgram(prog);
        // Don't leak shaders either.
        glDeleteShader(vertShader);
        glDeleteShader(fragShader);

        Logger::GetSystemLogger()->error("Program link error");
        Logger::GetSystemLogger()->error("{}", infoLog.data());
        return;
    }

    glDetachShader(prog, vertShader);
    glDetachShader(prog, fragShader);

    glGenTextures(1, &texID); // generate texture ID

    glBindTexture(GL_TEXTURE_2D, texID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

    LoadTexture(texSrcFile);
}

Here's how I load my image as a texture:

void Shader::LoadTexture(const char* texSrcFile) const {
        int width, height, nrChannels;
        unsigned char* data = stbi_load(texSrcFile, &width, &height, &nrChannels, 0);
        if (data)
        {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        }
        else
        {
            Logger::GetSystemLogger()->error("Failed to load textures");
        }
        stbi_image_free(data);
    }

Here's my Shaders: Fragment:

#version 330
precision highp float; // needed only for version 1.30

layout(location = 0) out vec4 out_Color;

in vec2 ex_Tex;

uniform sampler2D textureUnit0;

void main(void){
    out_Color = texture(textureUnit0, ex_Tex);
}

vertex:

#version 330

layout (location = 0) in vec3 in_Position;
layout (location = 1) in vec2 in_Tex;

out vec2 ex_Tex;

void main(void){
    gl_Position = vec4(in_Position, 1.0);
    
    ex_Tex = in_Tex;
}

Here's the main code:

glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);

glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);

GLfloat vertices[4 * 5]{
      // Positions           // Textures
    -1.0f, -1.0f, 0.0f,      0.0f, 0.0f,
     1.0f, 1.0f, 0.0f,       1.0f, 1.0f,
    -1.0f, 1.0f, 0.0f,       0.0f, 1.0f,
     1.0f, -1.0f, 0.0f,      1.0f, 0.0f
};

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// Position
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);

// Texture coord
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);

glGenBuffers(1, &m_IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);

unsigned int indices[6] = { 0, 1, 2, 0, 1, 3 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

m_Shader = std::make_shared<Engine::Shader>("src/Shaders/Background.vert", "src/Shaders/Background.frag", "res/background.jpg");

And here's the main loop

while (m_Running) {
    // clear the screen and set the background color to grey
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    m_Shader->Bind();

    glBindTexture(GL_TEXTURE_2D, m_Shader->GetTexID());
    glBindVertexArray(m_VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

    m_Window->OnUpdate();
}

Solution

  • Though the viewport coordinates go from negative one to one [-1, 1), texture coordinates go from zero to one [0,1). Therefore three quarters of your viewport are filled with black according to the GL_CLAMP_TO_BORDER policy.

    You need to fix your VAO coordinates as follows:

    GLfloat vertices[4 * 5]{
          // Positions           // Textures
        -1.0f, -1.0f, 0.0f,     0.0f, 0.0f,
         1.0f, 1.0f, 0.0f,      1.0f, 1.0f,
        -1.0f, 1.0f, 0.0f,      0.0f, 1.0f,
         1.0f, -1.0f, 0.0f,     1.0f, 0.0f
    };
    

    EDIT: Also your texture coordinates offset is incorrect. It should be:

    // Texture coord
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
    

    Without the proper offset it is using the X and Y of the position for the texture coordinates.