Monday, 26 August 2013

mix opengl with cocos2d have error

mix opengl with cocos2d have error

I create a CCNode subclass and override the draw function use my own
opengles2.0 code,but it always have a error in log output:"OpenGL error
0x0500 in -[CCSprite draw] 530",what is more, when I add the subclass
instance to the scene, other layer will not show, and the Z order in
function(addChild:child z:child.zOrder tag:child.tag) also does not work.
here is the subclass code:(I used the default cocos2d template to create a
HelloWorld project, add add a CCNode subclass called MyNode,also create
two 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
}

No comments:

Post a Comment