Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Texture apply in a 3D environment with SFML  (Read 2511 times)

0 Members and 1 Guest are viewing this topic.

Sarcarx

  • Guest
Texture apply in a 3D environment with SFML
« on: June 24, 2014, 12:00:53 pm »
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.

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Texture apply in a 3D environment with SFML
« Reply #1 on: June 24, 2014, 12:04:44 pm »
Sounds like your texture/UV coordinates is the problem.

Sarcarx

  • Guest
Re: Texture apply in a 3D environment with SFML
« Reply #2 on: June 24, 2014, 12:09:58 pm »
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 .

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Texture apply in a 3D environment with SFML
« Reply #3 on: June 24, 2014, 12:14:52 pm »
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.
« Last Edit: June 24, 2014, 12:16:27 pm by Peteck »

Sarcarx

  • Guest
Re: Texture apply in a 3D environment with SFML
« Reply #4 on: June 24, 2014, 12:16:48 pm »
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 then you don't type 128.f, but 1.f :)

If you only want the half of 128 pixels then your need to put in 0.5f.

Ho Damn ! thanks a lot ! I was confused about this, but when a texture is repeated how do you configure your vertices according to this then ? Does it depend on the size of the face from vertices ?

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Texture apply in a 3D environment with SFML
« Reply #5 on: June 24, 2014, 12:22:27 pm »
Hmm, I was wrong when telling you that UV coordinates only goes from 0.f to 1.f.
But if you write 128.f then the texture will actually repeat 128 times - only if the texture settings is set the right way. But as I remember the sf::Texture has these paremeters set the right way for that :)

Sarcarx

  • Guest
Re: Texture apply in a 3D environment with SFML
« Reply #6 on: June 24, 2014, 12:34:11 pm »
Hmm, I was wrong when telling you that UV coordinates only goes from 0.f to 1.f.
But if you write 128.f then the texture will actually repeat 128 times - only if the texture settings is set the right way. But as I remember the sf::Texture has these paremeters set the right way for that :)

ok I get it, but i just have another small issue, my object is drawn with Triangles and my texture is applying itself the wrong you if you see what i mean, if i put a picture i will see twice the same side on both triangles of a same face instead of the full pictures :/

Actually i still have this small problem if i want to put a picture, the top of the picture is in the middle of my object ...
« Last Edit: June 24, 2014, 12:41:17 pm by Sarcarx »

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Texture apply in a 3D environment with SFML
« Reply #7 on: June 24, 2014, 12:59:36 pm »
ok I get it, but i just have another small issue, my object is drawn with Triangles and my texture is applying itself the wrong you if you see what i mean, if i put a picture i will see twice the same side on both triangles of a same face instead of the full pictures :/

Actually i still have this small problem if i want to put a picture, the top of the picture is in the middle of my object ...
Could you provide me a screenshot explaining your problem?