PDA

View Full Version : OpenGL Code in Cocos2d v2.0



marciokoko
07-10-2013, 10:09 AM
Here is the code:


-(void) drawGuy {
glLineWidth(4.0f);

ccPointSize(16);

//Enable line smoothing
//glEnable(GL_LINE_SMOOTH);

//Draw a blue quadratic bezier curve
ccDrawColor4B(0, 0, 255, 255);
ccDrawQuadBezier(ccp(100,0), ccp(240,70), ccp(380,0), 10);

//Draw a hollow purple circle
ccDrawColor4B(255, 0, 255, 255);
ccDrawCircle(ccp(240,160), 125.0f, 0.0f, 100, NO);

//Draw a solid red lines
ccDrawColor4B(255, 0, 0, 255);
ccDrawLine(ccp(170,220), ccp(220,190));
ccDrawLine(ccp(260,190), ccp(310,220));

//Draw a green point
ccDrawColor4B(0, 255, 0, 255);
ccDrawPoint(ccp(200,180));
ccDrawPoint(ccp(280,180));

//Draw a turquoise solid circle
ccDrawColor4B(0, 128, 255, 50);
ccDrawSolidCircle(ccp(200,180), 25.0f, 0.0f, 20, NO);
ccDrawSolidCircle(ccp(280,180), 25.0f, 0.0f, 20, NO);

//Draw a brown hollow circle
ccDrawColor4B(64,32, 0, 255);
ccDrawCircle(ccp(200,180), 25.0f, 0.0f, 100, NO);
ccDrawCircle(ccp(280,180), 25.0f, 0.0f, 100, NO);

//Draw brown lines
ccDrawColor4B(64,32, 0, 255);
ccDrawLine(ccp(225,180), ccp(255,180));
ccDrawLine(ccp(305,180), ccp(370,160));
ccDrawLine(ccp(175,180), ccp(110,160));

//Draw an orange polygon
ccDrawColor4B(255, 128, 0, 255);
CGPoint vertices[5]={ ccp(230,150),ccp(240,160),ccp(250,150),ccp(245,140 ),ccp(235,140) };
ccDrawPoly(vertices, 5, YES);

//Draw a yellow cubic bezier curve
ccDrawColor4B(255, 255, 0, 255);
ccDrawCubicBezier(ccp(170,90), ccp(220,150), ccp(260,50), ccp(320,100), 10);

//Restore original values
glLineWidth(1);
//glDisable(GL_LINE_SMOOTH);
ccDrawColor4B(255,255,255,255);
ccPointSize(1);
}

This is what it produces: - I cant insert an image into the post :(

marciokoko
07-15-2013, 07:49 PM
Hi, I was just wondering if anybody can still help me with this post.

Thanks

carsten neumann
07-17-2013, 07:49 AM
Please have a look at the Forum Posting Guidelines for suggestions how to improve the likelihood of getting an answer. Please also consider that you are asking about a library that maybe is built on top of OpenGL (I don't know it, is it?), so a better place to ask would probably be the cocos2d mailing list. And finally, your post does not contain a description of a problem or a question. Just going by the information you provided everything is working perfectly and you just wanted to share a picture of your work ;)

marciokoko
07-17-2013, 07:55 AM
Well the problem was I posted it in a hurry because I couldnt insert the image. Then I tried to go back and edit the actual phrasing of the question and Im unable to edit it. I cant delete it to re-post, so Im kinda stuck between a rock and a hard place.

What do I do? Modify and repost? What would you suggest...thx

Khronos_webmaster
07-17-2013, 09:09 AM
What do I do? Modify and repost? What would you suggest...thx

I'd follow up on this thread with more details and answering the questions asked by carsten neumann.

marciokoko
07-17-2013, 09:21 AM
Ok originally this code was used in cocos2d v1 which used openGL 1.0. The code works fine by generating this image in openGL1.0:

1075

But when I migrate the project to cocos2d v2 which uses openGL 2.0, the resulting image is this:

1076

So I was wondering, what needed to be changed in this code to fix those rays shooting from his eyes :)


-(void) drawGuy {
glLineWidth(4.0f);

ccPointSize(16);

//Enable line smoothing
//glEnable(GL_LINE_SMOOTH);

//Draw a blue quadratic bezier curve
ccDrawColor4B(0, 0, 255, 255);
ccDrawQuadBezier(ccp(100,0), ccp(240,70), ccp(380,0), 10);

//Draw a hollow purple circle
ccDrawColor4B(255, 0, 255, 255);
ccDrawCircle(ccp(240,160), 125.0f, 0.0f, 100, NO);

//Draw a solid red lines
ccDrawColor4B(255, 0, 0, 255);
ccDrawLine(ccp(170,220), ccp(220,190));
ccDrawLine(ccp(260,190), ccp(310,220));

//Draw a green point
ccDrawColor4B(0, 255, 0, 255);
ccDrawPoint(ccp(200,180));
ccDrawPoint(ccp(280,180));

//Draw a turquoise solid circle
ccDrawColor4B(0, 128, 255, 50);
ccDrawSolidCircle(ccp(200,180), 25.0f, 0.0f, 20, NO);
ccDrawSolidCircle(ccp(280,180), 25.0f, 0.0f, 20, NO);

//Draw a brown hollow circle
ccDrawColor4B(64,32, 0, 255);
ccDrawCircle(ccp(200,180), 25.0f, 0.0f, 100, NO);
ccDrawCircle(ccp(280,180), 25.0f, 0.0f, 100, NO);

//Draw brown lines
ccDrawColor4B(64,32, 0, 255);
ccDrawLine(ccp(225,180), ccp(255,180));
ccDrawLine(ccp(305,180), ccp(370,160));
ccDrawLine(ccp(175,180), ccp(110,160));

//Draw an orange polygon
ccDrawColor4B(255, 128, 0, 255);
CGPoint vertices[5]={ ccp(230,150),ccp(240,160),ccp(250,150),ccp(245,140 ),ccp(235,140) };
ccDrawPoly(vertices, 5, YES);

//Draw a yellow cubic bezier curve
ccDrawColor4B(255, 255, 0, 255);
ccDrawCubicBezier(ccp(170,90), ccp(220,150), ccp(260,50), ccp(320,100), 10);

//Restore original values
glLineWidth(1);
//glDisable(GL_LINE_SMOOTH);
ccDrawColor4B(255,255,255,255);
ccPointSize(1);
}

Ive already changed glPointSize to ccPointSize and glColorDraw to ccDrawColor4B because these were openGL1.0 apis. Also glEnable and glDisable for line smoothing supposedly were 'deprecated' so i commented those out. As I said, I know nothing about openGL, so Im just wondering how to fix the code.

Inicidentally I just recognized it would correspond to the ccDrawPoint call, i think. So i traced it to this method in CCDrawingPrimitives:


void ccDrawPoint( CGPoint point )
{
lazy_init();

ccVertex2F p = (ccVertex2F) {point.x, point.y};

ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];

[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
[shader_ setUniformLocation:pointSizeLocation_ withF1:pointSize_];

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &p);

glDrawArrays(GL_POINTS, 0, 1);

CC_INCREMENT_GL_DRAWS(1);
}

