merge cocos

This commit is contained in:
yangxiao 2015-03-26 12:50:18 +08:00
commit 000e34c6d0
10 changed files with 207 additions and 52 deletions

View File

@ -219,8 +219,10 @@
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCPlane.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCRay.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkeleton3D.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkybox.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSprite3D.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSprite3DMaterial.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCTextureCube.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\cocos3d.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\audio\include\AudioEngine.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\audio\include\Export.h" />
@ -802,8 +804,10 @@
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCPlane.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCRay.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkeleton3D.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkybox.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSprite3D.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSprite3DMaterial.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCTextureCube.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\audio\winrt\Audio.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<ForcedIncludeFiles>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="$(MSBuildThisFileDirectory)targetver.h" />
@ -1737,6 +1737,12 @@
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\editor-support\cocostudio\CCObjectExtensionData.h">
<Filter>cocostudio\json</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkybox.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCTextureCube.h">
<Filter>3d</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\cocos2d.cpp" />
@ -3300,6 +3306,12 @@
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\editor-support\cocostudio\CCObjectExtensionData.cpp">
<Filter>cocostudio\json</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCSkybox.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)..\..\..\..\3d\CCTextureCube.cpp">
<Filter>3d</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="2d">

View File

@ -61,22 +61,8 @@ bool Skybox::init()
state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
setGLProgramState(state);
if (Configuration::getInstance()->supportsShareableVAO())
{
glGenVertexArrays(1, &_vao);
GL::bindVAO(_vao);
}
initBuffers();
if (Configuration::getInstance()->supportsShareableVAO())
{
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
state->applyAttributes(false);
GL::bindVAO(0);
}
CHECK_GL_ERROR_DEBUG();
return true;
@ -84,6 +70,12 @@ bool Skybox::init()
void Skybox::initBuffers()
{
if (Configuration::getInstance()->supportsShareableVAO())
{
glGenVertexArrays(1, &_vao);
GL::bindVAO(_vao);
}
// init vertex buffer object
Vec3 vexBuf[] =
{
@ -96,7 +88,7 @@ void Skybox::initBuffers()
glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW);
// init index buffer object
unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font
const unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, // font
1, 5, 4, 1, 4, 0, // right
4, 5, 6, 4, 6, 7, // back
7, 6, 2, 7, 2, 3, // left
@ -107,6 +99,14 @@ void Skybox::initBuffers()
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
if (Configuration::getInstance()->supportsShareableVAO())
{
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
getGLProgramState()->applyAttributes(false);
GL::bindVAO(0);
}
}
void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
@ -122,7 +122,7 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags)
state->apply(transform);
GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST);
GLint depthFunc;
GLint depthFunc;
glGetIntegerv(GL_DEPTH_FUNC, &depthFunc);
glEnable(GL_DEPTH_TEST);
@ -167,11 +167,25 @@ void Skybox::setTexture(TextureCube* texture)
{
CCASSERT(texture != nullptr, __FUNCTION__);
_texture = texture;
_texture->retain();
texture->retain();
auto state = getGLProgramState();
state->setUniformTexture("u_Env", _texture);
if (_texture)
_texture->release();
_texture = texture;
getGLProgramState()->setUniformTexture("u_Env", _texture);
}
void Skybox::reload()
{
auto glProgram = getGLProgramState()->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Shaders3D/Skybox.vert", "Shaders3D/Skybox.frag");
glProgram->link();
glProgram->updateUniforms();
initBuffers();
}
NS_CC_END

View File

@ -43,6 +43,8 @@ public:
// Overrides
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
void reload();
CC_CONSTRUCTOR_ACCESS:
Skybox();
virtual ~Skybox();

View File

