《Cocos2d-x for iPhone游戏开发实例详解---1.5 绘制OpenGL图元》

《Cocos2d-x for iPhone游戏开发实例详解》系列博文,基于 人民邮电出版社 《Cocos2d for iPhone游戏开发实例详解》一书,使用Cocosd-x技术重写。示例代码中所引用的相关资料归于原书作者 Nathan Burba 以及出版社所有,本系列博文提供的C++版代码仅供学习使用。

======================================================================================

开发环境:

  Cocos2d-x  cocos2d-2.1rc0-x-2.1.2-hotfix.zip @ Apr.08, 2013

  MacBook Pro 13'  Mountain Lion 10.8.3

  Xcode 4.6

  iPhone5   IOS 6.1.4

======================================================================================


本章 代码相对于中文翻译版以及英文原版有很大出入,以英文原版Cocos2d.for.iPhone.1.Game.Development.Cookbook Coder Source 为基准修改。

本示例展示使用Cocos2d-x 自带的OpenGL绘制图形方法。


1.修改Cocs2d-x 框架,增加绘制实心圆的方法,

1.1修改 CCDrawingPrimitives.h 源文件 基于

void CC_DLL ccDrawCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter);

添加以下方法:

/**
 draws a solid Circle bt 2013-08-17
 **/
void CC_DLL ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter);

1.2 修改  CCDrawingPrimitives.cpp 源文件 基于

void ccDrawCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
{
    lazy_init();

    int additionalSegment = 1;
    if (drawLineToCenter)
        additionalSegment++;

    const float coef = 2.0f * (float)M_PI/segments;

    GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
    if( ! vertices )
        return;

    for(unsigned int i = 0;i <= segments; i++) {
        float rads = i*coef;
        GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
        GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;

        vertices[i*2] = j;
        vertices[i*2+1] = k;
    }
    vertices[(segments+1)*2] = center.x;
    vertices[(segments+1)*2+1] = center.y;

    s_pShader->use();
    s_pShader->setUniformsForBuiltins();
    s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1);

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segments+additionalSegment);

    free( vertices );

    CC_INCREMENT_GL_DRAWS(1);
}

void CC_DLL ccDrawCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
{
    ccDrawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
}
添加以下方法:

/**
**Draw Solid Circle
**/
void ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
{
    lazy_init();
    
    int additionalSegment = 1;
    if (drawLineToCenter)
        additionalSegment++;
    
    const float coef = 2.0f * (float)M_PI/segments;
    
    GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
    if( ! vertices )
        return;
    
    for(unsigned int i = 0;i <= segments; i++) {
        float rads = i*coef;
        GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
        GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
        
        vertices[i*2] = j;
        vertices[i*2+1] = k;
    }
    vertices[(segments+1)*2] = center.x;
    vertices[(segments+1)*2+1] = center.y;
    
    s_pShader->use();
    s_pShader->setUniformsForBuiltins();
    s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1);
    
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+additionalSegment);
    
    free( vertices );
    
    CC_INCREMENT_GL_DRAWS(1);
}
void CC_DLL ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
{
    ccDrawSolidCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
}

1.3 绘制实心圆秘诀: “GL_LINE_STRIP”替换成“GL_TRIANGLE_FAN”



2 贴关键代码

//****************************************************************************************************
//            Author:          Last Ranker
//            DateTime:        2013年08月18日
//            SearchMe:        http://blog.csdn.net/lastranker
//            Email:           tubufeng@foxmail.com
//
//****************************************************************************************************
//
//  OpenGLPrimitives.h
//  Chapter_1
//
//  Created by Last Ranker on 13-8-18.
//
//

#ifndef __Chapter_1__OpenGLPrimitives__
#define __Chapter_1__OpenGLPrimitives__

#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class OpenGLPrimitives:public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene * scene();
    CREATE_FUNC(OpenGLPrimitives);
    virtual void draw();
   
};
#endif /* defined(__Chapter_1__OpenGLPrimitives__) */

//****************************************************************************************************
//            Author:          Last Ranker
//            DateTime:        2013年08月18日
//            SearchMe:        http://blog.csdn.net/lastranker
//            Email:           tubufeng@foxmail.com
//
//****************************************************************************************************
//
//  OpenGLPrimitives.cpp
//  Chapter_1
//
//  Created by Last Ranker on 13-8-18.
//
//

#include "OpenGLPrimitives.h"
CCScene * OpenGLPrimitives::scene()
{
    CCScene *scene=CCScene::create();
    OpenGLPrimitives *layer=OpenGLPrimitives::create();
    scene->addChild(layer);
    return scene;
    
}

bool OpenGLPrimitives::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    draw();
    
    return true;
}
void OpenGLPrimitives::draw()
{
 
    //Set line width.
	glLineWidth(4.0f);
	
	//Set point size
	ccPointSize(16);
    
	//Draw a blue quadratic bezier curve
    ccDrawColor4B(0, 0, 255, 128);
    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, false);
   
	//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, false);
    ccDrawSolidCircle(ccp(280, 180), 25.0f, 0.0f, 20, false);

    
	//Draw a brown hollow circle
    ccDrawColor4B(64, 32, 0, 255);
	ccDrawCircle(ccp(200,180), 25.0f, 0.0f, 100, false);
	ccDrawCircle(ccp(280,180), 25.0f, 0.0f, 100, false);
	
	//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);
    CCPoint vertices[5]={ ccp(230,150),ccp(240,160),ccp(250,150),ccp(245,140),ccp(235,140) };
	ccDrawPoly(vertices, 5, false);
    
	//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);
    ccDrawColor4B(255, 255, 255, 255);
    ccPointSize(1);



}



源代码:http://pan.baidu.com/share/link?shareid=339406256&uk=1962963338

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值