From dccd1b40deb829c2890d43e9571dccb6d6416026 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Wed, 8 Jan 2014 16:28:18 -0800 Subject: [PATCH 1/5] Adds a new test that shows the problem with the Camera Apprently the bug is in QuadCommand, since if we use CustomCommand the camera works OK. --- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 122 ++++++++++++++++++ .../Cpp/TestCpp/Classes/NodeTest/NodeTest.h | 18 +++ 2 files changed, 140 insertions(+) diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index 77c7c907d7..5c2534dee0 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -25,6 +25,7 @@ static int sceneIdx = -1; static std::function createFunctions[] = { + CL(CameraTest2), CL(CameraCenterTest), CL(Test2), CL(Test4), @@ -827,6 +828,127 @@ std::string NodeNonOpaqueTest::subtitle() const return "Node rendered with GL_BLEND enabled"; } +//------------------------------------------------------------------ +// +// CameraTest2 +// +//------------------------------------------------------------------ + +class MySprite : public Sprite +{ +public: + static MySprite* create(const std::string &spritefilename) + { + auto sprite = new MySprite; + sprite->initWithFile(spritefilename); + sprite->autorelease(); + + auto shader = CCShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR); + sprite->setShaderProgram(shader); + return sprite; + } + virtual void draw() override; + void onDraw(); + +protected: + CustomCommand _customCommand; +}; + +void MySprite::draw() +{ + _customCommand.init(0, _vertexZ); + _customCommand.func = CC_CALLBACK_0(MySprite::onDraw, this); + Director::getInstance()->getRenderer()->addCommand(&_customCommand); +} + +void MySprite::onDraw() +{ + CC_NODE_DRAW_SETUP(); + + GL::blendFunc( _blendFunc.src, _blendFunc.dst ); + + GL::bindTexture2D( _texture->getName() ); + GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX ); + +#define kQuadSize sizeof(_quad.bl) + long offset = (long)&_quad; + + // vertex + int diff = offsetof( V3F_C4B_T2F, vertices); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)); + + // texCoods + diff = offsetof( V3F_C4B_T2F, texCoords); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)); + + // color + diff = offsetof( V3F_C4B_T2F, colors); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)); + + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + CHECK_GL_ERROR_DEBUG(); + CC_INCREMENT_GL_DRAWS(1); +} +void CameraTest2::onEnter() +{ + TestCocosNodeDemo::onEnter(); + Director::getInstance()->setProjection(Director::Projection::_3D); + Director::getInstance()->setDepthTest(true); +} + +void CameraTest2::onExit() +{ + Director::getInstance()->setProjection(Director::Projection::_2D); + TestCocosNodeDemo::onExit(); +} + +CameraTest2::CameraTest2() +{ + auto s = Director::getInstance()->getWinSize(); + + _sprite1 = MySprite::create(s_back3); + addChild( _sprite1 ); + _sprite1->setPosition( Point(1*s.width/4, s.height/2) ); + _sprite1->setScale(0.5); + + _sprite2 = Sprite::create(s_back3); + addChild( _sprite2 ); + _sprite2->setPosition( Point(3*s.width/4, s.height/2) ); + _sprite2->setScale(0.5); + + scheduleUpdate(); +} + +std::string CameraTest2::title() const +{ + return "Camera Test 2"; +} + +std::string CameraTest2::subtitle() const +{ + return "Background image should be rotated in 3D"; +} + +void CameraTest2::update(float dt) +{ + kmVec3 eye, center, up; + + kmVec3Fill(&eye, 150, 0, 200); + kmVec3Fill(¢er, 0, 0, 0); + kmVec3Fill(&up, 0, 1, 0); + + + kmMat4 lookupMatrix; + kmMat4LookAt(&lookupMatrix, &eye, ¢er, &up); + + _sprite1->setAdditionalTransform(lookupMatrix); + _sprite2->setAdditionalTransform(lookupMatrix); +} + +/// +/// void CocosNodeTestScene::runThisTest() { auto layer = nextCocosNodeAction(); diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h index 3e6f8fa9ce..50959ced5a 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h @@ -148,6 +148,24 @@ protected: CameraCenterTest(); }; +class CameraTest2 : public TestCocosNodeDemo +{ +public: + CREATE_FUNC(CameraTest2); + virtual std::string title() const override; + virtual std::string subtitle() const override; + virtual void onEnter() override; + virtual void onExit() override; + +protected: + virtual void update(float dt) override; + CameraTest2(); + + Sprite *_sprite1; + Sprite *_sprite2; + +}; + class ConvertToNode : public TestCocosNodeDemo { public: From 2e4c76a5d7e4d987d5d967a6a7ad898f09ba0e12 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 9 Jan 2014 18:34:04 +0800 Subject: [PATCH 2/5] closed #3644: Keyboard pressed events are being repeatedly fired before keyboard is released. --- cocos/2d/platform/linux/CCEGLView.cpp | 9 ++++++--- cocos/2d/platform/mac/CCEGLView.mm | 9 ++++++--- cocos/2d/platform/win32/CCEGLView.cpp | 9 ++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cocos/2d/platform/linux/CCEGLView.cpp b/cocos/2d/platform/linux/CCEGLView.cpp index 2cab145265..ac410b2f74 100644 --- a/cocos/2d/platform/linux/CCEGLView.cpp +++ b/cocos/2d/platform/linux/CCEGLView.cpp @@ -274,9 +274,12 @@ void EGLViewEventHandler::OnGLFWMouseScrollCallback(GLFWwindow* window, double x void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action || GLFW_REPEAT == action); - auto dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->dispatchEvent(&event); + if (GLFW_REPEAT != action) + { + EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); + } } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character) diff --git a/cocos/2d/platform/mac/CCEGLView.mm b/cocos/2d/platform/mac/CCEGLView.mm index 920212e5f5..afd9c66b92 100644 --- a/cocos/2d/platform/mac/CCEGLView.mm +++ b/cocos/2d/platform/mac/CCEGLView.mm @@ -279,9 +279,12 @@ void EGLViewEventHandler::OnGLFWMouseScrollCallback(GLFWwindow* window, double x void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action || GLFW_REPEAT == action); - auto dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->dispatchEvent(&event); + if (GLFW_REPEAT != action) + { + EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); + } } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character) diff --git a/cocos/2d/platform/win32/CCEGLView.cpp b/cocos/2d/platform/win32/CCEGLView.cpp index 953dede055..b6a7c5e62b 100644 --- a/cocos/2d/platform/win32/CCEGLView.cpp +++ b/cocos/2d/platform/win32/CCEGLView.cpp @@ -374,9 +374,12 @@ void EGLViewEventHandler::OnGLFWMouseScrollCallback(GLFWwindow* window, double x void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action || GLFW_REPEAT == action); - auto dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->dispatchEvent(&event); + if (GLFW_REPEAT != action) + { + EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action); + auto dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->dispatchEvent(&event); + } } void EGLViewEventHandler::OnGLFWCharCallback(GLFWwindow *window, unsigned int character) From d13612ef32efdaf054d43cb05d4f5c1ee1516447 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 9 Jan 2014 11:38:44 -0800 Subject: [PATCH 3/5] Improves description in camera test --- samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index 5c2534dee0..8ea66b04b7 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -852,6 +852,7 @@ public: protected: CustomCommand _customCommand; + }; void MySprite::draw() @@ -928,7 +929,7 @@ std::string CameraTest2::title() const std::string CameraTest2::subtitle() const { - return "Background image should be rotated in 3D"; + return "Both images should look the same"; } void CameraTest2::update(float dt) @@ -948,6 +949,7 @@ void CameraTest2::update(float dt) } /// +/// main /// void CocosNodeTestScene::runThisTest() { From 1dc169b19f170e69c1c28c28b62a903831c1d4c4 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 9 Jan 2014 14:26:22 -0800 Subject: [PATCH 4/5] Camera is working again Projection is passed in the shader. Since P is in the shader, QuadCommand uses `Vec3Transform` instead of `Vec3TransformCoord` since it is faster. --- .../2d/ccShader_PositionTextureColor_noMVP_vert.h | 2 +- cocos/2d/renderer/CCQuadCommand.cpp | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/cocos/2d/ccShader_PositionTextureColor_noMVP_vert.h b/cocos/2d/ccShader_PositionTextureColor_noMVP_vert.h index fb511ba1dd..bae95bcfea 100644 --- a/cocos/2d/ccShader_PositionTextureColor_noMVP_vert.h +++ b/cocos/2d/ccShader_PositionTextureColor_noMVP_vert.h @@ -38,7 +38,7 @@ varying vec2 v_texCoord; \n\ \n\ void main() \n\ { \n\ - gl_Position = a_position; \n\ + gl_Position = CC_PMatrix * a_position; \n\ v_fragmentColor = a_color; \n\ v_texCoord = a_texCoord; \n\ } \n\ diff --git a/cocos/2d/renderer/CCQuadCommand.cpp b/cocos/2d/renderer/CCQuadCommand.cpp index bbb7ba018c..b69fd075d3 100644 --- a/cocos/2d/renderer/CCQuadCommand.cpp +++ b/cocos/2d/renderer/CCQuadCommand.cpp @@ -58,12 +58,6 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* _capacity = quadCount; } - kmMat4 p, mvp; - kmGLGetMatrix(KM_GL_PROJECTION, &p); - - kmMat4Multiply(&mvp, &p, &mv); - - _quadCount = quadCount; memcpy(_quad, quad, sizeof(V3F_C4B_T2F_Quad) * quadCount); @@ -74,7 +68,7 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* vec1.x = q->bl.vertices.x; vec1.y = q->bl.vertices.y; vec1.z = q->bl.vertices.z; - kmVec3TransformCoord(&out1, &vec1, &mvp); + kmVec3Transform(&out1, &vec1, &mv); q->bl.vertices.x = out1.x; q->bl.vertices.y = out1.y; q->bl.vertices.z = out1.z; @@ -83,7 +77,7 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* vec2.x = q->br.vertices.x; vec2.y = q->br.vertices.y; vec2.z = q->br.vertices.z; - kmVec3TransformCoord(&out2, &vec2, &mvp); + kmVec3Transform(&out2, &vec2, &mv); q->br.vertices.x = out2.x; q->br.vertices.y = out2.y; q->br.vertices.z = out2.z; @@ -92,7 +86,7 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* vec3.x = q->tr.vertices.x; vec3.y = q->tr.vertices.y; vec3.z = q->tr.vertices.z; - kmVec3TransformCoord(&out3, &vec3, &mvp); + kmVec3Transform(&out3, &vec3, &mv); q->tr.vertices.x = out3.x; q->tr.vertices.y = out3.y; q->tr.vertices.z = out3.z; @@ -101,7 +95,7 @@ void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* vec4.x = q->tl.vertices.x; vec4.y = q->tl.vertices.y; vec4.z = q->tl.vertices.z; - kmVec3TransformCoord(&out4, &vec4, &mvp); + kmVec3Transform(&out4, &vec4, &mv); q->tl.vertices.x = out4.x; q->tl.vertices.y = out4.y; q->tl.vertices.z = out4.z; From 95b7196a2a26ee71b2424444bc95a86657b7f094 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 9 Jan 2014 14:26:36 -0800 Subject: [PATCH 5/5] Adds more tests cases for Node Adds more tests cases for Node --- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp | 114 +++++++++++++++++- .../Cpp/TestCpp/Classes/NodeTest/NodeTest.h | 27 ++++- 2 files changed, 134 insertions(+), 7 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp index 8ea66b04b7..843b72bbb6 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.cpp @@ -25,6 +25,7 @@ static int sceneIdx = -1; static std::function createFunctions[] = { + CL(CameraTest1), CL(CameraTest2), CL(CameraCenterTest), CL(Test2), @@ -34,6 +35,7 @@ static std::function createFunctions[] = CL(StressTest1), CL(StressTest2), CL(NodeToWorld), + CL(NodeToWorld3D), CL(SchedulerTest1), CL(CameraOrbitTest), CL(CameraZoomTest), @@ -492,6 +494,56 @@ std::string NodeToWorld::title() const return "nodeToParent transform"; } +//------------------------------------------------------------------ +// +// NodeToWorld3D +// +//------------------------------------------------------------------ +NodeToWorld3D::NodeToWorld3D() +{ + // + // This code tests that nodeToParent works OK: + // - It tests different anchor Points + // - It tests different children anchor points + + Size s = Director::getInstance()->getWinSize(); + auto parent = Node::create(); + parent->setContentSize(s); + parent->setAnchorPoint(Point(0.5, 0.5)); + parent->setPosition(s.width/2, s.height/2); + this->addChild(parent); + + auto back = Sprite::create(s_back3); + parent->addChild( back, -10); + back->setAnchorPoint( Point(0,0) ); + auto backSize = back->getContentSize(); + + auto item = MenuItemImage::create(s_PlayNormal, s_PlaySelect); + auto menu = Menu::create(item, NULL); + menu->alignItemsVertically(); + menu->setPosition( Point(backSize.width/2, backSize.height/2)); + back->addChild(menu); + + auto rot = RotateBy::create(5, 360); + auto fe = RepeatForever::create( rot); + item->runAction( fe ); + + auto move = MoveBy::create(3, Point(200,0)); + auto move_back = move->reverse(); + auto seq = Sequence::create( move, move_back, NULL); + auto fe2 = RepeatForever::create(seq); + back->runAction(fe2); + + auto orbit = OrbitCamera::create(10, 0, 1, 0, 360, 0, 90); + parent->runAction(orbit); +} + +std::string NodeToWorld3D::title() const +{ + return "nodeToParent transform in 3D"; +} + + //------------------------------------------------------------------ // // CameraOrbitTest @@ -828,12 +880,10 @@ std::string NodeNonOpaqueTest::subtitle() const return "Node rendered with GL_BLEND enabled"; } -//------------------------------------------------------------------ -// -// CameraTest2 -// -//------------------------------------------------------------------ +// +// MySprite: Used by CameraTest1 and CameraTest2 +// class MySprite : public Sprite { public: @@ -852,7 +902,7 @@ public: protected: CustomCommand _customCommand; - + }; void MySprite::draw() @@ -892,6 +942,58 @@ void MySprite::onDraw() CHECK_GL_ERROR_DEBUG(); CC_INCREMENT_GL_DRAWS(1); } +//------------------------------------------------------------------ +// +// CameraTest1 +// +//------------------------------------------------------------------ + +void CameraTest1::onEnter() +{ + TestCocosNodeDemo::onEnter(); + Director::getInstance()->setProjection(Director::Projection::_3D); + Director::getInstance()->setDepthTest(true); +} + +void CameraTest1::onExit() +{ + Director::getInstance()->setProjection(Director::Projection::_2D); + TestCocosNodeDemo::onExit(); +} + +CameraTest1::CameraTest1() +{ + auto s = Director::getInstance()->getWinSize(); + + _sprite1 = MySprite::create(s_back3); + addChild( _sprite1 ); + _sprite1->setPosition( Point(1*s.width/4, s.height/2) ); + _sprite1->setScale(0.5); + + _sprite2 = Sprite::create(s_back3); + addChild( _sprite2 ); + _sprite2->setPosition( Point(3*s.width/4, s.height/2) ); + _sprite2->setScale(0.5); + + auto camera = OrbitCamera::create(10, 0, 1, 0, 360, 0, 0); + _sprite1->runAction( camera ); + _sprite2->runAction( camera->clone() ); +} + +std::string CameraTest1::title() const +{ + return "Camera Test 1"; +} + +std::string CameraTest1::subtitle() const +{ + return "Both images should rotate with a 3D effect"; +} +//------------------------------------------------------------------ +// +// CameraTest2 +// +//------------------------------------------------------------------ void CameraTest2::onEnter() { TestCocosNodeDemo::onEnter(); diff --git a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h index 50959ced5a..7a6a63d7c2 100644 --- a/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h +++ b/samples/Cpp/TestCpp/Classes/NodeTest/NodeTest.h @@ -110,6 +110,16 @@ protected: NodeToWorld(); }; +class NodeToWorld3D : public TestCocosNodeDemo +{ +public: + CREATE_FUNC(NodeToWorld3D); + virtual std::string title() const override; + +protected: + NodeToWorld3D(); +}; + class CameraOrbitTest : public TestCocosNodeDemo { public: @@ -148,6 +158,22 @@ protected: CameraCenterTest(); }; +class CameraTest1 : public TestCocosNodeDemo +{ +public: + CREATE_FUNC(CameraTest1); + virtual std::string title() const override; + virtual std::string subtitle() const override; + virtual void onEnter() override; + virtual void onExit() override; + +protected: + CameraTest1(); + + Sprite *_sprite1; + Sprite *_sprite2; +}; + class CameraTest2 : public TestCocosNodeDemo { public: @@ -163,7 +189,6 @@ protected: Sprite *_sprite1; Sprite *_sprite2; - }; class ConvertToNode : public TestCocosNodeDemo