0
\$\begingroup\$

I'm trying to load a texture loaded with libPNG on a XY coordinates. I know the width and the height of the texture (32x32).

The texture is already loaded successfully and I have his Id.

Here is my function to print any (32x32) texture on a XY position :

void printTexture(GLuint idTexture, GLfloat x, GLfloat y) {
    glActiveTexture(GL_TEXTURE0);
    glBindVertexArray(this->VAO[0]);
    glUniform1i(this->mTagShaderHandle, 0); //to execute a particular code on the Pixel Shader
    glUniform1i(this->mTextShaderHandle, 0); //the uniform Sampler2D from my Fragment Shader bound to 0.

    GLfloat xpos = x;
    GLfloat ypos = y;

    GLfloat w = 32;
    GLfloat h = 32;

    GLfloat vertices[6][4] = {
            { xpos,     ypos + h,       0.0, 0.0 },
            { xpos,     ypos,           0.0, 1.0 },
            { xpos + w, ypos,           1.0, 1.0 },

            { xpos,     ypos + h,       0.0, 0.0 },
            { xpos + w, ypos,           1.0, 1.0 },
            { xpos + w, ypos + h,       1.0, 0.0 }          
    };

    glBindTexture(GL_TEXTURE_2D, idTexture);
    checkGlError("glBindBuffer");

    glBufferSubData(GL_ARRAY_BUFFER, GL_ZERO, sizeof(vertices), vertices);
    glDrawArrays(GL_TRIANGLE_STRIP, GL_ZERO, 6);
    glBindVertexArray(GL_ZERO);
    glBindTexture(GL_TEXTURE_2D, GL_ZERO);
}

Here is how I initialize my VAO and VBO :

glBindVertexArray(this->VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, GL_ZERO, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(GL_ZERO);
glVertexAttribPointer(GL_ZERO, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), GL_ZERO);
glBindVertexArray(GL_ZERO);

Here is how I set up my texture:

glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, temp_width, temp_height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, image_data); 
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_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

And my Vertex Shader :

const char           gVertexShader[] =
        "#version 320 es\n"
        "layout (location = 0) in vec4 vertex;\n"
        "out vec2 TexCoords;\n"
        "uniform mat4 projection;\n"
        "uniform int tag;"
        "void main() {\n"
                "  gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
        "  if (tag > 0) {\n"
        "      TexCoords = vertex.zw;\n"
        "  } else {\n"
        "      TexCoords = vertex.xy;\n" //here is the code that should be executed by the pixel shader program because Tag = 0.
        "  }\n"
        "}\n";

The texture is not displaying at all. I tried a lot of things but nothing change. I should keep this pixel shader because I have some functions to print texts so that's why I have the if/else statement on my Pixel Shader code.

What's wrong with my code ?

Thanks a lot for help.

\$\endgroup\$
3
  • \$\begingroup\$ Might not be the only issue, but when defining your texture object make sure to explicitly set the wrapping states, as the defaults may not work out for your use case. \$\endgroup\$ May 23, 2018 at 12:26
  • \$\begingroup\$ glTexImage2D(GL_TEXTURE_2D, GL_ZERO, GL_RGB, temp_width, temp_height, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, image_data); 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_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); \$\endgroup\$ May 23, 2018 at 12:38
  • \$\begingroup\$ Can you show us your fragment shader too? Also, disable backface culling and depth test, to rule out those issues. \$\endgroup\$
    – Bram
    Sep 5, 2018 at 16:06

1 Answer 1

0
\$\begingroup\$

The line of the vertex shader that you've highlighted is setting the texture coordinates to be the same as the vertex coordinates. So if you're drawing a quad at (96,96) to (128, 128), then your texture coordinates will be (96, 96) to (128, 128). But since you're using the GL_TEXTURE_2D target, the texture coordinates should be normalized. Otherwise, you'll be seeing just the edge color of the texture repeated across the quad. That probably not what you want. You probably do want to use 0-1 as the texture coords in both x and y directions.

\$\endgroup\$
5
  • \$\begingroup\$ I'm not sure to understand what do you mean. Should I set a new size for my VBO and modify my vertices then ? \$\endgroup\$ May 24, 2018 at 11:10
  • \$\begingroup\$ I think the issue is just that you've got tag set to 0 when the vertex shader runs. It should be set to some non-zero value to get the proper texture coordinates. \$\endgroup\$ May 24, 2018 at 14:37
  • \$\begingroup\$ Either that, or use GL_TEXTURE_RECTANGLE instead of GL_TEXTURE_2D, and make sure your fragment program matches. \$\endgroup\$ May 24, 2018 at 14:38
  • \$\begingroup\$ Please could you check this link, there is my whole code and let me know if you find something I did wrong or some ameliorations. Thanks. gist.github.com/AhmedX6/c18a753ea1d0ab09c03238c4d6395c3a \$\endgroup\$ May 25, 2018 at 8:41
  • \$\begingroup\$ @X6Entrepreneur , if it is a private repository, we don't have access. \$\endgroup\$
    – gotanod
    Jul 21 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .