纹理映射 - Cocos2d / OpenGL ES 1.0

时间:2012-05-15 18:57:17

标签: iphone opengl-es cocos2d-iphone

这种闪电确实影响了我的游戏性能,因为我不断添加和移除闪电,但每次灯光攻击都由3条反锯线组成:

void ccDrawSmoothLine(CGPoint pos1, CGPoint pos2, float width)
{
    GLfloat lineVertices[12], curc[4];
    GLint   ir, ig, ib, ia;
    CGPoint dir, tan;

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    //glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    pos1.x *= CC_CONTENT_SCALE_FACTOR();
    pos1.y *= CC_CONTENT_SCALE_FACTOR();
    pos2.x *= CC_CONTENT_SCALE_FACTOR();
    pos2.y *= CC_CONTENT_SCALE_FACTOR();
    width *= CC_CONTENT_SCALE_FACTOR();

    width = width*2;
    dir.x = pos2.x - pos1.x;
    dir.y = pos2.y - pos1.y;
    float len = sqrtf(dir.x*dir.x+dir.y*dir.y);
    if(len<0.00001)
        return;
    dir.x = dir.x/len;
    dir.y = dir.y/len;
    tan.x = -width*dir.y;
    tan.y = width*dir.x;

    lineVertices[0] = pos1.x + tan.x;
    lineVertices[1] = pos1.y + tan.y;
    lineVertices[2] = pos2.x + tan.x;
    lineVertices[3] = pos2.y + tan.y;
    lineVertices[4] = pos1.x;
    lineVertices[5] = pos1.y;
    lineVertices[6] = pos2.x;
    lineVertices[7] = pos2.y;
    lineVertices[8] = pos1.x - tan.x;
    lineVertices[9] = pos1.y - tan.y;
    lineVertices[10] = pos2.x - tan.x;
    lineVertices[11] = pos2.y - tan.y;

    glGetFloatv(GL_CURRENT_COLOR,curc);
    ir = 255.0*curc[0];
    ig = 255.0*curc[1];
    ib = 255.0*curc[2];
    ia = 255.0*curc[3];

    const GLubyte lineColors[] = {
        ir, ig, ib, 0,
        ir, ig, ib, 0,
        ir, ig, ib, ia,
        ir, ig, ib, ia,
        ir, ig, ib, 0,
        ir, ig, ib, 0,
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, lineVertices);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, lineColors);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
    glDisableClientState(GL_COLOR_ARRAY);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
}

我的FPS将下降到大约40,然后回击到60.我已经读过纹理映射线可以提高我的游戏性能。

我一直试图解决这个问题好几个星期,没有运气。有人可以帮我这个吗?

这是我当前的ccDrawLines和draw方法

-(void) draw
{
    numPoints_ = 0;
    glColor4ub(_color.r, _color.g, _color.b, _opacity);

    if (_opacity != 255)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    drawLightning(_strikePoint2, _strikePoint, _displacement, _minDisplacement, _seed, lightningPoints_, &numPoints_);
    ccDrawLines(lightningPoints_, numPoints_, texture);

    if (_opacity != 255)
        glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);

    glColor4f(1.0, 1.0, 1.0, 1.0);
}

void ccDrawLines( CGPoint* points, uint numberOfPoints, CCTexture2D* texture )
{
    //layout of points [0] = origin, [1] = destination and so on

    ccVertex2F vertices[numberOfPoints];
    if (CC_CONTENT_SCALE_FACTOR() != 1 )
    {
        for (int i = 0; i < numberOfPoints; i++)
        {
            vertices[i].x = points[i].x * CC_CONTENT_SCALE_FACTOR();
            vertices[i].y= points[i].y * CC_CONTENT_SCALE_FACTOR();
        }
        glVertexPointer(2, GL_FLOAT, 0, vertices);
    }
    else glVertexPointer(2, GL_FLOAT, 0, points);

    ccTex2F texCoords[numberOfPoints];

    float width = texture.pixelsWide;
    float height = texture.pixelsHigh;

    if (CC_CONTENT_SCALE_FACTOR() != 1 )
    {
        for (int i = 0; i < numberOfPoints; i++)
        {
            texCoords[i].u = (vertices[i].x * CC_CONTENT_SCALE_FACTOR()) / width;
            texCoords[i].v = (vertices[i].y * CC_CONTENT_SCALE_FACTOR()) / height;
        }
        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    }
    else glTexCoordPointer(2, GL_FLOAT, 0, points);

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, [texture name]);
    glDisableClientState(GL_COLOR_ARRAY);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, numberOfPoints);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);

    glPopMatrix();
}

纹理只是一个32x32 .png文件,带有一个小蓝点。

如果你看一下ccDrawLines,我已经将代码添加到纹理贴图中。它的问题在于,线条中的间隙,绘制了多条线条,看起来很糟糕。


修改 的 我决定不对该线进行纹理贴图并使用ccDrawSmoothLine。

我所做的只是在我的游戏玩家的init中分配闪电

lightningStrike_ = [Lightning lightningWithStrikePoint:ccp(-100, -100) strikePoint2:ccp(-100, -100)];
[self addChild:lightningStrike_ z:1];

然后,我创建了一个实例方法来设置_strikePoint和_strikePoint2属性并调用strikeRandom方法。

-(Lightning *)lightningStrike:(CGPoint)p end:(CGPoint)p2
{
    lightningStrike_.strikePoint = ccp(p.x, p.y);
    lightningStrike_.strikePoint2 = ccp(p2.x, p2.y);
    [lightningStrike_ strikeRandom];

    return lightningStrike_;
}

用法:

[self lightningStrike:ccp(100, 100) end:ccp(100, 100)];

这解决了FPS下降问题。 24小时后,我会回答并接受我自己的答案。

1 个答案:

答案 0 :(得分:0)

编辑:我决定不对该线进行纹理贴图并使用ccDrawSmoothLine。

我所做的只是在我的游戏玩家的init中分配闪电

lightningStrike_ = [Lightning lightningWithStrikePoint:ccp(-100,-100)strikePoint2:ccp(-100,-100)]; [self addChild:lightningStrike_ z:1];

然后,我创建了一个实例方法来设置_strikePoint和_strikePoint2属性并调用strikeRandom方法。

- (闪电*)lightningStrike:(CGPoint)p end:(CGPoint)p2 { lightningStrike_.strikePoint = ccp(p.x,p.y); lightningStrike_.strikePoint2 = ccp(p2.x,p2.y); [lightningStrike_ strikeRandom];

return lightningStrike_; }

用法:

[self lightningStrike:ccp(100,100)end:ccp(100,100)];

这解决了FPS下降问题。 24小时后,我会回答并接受我自己的答案。