@ -146,6 +146,7 @@ Image* createImage(const std::string& path)
TextureCube::TextureCube()
{
_imgPath.resize(6);
}
TextureCube::~TextureCube()
@ -170,6 +171,13 @@ bool TextureCube::init(const std::string& positive_x, const std::string& negativ
const std::string& positive_y, const std::string& negative_y,
const std::string& positive_z, const std::string& negative_z)
{
_imgPath[0] = positive_x;
_imgPath[1] = negative_x;
_imgPath[2] = positive_y;
_imgPath[3] = negative_y;
_imgPath[4] = positive_z;
_imgPath[5] = negative_z;
std::vector<Image*> images(6);
images[0] = createImage(positive_x);
@ -247,4 +255,9 @@ void TextureCube::setTexParameters(const TexParams& texParams)
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
bool TextureCube::reloadTexture()
{
return init(_imgPath[0], _imgPath[1], _imgPath[2], _imgPath[3], _imgPath[4], _imgPath[5]);
}
NS_CC_END

View File

@ -51,6 +51,8 @@ public:
const std::string& positive_z, const std::string& negative_z);
void setTexParameters(const TexParams&);
bool reloadTexture();
CC_CONSTRUCTOR_ACCESS:
TextureCube();
TextureCube(const TextureCube&);
@ -60,6 +62,8 @@ protected:
bool init(const std::string& positive_x, const std::string& negative_x,
const std::string& positive_y, const std::string& negative_y,
const std::string& positive_z, const std::string& negative_z);
private:
std::vector<std::string> _imgPath;
};
NS_CC_END

View File

@ -285,6 +285,8 @@ THE SOFTWARE.
#include "3d/CCBillBoard.h"
#include "3d/CCFrustum.h"
#include "3d/CCPlane.h"
#include "3d/CCTextureCube.h"
#include "3d/CCSkybox.h"
// Deprecated include
#include "deprecated/CCDictionary.h"

View File

@ -2341,8 +2341,9 @@ void UseCaseSprite3D::update(float delta)
}
Sprite3DCubeMapTest::Sprite3DCubeMapTest() :
_sprite(nullptr),
_textureCube(nullptr)
_textureCube(nullptr),
_skyBox(nullptr),
_teapot(nullptr)
{
auto s = Director::getInstance()->getWinSize();
addNewSpriteWithCoords(Vec2(s.width / 2, s.height / 2));
@ -2350,6 +2351,12 @@ _textureCube(nullptr)
Sprite3DCubeMapTest::~Sprite3DCubeMapTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
_teapot->release();
_skyBox->release();
_textureCube->release();
}
@ -2370,18 +2377,18 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
_camera->setCameraFlag(CameraFlag::USER1);
// create a teapot
auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
_teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
_teapot->retain();
//create and set our custom shader
auto shader = GLProgram::createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
auto _state = GLProgramState::create(shader);
auto state = GLProgramState::create(shader);
// create the second texture for cylinder
_textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
"Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
"Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
// temporary solution
_textureCube->retain();
//set texture parameters
@ -2393,40 +2400,67 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
_textureCube->setTexParameters(tRepeatParams);
// pass the texture sampler to our custom shader
_state->setUniformTexture("u_cubeTex", _textureCube);
state->setUniformTexture("u_cubeTex", _textureCube);
teapot->setGLProgramState(_state);
teapot->setPosition3D(Vec3(0, -5, -20));
teapot->setRotation3D(Vec3(-90, 180, 0));
_teapot->setGLProgramState(state);
_teapot->setPosition3D(Vec3(0, -5, -20));
_teapot->setRotation3D(Vec3(-90, 180, 0));
auto rotate_action = RotateBy::create(1.5, Vec3(0, 30, 0));
teapot->runAction(RepeatForever::create(rotate_action));
_teapot->runAction(RepeatForever::create(rotate_action));
//pass mesh's attribute to shader
long offset = 0;
auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount();
auto attributeCount = _teapot->getMesh()->getMeshVertexAttribCount();
for (auto i = 0; i < attributeCount; i++)
{
auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i);
_state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
auto meshattribute = _teapot->getMesh()->getMeshVertexAttribute(i);
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size,
meshattribute.type,
GL_FALSE,
teapot->getMesh()->getVertexSizeInBytes(),
_teapot->getMesh()->getVertexSizeInBytes(),
(GLvoid*)offset);
offset += meshattribute.attribSizeBytes;
}
addChild(teapot);
addChild(_teapot);
addChild(_camera);
setCameraMask(2);
{
// config skybox
Skybox* box = Skybox::create();
box->setTexture(_textureCube);
addChild(box);
// config skybox
_skyBox = Skybox::create();
_skyBox->retain();
//auto rotate_action = RotateBy::create(1.5, Vec3(0, -30, 0));
//box->runAction(RepeatForever::create(rotate_action));
_skyBox->setTexture(_textureCube);
addChild(_skyBox);
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
auto state = _teapot->getGLProgramState();
auto glProgram = state->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag");
glProgram->link();
glProgram->updateUniforms();
_textureCube->reloadTexture();
Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_NEAREST;
tRepeatParams.minFilter = GL_NEAREST;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams);
state->setUniformTexture("u_cubeTex", _textureCube);
_skyBox->reload();
_skyBox->setTexture(_textureCube);
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}

