Hi everyone !
SFML has really helped me to write my programm, i am using it to bind a texture to OpenGL in a 3D environment using vertices arrays.
Here is my code to use my textue :
// Get a handle for our "myTextureSampler" uniform
GLuint TextureID = glGetUniformLocation(shaderProgram, "myTextureSampler");
// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
sf::Texture::bind(texture);
//sf already does this -> glBindTexture(GL_TEXTURE_2D, texture);
// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(TextureID, 0);
// 2nd attribute buffer : UVs
GLuint vertexUVID = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(vertexUVID);
glBindBuffer(GL_ARRAY_BUFFER, color_array_buffer);
glVertexAttribPointer(
vertexUVID,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
The problem is that there is some color appearing (Pink as my texture has some pink in it) but it doesn't fit with the entire texture, it seems there is only ONE pixel from the texture applied to the whole object.
Can anyone help me ?
Thanks a lot.
Sounds like your texture/UV coordinates is the problem.
Yeah it seems so, I am loading my texture like this :
texture = new sf::Texture();
if(!texture->loadFromFile("textures/simple.jpeg",sf::IntRect(0, 0, 128, 128)))
std::cout << "Error loading texture !!" << std::endl;
glGenBuffers(1, &color_array_buffer);
glBindBuffer(GL_ARRAY_BUFFER, color_array_buffer);
glBufferData(GL_ARRAY_BUFFER, vertices_size*sizeof(GLfloat), color, GL_STATIC_DRAW);
and my UV thing is this :
std::vector<GLfloat> texture_default = {
0.0f, 0.0f,
// Top-left
0.0f, 128.0f,
// Top-right
128.0f, 0.0f,
// Bottom-right
128.0f, 128.0f,
// Bottom-left
};
I cannot figure out where i am wrong, notice that i don't really care about wher is my "bottom left" i wil change that later to have a proper position of my texture .
std::vector<GLfloat> texture_default = {
0.0f, 0.0f,
// Top-left
0.0f, 128.0f,
// Top-right
128.0f, 0.0f,
// Bottom-right
128.0f, 128.0f,
// Bottom-left
};
There is your problem. UV coordinates only goes from 0.f to 1.f.
So if your image is 128 pixels width and you want the full width, you don't type 128.f, but 1.f :)
If you only want the half of 128 pixels (64 pixels) then your need to put in 0.5f.