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.