Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: OpenGL Code in Cocos2d v2.0

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12

    Arrow OpenGL Code in Cocos2d v2.0

    Here is the code:

    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

  2. #2
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12
    Hi, I was just wondering if anybody can still help me with this post.

    Thanks

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    788
    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

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12
    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

  5. #5
    Administrator Regular Contributor Khronos_webmaster's Avatar
    Join Date
    Apr 2007
    Location
    Montreal
    Posts
    151
    Quote Originally Posted by marciokoko View Post
    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.
    Webmaster Khronos.org and OpenGL.org

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12

    Talking

    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:

    Click image for larger version. 

Name:	iOS Simulator Screen shot Jul 17, 2013 10.14.11 AM.jpg 
Views:	78 
Size:	5.7 KB 
ID:	1075

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

    Click image for larger version. 

Name:	iOS Simulator Screen shot Jul 17, 2013 10.17.27 AM.jpg 
Views:	81 
Size:	11.3 KB 
ID:	1076

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

    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);
    }

    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:

    Code :
    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
    Last edited by marciokoko; 07-17-2013 at 09:31 AM.

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    788
    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.

  8. #8
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12
    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.

  9. #9
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    788
    Seems to be a bug in the version of cocos2d that you have. Although if I look into the sources on github 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?

  10. #10
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    12
    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?
    Last edited by marciokoko; 07-17-2013 at 01:06 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •