在调用ccDrawLine之前,glDrawArray不会绘制任何内容

时间:2013-08-03 07:51:03

标签: opengl-es cocos2d-iphone

在游戏开发方面,我是一个初学者。我现在用Cocos2D迈出了第一步,我有一个奇怪的问题,我只是不明白。

我尝试创建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事物都是全新的。请耐心等待我。 :-)我真的想了解这一点。因此,如果你有一个解决我的问题的方法和/或一个好的指针,可以在哪里寻找关于这个主题的好读物,请告诉我。

谢谢和问候, 托马斯

1 个答案:

答案 0 :(得分:0)

我发现了问题。这是一个简单的错字。

每个sé的代码没有任何问题。但我只是使用了错误的调用来设置着色器。

这部分错了:

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。 : - )

谢谢和问候, 托马斯

相关问题