View File

@ -30,12 +30,12 @@
#include <string>
namespace cocos2d {
class Animate3D;
class Sprite3D;
class Delay;
class Ray;
class DrawNode3D;
class GLProgramState;
class Animate3D;
class Sprite3D;
class Delay;
class Ray;
class DrawNode3D;
class GLProgramState;
}
class Sprite3DTestDemo : public BaseTest
@ -500,7 +500,8 @@ public:
namespace cocos2d
{
class TextureCube;
class TextureCube;
class Skybox;
}
class Sprite3DCubeMapTest : public Sprite3DTestDemo
{
@ -514,8 +515,13 @@ public:
void addNewSpriteWithCoords(Vec2);
protected:
cocos2d::Sprite3D* _sprite;
cocos2d::TextureCube* _textureCube;
Skybox* _skyBox;
Sprite3D* _teapot;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener;
#endif
};
#endif

View File

@ -1026,12 +1026,75 @@ end
function AsyncLoadSprite3DTest:onExit()
----------------------------------------
----Sprite3DCubeTexture
----------------------------------------
local Sprite3DCubeMap = {}
Sprite3DCubeMap.__index = Sprite3DCubeMap
function Sprite3DCubeMap.create()
local layer = cc.Layer:create()
Helper.initWithLayer(layer)
Helper.titleLabel:setString("Sprite3D CubeMap/Skybox Test")
local visSize = cc.Director:getInstance():getVisibleSize()
local camera = cc.Camera:createPerspective(68, visSize.width / visSize.height, 0.1, 200)
camera:setCameraFlag(2)
layer:addChild(camera)
local fileName = "Sprite3DTest/teapot.c3b"
local teapot = cc.Sprite3D:create(fileName)
teapot:setPosition3D({x = 0, y = -5, z = -20})
teapot:setRotation3D({x = -90, y = 180, z = 0})
local texCube = cc.TextureCube:create("Sprite3DTest/skybox/left.jpg", "Sprite3DTest/skybox/right.jpg",
"Sprite3DTest/skybox/top.jpg", "Sprite3DTest/skybox/bottom.jpg",
"Sprite3DTest/skybox/front.jpg", "Sprite3DTest/skybox/back.jpg");
local program = cc.GLProgram:createWithFilenames("Sprite3DTest/cube_map.vert", "Sprite3DTest/cube_map.frag")
local state = cc.GLProgramState:create(program)
attriNames = {
"a_position", "a_color",
"a_texCoord", "a_texCoord1", "a_texCoord2", "a_texCoord3",
"a_normal", "a_blendWeight", "a_blendIndex"
}
--pass mesh's attribute to shader
local offset = 0
local attributeCount = teapot:getMesh():getMeshVertexAttribCount()
for i = 0, attributeCount-1 do
local meshattribute = teapot:getMesh():getMeshVertexAttribute(i)
state:setVertexAttribPointer(attriNames[meshattribute.vertexAttrib+1],
meshattribute.size,
meshattribute.type,
false,
teapot:getMesh():getVertexSizeInBytes(),
offset);
offset = offset + meshattribute.attribSizeBytes
end
state:setUniformTexture("u_cubeTex", texCube:getName())
teapot:setGLProgramState(state)
teapot:setCameraMask(2)
local rotate_action = cc.RotateBy:create(1.5, { x = 0.0, y = 30.0, z = 0.0})
teapot:runAction(cc.RepeatForever:create(rotate_action));
layer:addChild(teapot)
local skybox = cc.Skybox:create()
skybox:setTexture(texCube)
layer:addChild(skybox)
return layer
>>>>>>> 4ea27173daf6b04cf0b917faa7911dde448cbdca
end
function Sprite3DTest()
local scene = cc.Scene:create()
Helper.createFunctionTable =
Helper.createFunctionTable =
{
Sprite3DBasicTest.create,
Sprite3DHitTest.create,
@ -1041,7 +1104,8 @@ function Sprite3DTest()
Sprite3DReskinTest.create,
Sprite3DWithOBBPerfromanceTest.create,
Sprite3DMirrorTest.create,
AsyncLoadSprite3DTest.create
AsyncLoadSprite3DTest.create,
Sprite3DCubeMap.create,
}
scene:addChild(Sprite3DBasicTest.create())