But I would need help fixing it. XCode doesnt complain about anything

Thanks

carsten neumann
07-17-2013, 10:32 AM
Hmm, weird. Normally this sort of thing happens if you have vertices that get transformed to +/-Inf or NaN by mistakes in some calculation. As I mentioned before I don't know cocos2d, but I don't see anything that looks like a possible cause in the code you posted. Does the problem also happen if you comment all the other drawing operations except for the eyes?
Note that you still have some gl calls in there that could confuse cocos2d (for example if it internally tracks OpenGL state, then any gl call you make yourself means cocos2d does not know about the changed OpenGL state), or is this mixture of APIs a common style in cocos2d? As you can see from all my questions, you may need someone who knows the library and perhaps its implementation and I think you'd have more luck finding such a person on their mailing list than here.

marciokoko
07-17-2013, 10:51 AM
Awesome! I narrowed it down to the turquoise circles. If I comment out just the turquoise circles code, it works fine, but I dont get the turquoise circles, obviously.

So I jumped to the definition of ccDrawSolidCircle and found this xcode warning:


glVertexPointer(2, GL_FLOAT, 0, vertices);

implicit declaration of function in invalid in c99.

There is no definition for it anymore, only for glVertexAttribPointer, but I dont understand its definition:


glVertexAttribPointer (GLuint indx, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr);

Any ideas?? Thx so much for the help so far.

carsten neumann
07-17-2013, 12:06 PM
Seems to be a bug in the version of cocos2d that you have. Although if I look into the sources on github (https://github.com/cocos2d/cocos2d-iphone/blob/develop-v2/cocos2d/CCDrawingPrimitives.m) it uses glVertexAttribPointer and if I'm reading the history correctly it always has done so. Are you perhaps mixing sources from cocos2d 1.x and 2.x?

marciokoko
07-17-2013, 12:43 PM
Wait, I just read more carefully what you wrote. I know CCDrawingPrimitives uses glVertexAttribPointer. But what I meant was that I Jumped to Definition of ccDrawSolidCircle method call IN my ccDrawSolidCircle and it takes me to ExtraPrimitives.h where glVertexPointer is.

Turns out the only method inside the ExtraPrimitives.h file was that ccDrawSolidCircle. Im guessing it could be something added in by someone else.

Could you tell me what the code for drawing a solid circle in openGL 2.0 is?

carsten neumann
07-17-2013, 01:31 PM
Hmm, no idea where this ExtraPrimitives.h comes from, I don't see it in the cocos2d repository. Why don't you just use the ccDrawSolidCircle from CCDrawingPrimitives? You can look at their source code to see how to do it, I linked to it from my previous post.

marciokoko
07-17-2013, 01:59 PM
Yeah I just noticed my version of CCDrawingPrimitives doesnt have ccDrawSolidCircle. im just downloading cocos2d v2.1 which contains a CCDrawingPrimitives with ccDrawSolidCircle. Thx

Yup, it worked! I just had to comment out:


//[shader_ setUniformsForBuiltins];

because it doesnt exist in whatever version im using.

Thx