3D ToolKit for cocos2dx 是一个简单的cocos2d-x 3D扩展,可以方便地在cocos2dx环境中创建3D图形。
基于cocos2d-x 2.2开发。
已实现功能:
1,向量和矩阵运算。
2,扩展的3D相机。(支持 透视投影 和 正交投影)
3,3D Sprite。(支持空间变换,更换mesh,更换shader)
4,扩展的shader program和uniform回调。
资料:http://www.oschina.net/p/3d-toolkit-for-cocos2d-x
源码:https://github.com/wantnon2/3DToolKit-for-cocos2dx
环境准备:
cocos2dx-2.2.2,eclipse-android开发环境(含ndk)
我之前已经配好了cocos2dx2.2.2的环境,我一般从vs编译好(可以跳过此步骤),按照源码提供的教程,配好win32环境,通过了再进行配置android的。在vs新建toolkit3dtest项目,右键工程添加现有项,将工具箱HelloCPP的源文件导入,
测试通过,然后开始配置Android
1、创建新工程,或者利用工具箱的HelloCPP改也可以,这里我新建了:
(要编译Android,需要安装CDT,eclipse中Help->install new software 搜索CDT安装,并安装android Sdk。)
利用D:\AndroidDev\cocos2d-x-2.2.2\tools\project-creator\create_project.py创建好一个新项目HelloTool,打开eclipse,从HelloTool导入现有android工程,只需要打勾选择proj.android就可以了,如图:
如果提示Cocos2dxActivity cannot be resolved to a type错误,
2、完成之后,右键工程-properties,新建一个Builder,
点击new之后,在弹出框的Main选项卡的Location选择android-ndk的ndk-build.cmd路径,工作目录要选择HelloTool
然后点击Environment选项卡,添加NDK_MODULE_PATH变量:
Name是:NDK_MODULE_PATH
Value是:D:\AndroidDev\cocos2d-x-2.2.2;D:\AndroidDev\cocos2d-x-2.2.2\cocos2dx\platform\third_party\android\prebuilt
然后设置 build Options ;勾上HelloTool 的jni文件夹
最后如下图,关闭该窗口即可,
3、加入源码:
在HelloTool工程里Classes文件夹加入3d工具箱HelloCPP自带的源码文件:
//demo name
{
CCLabelTTF* pLabel = CCLabelTTF::create("3D ToolKit for cocos2d-x - Test 1", "Arial", 45);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);
}
// author info
{
CCLabelTTF* pLabel = CCLabelTTF::create("by Yang Chao (wantnon) 2014-1-5", "Arial", 25);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height-60));
this->addChild(pLabel, 1);
}
这外面的大括号必须去掉,然后修改同名变量。
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#define C3DFINAL //vs2010 not support final key word, so just ignor
#else
#define C3DFINAL final
#endif
#define USED_FOR_SUBMIT_TO_VBO C3DFINAL
4、拷贝资源
将HelloCPP自带的Resource文件夹覆盖掉HelloTool的Resource文件夹,并且在HelloTool的proj.android中新建文件夹assets,将Resource中内容全部拷入。(同级:如Resource\1.jpg,那么assets\1.jpg)。
5、配置Android.mk
在LOCAL_SRC_FILES后加入新添加的文件:整个变量如下:
LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp \
../../Classes/scene3DLayer.cpp \
../../Classes/c3dToolKit/c3dCamera.cpp \
../../Classes/c3dToolKit/c3dCommonFunc.cpp \
../../Classes/c3dToolKit/c3dDefaultMeshes.cpp \
../../Classes/c3dToolKit/c3dDefaultPassUniformCallback.cpp \
../../Classes/c3dToolKit/c3dDefaultPrograms.cpp \
../../Classes/c3dToolKit/c3dDefine.cpp \
../../Classes/c3dToolKit/c3dGLMath.cpp \
../../Classes/c3dToolKit/c3dIDTriangle.cpp \
../../Classes/c3dToolKit/c3dIndexVBO3d.cpp \
../../Classes/c3dToolKit/c3dLightSource.cpp \
../../Classes/c3dToolKit/c3dLog.cpp \
../../Classes/c3dToolKit/c3dMaterial.cpp \
../../Classes/c3dToolKit/c3dMath.cpp \
../../Classes/c3dToolKit/c3dMatrix.cpp \
../../Classes/c3dToolKit/c3dMatrixStackInfoGetor.cpp \
../../Classes/c3dToolKit/c3dMesh.cpp \
../../Classes/c3dToolKit/c3dNode.cpp \
../../Classes/c3dToolKit/c3dProgram.cpp \
../../Classes/c3dToolKit/c3dRange.cpp \
../../Classes/c3dToolKit/c3dScene.cpp \
../../Classes/c3dToolKit/c3dSprite.cpp \
../../Classes/c3dToolKit/c3dUniformPassor.cpp \
../../Classes/c3dToolKit/c3dValueCache.cpp \
../../Classes/c3dToolKit/c3dVector.cpp
保存Android.mk,eclipse就会自动编译,然后右键工程,RunAs Android Application,就会在bin文件夹生成apk了。插入手机就可以看到运行结果。
网上说自动添加文件用下面三行:
FILE_LIST := $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/c3dToolKit/*.cpp)
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/%=%)
在Android.mk代替:
#LOCAL_SRC_FILES := hellocpp/main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/HelloWorldScene.cpp
但是用了会在android程序运行时闪退!!!