Merge pull request #7708 from super626/obb

AABB, OBB
This commit is contained in:
minggo 2014-08-15 15:32:14 +08:00
commit f2f8035724
27 changed files with 1955 additions and 5 deletions

View File

@ -1875,6 +1875,18 @@
B3AF01A31842FBA400A98B85 /* b2MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */; };
B3B12A5A17E7F44000026B4A /* libchipmunk Mac.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A03F2CB81780BD04006731B9 /* libchipmunk Mac.a */; };
B3B12A5B17E7F45C00026B4A /* libchipmunk iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A07A4F3B178387670073F6A7 /* libchipmunk iOS.a */; };
B610517B199CB1A600DC41CE /* CCAABB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105175199CB1A600DC41CE /* CCAABB.cpp */; };
B610517C199CB1A600DC41CE /* CCAABB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105175199CB1A600DC41CE /* CCAABB.cpp */; };
B610517D199CB1A600DC41CE /* CCAABB.h in Headers */ = {isa = PBXBuildFile; fileRef = B6105176199CB1A600DC41CE /* CCAABB.h */; };
B610517E199CB1A600DC41CE /* CCAABB.h in Headers */ = {isa = PBXBuildFile; fileRef = B6105176199CB1A600DC41CE /* CCAABB.h */; };
B610517F199CB1A600DC41CE /* CCOBB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105177199CB1A600DC41CE /* CCOBB.cpp */; };
B6105180199CB1A600DC41CE /* CCOBB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105177199CB1A600DC41CE /* CCOBB.cpp */; };
B6105181199CB1A600DC41CE /* CCOBB.h in Headers */ = {isa = PBXBuildFile; fileRef = B6105178199CB1A600DC41CE /* CCOBB.h */; };
B6105182199CB1A600DC41CE /* CCOBB.h in Headers */ = {isa = PBXBuildFile; fileRef = B6105178199CB1A600DC41CE /* CCOBB.h */; };
B6105183199CB1A600DC41CE /* CCRay.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105179199CB1A600DC41CE /* CCRay.cpp */; };
B6105184199CB1A600DC41CE /* CCRay.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6105179199CB1A600DC41CE /* CCRay.cpp */; };
B6105185199CB1A600DC41CE /* CCRay.h in Headers */ = {isa = PBXBuildFile; fileRef = B610517A199CB1A600DC41CE /* CCRay.h */; };
B6105186199CB1A600DC41CE /* CCRay.h in Headers */ = {isa = PBXBuildFile; fileRef = B610517A199CB1A600DC41CE /* CCRay.h */; };
B6AAF84119404E0D0069DE01 /* CCBundle3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */; };
B6AAF84219404E0D0069DE01 /* CCBundle3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */; };
B6AAF84319404E0D0069DE01 /* CCBundle3D.h in Headers */ = {isa = PBXBuildFile; fileRef = B6AAF84019404E0D0069DE01 /* CCBundle3D.h */; };
@ -3071,6 +3083,12 @@
B375104F1823AC7B00B3BA6A /* CCPhysicsWorldInfo_chipmunk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPhysicsWorldInfo_chipmunk.h; sourceTree = "<group>"; };
B3AF019E1842FBA400A98B85 /* b2MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2MotorJoint.cpp; sourceTree = "<group>"; };
B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = "<group>"; };
B6105175199CB1A600DC41CE /* CCAABB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCAABB.cpp; sourceTree = "<group>"; };
B6105176199CB1A600DC41CE /* CCAABB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCAABB.h; sourceTree = "<group>"; };
B6105177199CB1A600DC41CE /* CCOBB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCOBB.cpp; sourceTree = "<group>"; };
B6105178199CB1A600DC41CE /* CCOBB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCOBB.h; sourceTree = "<group>"; };
B6105179199CB1A600DC41CE /* CCRay.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCRay.cpp; sourceTree = "<group>"; };
B610517A199CB1A600DC41CE /* CCRay.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCRay.h; sourceTree = "<group>"; };
B6AAF83F19404E0D0069DE01 /* CCBundle3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBundle3D.cpp; sourceTree = "<group>"; };
B6AAF84019404E0D0069DE01 /* CCBundle3D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBundle3D.h; sourceTree = "<group>"; };
B6ACD895193D6693005E0B8A /* CCMeshSkin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCMeshSkin.cpp; sourceTree = "<group>"; };
@ -5237,6 +5255,12 @@
B29594B81926D61F003EEF37 /* 3d */ = {
isa = PBXGroup;
children = (
B6105175199CB1A600DC41CE /* CCAABB.cpp */,
B6105176199CB1A600DC41CE /* CCAABB.h */,
B6105177199CB1A600DC41CE /* CCOBB.cpp */,
B6105178199CB1A600DC41CE /* CCOBB.h */,
B6105179199CB1A600DC41CE /* CCRay.cpp */,
B610517A199CB1A600DC41CE /* CCRay.h */,
3EA2A83E1987A6810077B3D4 /* CCSkeleton3D.cpp */,
3EA2A83F1987A6810077B3D4 /* CCSkeleton3D.h */,
3E9E80E3198639EF00FA95D0 /* CCAttachNode.cpp */,
@ -5342,6 +5366,7 @@
1AC0269C1914068200FA920D /* ConvertUTF.h in Headers */,
50ABBED11925AB6F00A911A9 /* TGAlib.h in Headers */,
1A57019F180BCB590088DEC7 /* CCFont.h in Headers */,
B6105185199CB1A600DC41CE /* CCRay.h in Headers */,
1A5701A3180BCB590088DEC7 /* CCFontAtlas.h in Headers */,
1A01C68618F57BE800EFE3A6 /* CCArray.h in Headers */,
1A5701A7180BCB590088DEC7 /* CCFontAtlasCache.h in Headers */,
@ -5370,6 +5395,7 @@
1A5701FD180BCBAD0088DEC7 /* CCMenuItem.h in Headers */,
1A570204180BCBD40088DEC7 /* CCClippingNode.h in Headers */,
1A01C6A618F58F7500EFE3A6 /* CCNotificationCenter.h in Headers */,
B6105181199CB1A600DC41CE /* CCOBB.h in Headers */,
1A57020A180BCBDF0088DEC7 /* CCMotionStreak.h in Headers */,
1A570212180BCBF40088DEC7 /* CCProgressTimer.h in Headers */,
1A570216180BCBF40088DEC7 /* CCRenderTexture.h in Headers */,
@ -5493,6 +5519,7 @@
50ABBDB31925AB4100A911A9 /* ccShaders.h in Headers */,
50ABBDAB1925AB4100A911A9 /* CCRenderCommandPool.h in Headers */,
5034CA45191D591100CE6051 /* ccShader_Label_outline.frag in Headers */,
B610517D199CB1A600DC41CE /* CCAABB.h in Headers */,
50ABBEB11925AB6F00A911A9 /* CCUserDefault.h in Headers */,
50ABBEC71925AB6F00A911A9 /* etc1.h in Headers */,
50ABBEA91925AB6F00A911A9 /* CCTouch.h in Headers */,
@ -5759,6 +5786,7 @@
50ABC00C1926664800A911A9 /* CCDevice.h in Headers */,
1A570205180BCBD40088DEC7 /* CCClippingNode.h in Headers */,
5034CA34191D591100CE6051 /* ccShader_PositionTexture_uColor.frag in Headers */,
B6105182199CB1A600DC41CE /* CCOBB.h in Headers */,
50ABBEA21925AB6F00A911A9 /* CCScheduler.h in Headers */,
1A57020B180BCBDF0088DEC7 /* CCMotionStreak.h in Headers */,
1A570213180BCBF40088DEC7 /* CCProgressTimer.h in Headers */,
@ -5856,6 +5884,7 @@
50ABBE821925AB6F00A911A9 /* CCEventType.h in Headers */,
1AAF5852180E40B9000584C8 /* LocalStorage.h in Headers */,
50ABBD471925AB0000A911A9 /* CCVertex.h in Headers */,
B6105186199CB1A600DC41CE /* CCRay.h in Headers */,
1A9DCA2A180E6955007A3AD4 /* CCGLBufferedNode.h in Headers */,
1A01C69F18F57BE800EFE3A6 /* CCString.h in Headers */,
464AD6E8197EBB1400E502D8 /* pvr.h in Headers */,
@ -5871,6 +5900,7 @@
B276EF641988D1D500CD400F /* CCVertexIndexBuffer.h in Headers */,
ED9C6A9718599AD8000A5232 /* CCNodeGrid.h in Headers */,
50ABC0201926664800A911A9 /* CCThread.h in Headers */,
B610517E199CB1A600DC41CE /* CCAABB.h in Headers */,
1A01C69318F57BE800EFE3A6 /* CCDouble.h in Headers */,
50ABBE221925AB6F00A911A9 /* atitc.h in Headers */,
3EA47873195478E00068D9D1 /* CCBundleReader.h in Headers */,
@ -6793,6 +6823,7 @@
46A170ED1807CECA005B8026 /* CCPhysicsShape.cpp in Sources */,
50ABBE991925AB6F00A911A9 /* CCRef.cpp in Sources */,
ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */,
B610517F199CB1A600DC41CE /* CCOBB.cpp in Sources */,
46A170E61807CECA005B8026 /* CCPhysicsBody.cpp in Sources */,
50ABBDA71925AB4100A911A9 /* CCRenderCommand.cpp in Sources */,
50ABBD501925AB0000A911A9 /* Quaternion.cpp in Sources */,
@ -6910,6 +6941,7 @@
1A01C69C18F57BE800EFE3A6 /* CCString.cpp in Sources */,
50ABBD3C1925AB0000A911A9 /* CCGeometry.cpp in Sources */,
50ABC0011926664800A911A9 /* CCLock.cpp in Sources */,
B6105183199CB1A600DC41CE /* CCRay.cpp in Sources */,
50ABBD931925AB4100A911A9 /* CCGLProgramState.cpp in Sources */,
1A57034B180BD09B0088DEC7 /* tinyxml2.cpp in Sources */,
1A570354180BD0B00088DEC7 /* ioapi.cpp in Sources */,
@ -6920,6 +6952,7 @@
50ABBE831925AB6F00A911A9 /* ccFPSImages.c in Sources */,
50ABBEA71925AB6F00A911A9 /* CCTouch.cpp in Sources */,
50ABBD8B1925AB4100A911A9 /* CCGLProgram.cpp in Sources */,
B610517B199CB1A600DC41CE /* CCAABB.cpp in Sources */,
464AD6E5197EBB1400E502D8 /* pvr.cpp in Sources */,
50ABBDA31925AB4100A911A9 /* CCQuadCommand.cpp in Sources */,
B29594C61926D61F003EEF37 /* CCObjLoader.cpp in Sources */,
@ -7142,7 +7175,9 @@
1A570072180BC5A10088DEC7 /* CCActionGrid.cpp in Sources */,
50ABBE621925AB6F00A911A9 /* CCEventListenerAcceleration.cpp in Sources */,
3E6176681960F89B00DE83F5 /* CCController-iOS.mm in Sources */,
B6105180199CB1A600DC41CE /* CCOBB.cpp in Sources */,
B29594B51926D5EC003EEF37 /* CCMeshCommand.cpp in Sources */,
B6105184199CB1A600DC41CE /* CCRay.cpp in Sources */,
50ABBE7E1925AB6F00A911A9 /* CCEventTouch.cpp in Sources */,
50ABBE6E1925AB6F00A911A9 /* CCEventListenerKeyboard.cpp in Sources */,
50ABBE461925AB6F00A911A9 /* CCEvent.cpp in Sources */,
@ -7296,6 +7331,7 @@
50ABBE4A1925AB6F00A911A9 /* CCEventAcceleration.cpp in Sources */,
46C02E0818E91123004B7456 /* xxhash.c in Sources */,
50ABBED01925AB6F00A911A9 /* TGAlib.cpp in Sources */,
B610517C199CB1A600DC41CE /* CCAABB.cpp in Sources */,
1A01C68518F57BE800EFE3A6 /* CCArray.cpp in Sources */,
503DD8E31926736A00CD74DD /* CCDevice.mm in Sources */,
50ABBEB41925AB6F00A911A9 /* CCUserDefault.mm in Sources */,

View File

@ -962,6 +962,8 @@
3EA0FB72191C844400B170C8 /* UIVideoPlayerTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3EA0FB70191C844400B170C8 /* UIVideoPlayerTest.cpp */; };
59620E8F1921E5CF002021B6 /* Bug-Child.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59620E8D1921E5CF002021B6 /* Bug-Child.cpp */; };
59620E901921E5CF002021B6 /* Bug-Child.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 59620E8D1921E5CF002021B6 /* Bug-Child.cpp */; };
5EBEECB01995247000429821 /* DrawNode3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5EBEECAE1995247000429821 /* DrawNode3D.cpp */; };
5EBEECB11995247000429821 /* DrawNode3D.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5EBEECAE1995247000429821 /* DrawNode3D.cpp */; };
A05FCACA177C124500BE600E /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 15C64822165F391E007D4F18 /* Cocoa.framework */; };
A07A521E1783A1D20073F6A7 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 15C6482E165F399D007D4F18 /* libz.dylib */; };
A07A521F1783A1D20073F6A7 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 15C64832165F3AFD007D4F18 /* Foundation.framework */; };
@ -3651,6 +3653,8 @@
46A15F9C1807A4F8005B8026 /* cocos2d_libs.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = cocos2d_libs.xcodeproj; sourceTree = "<group>"; };
59620E8D1921E5CF002021B6 /* Bug-Child.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "Bug-Child.cpp"; sourceTree = "<group>"; };
59620E8E1921E5CF002021B6 /* Bug-Child.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Bug-Child.h"; sourceTree = "<group>"; };
5EBEECAE1995247000429821 /* DrawNode3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DrawNode3D.cpp; path = Sprite3DTest/DrawNode3D.cpp; sourceTree = "<group>"; };
5EBEECAF1995247000429821 /* DrawNode3D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DrawNode3D.h; path = Sprite3DTest/DrawNode3D.h; sourceTree = "<group>"; };
70A7F72D191D3E4900F0F206 /* shaderTest.psh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = shaderTest.psh.h; sourceTree = "<group>"; };
70A7F730191D421B00F0F206 /* ShaderTest.vsh.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShaderTest.vsh.h; sourceTree = "<group>"; };
A035A71117822E9E00987F6C /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = usr/lib/libsqlite3.dylib; sourceTree = SDKROOT; };
@ -7882,6 +7886,8 @@
3E92EA7D1921A0C60094CD21 /* Sprite3DTest */ = {
isa = PBXGroup;
children = (
5EBEECAE1995247000429821 /* DrawNode3D.cpp */,
5EBEECAF1995247000429821 /* DrawNode3D.h */,
3E92EA801921A1400094CD21 /* Sprite3DTest.cpp */,
3E92EA811921A1400094CD21 /* Sprite3DTest.h */,
);
@ -9138,6 +9144,7 @@
29080DC3191B595E0066F8DF /* UIScene.cpp in Sources */,
29080D9B191B595E0066F8DF /* CustomParticleWidget.cpp in Sources */,
29080D8D191B595E0066F8DF /* CocosGUIScene.cpp in Sources */,
5EBEECB01995247000429821 /* DrawNode3D.cpp in Sources */,
1AC35BED18CECF0C00F37B72 /* CCControlStepperTest.cpp in Sources */,
1AC35C3318CECF0C00F37B72 /* PerformanceScenarioTest.cpp in Sources */,
1AC35C5918CECF0C00F37B72 /* TextureAtlasEncryptionTest.cpp in Sources */,
@ -9232,6 +9239,7 @@
29080DB4191B595E0066F8DF /* UILayoutTest_Editor.cpp in Sources */,
1AC35C4E18CECF0C00F37B72 /* SpineTest.cpp in Sources */,
1AC35C1E18CECF0C00F37B72 /* NewRendererTest.cpp in Sources */,
5EBEECB11995247000429821 /* DrawNode3D.cpp in Sources */,
1AC35B6818CECF0C00F37B72 /* AnimationsTestLayer.cpp in Sources */,
29080D8E191B595E0066F8DF /* CocosGUIScene.cpp in Sources */,
1AC35BFA18CECF0C00F37B72 /* WebSocketTest.cpp in Sources */,

View File

@ -183,6 +183,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClCompile Include="..\..\external\unzip\ioapi.cpp" />
<ClCompile Include="..\..\external\unzip\unzip.cpp" />
<ClCompile Include="..\..\external\xxhash\xxhash.c" />
<ClCompile Include="..\3d\CCAABB.cpp" />
<ClCompile Include="..\3d\CCAnimate3D.cpp" />
<ClCompile Include="..\3d\CCAnimation3D.cpp" />
<ClCompile Include="..\3d\CCAttachNode.cpp" />
@ -190,7 +191,9 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClCompile Include="..\3d\CCBundleReader.cpp" />
<ClCompile Include="..\3d\CCMesh.cpp" />
<ClCompile Include="..\3d\CCMeshSkin.cpp" />
<ClCompile Include="..\3d\CCOBB.cpp" />
<ClCompile Include="..\3d\CCObjLoader.cpp" />
<ClCompile Include="..\3d\CCRay.cpp" />
<ClCompile Include="..\3d\CCSkeleton3D.cpp" />
<ClCompile Include="..\3d\CCSprite3D.cpp" />
<ClCompile Include="..\3d\CCSprite3DMaterial.cpp" />
@ -374,6 +377,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClInclude Include="..\..\external\unzip\ioapi.h" />
<ClInclude Include="..\..\external\unzip\unzip.h" />
<ClInclude Include="..\..\external\xxhash\xxhash.h" />
<ClInclude Include="..\3d\CCAABB.h" />
<ClInclude Include="..\3d\CCAnimate3D.h" />
<ClInclude Include="..\3d\CCAnimation3D.h" />
<ClInclude Include="..\3d\CCAnimationCurve.h" />
@ -383,7 +387,9 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
<ClInclude Include="..\3d\CCBundleReader.h" />
<ClInclude Include="..\3d\CCMesh.h" />
<ClInclude Include="..\3d\CCMeshSkin.h" />
<ClInclude Include="..\3d\CCOBB.h" />
<ClInclude Include="..\3d\CCObjLoader.h" />
<ClInclude Include="..\3d\CCRay.h" />
<ClInclude Include="..\3d\CCSkeleton3D.h" />
<ClInclude Include="..\3d\CCSprite3D.h" />
<ClInclude Include="..\3d\CCSprite3DMaterial.h" />

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="physics">
@ -613,6 +613,15 @@
<ClCompile Include="..\base\CCCamera.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCAABB.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCOBB.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCRay.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\renderer\CCPrimitive.cpp">
<Filter>renderer</Filter>
</ClCompile>
@ -1261,6 +1270,15 @@
<ClInclude Include="..\base\CCCamera.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCAABB.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCOBB.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCRay.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\renderer\CCPrimitive.h">
<Filter>renderer</Filter>
</ClInclude>

184
cocos/3d/CCAABB.cpp Executable file
View File

@ -0,0 +1,184 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "3d/CCAABB.h"
NS_CC_BEGIN
AABB::AABB()
{
reset();
}
AABB::AABB(const Vec3& min, const Vec3& max)
{
set(min, max);
}
AABB::AABB(const AABB& box)
{
set(box._min,box._max);
}
Vec3 AABB::getCenter()
{
Vec3 center;
center.x = 0.5f*(_min.x+_max.x);
center.y = 0.5f*(_min.y+_max.y);
center.z = 0.5f*(_min.z+_max.z);
return center;
}
void AABB::getCorners(Vec3 *dst) const
{
assert(dst);
// Near face, specified counter-clockwise looking towards the origin from the positive z-axis.
// Left-top-front.
dst[0].set(_min.x, _max.y, _max.z);
// Left-bottom-front.
dst[1].set(_min.x, _min.y, _max.z);
// Right-bottom-front.
dst[2].set(_max.x, _min.y, _max.z);
// Right-top-front.
dst[3].set(_max.x, _max.y, _max.z);
// Far face, specified counter-clockwise looking towards the origin from the negative z-axis.
// Right-top-back.
dst[4].set(_max.x, _max.y, _min.z);
// Right-bottom-back.
dst[5].set(_max.x, _min.y, _min.z);
// Left-bottom-back.
dst[6].set(_min.x, _min.y, _min.z);
// Left-top-back.
dst[7].set(_min.x, _max.y, _min.z);
}
bool AABB::intersects(const AABB& aabb) const
{
return ((_min.x >= aabb._min.x && _min.x <= aabb._max.x) || (aabb._min.x >= _min.x && aabb._min.x <= _max.x)) &&
((_min.y >= aabb._min.y && _min.y <= aabb._max.y) || (aabb._min.y >= _min.y && aabb._min.y <= _max.y)) &&
((_min.z >= aabb._min.z && _min.z <= aabb._max.z) || (aabb._min.z >= _min.z && aabb._min.z <= _max.z));
}
bool AABB::containPoint(const Vec3& point) const
{
if (point.x < _min.x) return false;
if (point.y < _min.y) return false;
if (point.z < _min.z) return false;
if (point.x > _max.x) return false;
if (point.y > _max.y) return false;
if (point.z > _max.z) return false;
return true;
}
void AABB::merge(const AABB& box)
{
// Calculate the new minimum point.
_min.x = std::min(_min.x, box._min.x);
_min.y = std::min(_min.y, box._min.y);
_min.z = std::min(_min.z, box._min.z);
// Calculate the new maximum point.
_max.x = std::max(_max.x, box._max.x);
_max.y = std::max(_max.y, box._max.y);
_max.z = std::max(_max.z, box._max.z);
}
void AABB::set(const Vec3& min, const Vec3& max)
{
this->_min = min;
this->_max = max;
}
void AABB::reset()
{
_min.set(99999.0f, 99999.0f, 99999.0f);
_max.set(-99999.0f, -99999.0f, -99999.0f);
}
void AABB::updateMinMax(const Vec3* point, ssize_t num)
{
for (ssize_t i = 0; i < num; i++)
{
// Leftmost point.
if (point[i].x < _min.x)
_min.x = point[i].x;
// Lowest point.
if (point[i].y < _min.y)
_min.y = point[i].y;
// Farthest point.
if (point[i].z < _min.z)
_min.z = point[i].z;
// Rightmost point.
if (point[i].x > _max.x)
_max.x = point[i].x;
// Highest point.
if (point[i].y > _max.y)
_max.y = point[i].y;
// Nearest point.
if (point[i].z > _max.z)
_max.z = point[i].z;
}
}
void AABB::transform(const Mat4& mat)
{
Vec3 corners[8];
// Near face, specified counter-clockwise
// Left-top-front.
corners[0].set(_min.x, _max.y, _max.z);
// Left-bottom-front.
corners[1].set(_min.x, _min.y, _max.z);
// Right-bottom-front.
corners[2].set(_max.x, _min.y, _max.z);
// Right-top-front.
corners[3].set(_max.x, _max.y, _max.z);
// Far face, specified clockwise
// Right-top-back.
corners[4].set(_max.x, _max.y, _min.z);
// Right-bottom-back.
corners[5].set(_max.x, _min.y, _min.z);
// Left-bottom-back.
corners[6].set(_min.x, _min.y, _min.z);
// Left-top-back.
corners[7].set(_min.x, _max.y, _min.z);
// Transform the corners, recalculate the min and max points along the way.
for (int i = 0; i < 8; i++)
mat.transformPoint(&corners[i]);
reset();
updateMinMax(corners, 8);
}
NS_CC_END

114
cocos/3d/CCAABB.h Executable file
View File

@ -0,0 +1,114 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_AABB_H__
#define __CC_AABB_H__
#include "base/ccMacros.h"
#include "math/CCMath.h"
NS_CC_BEGIN
class CC_DLL AABB
{
public:
/**
* Constructor.
*/
AABB();
/**
* Constructor.
*/
AABB(const Vec3& min, const Vec3& max);
/**
* Constructor.
*/
AABB(const AABB& box);
/**
* Gets the center point of the bounding box.
*/
Vec3 getCenter();
/* Near face, specified counter-clockwise looking towards the origin from the positive z-axis.
* verts[0] : left top front
* verts[1] : left bottom front
* verts[2] : right bottom front
* verts[3] : right top front
*
* Far face, specified counter-clockwise looking towards the origin from the negative z-axis.
* verts[4] : right top back
* verts[5] : right bottom back
* verts[6] : left bottom back
* verts[7] : left top back
*/
void getCorners(Vec3 *dst) const;
/**
* Tests whether this bounding box intersects the specified bounding object.
*/
bool intersects(const AABB& aabb) const;
/**
* check whether the point is in.
*/
bool containPoint(const Vec3& point) const;
/**
* Sets this bounding box to the smallest bounding box
* that contains both this bounding object and the specified bounding box.
*/
void merge(const AABB& box);
/**
* Sets this bounding box to the specified values.
*/
void set(const Vec3& min, const Vec3& max);
/**
* reset min and max value.
*/
void reset();
/**
* update the _min and _max from the given point.
*/
void updateMinMax(const Vec3* point, ssize_t num);
/**
* Transforms the bounding box by the given transformation matrix.
*/
void transform(const Mat4& mat);
public:
Vec3 _min;
Vec3 _max;
};
NS_CC_END
#endif

View File

@ -207,6 +207,9 @@ bool Mesh::init(const std::vector<float>& positions, const std::vector<float>& n
buildSubMeshes();
buildBuffer();
calOriginAABB(positions, 3);
return true;
}
@ -220,6 +223,10 @@ bool Mesh::init(const std::vector<float>& vertices, int vertexSizeInFloat, const
buildSubMeshes();
buildBuffer();
unsigned int perVertexSize = _renderdata.calVertexSizeBytes() / sizeof(float);
calOriginAABB(vertices, perVertexSize);
return true;
}
@ -245,6 +252,16 @@ void Mesh::cleanAndFreeBuffers()
}
}
void Mesh::calOriginAABB(const std::vector<float>& vertices, unsigned int stride)
{
ssize_t vertexNum = vertices.size() / stride;
for(unsigned int i = 0; i < vertexNum; i++)
{
Vec3 point = Vec3(vertices[i * stride], vertices[i * stride + 1], vertices[i * stride + 2]);
_originAABB.updateMinMax(&point, 1);
}
}
void Mesh::buildBuffer()
{
cleanAndFreeBuffers();
@ -273,6 +290,11 @@ void Mesh::restore()
buildBuffer();
}
const AABB& Mesh::getOriginAABB() const
{
return _originAABB;
}
/**
* MeshCache
*/

