Add CameraTest、OBBTest and related lua bindings

This commit is contained in:
samuele3hu 2014-10-10 09:49:25 +08:00
parent 87fccd7881
commit 115d81574c
20 changed files with 3042 additions and 17 deletions

View File

@ -58,6 +58,8 @@
1503FAC418DA8B6C00F6518C /* tp.lua in Resources */ = {isa = PBXBuildFile; fileRef = 1503FAB118DA8B6C00F6518C /* tp.lua */; };
1503FAC518DA8B6C00F6518C /* url.lua in Resources */ = {isa = PBXBuildFile; fileRef = 1503FAB218DA8B6C00F6518C /* url.lua */; };
1503FAC618DA8B6C00F6518C /* url.lua in Resources */ = {isa = PBXBuildFile; fileRef = 1503FAB218DA8B6C00F6518C /* url.lua */; };
150F918819DA409E00B89F57 /* lua_test_bindings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 150F918619DA409E00B89F57 /* lua_test_bindings.cpp */; };
150F918919DA409F00B89F57 /* lua_test_bindings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 150F918619DA409E00B89F57 /* lua_test_bindings.cpp */; };
15427B79198B879900DC375D /* libluacocos2d iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 15EFA665198B33EE000C57D3 /* libluacocos2d iOS.a */; };
15427B7A198B87AA00DC375D /* libcocos2d iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 46A15FBE1807A4F9005B8026 /* libcocos2d iOS.a */; };
15427B7D198B880100DC375D /* lua_cocos2dx_controller_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 15427B7B198B880100DC375D /* lua_cocos2dx_controller_manual.cpp */; };
@ -1158,6 +1160,8 @@
1503FAB018DA8B6C00F6518C /* socket.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = socket.lua; path = ../external/lua/luasocket/socket.lua; sourceTree = "<group>"; };
1503FAB118DA8B6C00F6518C /* tp.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = tp.lua; path = ../external/lua/luasocket/tp.lua; sourceTree = "<group>"; };
1503FAB218DA8B6C00F6518C /* url.lua */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = url.lua; path = ../external/lua/luasocket/url.lua; sourceTree = "<group>"; };
150F918619DA409E00B89F57 /* lua_test_bindings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_test_bindings.cpp; sourceTree = "<group>"; };
150F918719DA409E00B89F57 /* lua_test_bindings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_test_bindings.h; sourceTree = "<group>"; };
15427B76198B750300DC375D /* lua_module_register.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_module_register.h; sourceTree = "<group>"; };
15427B77198B843300DC375D /* lua_module_register.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_module_register.h; sourceTree = "<group>"; };
15427B7B198B880100DC375D /* lua_cocos2dx_controller_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = lua_cocos2dx_controller_manual.cpp; path = "../../../../cocos/scripting/lua-bindings/manual/controller/lua_cocos2dx_controller_manual.cpp"; sourceTree = "<group>"; };
@ -6609,6 +6613,8 @@
1AC35D7C18CEE5B100F37B72 /* Classes */ = {
isa = PBXGroup;
children = (
150F918619DA409E00B89F57 /* lua_test_bindings.cpp */,
150F918719DA409E00B89F57 /* lua_test_bindings.h */,
15427B76198B750300DC375D /* lua_module_register.h */,
1AC35D7D18CEE5B100F37B72 /* AppDelegate.cpp */,
1AC35D7E18CEE5B100F37B72 /* AppDelegate.h */,
@ -7992,6 +7998,7 @@
files = (
1AC35D8118CEE5B100F37B72 /* AppDelegate.cpp in Sources */,
1AC35DB518CEE5DA00F37B72 /* LuaObjectCBridgeTest.mm in Sources */,
150F918819DA409E00B89F57 /* lua_test_bindings.cpp in Sources */,
1AC35DB618CEE5DA00F37B72 /* main.cpp in Sources */,
1AC35D8318CEE5B100F37B72 /* lua_assetsmanager_test_sample.cpp in Sources */,
);
@ -8001,6 +8008,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
150F918919DA409F00B89F57 /* lua_test_bindings.cpp in Sources */,
1AC35DA518CEE5D100F37B72 /* main.m in Sources */,
1AC35DA418CEE5D100F37B72 /* LuaObjectCBridgeTest.mm in Sources */,
1AC35D9818CEE5D100F37B72 /* AppController.mm in Sources */,

View File

@ -186,6 +186,7 @@ bool LuaStack::init(void)
tolua_opengl_open(_state);
register_all_cocos2dx_manual(_state);
register_all_cocos2dx_module_manual(_state);
register_all_cocos2dx_math_manual(_state);
register_all_cocos2dx_experimental(_state);
register_all_cocos2dx_experimental_manual(_state);

View File

@ -371,12 +371,12 @@ bool luaval_to_vec4(lua_State* L,int lo,cocos2d::Vec4* outValue, const char* fun
lua_pushstring(L, "z");
lua_gettable(L, lo);
outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
outValue->z = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "w");
lua_gettable(L, lo);
outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
outValue->w = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
}
return ok;
@ -1997,6 +1997,48 @@ bool luaval_to_std_vector_ushort(lua_State* L, int lo, std::vector<unsigned shor
return ok;
}
bool luaval_to_quaternion(lua_State* L,int lo,cocos2d::Quaternion* outValue, const char* funcName)
{
if (nullptr == L || nullptr == outValue)
return false;
bool ok = true;
tolua_Error tolua_err;
if (!tolua_istable(L, lo, 0, &tolua_err) )
{
#if COCOS2D_DEBUG >=1
luaval_to_native_err(L,"#ferror:",&tolua_err,funcName);
#endif
ok = false;
}
if (ok)
{
lua_pushstring(L, "x");
lua_gettable(L, lo);
outValue->x = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "y");
lua_gettable(L, lo);
outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "z");
lua_gettable(L, lo);
outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "w");
lua_gettable(L, lo);
outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1);
lua_pop(L, 1);
}
return ok;
}
void vec2_array_to_luaval(lua_State* L,const cocos2d::Vec2* points, int count)
{
if (NULL == L)
@ -2939,3 +2981,23 @@ void ccvector_ushort_to_luaval(lua_State* L, const std::vector<unsigned short>&
++index;
}
}
void quaternion_to_luaval(lua_State* L,const cocos2d::Quaternion& inValue)
{
if (NULL == L)
return;
lua_newtable(L); /* L: table */
lua_pushstring(L, "x"); /* L: table key */
lua_pushnumber(L, (lua_Number) inValue.x); /* L: table key value*/
lua_rawset(L, -3); /* table[key] = value, L: table */
lua_pushstring(L, "y"); /* L: table key */
lua_pushnumber(L, (lua_Number) inValue.y); /* L: table key value*/
lua_rawset(L, -3);
lua_pushstring(L, "z"); /* L: table key */
lua_pushnumber(L, (lua_Number) inValue.z); /* L: table key value*/
lua_rawset(L, -3);
lua_pushstring(L, "w"); /* L: table key */
lua_pushnumber(L, (lua_Number) inValue.w); /* L: table key value*/
lua_rawset(L, -3);
}

