我创建了一个CCNode子类并使用我自己的opengles2.0代码覆盖了draw函数,但它在日志输出中总是有错误:“OpenGL error 0x0500 in -[CCSprite draw] 530”,更重要的是,当我添加子类时实例到场景,其他图层不会显示,函数中的Z顺序(addChild:child z:child.zOrder tag:child.tag)也不起作用。这是子类代码:(我使用默认的cocos2d模板创建了一个HelloWorld项目,添加了一个名为MyNode的CCNode子类,同时创建了两个shader)
//shaderf.glsl fragmentShader
precision mediump float;
void main()
{
gl_FragColor=vec4(0.0,0.0,1.0,1.0);
}
//shaderv.glsl //vertexShader
attribute vec4 vPosition;
void main(void)
{
gl_Position=vPosition;
}
MyNode.h
@interface MyNode : CCNode {
GLuint _programeHandle;
GLuint _positionSlot;
}
@end
MyNode.m
@implementation MyNode
- (id) init {
self = [super init];
if (self) {
}
return self;
}
-(void)DrawOpengl
{
NSString * vertexShaderPath=[[NSBundle mainBundle] pathForResource:@"shaderv" ofType:@"glsl"];
NSString * fragmentShaderPath=[[NSBundle mainBundle]pathForResource:@"shaderf" ofType:@"glsl"];
GLuint vertexShader=[ZZZGLESUtils loadShader:GL_VERTEX_SHADER withFilePath:vertexShaderPath];
GLuint fragmentShader=[ZZZGLESUtils loadShader:GL_FRAGMENT_SHADER withFilePath:fragmentShaderPath];
_programeHandle=glCreateProgram();
if (!_programeHandle) {
NSLog(@"failed to created programe");
return;
}
glAttachShader(_programeHandle, vertexShader);
glAttachShader(_programeHandle, fragmentShader);
glLinkProgram(_programeHandle);
GLint linked;
glGetProgramiv(_programeHandle, GL_LINK_STATUS, &linked);
if (!linked) {
GLint infolen=0;
glGetProgramiv(_programeHandle, GL_INFO_LOG_LENGTH, &infolen);
if(infolen>1)
{
char * infomsg=malloc(sizeof(char)*infolen);
glGetProgramInfoLog(_programeHandle, infolen, NULL, infomsg);
NSLog(@"Link programe error is: \n%s\n",infomsg);
free(infomsg);
}
glDeleteProgram(_programeHandle);
_programeHandle=0;
return;
}
glUseProgram(_programeHandle);
_positionSlot=glGetAttribLocation(_programeHandle, "vPosition");
}
-(void)DrawTriCone
{
float victex[]={ 0.5,0.5,0.0,
0.5,-0.5,0.0,
-0.5,-0.5,0.0,
-0.5,0.5,0.0,
0.0,0.0,-0.7,};
GLubyte indicx[]={
0,1,1,2,2,3,3,0,
4,0,4,1,4,2,4,3,
};
glEnableVertexAttribArray(kCCVertexAttribFlag_Position);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, victex);
glEnableVertexAttribArray(_positionSlot);
glDrawElements(GL_LINES, sizeof(indicx)/sizeof(GLubyte), GL_UNSIGNED_BYTE, indicx);
glDisableVertexAttribArray(_positionSlot);
}
- (void) draw {
glDisable(GL_TEXTURE_2D);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );
[self DrawOpengl];
CGSize size = [[CCDirector sharedDirector] winSize];
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0,size.width/2,size.height/2);
[self DrawTriCone];
glDisableVertexAttribArray(kCCVertexAttribFlag_Position);
glEnable(GL_TEXTURE_2D);
}
@end
below is the code in HelloWorldLayer:
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
// ask director for the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild:label];
CCSprite *testsprite=[CCSprite spriteWithFile:@"grass.png" rect:CGRectMake(0, 0, 600, 600)];
[self addChild:testsprite];
MyNode *myNode =[MyNode node];
myNode.position=ccp(size.width /2 , size.height/2);
[self addChild:myNode];
}
return self
}