سؤال

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};
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top