diff --git a/cocos/2d/libcocos2d.vcxproj b/cocos/2d/libcocos2d.vcxproj index 2a3490c64f..17a7c31dcd 100644 --- a/cocos/2d/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d.vcxproj @@ -237,10 +237,10 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + - @@ -608,11 +608,11 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* + - diff --git a/cocos/2d/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d.vcxproj.filters index 4da4217a2c..3c3a28643b 100644 --- a/cocos/2d/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d.vcxproj.filters @@ -1301,12 +1301,14 @@ - 3d 3d + + 3d + @@ -2551,12 +2553,13 @@ physics - - 3d 3d + + 3d + diff --git a/cocos/3d/CCSkybox.cpp b/cocos/3d/CCSkybox.cpp index ab298c5cc1..06ad532217 100644 --- a/cocos/3d/CCSkybox.cpp +++ b/cocos/3d/CCSkybox.cpp @@ -26,25 +26,21 @@ NS_CC_BEGIN - Skybox::Skybox() : _vao(0) , _vbo(0) - , _bufferCapacity(0) - , _bufferCount(0) - , _buffer(nullptr) - , _dirty(false) + , _vio(0) ,_texture(nullptr) { } Skybox::~Skybox() { - free(_buffer); - _buffer = nullptr; - glDeleteBuffers(1, &_vbo); + glDeleteBuffers(1, &_vio); + _vbo = 0; + _vio = 0; if (Configuration::getInstance()->supportsShareableVAO()) { @@ -56,86 +52,57 @@ Skybox::~Skybox() _texture->release(); } -Skybox* Skybox::create() -{ - Skybox* ret = new (std::nothrow) Skybox(); - if (ret && ret->init()) - { - ret->autorelease(); - } - else - { - CC_SAFE_DELETE(ret); - } - - return ret; -} - -void Skybox::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 Skybox::init() { - //setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR)); - //create and set our custom shader + // create and set our custom shader auto shader = GLProgram::createWithFilenames("Sprite3DTest/skybox.vert", "Sprite3DTest/skybox.frag"); auto state = GLProgramState::create(shader); setGLProgramState(state); - 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)); - - glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD,2,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B),(GLvoid*)offsetof(V3F_C4B, texcoord)); - - glBindBuffer(GL_ARRAY_BUFFER, 0); + initBuffers(); if (Configuration::getInstance()->supportsShareableVAO()) { + glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); + 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 Skybox::initBuffers() +{ + // init vertex buffer object + Vec3 vexBuf[] = + { + Vec3(1, -1, 1), Vec3(1, 1, 1), Vec3(-1, 1, 1), Vec3(-1, -1, 1), + Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1) + }; + + glGenBuffers(1, &_vbo); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // init index buffer object + unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, 1, 5, 4, 1, 4, 0, 4, 5, 6, 4, 6, 7, 7, 6, 2, 7, 2, 3 }; + + glGenBuffers(1, &_vio); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vio); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags) { _customCommand.init(_globalZOrder); @@ -153,14 +120,6 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags) glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); - - 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()) { @@ -168,96 +127,34 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags) } else { - GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); + GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION); glBindBuffer(GL_ARRAY_BUFFER, _vbo); - // vertex - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, vertices)); + glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr); - // color - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, colors)); - - //texcoord - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD,2,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B),(GLvoid*)offsetof(V3F_C4B, texcoord)); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vio); } - glDrawArrays(GL_TRIANGLES, 0, _bufferCount); - glBindBuffer(GL_ARRAY_BUFFER, 0); + glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr); - CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount); + if (Configuration::getInstance()->supportsShareableVAO()) + { + GL::bindVAO(0); + } + else + { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } - glCullFace(GL_BACK); + CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8); + + glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); CHECK_GL_ERROR_DEBUG(); } -void Skybox::drawSlice(Vec3* points, const Color4F& color) -{ - unsigned int vertex_count = 6; - ensureCapacity(vertex_count); - - Color4B col = Color4B(color); - V3F_C4B a = {Vec3(points[0].x, points[0].y, points[0].z), col, Tex2F(0,0)}; - V3F_C4B b = {Vec3(points[1].x, points[1].y, points[1].z), col, Tex2F(1,0)}; - V3F_C4B c = {Vec3(points[2].x, points[2].y, points[2].z), col, Tex2F(1,1)}; - V3F_C4B d = {Vec3(points[3].x, points[3].y, points[3].z), col, Tex2F(0,1)}; - V3F_C4B e = {Vec3(points[0].x, points[0].y, points[0].z), col, Tex2F(0,0)}; - V3F_C4B f = {Vec3(points[2].x, points[2].y, points[2].z), col, Tex2F(1,1)}; - - V3F_C4B* slice = (V3F_C4B*)(_buffer + _bufferCount); - slice[0] = a; - slice[1] = b; - slice[2] = c; - slice[3] = d; - slice[4] = e; - slice[5] = f; - - _bufferCount += vertex_count; - _dirty = true; -} - -void Skybox::drawSphere(const Vec3& origin, const float radius, const float M, const float N, const Color4F& color) -{ - float step_y = M_PI/M; - float step_xz = 2*M_PI/N; - - Vec3 points[4]; - float angle_y = 0.0; - float angle_xz = 0.0; - int i=0, j=0; - - for(i=0; isetUniformTexture("u_Env", _texture); } -void Skybox::clear() -{ - _bufferCount = 0; - _dirty = true; -} - NS_CC_END diff --git a/cocos/3d/CCSkybox.h b/cocos/3d/CCSkybox.h index 99a504d139..2b6e115f38 100644 --- a/cocos/3d/CCSkybox.h +++ b/cocos/3d/CCSkybox.h @@ -32,15 +32,10 @@ NS_CC_BEGIN class CC_DLL Skybox : public Node { public: - static Skybox* create(); - - void drawSphere(const Vec3& origin, const float radius, const float M, const float N, const Color4F& color); + CREATE_FUNC(Skybox); void setTexture(Texture2D*); - /** Clear the geometry in the node's buffer. */ - void clear(); - void onDraw(const Mat4& transform, uint32_t flags); // Overrides @@ -52,27 +47,15 @@ CC_CONSTRUCTOR_ACCESS: virtual bool init(); protected: - struct V3F_C4B - { - Vec3 vertices; - Color4B colors; - Tex2F texcoord; - }; - void ensureCapacity(int count); - void drawSlice(Vec3* points, const Color4F& color); + void initBuffers(); GLuint _vao; GLuint _vbo; - - int _bufferCapacity; - GLsizei _bufferCount; - V3F_C4B* _buffer; + GLuint _vio; CustomCommand _customCommand; - bool _dirty; - Texture2D* _texture; private: CC_DISALLOW_COPY_AND_ASSIGN(Skybox); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 7bf4766850..edbb889330 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -2374,7 +2374,6 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) // config skybox Skybox* box = Skybox::create(); box->setTexture(_textureCube); - box->drawSphere(Vec3::ZERO, 1.6f, 20, 20, Color4F(0, 0, 0, 0)); addChild(box); //auto rotate_action = RotateBy::create(1.5, Vec3(0, -30, 0)); diff --git a/tests/cpp-tests/Resources/Sprite3DTest/skybox.vert b/tests/cpp-tests/Resources/Sprite3DTest/skybox.vert index 735ddb55ed..eb11185ebb 100644 --- a/tests/cpp-tests/Resources/Sprite3DTest/skybox.vert +++ b/tests/cpp-tests/Resources/Sprite3DTest/skybox.vert @@ -4,9 +4,9 @@ varying vec3 v_reflect; void main(void) { mat4 MVMatrixLimited = CC_MVMatrix; - MVMatrixLimited[0][3] = 0; - MVMatrixLimited[1][3] = 0; - MVMatrixLimited[2][3] = 0; + MVMatrixLimited[0][3] = 0.0; + MVMatrixLimited[1][3] = 0.0; + MVMatrixLimited[2][3] = 0.0; vec4 position = MVMatrixLimited* a_position;