View File

@ -37,6 +37,9 @@
#include "math/CCMath.h"
#include "renderer/CCGLProgram.h"
#include "CCAABB.h"
NS_CC_BEGIN
class EventListenerCustom;
@ -121,6 +124,9 @@ public:
/**build vertex buffer from renderdata*/
void restore();
/** get origin aabb that calculate from vertices*/
const AABB& getOriginAABB() const;
/**to be deprecated, those functions have been moved to SubMesh*/
/** get primitive type*/
CC_DEPRECATED_ATTRIBUTE PrimitiveType getPrimitiveType() const { return _subMeshes.at(0)->getPrimitiveType(); }
@ -147,12 +153,21 @@ CC_CONSTRUCTOR_ACCESS:
void buildBuffer();
/**free buffer*/
void cleanAndFreeBuffers();
/*
* calculate AABB by origin vertices
* @param the vertices list
* @param stride the stride between two vertex's position data.
*/
void calOriginAABB(const std::vector<float>& vertices, unsigned int stride);
protected:
GLuint _vertexBuffer;
Vector<SubMesh*> _subMeshes;
RenderMeshData _renderdata;
AABB _originAABB;
};
/**

452
cocos/3d/CCOBB.cpp Executable file
View File

@ -0,0 +1,452 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "3d/CCOBB.h"
NS_CC_BEGIN
#define ROTATE(a,i,j,k,l) g=a.m[i + 4 * j]; h=a.m[k + 4 * l]; a.m[i + 4 * j]=(float)(g-s*(h+g*tau)); a.m[k + 4 * l]=(float)(h+s*(g-h*tau));
static Mat4 _getConvarianceMatrix(const Vec3* vertPos, int vertCount)
{
int i;
Mat4 Cov;
double S1[3];
double S2[3][3];
S1[0] = S1[1] = S1[2] = 0.0;
S2[0][0] = S2[1][0] = S2[2][0] = 0.0;
S2[0][1] = S2[1][1] = S2[2][1] = 0.0;
S2[0][2] = S2[1][2] = S2[2][2] = 0.0;
// get center of mass
for(i=0; i<vertCount; i++)
{
S1[0] += vertPos[i].x;
S1[1] += vertPos[i].y;
S1[2] += vertPos[i].z;
S2[0][0] += vertPos[i].x * vertPos[i].x;
S2[1][1] += vertPos[i].y * vertPos[i].y;
S2[2][2] += vertPos[i].z * vertPos[i].z;
S2[0][1] += vertPos[i].x * vertPos[i].y;
S2[0][2] += vertPos[i].x * vertPos[i].z;
S2[1][2] += vertPos[i].y * vertPos[i].z;
}
float n = (float)vertCount;
// now get covariances
Cov.m[0] = (float)(S2[0][0] - S1[0]*S1[0] / n) / n;
Cov.m[5] = (float)(S2[1][1] - S1[1]*S1[1] / n) / n;
Cov.m[10] = (float)(S2[2][2] - S1[2]*S1[2] / n) / n;
Cov.m[4] = (float)(S2[0][1] - S1[0]*S1[1] / n) / n;
Cov.m[9] = (float)(S2[1][2] - S1[1]*S1[2] / n) / n;
Cov.m[8] = (float)(S2[0][2] - S1[0]*S1[2] / n) / n;
Cov.m[1] = Cov.m[4];
Cov.m[2] = Cov.m[8];
Cov.m[6] = Cov.m[9];
return Cov;
}
static float& _getElement( Vec3& point, int index)
{
if (index == 0)
return point.x;
if (index == 1)
return point.y;
if (index == 2)
return point.z;
CC_ASSERT(0);
return point.x;
}
static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a)
{
int n = 3;
int j,iq,ip,i;
double tresh, theta, tau, t, sm, s, h, g, c;
int nrot;
Vec3 b;
Vec3 z;
Mat4 v;
Vec3 d;
v = Mat4::IDENTITY;
for(ip = 0; ip < n; ip++)
{
_getElement(b, ip) = a.m[ip + 4 * ip];
_getElement(d, ip) = a.m[ip + 4 * ip];
_getElement(z, ip) = 0.0;
}
nrot = 0;
for(i = 0; i < 50; i++)
{
sm = 0.0;
for(ip = 0; ip < n; ip++) for(iq = ip+1; iq < n; iq++) sm += fabs(a.m[ip + 4 * iq]);
if( fabs(sm) < FLT_EPSILON )
{
v.transpose();
*vout = v;
*dout = d;
return;
}
if (i < 3)
tresh = 0.2 * sm / (n*n);
else
tresh = 0.0;
for(ip = 0; ip < n; ip++)
{
for(iq = ip + 1; iq < n; iq++)
{
g = 100.0 * fabs(a.m[ip + iq * 4]);
float dmip = _getElement(d, ip);
float dmiq = _getElement(d, iq);
if( i>3 && fabs(dmip) + g == fabs(dmip) && fabs(dmiq) + g == fabs(dmiq) )
{
a.m[ip + 4 * iq] = 0.0;
}
else if (fabs(a.m[ip + 4 * iq]) > tresh)
{
h = dmiq - dmip;
if (fabs(h) + g == fabs(h))
{
t=(a.m[ip + 4 * iq])/h;
}
else
{
theta = 0.5 * h / (a.m[ip + 4 * iq]);
t=1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
if (theta < 0.0) t = -t;
}
c = 1.0 / sqrt(1+t*t);
s = t*c;
tau = s / (1.0+c);
h = t * a.m[ip + 4 * iq];
_getElement(z, ip) -= (float)h;
_getElement(z, iq) += (float)h;
_getElement(d, ip) -= (float)h;
_getElement(d, iq) += (float)h;
a.m[ip + 4 * iq]=0.0;
for(j = 0; j < ip; j++) { ROTATE(a,j,ip,j,iq); }
for(j = ip + 1; j < iq; j++) { ROTATE(a,ip,j,j,iq); }
for(j = iq + 1; j < n; j++) { ROTATE(a,ip,j,iq,j); }
for(j = 0; j < n; j++) { ROTATE(v,j,ip,j,iq); }
nrot++;
}
}
}
for(ip = 0; ip < n; ip++)
{
_getElement(b, ip) += _getElement(z, ip);
_getElement(d, ip) = _getElement(b, ip);
_getElement(z, ip) = 0.0f;
}
}
v.transpose();
*vout = v;
*dout = d;
return;
}
static Mat4 _getOBBOrientation(const Vec3* vertPos, int num)
{
Mat4 Cov;
if (num <= 0)
return Mat4::IDENTITY;
Cov = _getConvarianceMatrix(vertPos, num);
// now get eigenvectors
Mat4 Evecs;
Vec3 Evals;
_getEigenVectors(&Evecs, &Evals, Cov);
Evecs.transpose();
return Evecs;
}
OBB::OBB()
{
reset();
}
OBB::OBB(const AABB& aabb)
{
reset();
_center = (aabb._min + aabb._max);
_center.scale(0.5f);
_xAxis = Vec3(1.0f, 0.0f, 0.0f);
_yAxis = Vec3(0.0f, 1.0f, 0.0f);
_zAxis = Vec3(0.0f, 0.0f, 1.0f);
_extents = aabb._max - aabb._min;
_extents.scale(0.5f);
}
OBB::OBB(const Vec3* verts, int num)
{
if (!verts) return;
reset();
Mat4 matTransform = _getOBBOrientation(verts, num);
// For matTransform is orthogonal, so the inverse matrix is just rotate it;
matTransform.transpose();
Vec3 vecMax = matTransform * Vec3(verts[0].x, verts[0].y, verts[0].z);
Vec3 vecMin = vecMax;
for (int i = 1; i < num; i++)
{
Vec3 vect = matTransform * Vec3(verts[i].x, verts[i].y, verts[i].z);
vecMax.x = vecMax.x > vect.x ? vecMax.x : vect.x;
vecMax.y = vecMax.y > vect.y ? vecMax.y : vect.y;
vecMax.z = vecMax.z > vect.z ? vecMax.z : vect.z;
vecMin.x = vecMin.x < vect.x ? vecMin.x : vect.x;
vecMin.y = vecMin.y < vect.y ? vecMin.y : vect.y;
vecMin.z = vecMin.z < vect.z ? vecMin.z : vect.z;
}
matTransform.transpose();
_xAxis = Vec3(matTransform.m[0], matTransform.m[1], matTransform.m[2]);
_yAxis = Vec3(matTransform.m[4], matTransform.m[5], matTransform.m[6]);
_zAxis = Vec3(matTransform.m[8], matTransform.m[9], matTransform.m[10]);
_center = 0.5f * (vecMax + vecMin);
_center *= matTransform;
_xAxis.normalize();
_yAxis.normalize();
_zAxis.normalize();
_extents = 0.5f * (vecMax - vecMin);
}
bool OBB::containPoint(const Vec3& point) const
{
Vec3 vd = point - _center;
float d = vd.dot(_xAxis);
if (d > _extents.x || d < -_extents.x)
return false;
d = vd.dot(_yAxis);
if (d > _extents.y || d < -_extents.y)
return false;
d = vd.dot(_zAxis);
if (d > _extents.z || d < -_extents.z)
return false;
return true;
}
void OBB::set(const Vec3& center, const Vec3& xAxis, const Vec3& yAxis, const Vec3& zAxis, const Vec3& extents)
{
_center = center;
_xAxis = xAxis;
_yAxis = yAxis;
_zAxis = zAxis;
_extents = extents;
}
void OBB::reset()
{
memset(this, 0, sizeof(OBB));
}
void OBB::getCorners(Vec3* verts) const
{
Vec3 extX = _xAxis * _extents.x;
Vec3 extY = _yAxis * _extents.y;
Vec3 extZ = _zAxis * _extents.z;
verts[0] = _center - extX + extY + extZ; // left top front
verts[1] = _center - extX - extY + extZ; // left bottom front
verts[2] = _center + extX - extY + extZ; // right bottom front
verts[3] = _center + extX + extY + extZ; // right top front
verts[4] = _center + extX + extY - extZ; // right top back
verts[5] = _center + extX - extY - extZ; // right bottom back
verts[6] = _center - extX - extY - extZ; // left bottom back
verts[7] = _center - extX + extY - extZ; // left top back
}
float OBB::projectPoint(const Vec3& point, const Vec3& axis)const
{
float dot = axis.dot(point);
float ret = dot * point.length();
return ret;
}
void OBB::getInterval(const OBB& box, const Vec3& axis, float &min, float &max)const
{
Vec3 corners[8];
box.getCorners(corners);
float value;
min = max = projectPoint(axis, corners[0]);
for(int i = 1; i < 8; i++)
{
value = projectPoint(axis, corners[i]);
min = MIN(min, value);
max = MAX(max, value);
}
}
Vec3 OBB::getEdgeDirection(int index)const
{
Vec3 corners[8];
getCorners(corners);
Vec3 tmpLine;
switch(index)
{
case 0:// edge with x axis
tmpLine = corners[5] - corners[6];
tmpLine.normalize();
break;
case 1:// edge with y axis
tmpLine = corners[7] - corners[6];
tmpLine.normalize();
break;
case 2:// edge with z axis
tmpLine = corners[1] - corners[6];
tmpLine.normalize();
break;
default:
CCASSERT(0, "Invalid index!");
break;
}
return tmpLine;
}
Vec3 OBB::getFaceDirection(int index) const
{
Vec3 corners[8];
getCorners(corners);
Vec3 faceDirection, v0, v1;
switch(index)
{
case 0:// front and back
v0 = corners[2] - corners[1];
v1 = corners[0] - corners[1];
Vec3::cross(v0, v1, &faceDirection);
faceDirection.normalize();
break;
case 1:// left and right
v0 = corners[5] - corners[2];
v1 = corners[3] - corners[2];
Vec3::cross(v0, v1, &faceDirection);
faceDirection.normalize();
break;
case 2:// top and bottom
v0 = corners[1] - corners[2];
v1 = corners[5] - corners[2];
Vec3::cross(v0, v1, &faceDirection);
faceDirection.normalize();
break;
default:
CCASSERT(0, "Invalid index!");
break;
}
return faceDirection;
}
bool OBB::intersects(const OBB& box) const
{
float min1, max1, min2, max2;
for (int i = 0; i < 3; i++)
{
getInterval(*this, getFaceDirection(i), min1, max1);
getInterval(box, getFaceDirection(i), min2, max2);
if (max1 < min2 || max2 < min1) return false;
}
for (int i = 0; i < 3; i++)
{
getInterval(*this, box.getFaceDirection(i), min1, max1);
getInterval(box, box.getFaceDirection(i), min2, max2);
if (max1 < min2 || max2 < min1) return false;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Vec3 axis;
Vec3::cross(getFaceDirection(i), box.getFaceDirection(j), &axis);
getInterval(*this, axis, min1, max1);
getInterval(box, axis, min2, max2);
if (max1 < min2 || max2 < min1) return false;
}
}
return true;
}
void OBB::transform(const Mat4& mat)
{
Vec4 newcenter = mat * Vec4(_center.x, _center.y, _center.z, 1.0f);// center;
_center.x = newcenter.x;
_center.y = newcenter.y;
_center.z = newcenter.z;
_xAxis = mat * _xAxis;
_yAxis = mat * _yAxis;
_zAxis = mat * _zAxis;
_xAxis.normalize();
_yAxis.normalize();
_zAxis.normalize();
Vec3 scale, trans;
Quaternion quat;
mat.decompose(&scale, &quat, &trans);
_extents.x *= scale.x;
_extents.y *= scale.y;
_extents.z *= scale.z;
}
NS_CC_END

117
cocos/3d/CCOBB.h Executable file
View File

@ -0,0 +1,117 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_OBB_H__
#define __CC_OBB_H__
#include "CCAABB.h"
NS_CC_BEGIN
class CC_DLL OBB
{
public:
OBB();
/*
* Construct obb from oriented bounding box
*/
OBB(const AABB& aabb);
/*
* Construct obb from points
*/
OBB(const Vec3* verts, int num);
/*
* Check point in
*/
bool containPoint(const Vec3& point) const;
/*
* Specify obb values
*/
void set(const Vec3& center, const Vec3& _xAxis, const Vec3& _yAxis, const Vec3& _zAxis, const Vec3& _extents);
/*
* Clear obb
*/
void reset();
/* face to the obb's -z direction
* verts[0] : left top front
* verts[1] : left bottom front
* verts[2] : right bottom front
* verts[3] : right top front
*
* face to the obb's z direction
* verts[4] : right top back
* verts[5] : right bottom back
* verts[6] : left bottom back
* verts[7] : left top back
*/
void getCorners(Vec3* verts) const;
/*
* Check intersect with other
*/
bool intersects(const OBB& box) const;
/**
* Transforms the obb by the given transformation matrix.
*/
void transform(const Mat4& mat);
protected:
/*
* Project point to the target axis
*/
float projectPoint(const Vec3& point, const Vec3& axis) const;
/*
* Calculate the min and max project value of through the box's corners
*/
void getInterval(const OBB& box, const Vec3& axis, float &min, float &max) const;
/*
* Get the edege of x y z axis direction
*/
Vec3 getEdgeDirection(int index) const;
/*
* Get the face of x y z axis direction
*/
Vec3 getFaceDirection(int index) const;
public:
Vec3 _center; // obb center
Vec3 _xAxis; // x axis of obb, unit vector
Vec3 _yAxis; // y axis of obb, unit vecotr
Vec3 _zAxis; // z axis of obb, unit vector
Vec3 _extents; // obb length along each axis
};
NS_CC_END
#endif

