2014-01-26 23 views
0

我正在學習如何使用CCRenderTexture創建動態紋理,方法如下Ray Wenderlich's教程:How To Create Dynamic Textures如何將繪圖應用到動態紋理而不是精靈?

我並不擅長這個主題,尤其是當涉及到使用OpenGL進行繪圖時。請考慮下面的代碼:

CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureWidth height:textureHeight]; 

[rt beginWithClear:c1.r g:c1.g b:c1.b a:c1.a]; 

CGPoint vertices[nStripes * 6]; 
ccColor4F colors[nStripes * 6]; 

int nVertices = 0; 
float x1 = -textureHeight; 
float x2; 
float y1 = textureHeight; 
float y2 = 0; 
float dx = textureWidth/nStripes * 2; 
float stripeWidth = dx/2; 

for (int i = 0; i < nStripes; i++) 
{ 
    x2 = x1 + textureHeight; 

    vertices[nVertices] = CGPointMake(x1, y1); 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    vertices[nVertices] = CGPointMake(x1 + stripeWidth, y1); 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    vertices[nVertices] = CGPointMake(x2, y2); 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    vertices[nVertices] = vertices[nVertices - 2]; 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    vertices[nVertices] = vertices[nVertices - 2]; 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    vertices[nVertices] = CGPointMake(x2 + stripeWidth, y2); 
    colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a}; 

    x1 += dx; 
} 

self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor]; 

CCSprite *noise = [CCSprite spriteWithFile:@"Noise.png"]; 
[noise setBlendFunc:(ccBlendFunc){GL_DST_COLOR, GL_ZERO}]; 
noise.position = ccp(textureWidth/2, textureHeight/2); 
[noise visit]; 

CC_NODE_DRAW_SETUP(); 
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); 
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors); 
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); 
glDrawArrays(GL_TRIANGLES, 0, nVertices); 

[rt end]; 

這段代碼所做的就是創建一個動態紋理,具有精靈混合,然後穿過紋理繪製線條。但我注意到只有在CCSprite *noise已被添加到繪圖中時纔可以繪製這些線條。沒有精靈,線條不可見。所以在我看來,繪製是在精靈本身上執行的,而不是在動態紋理上執行的。所以我的問題是如何在紋理本身上繪製線條(或其他)而不使用精靈?

回答

0

檢查訪問的實施。你需要設置一些東西,比如你正在繪製的着色器和變換矩陣。這可能就是爲什麼您可以先繪製精靈之後看到這些線條的原因,因爲在繪製帶有訪問調用的線條之前已經設置了這些數據。