Merge pull request #302 from zhukaixy/texturecube

1: update cube texture for sky box
This commit is contained in:
XiaoYang 2015-04-14 13:34:55 +08:00
commit 43cf540915
10 changed files with 60 additions and 23 deletions

View File

@ -33,6 +33,8 @@
#include "3d/CCSkybox.h" #include "3d/CCSkybox.h"
#include "3d/CCTextureCube.h" #include "3d/CCTextureCube.h"
#include "2d/CCCamera.h"
NS_CC_BEGIN NS_CC_BEGIN
Skybox::Skybox() Skybox::Skybox()
@ -150,19 +152,32 @@ void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
void Skybox::onDraw(const Mat4& transform, uint32_t flags) void Skybox::onDraw(const Mat4& transform, uint32_t flags)
{ {
Mat4 trans(transform);
const cocos2d::Vec3 pos(Camera::getVisitingCamera()->getPosition3D());
trans.m[12] = pos.x;
trans.m[13] = pos.y;
trans.m[14] = pos.z;
auto state = getGLProgramState(); auto state = getGLProgramState();
state->apply(transform); state->apply(trans);
Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f); Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
state->setUniformVec4("u_color", color); state->setUniformVec4("u_color", color);
GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST); GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST);
GLint depthFunc; GLint depthFunc;
glGetIntegerv(GL_DEPTH_FUNC, &depthFunc); glGetIntegerv(GL_DEPTH_FUNC, &depthFunc);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
GLboolean cullFlag = glIsEnabled(GL_CULL_FACE);
GLint cullMode;
glGetIntegerv(GL_CULL_FACE_MODE, &cullMode);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
if (Configuration::getInstance()->supportsShareableVAO()) if (Configuration::getInstance()->supportsShareableVAO())
{ {
GL::bindVAO(_vao); GL::bindVAO(_vao);
@ -191,6 +206,10 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags)
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
glCullFace(cullMode);
if (!cullFlag)
glDisable(GL_CULL_FACE);
glDepthFunc(depthFunc); glDepthFunc(depthFunc);
if (!depthFlag) if (!depthFlag)
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);

View File

@ -1,18 +1,11 @@
const char* cc3D_Skybox_vert = STRINGIFY( const char* cc3D_Skybox_vert = STRINGIFY(
attribute vec4 a_position; attribute vec3 a_position;
varying vec3 v_reflect; varying vec3 v_reflect;
void main(void) void main(void)
{ {
mat4 MVMatrixLimited = CC_MVMatrix; v_reflect = normalize(CC_NormalMatrix * a_position);
MVMatrixLimited[0][3] = 0.0; gl_Position = CC_MVPMatrix * vec4(a_position, 1.0);
MVMatrixLimited[1][3] = 0.0;
MVMatrixLimited[2][3] = 0.0;
vec4 position = MVMatrixLimited* a_position;
v_reflect = a_position.xyz;
gl_Position = position.xyww;
} }
); );

View File

@ -2373,9 +2373,14 @@ std::string Sprite3DCubeMapTest::subtitle() const
void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p) void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
{ {
Size visibleSize = Director::getInstance()->getVisibleSize(); Size visibleSize = Director::getInstance()->getVisibleSize();
auto _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 0.1, 200); _camera = Camera::createPerspective(60, visibleSize.width / visibleSize.height, 10, 1000);
_camera->setPosition3D(Vec3(0.f, 0.f, 50.f));
_camera->setCameraFlag(CameraFlag::USER1); _camera->setCameraFlag(CameraFlag::USER1);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DCubeMapTest::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// create a teapot // create a teapot
_teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); _teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
_teapot->retain(); _teapot->retain();
@ -2393,8 +2398,8 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
//set texture parameters //set texture parameters
Texture2D::TexParams tRepeatParams; Texture2D::TexParams tRepeatParams;
tRepeatParams.magFilter = GL_NEAREST; tRepeatParams.magFilter = GL_LINEAR;
tRepeatParams.minFilter = GL_NEAREST; tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_MIRRORED_REPEAT; tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
tRepeatParams.wrapT = GL_MIRRORED_REPEAT; tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
_textureCube->setTexParameters(tRepeatParams); _textureCube->setTexParameters(tRepeatParams);
@ -2403,7 +2408,7 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
state->setUniformTexture("u_cubeTex", _textureCube); state->setUniformTexture("u_cubeTex", _textureCube);
_teapot->setGLProgramState(state); _teapot->setGLProgramState(state);
_teapot->setPosition3D(Vec3(0, -5, -20)); _teapot->setPosition3D(Vec3(0, -5, 0));
_teapot->setRotation3D(Vec3(-90, 180, 0)); _teapot->setRotation3D(Vec3(-90, 180, 0));
auto rotate_action = RotateBy::create(1.5, Vec3(0, 30, 0)); auto rotate_action = RotateBy::create(1.5, Vec3(0, 30, 0));
@ -2425,8 +2430,6 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
offset += meshattribute.attribSizeBytes; offset += meshattribute.attribSizeBytes;
} }
addChild(_teapot); addChild(_teapot);
addChild(_camera);
setCameraMask(2);
{ {
// config skybox // config skybox
@ -2435,8 +2438,13 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
_skyBox->setTexture(_textureCube); _skyBox->setTexture(_textureCube);
addChild(_skyBox); addChild(_skyBox);
_skyBox->setScale(700.f);
} }
addChild(_camera);
setCameraMask(2);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) #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, _backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*) [this](EventCustom*)
@ -2464,3 +2472,17 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1); Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
#endif #endif
} }
void Sprite3DCubeMapTest::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
if (touches.size())
{
auto touch = touches[0];
auto delta = touch->getDelta();
static float _angle = 0.f;
_angle -= CC_DEGREES_TO_RADIANS(delta.x);
_camera->setPosition3D(Vec3(50.0f * sinf(_angle), 0.0f, 50.0f * cosf(_angle)));
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
}
}

View File

@ -514,10 +514,13 @@ public:
void addNewSpriteWithCoords(Vec2); void addNewSpriteWithCoords(Vec2);
void onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event);
protected: protected:
cocos2d::TextureCube* _textureCube; cocos2d::TextureCube* _textureCube;
Skybox* _skyBox; Skybox* _skyBox;
Sprite3D* _teapot; Sprite3D* _teapot;
cocos2d::Camera *_camera;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener; EventListenerCustom* _backToForegroundListener;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB