0

I have tried to draw a part of a picture, but instead repeating the part of the picture, it has stretched the picture and it looked ugly.

How can I repeat picture instead of streching it.

The relevant part of the code:

glEnable (GL_BLEND);
glColor4f(1.0f,1.0f,1.0f,1.0f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1.f, 1.f, 1.f);
glBindTexture(GL_TEXTURE_2D, texture[image]);
int repeat = (x2-x)/73;
glBegin(GL_QUADS); //create polygon
glTexCoord2f((part/3.f), 0.0f);glVertex2d(x, y);
glTexCoord2f(((part+1.f)/3.f)+repeat, 0.0f);glVertex2d(x2, y);
glTexCoord2f(((part+1.f)/3.f)+repeat, 1.0f);glVertex2d(x2, y2);
glTexCoord2f((part/3.f), 1.0f);glVertex2d(x, y2);
glEnd();

and the load of the texture:

texture[fNum] = SOIL_load_OGL_texture
            (
            fName.c_str(),
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y 
            );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

This is the fixed code after your help, but now there is another problem, I want to repeat only a specific part of the image. Thank a lot!

fixed: now I maked my own code to repeat that and it works:

float nx=x, nx2 = x+length;
float ny2 = y2+25.f;
if((x2-x)/length > 1)
{
    while(nx2 < x2)
    {
        if(nx2+length < x2)
        {
            glBegin(GL_QUADS); //create polygon
            glTexCoord2f((part/3.f), 0.0f);glVertex2d(nx, y);
            glTexCoord2f(((part+1.f)/3.f), 0.0f);glVertex2d(nx2, y);
            glTexCoord2f(((part+1.f)/3.f), 1.0f);glVertex2d(nx2, ny2);
            glTexCoord2f((part/3.f), 1.0f);glVertex2d(nx, ny2);
            glEnd();
            nx = nx2;
            nx2 += length;
        }
        else
        {
            glBegin(GL_QUADS); //create polygon
            glTexCoord2f((part/3.f), 0.0f);glVertex2d(nx, y);
            glTexCoord2f(((part+1.f)/3.f), 0.0f);glVertex2d(x2, y);
            glTexCoord2f(((part+1.f)/3.f), 1.0f);glVertex2d(x2, ny2);
            glTexCoord2f((part/3.f), 1.0f);glVertex2d(nx, ny2);
            glEnd();
            break;
        }
    }
}
else
{
    glBegin(GL_QUADS); //create polygon
    glTexCoord2f((part/3.f), 0.0f);glVertex2d(x, y);
    glTexCoord2f(((part+1.f)/3.f), 0.0f);glVertex2d(x2, y);
    glTexCoord2f(((part+1.f)/3.f), 1.0f);glVertex2d(x2, ny2);
    glTexCoord2f((part/3.f), 1.0f);glVertex2d(x, ny2);
    glEnd();
}
7
  • 2
    Please show the relevant parts of code you used.
    – Mat
    Feb 18, 2012 at 9:03
  • alright, I added it now.
    – menta man
    Feb 18, 2012 at 9:06
  • The code you posted doesn't repeat anything, it's just one quad.
    – Mat
    Feb 18, 2012 at 9:09
  • 2
    Then you should have posted the code that doesn't work.
    – Mat
    Feb 18, 2012 at 9:13
  • 1
    Who cares if it's silly code. If someone needs it to give you a decent answer, then post it. Otherwise they can just guess.
    – Bart
    Feb 18, 2012 at 10:00

2 Answers 2

6

when mapping texture using glTexCoord2f(x, y) use x > 1.0 to repeat it in x direction and same for y. If you use glTexCoord2f(2.5, 3.0); then image will be repeated 2 times completely plus half of its width in x-direction and 3 times in y-direction.

1
  • It drew some part of it transparent.
    – menta man
    Feb 18, 2012 at 9:16
2

You need texture coordinates greater than 1.0 (2.0 means twice, 3.0 means thrice, etc...) and you need to set the specific states as well:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2
  • Actualy, there is another problem now, it maked a border, it wasn't there before.. and I want the repeat only about specific part.
    – menta man
    Feb 18, 2012 at 10:14
  • Can you describe the border or upload a screenshot ?
    – HardCoder
    Feb 18, 2012 at 10:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.