158
cocos/3d/CCRay.cpp Executable file
View File

@ -0,0 +1,158 @@
/****************************************************************************
Copyright (c) Chukong Technologies Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCRay.h"
#include "CCAABB.h"
#include "CCOBB.h"
NS_CC_BEGIN
Ray::Ray()
: _direction(0, 0, 1)
{
}
Ray::Ray(const Vec3& origin, const Vec3& direction)
{
set(origin, direction);
}
Ray::~Ray()
{
}
bool Ray::intersects(const AABB& aabb) const
{
Vec3 ptOnPlane;
Vec3 min = aabb._min;
Vec3 max = aabb._max;
const Vec3& origin = _origin;
const Vec3& dir = _direction;
float t;
if (dir.x != 0.f)
{
if (dir.x > 0)
t = (min.x - origin.x) / dir.x;
else
t = (max.x - origin.x) / dir.x;
if (t > 0.f)
{
ptOnPlane = origin + t * dir;
if (min.y < ptOnPlane.y && ptOnPlane.y < max.y && min.z < ptOnPlane.z && ptOnPlane.z < max.z)
{
return true;
}
}
}
if (dir.y != 0.f)
{
if (dir.y > 0)
t = (min.y - origin.y) / dir.y;
else
t = (max.y - origin.y) / dir.y;
if (t > 0.f)
{
ptOnPlane = origin + t * dir;
if (min.z < ptOnPlane.z && ptOnPlane.z < max.z && min.x < ptOnPlane.x && ptOnPlane.x < max.x)
{
return true;
}
}
}
if (dir.z != 0.f)
{
if (dir.z > 0)
t = (min.z - origin.z) / dir.z;
else
t = (max.z - origin.z) / dir.z;
if (t > 0.f)
{
ptOnPlane = origin + t * dir;
if (min.x < ptOnPlane.x && ptOnPlane.x < max.x && min.y < ptOnPlane.y && ptOnPlane.y < max.y)
{
return true;
}
}
}
return false;
}
bool Ray::intersects(const OBB& obb) const
{
AABB aabb;
aabb._min = - obb._extents;
aabb._max = obb._extents;
Ray ray;
ray._direction = _direction;
ray._origin = _origin;
Mat4 mat = Mat4::IDENTITY;
mat.m[0] = obb._xAxis.x;
mat.m[1] = obb._xAxis.y;
mat.m[2] = obb._xAxis.z;
mat.m[4] = obb._yAxis.x;
mat.m[5] = obb._yAxis.y;
mat.m[6] = obb._yAxis.z;
mat.m[8] = obb._zAxis.x;
mat.m[9] = obb._zAxis.y;
mat.m[10] = obb._zAxis.z;
mat.m[12] = obb._center.x;
mat.m[13] = obb._center.y;
mat.m[14] = obb._center.z;
mat = mat.getInversed();
ray.transform(mat);
return ray.intersects(aabb);
}
void Ray::set(const Vec3& origin, const Vec3& direction)
{
_origin = origin;
_direction = direction;
}
void Ray::transform(const Mat4& matrix)
{
matrix.transformPoint(&_origin);
matrix.transformVector(&_direction);
_direction.normalize();
}
NS_CC_END

92
cocos/3d/CCRay.h Executable file
View File

@ -0,0 +1,92 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_RAY_H_
#define __CC_RAY_H_
#include "base/ccMacros.h"
#include "math/CCMath.h"
#include "CCAABB.h"
#include "CCOBB.h"
NS_CC_BEGIN
class CC_DLL Ray
{
public:
/**
* Constructor.
*/
Ray();
/**
* Constructor.
*/
Ray(const Ray& ray);
/**
* Constructs a new ray initialized to the specified values.
*
* @param origin The ray's origin.
* @param direction The ray's direction.
*/
Ray(const Vec3& origin, const Vec3& direction);
/**
* Destructor.
*/
~Ray();
/**
* Check whether this ray intersects the specified bounding box.
*/
bool intersects(const AABB& aabb) const;
/**
* Check whether this ray intersects the specified obb.
*/
bool intersects(const OBB& obb) const;
/**
* Sets this ray to the specified values.
*
* @param origin The ray's origin.
* @param direction The ray's direction.
*/
void set(const Vec3& origin, const Vec3& direction);
/**
* Transforms this ray by the given transformation matrix.
*
* @param matrix The transformation matrix to transform by.
*/
void transform(const Mat4& matrix);
Vec3 _origin; // The ray origin position.
Vec3 _direction; // The ray direction vector.
};
NS_CC_END
#endif

