當談到遊戲開發時,我完全是初學者。我現在用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];
}
這給了我這樣的:
現在到了陌生的一部分。只要我去掉第6行的ccDrawLine指令,我得到如下:
現在繪製線條,我首先想的條紋。
我搜索了幾個小時才明白這一點,但是我從我的搜索中得到的所有東西都像「我必須添加一個着色器」,我認爲我做了。
正如我所說,我完全是新的OpenGL的事情。請耐心等待我。 :-)我真的很想理解這一點。因此,如果您有針對我的問題的解決方案和/或有關此主題的好閱讀的好指示,請告訴我。
感謝和問候, 托馬斯
檢查ccDrawLine功能,看看它做什麼,你不 – LearnCocos2D
我這樣做。當我複製並粘貼到我的代碼,它仍然無法正常工作。 – thomas