2012-05-15 169 views
-1

這閃電真的影響了我的比賽中的表現,因爲我不斷地添加和刪除的閃電,也是每個雷擊是使用由3條反鋸齒線:紋理映射 - cocos2d的/的OpenGL ES 1.0

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並繪製方法

-(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(); 
} 

質地只是一個小藍點32×32的PNG文件。

如果你看看ccDrawLines我已經添加了紋理貼圖代碼行。問題在於,線條上的空白,多條線條被畫出來,看起來很可怕。


編輯: 我決定不紋理貼圖行和使用ccDrawSmoothLine。

我所做的只是在我gamelayer的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小時後,我會回答並接受我自己的答案。

+0

由於您提供了太多東西供人們梳理,因此您的問題被降低了投票率。編輯你的問題,並重寫它只提供相關的部分,也許一些截圖更好地說明問題,你可能會得到更多的支持。 – Ani

+0

謝謝您的評論!我已經解決了我的問題。 – Jonathan

回答

0

編輯:我決定不紋理貼圖行並使用ccDrawSmoothLine。

我所做的只是在我gamelayer的init分配閃電

lightningStrike_ = [閃電lightningWithStrikePoint:CCP(-100,-100)strikePoint2:CCP(-100,-100)]; [self addChild:lightningStrike_ z:1];

然後,我創建了一個實例方法來設置_strikePoint和_strikePoint2屬性並調用strikeRandom方法。 (閃電*)lightningStrike:(CGPoint)p結束:(CGPoint)p2 { lightningStrike_.strikePoint = ccp(p.x,p.y); lightningStrike_.strikePoint2 = ccp(p2.x,p2。Y); [lightningStrike_ strikeRandom];

return lightningStrike_; }

用法:

[自lightningStrike:CCP(100,100)結束:CCP(100,100)];

這固定了FPS的下降。 24小時後,我會回答並接受我自己的答案。