View File

@ -235,6 +235,7 @@ bool luaval_to_object(lua_State* L, int lo, const char* type, T** ret)
extern bool luaval_to_mesh_vertex_attrib(lua_State* L, int lo, cocos2d::MeshVertexAttrib* ret, const char* funcName = "");
extern bool luaval_to_std_vector_float(lua_State* L, int lo, std::vector<float>* ret, const char* funcName = "");
extern bool luaval_to_std_vector_ushort(lua_State* L, int lo, std::vector<unsigned short>* ret, const char* funcName = "");
extern bool luaval_to_quaternion(lua_State* L,int lo,cocos2d::Quaternion* outValue, const char* funcName = "");
// from native
extern void vec2_to_luaval(lua_State* L,const cocos2d::Vec2& vec2);
@ -386,5 +387,6 @@ void ccvector_std_string_to_luaval(lua_State* L, const std::vector<std::string>&
void ccvector_int_to_luaval(lua_State* L, const std::vector<int>& inValue);
void ccvector_float_to_luaval(lua_State* L, const std::vector<float>& inValue);
void ccvector_ushort_to_luaval(lua_State* L, const std::vector<unsigned short>& inValue);
void quaternion_to_luaval(lua_State* L,const cocos2d::Quaternion& inValue);
#endif //__COCOS2DX_SCRIPTING_LUA_COCOS2DXSUPPORT_LUABAISCCONVERSIONS_H__

View File

@ -7232,3 +7232,254 @@ int register_all_cocos2dx_module_manual(lua_State* tolua_S)
return 0;
}
static int tolua_cocos2d_Mat4_getInversed(lua_State* tolua_S)
{
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_istable(tolua_S, 1, 0, &tolua_err))
goto tolua_lerror;
else
#endif
{
cocos2d::Mat4 mat;
bool ok = luaval_to_mat4(tolua_S, 1, &mat);
if (ok)
{
mat4_to_luaval(tolua_S, mat.getInversed());
return 1;
}
}
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'mat4_getInversed'.",&tolua_err);
return 0;
#endif
}
static int tolua_cocos2d_Mat4_transformVector(lua_State* tolua_S)
{
bool ok = true;
int argc = lua_gettop(tolua_S);
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
if (argc == 3)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_istable(tolua_S, 1, 0, &tolua_err) ||
!tolua_istable(tolua_S, 2, 0, &tolua_err) ||
!tolua_istable(tolua_S, 3, 0, &tolua_err))
goto tolua_lerror;
else
#endif
{
cocos2d::Mat4 mat;
cocos2d::Vec4 vector;
cocos2d::Vec4 dst;
ok &= luaval_to_mat4(tolua_S, 1, &mat);
if (!ok)
return 0;
ok &= luaval_to_vec4(tolua_S, 2, &vector);
if (!ok)
return 0;
ok &= luaval_to_vec4(tolua_S, 3, &dst);
if (!ok)
return 0;
mat.transformVector(vector, &dst);
vec4_to_luaval(tolua_S, dst);
return 1;
}
}
else if(argc == 6)
{
/*
float x, float y, float z, float w, Vec3* dst
*/
#if COCOS2D_DEBUG >= 1
if (!tolua_istable(tolua_S, 1, 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 2, 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 3, 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 4, 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 5, 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 6, 0, &tolua_err))
goto tolua_lerror;
else
#endif
{
cocos2d::Mat4 mat;
float x,y,z,w;
cocos2d::Vec3 dst;
ok &= luaval_to_mat4(tolua_S, 1, &mat);
if (!ok)
return 0;
x = tolua_tonumber(tolua_S, 2, 0);
y = tolua_tonumber(tolua_S, 3, 0);
z = tolua_tonumber(tolua_S, 4, 0);
w = tolua_tonumber(tolua_S, 5, 0);
ok &= luaval_to_vec3(tolua_S, 6, &dst);
if (!ok)
return 0;
mat.transformVector(x,y,z,w, &dst);
vec3_to_luaval(tolua_S, dst);
return 1;
}
}
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'mat4_transformVector'.",&tolua_err);
return 0;
#endif
}
static int tolua_cocos2d_Mat4_decompose(lua_State* tolua_S)
{
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_istable(tolua_S, 1, 0, &tolua_err) ||
!tolua_istable(tolua_S, 2, 0, &tolua_err) ||
!tolua_istable(tolua_S, 3, 0, &tolua_err) ||
!tolua_istable(tolua_S, 4, 0, &tolua_err))
goto tolua_lerror;
else
#endif
{
cocos2d::Mat4 mat;
cocos2d::Vec3 scale;
cocos2d::Quaternion rotation;
cocos2d::Vec3 translation;
bool ok = true;
ok &= luaval_to_mat4(tolua_S, 1, &mat);
if (!ok)
return 0;
ok &= luaval_to_vec3(tolua_S, 2, &scale);
if (!ok)
return 0;
ok &= luaval_to_quaternion(tolua_S, 3, &rotation);
if (!ok)
return 0;
ok &= luaval_to_vec3(tolua_S, 2, &translation);
if (!ok)
return 0;
mat.decompose(&scale, &rotation, &translation);
vec3_to_luaval(tolua_S, scale);
quaternion_to_luaval(tolua_S, rotation);
vec3_to_luaval(tolua_S, translation);
return 3;
}
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'mat4_decompose'.",&tolua_err);
return 0;
#endif
}
static int tolua_cocos2d_Vec3_cross(lua_State* tolua_S)
{
int argc = lua_gettop(tolua_S);
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
if (1 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_istable(tolua_S, 1, 0, &tolua_err) ||
!tolua_istable(tolua_S, 2, 0, &tolua_err) )
goto tolua_lerror;
else
#endif
{
cocos2d::Vec3 cobj;
cocos2d::Vec3 v;
bool ok = true;
ok &= luaval_to_vec3(tolua_S, 1, &cobj);
if (!ok)
return 0;
ok &= luaval_to_vec3(tolua_S, 2, &v);
if (!ok)
return 0;
cobj.cross(v);
vec3_to_luaval(tolua_S, cobj);
return 1;
}
}
else if (3 == argc)
{
#if COCOS2D_DEBUG >= 1
if (!tolua_istable(tolua_S, 1, 0, &tolua_err) ||
!tolua_istable(tolua_S, 2, 0, &tolua_err) ||
!tolua_istable(tolua_S, 3, 0, &tolua_err) )
goto tolua_lerror;
else
#endif
{
cocos2d::Vec3 v1;
cocos2d::Vec3 v2;
cocos2d::Vec3 dst;
bool ok = true;
ok &= luaval_to_vec3(tolua_S, 1, &v1);
if (!ok)
return 0;
ok &= luaval_to_vec3(tolua_S, 2, &v2);
if (!ok)
return 0;
ok &= luaval_to_vec3(tolua_S, 3, &dst);
if (!ok)
return 0;
cocos2d::Vec3::cross(v1, v2, &dst);
vec3_to_luaval(tolua_S, dst);
return 1;
}
}
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'MathUtil_gcrossVec3'.",&tolua_err);
return 0;
#endif
}
int register_all_cocos2dx_math_manual(lua_State* tolua_S)
{
if (nullptr == tolua_S)
return 0;
tolua_module(tolua_S, nullptr, 0);
tolua_beginmodule(tolua_S, nullptr);
tolua_function(tolua_S, "mat4_getInversed", tolua_cocos2d_Mat4_getInversed);
tolua_function(tolua_S, "mat4_transformVector", tolua_cocos2d_Mat4_transformVector);
tolua_function(tolua_S, "mat4_decompose", tolua_cocos2d_Mat4_decompose);
tolua_function(tolua_S, "vec3_cross", tolua_cocos2d_Vec3_cross);
tolua_endmodule(tolua_S);
return 0;
}