View File

@ -399,6 +399,39 @@ const BlendFunc& Sprite3D::getBlendFunc() const
return _blend;
}
AABB Sprite3D::getAABB() const
{
Mat4 nodeToWorldTransform(getNodeToWorldTransform());
// If nodeToWorldTransform matrix isn't changed, we don't need to transform aabb.
if (memcmp(_nodeToWorldTransform.m, nodeToWorldTransform.m, sizeof(Mat4)) == 0)
{
return _aabb;
}
else
{
Mat4 transform(nodeToWorldTransform);
_aabb = _mesh->getOriginAABB();
if (getSkin() && getSkin()->getRootBone())
{
transform = nodeToWorldTransform * getSkin()->getRootBone()->getWorldMat();
}
_aabb.transform(transform);
_nodeToWorldTransform = nodeToWorldTransform;
}
return _aabb;
}
Rect Sprite3D::getBoundingBox() const
{
AABB aabb = getAABB();
Rect ret(aabb._min.x, aabb._min.y, (aabb._max.x - aabb._min.x), (aabb._max.y - aabb._min.y));
return ret;
}
void Sprite3D::setCullFace(GLenum cullFace)
{
for (auto& it : _meshCommands) {

View File

@ -33,6 +33,7 @@
#include "base/CCProtocols.h"
#include "2d/CCNode.h"
#include "renderer/CCMeshCommand.h"
#include "CCAABB.h"
NS_CC_BEGIN
@ -80,6 +81,20 @@ public:
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
virtual const BlendFunc &getBlendFunc() const override;
/*
* Get AABB
* If the sprite has animation, it can't be calculated accuratly,
* because bone can drive the vertices, we just use the origin vertices
* to calculate the AABB.
*/
AABB getAABB() const;
/**
* Returns 2d bounding-box
* Note: the bouding-box is just get from the AABB which as Z=0, so that is not very accurate.
*/
virtual Rect getBoundingBox() const override;
// set which face is going to cull, GL_BACK, GL_FRONT, GL_FRONT_AND_BACK, default GL_BACK
void setCullFace(GLenum cullFace);
// set cull face enable or not
@ -124,6 +139,9 @@ protected:
std::unordered_map<std::string, AttachNode*> _attachments;
BlendFunc _blend;
mutable AABB _aabb; // cache current aabb
mutable Mat4 _nodeToWorldTransform; // cache the matrix
};
extern std::string CC_DLL s_attributeNames[];//attribute names array

View File

@ -1,4 +1,7 @@
set(COCOS_3D_SRC
3d/CCAABB.cpp
3d/CCOBB.cpp
3d/CCRay.cpp
3d/CCSkeleton3D.cpp
3d/CCAttachNode.cpp
3d/CCSubMesh.cpp

View File

@ -74,6 +74,9 @@ cocos2d.cpp \
2d/CCTransitionPageTurn.cpp \
2d/CCTransitionProgress.cpp \
2d/CCTweenFunction.cpp \
3d/CCRay.cpp\
3d/CCAABB.cpp \
3d/CCOBB.cpp \
3d/CCAnimate3D.cpp \
3d/CCAnimation3D.cpp \
3d/CCAttachNode.cpp \

View File

@ -262,6 +262,9 @@ THE SOFTWARE.
#include "2d/CCComponentContainer.h"
//3d
#include "3d/CCAABB.h"
#include "3d/CCOBB.h"
#include "3d/CCRay.h"
#include "3d/CCSprite3D.h"
#include "3d/CCMesh.h"
#include "3d/CCMeshSkin.h"

View File

@ -192,6 +192,10 @@
"cocos/2d/cocos2dx.props",
"cocos/2d/libcocos2d.vcxproj",
"cocos/2d/libcocos2d.vcxproj.filters",
"cocos/3d/CCAABB.cpp",
"cocos/3d/CCAABB.h,
"cocos/3d/CCOBB.cpp",
"cocos/3d/CCOBB.h,
"cocos/3d/CCAnimate3D.cpp",
"cocos/3d/CCAnimate3D.h",
"cocos/3d/CCAnimation3D.cpp",

View File

@ -168,6 +168,7 @@ Classes/ShaderTest/ShaderTest.cpp \
Classes/ShaderTest/ShaderTest2.cpp \
Classes/SpineTest/SpineTest.cpp \
Classes/SpriteTest/SpriteTest.cpp \
Classes/Sprite3DTest/DrawNode3D.cpp \
Classes/Sprite3DTest/Sprite3DTest.cpp \
Classes/TextInputTest/TextInputTest.cpp \
Classes/Texture2dTest/Texture2dTest.cpp \

View File

@ -162,6 +162,7 @@ set(SAMPLE_SRC
Classes/ShaderTest/ShaderTest2.cpp
Classes/SpriteTest/SpriteTest.cpp
Classes/Sprite3DTest/Sprite3DTest.cpp
Classes/Sprite3DTest/DrawNode3D.cpp
Classes/TextInputTest/TextInputTest.cpp
Classes/Texture2dTest/Texture2dTest.cpp
Classes/TexturePackerEncryptionTest/TextureAtlasEncryptionTest.cpp

View File

@ -0,0 +1,240 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "DrawNode3D.h"
#include "CCGL.h"
#include "base/CCEventType.h"
#include "base/CCConfiguration.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCGLProgramState.h"
#include "base/CCDirector.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h"
NS_CC_BEGIN
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 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

View File

@ -0,0 +1,117 @@
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __DRAW_NODE_3D_H__
#define __DRAW_NODE_3D_H__
#include "2d/CCNode.h"
#include "base/ccTypes.h"
#include "renderer/CCCustomCommand.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);
};
NS_CC_END
#endif // __DRAW_NODE_3D_H__

View File

@ -28,6 +28,8 @@
#include "3d/CCAnimate3D.h"
#include "3d/CCSubMesh.h"
#include "3d/CCAttachNode.h"
#include "3d/CCRay.h"
#include "DrawNode3D.h"
#include <algorithm>
#include "../testResource.h"
@ -56,6 +58,7 @@ static std::function<Layer*()> createFunctions[] =
#endif
CL(Animate3DTest),
CL(AttachmentTest),
CL(Sprite3DWithOBBPerfromanceTest),
CL(Sprite3DMirrorTest)
};
@ -995,6 +998,261 @@ void AttachmentTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* e
_hasWeapon = !_hasWeapon;
}
Sprite3DWithOBBPerfromanceTest::Sprite3DWithOBBPerfromanceTest()
{
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(Sprite3DWithOBBPerfromanceTest::onTouchesBegan, this);
listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DWithOBBPerfromanceTest::onTouchesEnded, this);
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DWithOBBPerfromanceTest::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto s = Director::getInstance()->getWinSize();
initDrawBox();
addNewSpriteWithCoords(Vec2(s.width/2, s.height/2));
MenuItemFont::setFontName("fonts/arial.ttf");
MenuItemFont::setFontSize(65);
auto decrease = MenuItemFont::create(" - ", CC_CALLBACK_1(Sprite3DWithOBBPerfromanceTest::delOBBCallback, this));
decrease->setColor(Color3B(0,200,20));
auto increase = MenuItemFont::create(" + ", CC_CALLBACK_1(Sprite3DWithOBBPerfromanceTest::addOBBCallback, this));
increase->setColor(Color3B(0,200,20));
auto menu = Menu::create(decrease, increase, nullptr);
menu->alignItemsHorizontally();
menu->setPosition(Vec2(s.width/2, s.height-65));
addChild(menu, 1);
TTFConfig ttfCount("fonts/Marker Felt.ttf", 30);
_labelCubeCount = Label::createWithTTF(ttfCount,"0 cubes");
_labelCubeCount->setColor(Color3B(0,200,20));
_labelCubeCount->setPosition(Vec2(s.width/2, s.height-90));
addChild(_labelCubeCount);
_hasCollider = false;
scheduleUpdate();
}
std::string Sprite3DWithOBBPerfromanceTest::title() const
{
return "OBB Collison Perfromance Test";
}
std::string Sprite3DWithOBBPerfromanceTest::subtitle() const
{
return "";
}
void Sprite3DWithOBBPerfromanceTest::addNewOBBWithCoords(Vec2 p)
{
Vec3 extents = Vec3(10, 10, 10);
AABB aabb(-extents, extents);
auto obb = OBB(aabb);
obb._center = Vec3(p.x,p.y,0);
_obb.push_back(obb);
}
void Sprite3DWithOBBPerfromanceTest::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
{
for (auto touch: touches)
{
auto location = touch->getLocationInView();
if(_obb.size() > 0)
{
_intersetList.clear();
Ray ray;
calculateRayByLocationInView(&ray,location);
for(int i = 0; i < _obb.size(); i++)
{
if(ray.intersects(_obb[i]))
{
_intersetList.insert(i);
return;
}
}
}
}
}
void Sprite3DWithOBBPerfromanceTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
}
void Sprite3DWithOBBPerfromanceTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
for (auto touch: touches)
{
auto location = touch->getLocation();
for(int i = 0; i < _obb.size(); i++)
{
if(_intersetList.find(i) != _intersetList.end())
_obb[i]._center = Vec3(location.x,location.y,0);
}
}
}
void Sprite3DWithOBBPerfromanceTest::update(float dt)
{
char szText[16];
sprintf(szText,"%lu cubes",_obb.size());
_labelCubeCount->setString(szText);
if (_drawDebug)
{
_drawDebug->clear();
Mat4 mat = _sprite->getNodeToWorldTransform();
mat.getRightVector(&_obbt._xAxis);
_obbt._xAxis.normalize();
mat.getUpVector(&_obbt._yAxis);
_obbt._yAxis.normalize();
mat.getForwardVector(&_obbt._zAxis);
_obbt._zAxis.normalize();
_obbt._center = _sprite->getPosition3D();
Vec3 corners[8] = {};
_obbt.getCorners(corners);
_drawDebug->drawCube(corners, Color4F(0,0,1,1));
}
if(_obb.size() > 0)
{
_drawOBB->clear();
for(int i =0; i < _obb.size(); i++)
{
Vec3 corners[8] = {};
_obb[i].getCorners(corners);
_drawOBB->drawCube(corners, _obbt.intersects(_obb[i])?Color4F(1,0,0,1):Color4F(0,1,0,1));
}
}
}
void Sprite3DWithOBBPerfromanceTest::initDrawBox()
{
_drawOBB = DrawNode3D::create();
addChild(_drawOBB);
}
void Sprite3DWithOBBPerfromanceTest::addNewSpriteWithCoords(Vec2 p)
{
std::string fileName = "Sprite3DTest/tortoise.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(0.1f);
auto s = Director::getInstance()->getWinSize();
sprite->setPosition(Vec2(s.width * 4.f / 5.f, s.height / 2.f));
addChild(sprite);
_sprite = sprite;
auto animation = Animation3D::create(fileName);
if (animation)
{
auto animate = Animate3D::create(animation, 0.f, 1.933f);
sprite->runAction(RepeatForever::create(animate));
}
_moveAction = MoveTo::create(4.f, Vec2(s.width / 5.f, s.height / 2.f));
_moveAction->retain();
auto seq = Sequence::create(_moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerfromanceTest::reachEndCallBack, this)), nullptr);
seq->setTag(100);
sprite->runAction(seq);
AABB aabb = _sprite->getAABB();
_obbt = OBB(aabb);
_drawDebug = DrawNode3D::create();
addChild(_drawDebug);
}
void Sprite3DWithOBBPerfromanceTest::reachEndCallBack()
{
_sprite->stopActionByTag(100);
auto inverse = (MoveTo*)_moveAction->reverse();
inverse->retain();
_moveAction->release();
_moveAction = inverse;
auto rot = RotateBy::create(1.0f, Vec3(0.f, 180.f, 0.f));
auto seq = Sequence::create(rot, _moveAction, CallFunc::create(CC_CALLBACK_0(Sprite3DWithOBBPerfromanceTest::reachEndCallBack, this)), nullptr);
seq->setTag(100);
_sprite->runAction(seq);
}
void Sprite3DWithOBBPerfromanceTest::addOBBCallback(Ref* sender)
{
addOBBWithCount(10);
}
void Sprite3DWithOBBPerfromanceTest::addOBBWithCount(float value)
{
for(int i = 0; i < value; i++)
{
Vec2 randompos = Vec2(CCRANDOM_0_1() * Director::getInstance()->getWinSize().width,CCRANDOM_0_1() * Director::getInstance()->getWinSize().height);
Vec3 extents = Vec3(10, 10, 10);
AABB aabb(-extents, extents);
auto obb = OBB(aabb);
obb._center = Vec3(randompos.x,randompos.y,0);
_obb.push_back(obb);
}
}
void Sprite3DWithOBBPerfromanceTest::delOBBCallback(Ref* sender)
{
delOBBWithCount(10);
}
void Sprite3DWithOBBPerfromanceTest::delOBBWithCount(float value)
{
if(_obb.size() >= 10)
{
_obb.erase(_obb.begin(),_obb.begin() + value);
_drawOBB->clear();
}
else
return;
}
void Sprite3DWithOBBPerfromanceTest::unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst)
{
assert(dst);
assert(viewport->width != 0.0f && viewport->height != 0.0f);
Vec4 screen(src->x / viewport->width, ((viewport->height - src->y)) / viewport->height, src->z, 1.0f);
screen.x = screen.x * 2.0f - 1.0f;
screen.y = screen.y * 2.0f - 1.0f;
screen.z = screen.z * 2.0f - 1.0f;
viewProjection.getInversed().transformVector(screen, &screen);
if (screen.w != 0.0f)
{
screen.x /= screen.w;
screen.y /= screen.w;
screen.z /= screen.w;
}
dst->set(screen.x, screen.y, screen.z);
}
void Sprite3DWithOBBPerfromanceTest::calculateRayByLocationInView(Ray* ray, const Vec2& location)
{
auto dir = Director::getInstance();
auto view = dir->getWinSize();
Mat4 mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Vec3 src = Vec3(location.x, location.y, -1);
Vec3 nearPoint;
unproject(mat, &view, &src, &nearPoint);
src = Vec3(location.x, location.y, 1);
Vec3 farPoint;
unproject(mat, &view, &src, &farPoint);
Vec3 direction;
Vec3::subtract(farPoint, nearPoint, &direction);
direction.normalize();
ray->_origin = nearPoint;
ray->_direction = direction;
}
Sprite3DMirrorTest::Sprite3DMirrorTest()
: _sprite(nullptr)
, _mirrorSprite(nullptr)

