1

This question is related to Repeating OpenGL-es texture bound to hills in cocos2d 2.0

After reading the answers posted in the above post, I've used the following code for computing the vertices and texture coordinates:

    CGPoint pt0,pt1;

    float ymid = (p0.y + p1.y) / 2;
    float ampl = (p0.y - p1.y) / 2;
    pt0 = p0;
    float U_Off = floor(pt0.x / 512);

    for (int j=1; j<_segments+1; j++)
    {
        pt1.x = p0.x + j*_dx;
        pt1.y = ymid + ampl * cosf(_da*j);
        float xTex0 = pt0.x/512 - U_Off;

        _vertices[vertices++]=CGPointMake(pt0.x, 0);
        _vertices[vertices++]=CGPointMake(pt0.x, pt0.y);

        _texCoords[texCoords++]=CGPointMake(xTex0, 1.0f);
        _texCoords[texCoords++]=CGPointMake(xTex0, 0);

        pt0 = pt1;
    }
    p0 = p1;

But unfortunately, I still get a tear / misalignment in my texture (circled in yellow):

Screenshot

I've attached dumps of the arrays of vertices and texcoords I'm new to OpenGl, and can't figure out where the miscalculation is. How do I prevent the line (circled in yellow in image) from appearing ?

EDIT: My texture is either 1024x512 or 512x512 depending on the device. I use the following texture parameters:

ccTexParams tp2 = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_CLAMP_TO_EDGE};
Community
  • 1
  • 1
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

1 Answers1

0

Most likely the reason is in non-continuous texture coordinates. In texcoords dump you have the following coordinates:

(CGPoint) 0x34b0b28 = (x=1.00390625, y=0)
(CGPoint) 0x34b0b30 = (x=0.005859375, y=1)

It means that between these two points texture is mapped from 1 to 0 (in reverse direction). You should continue texcoords after 1.00390625 => 1.005859375 => ... Also, your texture must have power-of-two size and must be set up with REPEAT mode.

If your texture is in atlas and you cannot set REPEAT mode, you may try to clamp texcoords to [0; 1] range and place two edge points with x=1 and x=0 in the same position.

And, at last, if your texture doesn't change in x-axis you may set x = 0.5 for all points.

brigadir
  • 6,874
  • 6
  • 46
  • 81
  • ES 2.0 supports non-power of two textures as long as mipmapping is not enabled. Many implementations also have the necessary extension of full NPOT support. – Reto Koradi May 13 '14 at 15:06
  • 1
    Reto Koradi, there is support of NPOT textures only for `GL_CLAMP_TO_EDGE` wrapping mode (internally NPOT texture is filled into POT memory area, AFAIK). – brigadir May 13 '14 at 15:21
  • Good point, I had forgotten about the limits on wrap modes for NPOT textures. I assume that most modern GPUs support NPOT textures natively. But the ES 2.0 specs were probably written to make life easier for ones that don't. ES 3.0 lifts these restrictions on NPOT textures. – Reto Koradi May 13 '14 at 15:37