View File

@ -57,6 +57,8 @@ TOLUA_API int register_cocos2dx_event_releated(lua_State* tolua_S);
TOLUA_API int register_all_cocos2dx_module_manual(lua_State* tolua_S);
TOLUA_API int register_all_cocos2dx_math_manual(lua_State* tolua_S);
struct LuaEventAccelerationData
{
void* acc;

View File

@ -367,6 +367,10 @@ function cc.vec3(_x, _y, _z)
return { x = _x, y = _y, z = _z }
end
function cc.vec4(_x, _y, _z, _w)
return { x = _x, y = _y, z = _z, w = _w }
end
function cc.vec3normalize(vec3)
local n = vec3.x * vec3.x + vec3.y * vec3.y + vec3.z * vec3.z
if n == 1.0 then
@ -382,3 +386,45 @@ function cc.vec3normalize(vec3)
n = 1.0 / n
return {x = vec3.x * n, y = vec3.y * n, z = vec3.z * n}
end
function cc.quaternion(_x, _y ,_z,_w)
return { x = _x, y = _y, z = _z, w = _w }
end
cc.mat4 = cc.mat4 or {}
function cc.mat4.new(...)
local params = {...}
local size = #params
local obj = {}
if 1 == size then
assert(type(params[1]) == "table" , "type of input params are wrong to new a mat4 when num of params is 1")
for i= 1, 16 do
if params[1][i] ~= nil then
obj[i] = params[1][i]
else
obj[i] = 0
end
end
elseif 16 == size then
if params[i] ~= nil then
mat4[i] = params[i]
else
mat4[i] = 0
end
end
setmetatable(obj, {__index = cc.mat4})
return obj
end
function cc.mat4.getInversed(self)
return mat4_getInversed(self)
end
function cc.mat4.transformVector(self, vector, dst)
return mat4_transformVector(self, vector, dst)
end

View File

@ -523,3 +523,30 @@ cc.BillBoard_Mode =
VIEW_POINT_ORIENTED = 0,
VIEW_PLANE_ORIENTED = 1,
}
cc.GLProgram_VERTEX_ATTRIB =
{
POSITION = 0,
COLOR = 1,
TEX_COORD = 2,
TEX_COORD1 = 3,
TEX_COORD2 = 4,
TEX_COORD3 = 5,
TEX_COORD4 = 6,
TEX_COORD5 = 7,
TEX_COORD6 = 8,
TEX_COORD7 = 9,
NORMAL = 10,
BLEND_WEIGHT = 11,
BLEND_INDEX =12,
MAX = 13,
--backward compatibility
TEX_COORDS = 2,
}
cc.MATRIX_STACK_TYPE =
{
MODELVIEW = 0,
PROJECTION = 1,
TEXTURE = 2,
}

