Opengl es 在调用ccDrawLine之前,glDrawArray不会绘制任何内容
说到游戏开发,我完全是个新手。我用Cocos2D迈出了第一步,现在我有一个奇怪的问题,我就是不明白 我尝试创建CCRenderTexture,以便为滚动背景创建动态纹理。现在,它应该只渲染一个黄色背景和一些黑色条纹 为此,我写了以下内容:Opengl es 在调用ccDrawLine之前,glDrawArray不会绘制任何内容,opengl-es,cocos2d-iphone,Opengl Es,Cocos2d Iphone,说到游戏开发,我完全是个新手。我用Cocos2D迈出了第一步,现在我有一个奇怪的问题,我就是不明白 我尝试创建CCRenderTexture,以便为滚动背景创建动态纹理。现在,它应该只渲染一个黄色背景和一些黑色条纹 为此,我写了以下内容: - (CCSprite *)debugTextureWidth:(float)textureWidth textureHeight:(float)textureHeight { // Create a texture and set the backgr
- (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];
}
-(CCSprite*)调试纹理宽度:(浮点)纹理宽度纹理高度:(浮点)纹理高度{
//创建纹理并将背景色设置为黄色
CCRenderTexture*debugTexture=[CCRenderTexture RenderTextWithWidth:textureWidth height:textureHeight];
[debugTexture beginWithClear:1.0f g:204.0f/255.0f b:0a:1.0f];
//ccp抽绳(ccp(0,0),ccp(200.0f,200.0f));
//画一些斜线
int nStripes=((arc4random()%4)+1)*2;
CGPoint顶点[n条*6];
ccColor4F颜色[nStripes*6];
int-nVertices=0;
浮点x1=-纹理高度;
浮动x2;
浮动y1=纹理高度;
浮动y2=0;
float dx=纹理宽度/n条纹*2;
浮带宽度=dx/2;
ccColor4F stripeColor=(ccColor4F){1.0f,0,0,1.0f};
对于(int i=0;i
这给了我这样的东西:
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Color);
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);
CC_NODE_DRAW_SETUP();
现在是奇怪的部分。一旦我在第6行取消对ccDrawLine指令的注释,就会得到以下结果:
现在它画出了我首先想要的线条和条纹
为了理解这一点,我搜索了几个小时,但我从搜索中得到的只是类似“你必须添加一个着色器”的东西,我想我是这样做的
正如我所说的,我对OpenGL完全是新手。请对我耐心点。:-)我真的很想了解这一点。因此,如果你对我的问题有一个解决方案和/或一个很好的指针,在哪里可以找到关于这个主题的好读物,请让我知道
感谢并问候,
托马斯我发现了这个问题。这是一个简单的打字错误 每个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配合使用。:-)
感谢并问候,
Thomas检查ccDrawLine功能,看看它能做什么而你没有做什么。当我复制并粘贴到代码中时,它仍然不起作用。