View File

@ -33,6 +33,8 @@ namespace cocos2d {
class Animate3D;
class Sprite3D;
class Delay;
class Ray;
class DrawNode3D;
}
class Sprite3DTestDemo : public BaseTest
@ -244,6 +246,40 @@ protected:
cocos2d::Sprite3D* _sprite;
};
class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DWithOBBPerfromanceTest);
Sprite3DWithOBBPerfromanceTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual void update(float dt) override;
void addNewOBBWithCoords(Vec2 p);
void addNewSpriteWithCoords(Vec2 p);
void onTouchesBegan(const std::vector<Touch*>& touches, Event* event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event* event);
void addOBBCallback(Ref* sender);
void delOBBCallback(Ref* sender);
void addOBBWithCount(float value);
void delOBBWithCount(float value);
protected:
cocos2d::Sprite3D* _sprite;
std::vector<OBB> _obb;
DrawNode3D* _drawOBB;
Label* _labelCubeCount;
MoveTo* _moveAction;
OBB _obbt;
DrawNode3D* _drawDebug;
bool _hasCollider;
std::set<int> _intersetList;
void initDrawBox();
void reachEndCallBack();
void unproject(const Mat4& viewProjection, const Size* viewport, Vec3* src, Vec3* dst);
void calculateRayByLocationInView(Ray* ray, const Vec2& location);
};
class Sprite3DMirrorTest : public Sprite3DTestDemo
{
public:

View File

@ -183,6 +183,7 @@
<ClCompile Include="..\Classes\ReleasePoolTest\ReleasePoolTest.cpp" />
<ClCompile Include="..\Classes\ShaderTest\ShaderTest2.cpp" />
<ClCompile Include="..\Classes\SpineTest\SpineTest.cpp" />
<ClCompile Include="..\Classes\Sprite3DTest\DrawNode3D.cpp" />
<ClCompile Include="..\Classes\Sprite3DTest\Sprite3DTest.cpp" />
<ClCompile Include="..\Classes\TexturePackerEncryptionTest\TextureAtlasEncryptionTest.cpp" />
<ClCompile Include="..\Classes\TileMapTest\TileMapTest2.cpp" />
@ -370,6 +371,7 @@
<ClInclude Include="..\Classes\ReleasePoolTest\ReleasePoolTest.h" />
<ClInclude Include="..\Classes\ShaderTest\ShaderTest2.h" />
<ClInclude Include="..\Classes\SpineTest\SpineTest.h" />
<ClInclude Include="..\Classes\Sprite3DTest\DrawNode3D.h" />
<ClInclude Include="..\Classes\Sprite3DTest\Sprite3DTest.h" />
<ClInclude Include="..\Classes\TexturePackerEncryptionTest\TextureAtlasEncryptionTest.h" />
<ClInclude Include="..\Classes\TileMapTest\TileMapTest2.h" />

View File

@ -864,6 +864,9 @@
<ClCompile Include="..\Classes\Camera3DTest\Camera3DTest.cpp">
<Filter>Classes\Camera3DTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\Sprite3DTest\DrawNode3D.cpp">
<Filter>Classes\Sprite3DTest</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -1595,5 +1598,8 @@
<ClInclude Include="..\Classes\Camera3DTest\Camera3DTest.h">
<Filter>Classes\Camera3DTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\Sprite3DTest\DrawNode3D.h">
<Filter>Classes\Sprite3DTest</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -26,7 +26,7 @@ headers = %(cocosdir)s/cocos/cocos2d.h %(cocosdir)s/cocos/2d/CCProtectedNode.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 = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* .*TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Ref$ UserDefault GLViewImpl GLView Image Event(?!.*(Physics).*).* Component ProtectedNode Console Mesh GLProgramCache GLProgramState SubMeshState AttachNode Camera
classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* .*TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Ref$ UserDefault GLViewImpl GLView Image Event(?!.*(Physics).*).* Component ProtectedNode Console Mesh GLProgramCache GLProgramState SubMeshState AttachNode Camera Ray AABB OBB
# 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
@ -126,14 +126,17 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
TurnOffTiles::[shuffle],
LabelTTF::[*],
LabelBMFont::[*],
Mesh::[create],
Mesh::[create getOriginAABB],
SubMeshState::[getSkin setSkin create],
Sprite3D::[getSkin getSubMeshState],
Sprite3D::[getSkin getSubMeshState getAABB],
Animation3D::[getBoneCurveByName],
Animation3DCache::[*],
Sprite3DMaterialCache::[*],
Bone3D::[*],
Scene::[getCameras]
Scene::[getCameras],
Ray::[*],
AABB::[*],
OBB::[*]
rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame],
ProgressTimer::[setReverseProgress=setReverseDirection],