View File

@ -822,3 +822,5 @@ gl.WRITEONLY_RENDERING_QCOM = 0x8823
gl.WRITE_ONLY_OES = 0x88b9
gl.Z400_BINARY_AMD = 0x8740
gl.ZERO = 0x0
gl.VERTEX_ATTRIB_POINTER_VEC3 = 0
gl.VERTEX_ATTRIB_POINTER_COLOR4B = 1

View File

@ -3,6 +3,7 @@ set(APP_NAME lua-tests)
set(SAMPLE_SRC
Classes/AppDelegate.cpp
Classes/lua_assetsmanager_test_sample.cpp
Classes/lua_test_bindings.cpp
)
if(LINUX)

View File

@ -4,6 +4,7 @@
#include "audio/include/SimpleAudioEngine.h"
#include "lua_assetsmanager_test_sample.h"
#include "lua_module_register.h"
#include "lua_test_bindings.h"
using namespace CocosDenshion;
@ -43,6 +44,7 @@ bool AppDelegate::applicationDidFinishLaunching()
if (lua_istable(L,-1))//stack:...,_G,
{
register_assetsmanager_test_sample(L);
register_test_binding(L);
}
lua_pop(L, 1);
#endif

View File

@ -0,0 +1,621 @@
#include "lua_test_bindings.h"
#include "cocos2d.h"
#include "LuaBasicConversions.h"
NS_CC_BEGIN
/**
* Copy DrawNode for 3D geometry drawing.
*/
class DrawNode3D: public Node
{
public:
/** creates and initialize a DrawNode3D node */
static DrawNode3D* create();
/**
* Draw 3D Line
*/
void drawLine(const Vec3 &from, const Vec3 &to, const Color4F &color);
/**
* Draw 3D cube
* @param point to a vertex array who has 8 element.
* vertices[0]:Left-top-front,
* vertices[1]:Left-bottom-front,
* vertices[2]:Right-bottom-front,
* vertices[3]:Right-top-front,
* vertices[4]:Right-top-back,
* vertices[5]:Right-bottom-back,
* vertices[6]:Left-bottom-back,
* vertices[7]:Left-top-back.
* @param color
*/
void drawCube(Vec3* vertices, const Color4F &color);
/** Clear the geometry in the node's buffer. */
void clear();
/**
* @js NA
* @lua NA
*/
const BlendFunc& getBlendFunc() const;
/**
* @code
* When this function bound into js or lua,the parameter will be changed
* In js: var setBlendFunc(var src, var dst)
* @endcode
* @lua NA
*/
void setBlendFunc(const BlendFunc &blendFunc);
void onDraw(const Mat4 &transform, uint32_t flags);
// Overrides
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
CC_CONSTRUCTOR_ACCESS:
DrawNode3D();
virtual ~DrawNode3D();
virtual bool init();
protected:
struct V3F_C4B
{
Vec3 vertices;
Color4B colors;
};
void ensureCapacity(int count);
GLuint _vao;
GLuint _vbo;
int _bufferCapacity;
GLsizei _bufferCount;
V3F_C4B* _buffer;
BlendFunc _blendFunc;
CustomCommand _customCommand;
bool _dirty;
private:
CC_DISALLOW_COPY_AND_ASSIGN(DrawNode3D);
};
DrawNode3D::DrawNode3D()
: _vao(0)
, _vbo(0)
, _bufferCapacity(0)
, _bufferCount(0)
, _buffer(nullptr)
, _dirty(false)
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}
DrawNode3D::~DrawNode3D()
{
free(_buffer);
_buffer = nullptr;
glDeleteBuffers(1, &_vbo);
_vbo = 0;
if (Configuration::getInstance()->supportsShareableVAO())
{
glDeleteVertexArrays(1, &_vao);
GL::bindVAO(0);
_vao = 0;
}
}
DrawNode3D* DrawNode3D::create()
{
DrawNode3D* ret = new (std::nothrow) DrawNode3D();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
void DrawNode3D::ensureCapacity(int count)
{
CCASSERT(count>=0, "capacity must be >= 0");
if(_bufferCount + count > _bufferCapacity)
{
_bufferCapacity += MAX(_bufferCapacity, count);
_buffer = (V3F_C4B*)realloc(_buffer, _bufferCapacity*sizeof(V3F_C4B));
}
}
bool DrawNode3D::init()
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR));
ensureCapacity(512);
if (Configuration::getInstance()->supportsShareableVAO())
{
glGenVertexArrays(1, &_vao);
GL::bindVAO(_vao);
}
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, vertices));
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, colors));
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (Configuration::getInstance()->supportsShareableVAO())
{
GL::bindVAO(0);
}
CHECK_GL_ERROR_DEBUG();
_dirty = true;
#if CC_ENABLE_CACHE_TEXTURE_DATA
// Need to listen the event only when not use batchnode, because it will use VBO
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
/** listen the event that coming to foreground on Android */
this->init();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
return true;
}
void DrawNode3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(DrawNode3D::onDraw, this, transform, flags);
renderer->addCommand(&_customCommand);
}
void DrawNode3D::onDraw(const Mat4 &transform, uint32_t flags)
{
auto glProgram = getGLProgram();
glProgram->use();
glProgram->setUniformsForBuiltins(transform);
glEnable(GL_DEPTH_TEST);
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
if (_dirty)
{
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
_dirty = false;
}
if (Configuration::getInstance()->supportsShareableVAO())
{
GL::bindVAO(_vao);
}
else
{
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
// vertex
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, vertices));
// color
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid *)offsetof(V3F_C4B, colors));
}
glDrawArrays(GL_LINES, 0, _bufferCount);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCount);
glDisable(GL_DEPTH_TEST);
CHECK_GL_ERROR_DEBUG();
}
void DrawNode3D::drawLine(const Vec3 &from, const Vec3 &to, const Color4F &color)
{
unsigned int vertex_count = 2;
ensureCapacity(vertex_count);
Color4B col = Color4B(color);
V3F_C4B a = {Vec3(from.x, from.y, from.z), col};
V3F_C4B b = {Vec3(to.x, to.y, to.z), col, };
V3F_C4B *lines = (V3F_C4B *)(_buffer + _bufferCount);
lines[0] = a;
lines[1] = b;
_bufferCount += vertex_count;
_dirty = true;
}
void DrawNode3D::drawCube(Vec3* vertices, const Color4F &color)
{
// front face
drawLine(vertices[0], vertices[1], color);
drawLine(vertices[1], vertices[2], color);
drawLine(vertices[2], vertices[3], color);
drawLine(vertices[3], vertices[0], color);
// back face
drawLine(vertices[4], vertices[5], color);
drawLine(vertices[5], vertices[6], color);
drawLine(vertices[6], vertices[7], color);
drawLine(vertices[7], vertices[4], color);
// edge
drawLine(vertices[0], vertices[7], color);
drawLine(vertices[1], vertices[6], color);
drawLine(vertices[2], vertices[5], color);
drawLine(vertices[3], vertices[4], color);
}
void DrawNode3D::clear()
{
_bufferCount = 0;
_dirty = true;
}
const BlendFunc& DrawNode3D::getBlendFunc() const
{
return _blendFunc;
}
void DrawNode3D::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
}
NS_CC_END
int lua_cocos2dx_DrawNode3D_getBlendFunc(lua_State* L)
{
int argc = 0;
cocos2d::DrawNode3D* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_getBlendFunc'", nullptr);
return 0;
}
#endif
argc = lua_gettop(L)-1;
if (argc == 0)
{
if(!ok)
return 0;
const cocos2d::BlendFunc& ret = cobj->getBlendFunc();
blendfunc_to_luaval(L, ret);
return 1;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "cc.DrawNode3D:getBlendFunc",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_getBlendFunc'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_DrawNode3D_setBlendFunc(lua_State* L)
{
int argc = 0;
cocos2d::DrawNode3D* cobj = nullptr;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_setBlendFunc'", nullptr);
return 0;
}
#endif
argc = lua_gettop(L)-1;
if (argc == 2)
{
GLenum src, dst;
if (!luaval_to_int32(L, 2, (int32_t*)&src, "cc.DrawNode3D:setBlendFunc"))
return 0;
if (!luaval_to_int32(L, 3, (int32_t*)&dst, "cc.DrawNode3D:setBlendFunc"))
return 0;
BlendFunc blendFunc = {src, dst};
cobj->setBlendFunc(blendFunc);
return 0;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "cc.DrawNode3D:setBlendFunc",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_setBlendFunc'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_DrawNode3D_drawLine(lua_State* L)
{
int argc = 0;
cocos2d::DrawNode3D* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_drawLine'", nullptr);
return 0;
}
#endif
argc = lua_gettop(L)-1;
if (argc == 3)
{
cocos2d::Vec3 arg0;
cocos2d::Vec3 arg1;
cocos2d::Color4F arg2;
ok &= luaval_to_vec3(L, 2, &arg0, "cc.DrawNode3D:drawLine");
ok &= luaval_to_vec3(L, 3, &arg1, "cc.DrawNode3D:drawLine");
ok &=luaval_to_color4f(L, 4, &arg2, "cc.DrawNode3D:drawLine");
if(!ok)
return 0;
cobj->drawLine(arg0, arg1, arg2);
return 0;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "cc.DrawNode3D:drawLine",argc, 3);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_drawLine'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_DrawNode3D_clear(lua_State* L)
{
int argc = 0;
cocos2d::DrawNode3D* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_clear'", nullptr);
return 0;
}
#endif
argc = lua_gettop(L)-1;
if (argc == 0)
{
if(!ok)
return 0;
cobj->clear();
return 0;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "cc.DrawNode3D:clear",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_clear'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_DrawNode3D_drawCube(lua_State* L)
{
int argc = 0;
cocos2d::DrawNode3D* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::DrawNode3D*)tolua_tousertype(L,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(L,"invalid 'cobj' in function 'lua_cocos2dx_DrawNode3D_drawCube'", nullptr);
return 0;
}
#endif
argc = lua_gettop(L)-1;
if (argc == 2)
{
std::vector<cocos2d::Vec3> arg0;
cocos2d::Color4F arg1;
Vec3 vec3;
#if COCOS2D_DEBUG >= 1
if (!tolua_istable(L, 2, 0, &tolua_err))
goto tolua_lerror;
#endif
size_t size = lua_objlen(L, 2);
for (int i = 0; i < size; i++)
{
lua_pushnumber(L, i + 1);
lua_gettable(L, 2);
#ifndef TOLUA_RELEASE
if (!tolua_istable(L, -1, 0, &tolua_err))
{
lua_pop(L, 1);
goto tolua_lerror;
}
#endif
ok &= luaval_to_vec3(L, lua_gettop(L), &vec3);
#ifndef TOLUA_RELEASE
if (!ok)
{
lua_pop(L, 1);
goto tolua_lerror;
}
#endif
//arg0[i] = vec3;
arg0.push_back(vec3);
lua_pop(L, 1);
}
ok &=luaval_to_color4f(L, 3, &arg1, "cc.DrawNode3D:drawCube");
if(!ok)
return 0;
cobj->drawCube(&arg0[0], arg1);
return 0;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "cc.DrawNode3D:drawCube",argc, 2);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_drawCube'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_DrawNode3D_create(lua_State* L)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(L,1,"cc.DrawNode3D",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(L) - 1;
if (argc == 0)
{
if(!ok)
return 0;
cocos2d::DrawNode3D* ret = cocos2d::DrawNode3D::create();
object_to_luaval<cocos2d::DrawNode3D>(L, "cc.DrawNode3D",(cocos2d::DrawNode3D*)ret);
return 1;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "cc.DrawNode3D:create",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(L,"#ferror in function 'lua_cocos2dx_DrawNode3D_create'.",&tolua_err);
#endif
return 0;
}
int lua_register_cocos2dx_DrawNode3D(lua_State* L)
{
tolua_usertype(L,"cc.DrawNode3D");
tolua_cclass(L,"DrawNode3D","cc.DrawNode3D","cc.Node",nullptr);
tolua_beginmodule(L,"DrawNode3D");
tolua_function(L,"getBlendFunc",lua_cocos2dx_DrawNode3D_getBlendFunc);
tolua_function(L,"setBlendFunc",lua_cocos2dx_DrawNode3D_setBlendFunc);
tolua_function(L,"drawLine",lua_cocos2dx_DrawNode3D_drawLine);
tolua_function(L,"clear",lua_cocos2dx_DrawNode3D_clear);
tolua_function(L,"drawCube",lua_cocos2dx_DrawNode3D_drawCube);
tolua_function(L,"create", lua_cocos2dx_DrawNode3D_create);
tolua_endmodule(L);
std::string typeName = typeid(cocos2d::DrawNode3D).name();
g_luaType[typeName] = "cc.DrawNode3D";
g_typeCast["DrawNode3D"] = "cc.DrawNode3D";
return 1;
}
int register_test_binding(lua_State* L)
{
tolua_open(L);
tolua_module(L, "cc", 0);
tolua_beginmodule(L, "cc");
lua_register_cocos2dx_DrawNode3D(L);
tolua_endmodule(L);
return 0;
}

View File

@ -0,0 +1,14 @@
#ifndef COCOS2DX_TEST_LUA_TESTS_PROJECT_CLASSES_LUA_TEST_BINDINGS_H
#define COCOS2DX_TEST_LUA_TESTS_PROJECT_CLASSES_LUA_TEST_BINDINGS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif
TOLUA_API int register_test_binding(lua_State* tolua_S);
#endif // #ifndef COCOS2DX_TEST_LUA_TESTS_PROJECT_CLASSES_LUA_TEST_BINDINGS_H

View File

@ -8,7 +8,8 @@ LOCAL_MODULE_FILENAME := liblua_tests
LOCAL_SRC_FILES += main.cpp \
../../Classes/AppDelegate.cpp \
../../Classes/lua_assetsmanager_test_sample.cpp
../../Classes/lua_assetsmanager_test_sample.cpp \
../../Classes/lua_test_bindings.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes

View File

@ -182,12 +182,14 @@ xcopy "$(ProjectDir)..\..\..\cpp-tests\Resources" "$(ProjectDir)..\..\res" /e /Y
<ItemGroup>
<ClInclude Include="..\Classes\AppDelegate.h" />
<ClInclude Include="..\Classes\lua_assetsmanager_test_sample.h" />
<ClInclude Include="..\Classes\lua_test_bindings.h" />
<ClInclude Include="..\Classes\lua_module_register.h" />
<ClInclude Include="main.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Classes\AppDelegate.cpp" />
<ClCompile Include="..\Classes\lua_assetsmanager_test_sample.cpp" />
<ClCompile Include="..\Classes\lua_test_bindings.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>

View File

@ -21,6 +21,9 @@
<ClInclude Include="..\Classes\lua_module_register.h">
<Filter>Classes</Filter>
</ClInclude>
<ClInclude Include="..\Classes\lua_test_bindings.h">
<Filter>Classes</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Classes\AppDelegate.cpp">
@ -32,5 +35,8 @@
<ClCompile Include="..\Classes\lua_assetsmanager_test_sample.cpp">
<Filter>Classes</Filter>
</ClCompile>
<ClCompile Include="..\Classes\lua_test_bindings.cpp">
<Filter>Classes</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -413,7 +413,22 @@ function Camera3DTestDemo:onEnter()
self:SwitchViewCallback(self, CameraType.ThirdCamera)
--UNDO DrawLine3D drwaline
local line = cc.DrawNode3D:create()
--draw x
for i = -20 ,20 do
line:drawLine(cc.vec3(-100, 0, 5 * i), cc.vec3(100, 0, 5 * i), cc.c4f(1, 0, 0, 0))
end
--draw z
for i = -20, 20 do
line:drawLine(cc.vec3(5 * i, 0, -100), cc.vec3(5 * i, 0, 100), cc.c4f(0, 0, 1, 1))
end
--draw y
line:drawLine(cc.vec3(0, -50, 0), cc.vec3(0,0,0), cc.c4f(0, 0.5, 0, 1))
line:drawLine(cc.vec3(0, 0, 0), cc.vec3(0,50,0), cc.c4f(0, 1, 0, 1))
self._layer3D:addChild(line)
self._layer3D:setCameraMask(2)
end

View File

@ -629,6 +629,245 @@ function Sprite3DReskinTest.create()
return layer
end
----------------------------------------
----Sprite3DWithOBBPerfromanceTest
----------------------------------------
local Sprite3DWithOBBPerfromanceTest = class("Sprite3DWithOBBPerfromanceTest",function()
return cc.Layer:create()
end)
function Sprite3DWithOBBPerfromanceTest:ctor()
self._obb = {}
local listener = cc.EventListenerTouchAllAtOnce:create()
listener:registerScriptHandler(function (touches, event)
for i,touch in ipairs(touches) do
local location = touch:getLocationInView()
if nil ~= self._obb and #self._obb > 0 then
self._intersetList = {}
local ray = cc.Ray:new()
self:calculateRayByLocationInView(ray, location)
for idx,value in ipairs(self._obb) do
if ray:intersects(value) then
table.insert(self._intersetList, idx)
return
end
end
end
end
end,cc.Handler.EVENT_TOUCHES_BEGAN)
listener:registerScriptHandler(function (touches, event)
end,cc.Handler.EVENT_TOUCHES_ENDED)
listener:registerScriptHandler(function (touches, event)
for i,touch in ipairs(touches) do
local location = touch:getLocation()
for idx,value in ipairs(self._obb) do
for lstIdx,lstValue in ipairs(self._intersetList) do
if idx == lstValue then
self._obb[idx]._center = cc.vec3(location.x,location.y,0)
end
end
end
end
end,cc.Handler.EVENT_TOUCHES_MOVED)
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
local s = cc.Director:getInstance():getWinSize()
self:initDrawBox()
self:addNewSpriteWithCoords(cc.p(s.width/2, s.height/2))
cc.MenuItemFont:setFontName("fonts/arial.ttf")
cc.MenuItemFont:setFontSize(65)
local decrease = cc.MenuItemFont:create(" - ")
decrease:registerScriptTapHandler(function(tag, sender)
self:delOBBWithCount(10)
end)
decrease:setColor(cc.c3b(0, 200, 20))
local increase = cc.MenuItemFont:create(" + ")
increase:registerScriptTapHandler(function(tag, sender)
self:addOBBWithCount(10)
end)
increase:setColor(cc.c3b(0, 200, 20))
local menu = cc.Menu:create(decrease, increase)
menu:alignItemsHorizontally()
menu:setPosition(cc.p(s.width/2, s.height - 90))
self:addChild(menu, 1)
local ttfConfig = {}
ttfConfig.fontFilePath = "fonts/Marker Felt.ttf"
ttfConfig.fontSize = 30
self._labelCubeCount = cc.Label:createWithTTF(ttfConfig,"0 cubes")
self._labelCubeCount:setColor(cc.c3b(0,200,20))
self._labelCubeCount:setPosition(cc.p(s.width/2, s.height-90))
self:addChild(self._labelCubeCount)
self:addOBBWithCount(10)
self:scheduleUpdateWithPriorityLua(function(dt)
self._labelCubeCount:setString(string.format("%u cubes", #self._obb))
if nil ~= self._drawDebug then
self._drawDebug:clear()
local mat = self._sprite:getNodeToWorldTransform()
self._obbt._xAxis = cc.vec3(mat[1], mat[2], mat[3])
self._obbt._xAxis = cc.vec3normalize(self._obbt._xAxis)
self._obbt._yAxis = cc.vec3(mat[5], mat[6], mat[7])
self._obbt._yAxis = cc.vec3normalize(self._obbt._yAxis)
self._obbt._zAxis = cc.vec3(-mat[9], -mat[10], -mat[11])
self._obbt._zAxis = cc.vec3normalize(self._obbt._zAxis)
self._obbt._center = self._sprite:getPosition3D()
local corners = {}
for i=1,8 do
corners[i] = {}
end
corners = self._obbt:getCorners(corners)
self._drawDebug:drawCube(corners, cc.c4f(0, 0, 1, 1))
end
if #self._obb > 0 then
self._drawOBB:clear()
for i= 1, #self._obb do
local corners = {}
for i=1,8 do
corners[i] = {}
end
corners = self._obb[i]:getCorners(corners)
if self._obbt:intersects(self._obb[i]) then
self._drawOBB:drawCube(corners, cc.c4f(1, 0, 0, 1))
else
self._drawOBB:drawCube(corners, cc.c4f(0, 1, 0, 1))
end
end
end
end, 0)
end
function Sprite3DWithOBBPerfromanceTest:addOBBWithCount( value )
for i=1,value do
local randompos = cc.p(math.random() * cc.Director:getInstance():getWinSize().width, math.random() * cc.Director:getInstance():getWinSize().height)
local extents = cc.vec3(10, 10, 10)
local aabb = cc.AABB:new({x = -10, y = -10, z = -10}, extents)
local obb = cc.OBB:new(aabb)
obb._center = cc.vec3(randompos.x,randompos.y,0)
table.insert(self._obb, obb)
end
end
function Sprite3DWithOBBPerfromanceTest:delOBBWithCount( value )
if #self._obb >= 10 then
for i= 1, 10 do
table.remove(self._obb)
end
self._drawOBB:clear()
end
end
function Sprite3DWithOBBPerfromanceTest:initDrawBox()
self._drawOBB = cc.DrawNode3D:create()
self:addChild(self._drawOBB )
end
function Sprite3DWithOBBPerfromanceTest:unproject( viewProjection, viewport, src, dst)
assert(viewport.width ~= 0.0 and viewport.height ~= 0)
local screen = cc.vec4(src.x / viewport.width, (viewport.height - src.y) / viewport.height, src.z, 1.0)
screen.x = screen.x * 2.0 - 1.0
screen.y = screen.y * 2.0 - 1.0
screen.z = screen.z * 2.0 - 1.0
local inversed = cc.mat4.new(viewProjection:getInversed())
screen = inversed:transformVector(screen, screen)
if screen.w ~= 0.0 then
screen.x = screen.x / screen.w
screen.y = screen.y / screen.w
screen.z = screen.z / screen.w
end
dst.x = screen.x
dst.y = screen.y
dst.z = screen.z
return viewport, src, dst
end
function Sprite3DWithOBBPerfromanceTest:calculateRayByLocationInView(ray, location)
local dir = cc.Director:getInstance()
local view = dir:getWinSize()
local mat = cc.mat4.new(dir:getMatrix(cc.MATRIX_STACK_TYPE.PROJECTION))
local src = cc.vec3(location.x, location.y, -1)
local nearPoint = {}
view, src, nearPoint = self:unproject(mat, view, src, nearPoint)
src = cc.vec3(location.x, location.y, 1)
local farPoint = {}
view, src, farPoint = self:unproject(mat, view, src, farPoint)
local direction = {}
direction.x = farPoint.x - nearPoint.x
direction.y = farPoint.y - nearPoint.y
direction.z = farPoint.z - nearPoint.z
direction = cc.vec3normalize(direction)
ray._origin = nearPoint
ray._direction = direction
end
function Sprite3DWithOBBPerfromanceTest:addNewSpriteWithCoords(vec2)
local fileName = "Sprite3DTest/tortoise.c3b"
local sprite = cc.Sprite3D:create(fileName)
sprite:setScale(0.1)
local s = cc.Director:getInstance():getWinSize()
sprite:setPosition(cc.p(s.width * 4.0 / 5.0, s.height / 2.0))
self:addChild(sprite)
self._sprite = sprite
local animation = cc.Animation3D:create(fileName)
if nil ~= animation then
local animate = cc.Animate3D:create(animation, 0.0, 1.933)
sprite:runAction(cc.RepeatForever:create(animate))
end
self._moveAction = cc.MoveTo:create(4.0, cc.p(s.width / 5.0, s.height / 2.0))
self._moveAction:retain()
local function reachEndCallBack()
self._sprite:stopActionByTag(100)
local inverse = self._moveAction:reverse()
inverse:retain()
self._moveAction:release()
self._moveAction = inverse
local rot = cc.RotateBy:create(1.0, { x = 0.0, y = 180.0, z = 0.0})
local seq = cc.Sequence:create(rot, self._moveAction, cc.CallFunc:create(reachEndCallBack))
seq:setTag(100)
self._sprite:runAction(seq)
end
local seq = cc.Sequence:create(self._moveAction, cc.CallFunc:create(reachEndCallBack))
seq:setTag(100)
sprite:runAction(seq)
local aabb = self._sprite:getAABB()
self._obbt = cc.OBB:new(aabb)
self._drawDebug = cc.DrawNode3D:create()
self:addChild(self._drawDebug)
end
function Sprite3DWithOBBPerfromanceTest.create()
local layer = Sprite3DWithOBBPerfromanceTest.new()
Helper.initWithLayer(layer)
Helper.titleLabel:setString("OBB Collison Perfromance Test")
return layer
end
----------------------------------------
----Sprite3DMirrorTest
----------------------------------------
@ -680,7 +919,6 @@ function Sprite3DMirrorTest.create()
return layer
end
function Sprite3DTest()
local scene = cc.Scene:create()
@ -692,6 +930,7 @@ function Sprite3DTest()
Animate3DTest.create,
AttachmentTest.create,
Sprite3DReskinTest.create,
Sprite3DWithOBBPerfromanceTest.create,
Sprite3DMirrorTest.create,
}

View File

@ -26,7 +26,7 @@ headers = %(cocosdir)s/cocos/cocos2d.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = Animate3D Sprite3D Animation3D Skeleton3D ^Mesh$ AttachNode AABB OBB BillBoard
classes = Animate3D Sprite3D Animation3D Skeleton3D ^Mesh$ AttachNode BillBoard
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
@ -39,8 +39,6 @@ skip = Mesh::[create getAABB getVertexBuffer hasVertexAttrib getMeshVertexAttrib
Sprite3D::[getSkin getAABB setBlendFunc],
Skeleton3D::[create],
Animation3D::[getBoneCurveByName],
AABB::[intersects merge],
OBB::[intersects],
BillBoard::[draw]
@ -59,7 +57,7 @@ base_classes_to_skip = Clonable
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = OBB
abstract_classes =
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no