2013-08-03 94 views
0

當談到遊戲開發時,我完全是初學者。我現在用Cocos2D開始我的第一步,我有一個奇怪的問題,我只是不明白。在調用ccDrawLine之前,glDrawArray不會繪製任何東西

我嘗試創建CCRenderTexture以創建滾動背景的動態紋理。現在應該只渲染一個帶有黑色條紋的黃色背景。

所以我寫了下面來實現這一目標:

- (CCSprite *)debugTextureWidth:(float)textureWidth textureHeight:(float)textureHeight { 

    // Create a texture and set the background color to yellow 
    CCRenderTexture *debugTexture = [CCRenderTexture renderTextureWithWidth:textureWidth height:textureHeight]; 
    [debugTexture beginWithClear:1.0f g:204.0f /255.0f b:0 a:1.0f]; 

    // ccDrawLine(ccp(0, 0), ccp(200.0f, 200.0f)); 

    // Draw some diagonal stripes 
    int nStripes = ((arc4random() %4) +1) *2; 
    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; 

    ccColor4F stripeColor = (ccColor4F){1.0f, 0, 0, 1.0f}; 

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

    vertices[nVertices] = CGPointMake(x1, y1); 
    colors[nVertices++] = stripeColor; 

    vertices[nVertices] = CGPointMake(x1 +stripeWidth, y1); 
    colors[nVertices++] = stripeColor; 

    vertices[nVertices] = CGPointMake(x2, y2); 
    colors[nVertices++] = stripeColor; 

    vertices[nVertices] = vertices[nVertices -2]; 
    colors[nVertices++] = stripeColor; 

    vertices[nVertices] = vertices[nVertices -2]; 
    colors[nVertices++] = stripeColor; 

    vertices[nVertices] = CGPointMake(x2 +stripeWidth, y2); 
    colors[nVertices++] = stripeColor; 
    x1 += dx; 
    } 

    [self setShaderProgram:[[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor]]; 

    glEnableVertexAttribArray(kCCVertexAttribFlag_Color); 
    glEnableVertexAttribArray(kCCVertexAttrib_Color); 

    [self.shaderProgram use]; 
    [self.shaderProgram setUniformsForBuiltins]; 

    // CC_NODE_DRAW_SETUP(); 

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); 
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors); 
    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices); 

    [debugTexture end]; 

    return [CCSprite spriteWithTexture:debugTexture.sprite.texture]; 
} 

這給了我這樣的:

CCRenderTexture fail

現在到了陌生的一部分。只要我去掉第6行的ccDrawLine指令,我得到如下:

CCRenderTexture half win

現在繪製線條,我首先想的條紋。

我搜索了幾個小時才明白這一點,但是我從我的搜索中得到的所有東西都像「我必須添加一個着色器」,我認爲我做了。

正如我所說,我完全是新的OpenGL的事情。請耐心等待我。 :-)我真的很想理解這一點。因此,如果您有針對我的問題的解決方案和/或有關此主題的好閱讀的好指示,請告訴我。

感謝和問候, 托馬斯

+0

檢查ccDrawLine功能,看看它做什麼,你不 – LearnCocos2D

+0

我這樣做。當我複製並粘貼到我的代碼,它仍然無法正常工作。 – thomas

回答

0

我發現這個問題。這是一個簡單的錯字。

每個人的代碼沒有問題。但我只是使用錯誤的調用來設置着色器。

這部分是錯誤的:

所有我叫了錯誤的參數錯誤的方法
glEnableVertexAttribArray(kCCVertexAttribFlag_Color); 
glEnableVertexAttribArray(kCCVertexAttrib_Color); 

[self.shaderProgram use]; 
[self.shaderProgram setUniformsForBuiltins]; 

// CC_NODE_DRAW_SETUP(); 

第一。我猜自動完成在這裏騙了我。正確的調用必須如下所示:

ccGLEnableVertexAttribs(kCCVertexAttribFlag_Color); 
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position); 

CC_NODE_DRAW_SETUP(); 

其餘代碼保持不變。

說實話,我真的不明白我在這裏做什麼。所以,我仍然喜歡OpenGLES 2.0的教程。理想情況下如何與cocos2d-iphone一起使用。 :-)

感謝和問候, 托馬斯

相關問題