Merge pull request #302 from zhukaixy/texturecube
1: update cube texture for sky box
|
@ -33,6 +33,8 @@
|
|||
#include "3d/CCSkybox.h"
|
||||
#include "3d/CCTextureCube.h"
|
||||
|
||||
#include "2d/CCCamera.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
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)
|
||||
{
|
||||
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();
|
||||
state->apply(transform);
|
||||
|
||||
state->apply(trans);
|
||||
|
||||
Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
|
||||
state->setUniformVec4("u_color", color);
|
||||
|
||||
GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST);
|
||||
GLint depthFunc;
|
||||
GLboolean depthFlag = glIsEnabled(GL_DEPTH_TEST);
|
||||
GLint depthFunc;
|
||||
glGetIntegerv(GL_DEPTH_FUNC, &depthFunc);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
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())
|
||||
{
|
||||
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);
|
||||
|
||||
glCullFace(cullMode);
|
||||
if (!cullFlag)
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
glDepthFunc(depthFunc);
|
||||
if (!depthFlag)
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
|
|
@ -1,18 +1,11 @@
|
|||
const char* cc3D_Skybox_vert = STRINGIFY(
|
||||
|
||||
attribute vec4 a_position;
|
||||
attribute vec3 a_position;
|
||||
varying vec3 v_reflect;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
mat4 MVMatrixLimited = CC_MVMatrix;
|
||||
MVMatrixLimited[0][3] = 0.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;
|
||||
v_reflect = normalize(CC_NormalMatrix * a_position);
|
||||
gl_Position = CC_MVPMatrix * vec4(a_position, 1.0);
|
||||
}
|
||||
);
|
|
@ -2373,9 +2373,14 @@ std::string Sprite3DCubeMapTest::subtitle() const
|
|||
void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
||||
{
|
||||
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);
|
||||
|
||||
auto listener = EventListenerTouchAllAtOnce::create();
|
||||
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DCubeMapTest::onTouchesMoved, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
// create a teapot
|
||||
_teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
|
||||
_teapot->retain();
|
||||
|
@ -2393,8 +2398,8 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
|||
|
||||
//set texture parameters
|
||||
Texture2D::TexParams tRepeatParams;
|
||||
tRepeatParams.magFilter = GL_NEAREST;
|
||||
tRepeatParams.minFilter = GL_NEAREST;
|
||||
tRepeatParams.magFilter = GL_LINEAR;
|
||||
tRepeatParams.minFilter = GL_LINEAR;
|
||||
tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
|
||||
tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
|
||||
_textureCube->setTexParameters(tRepeatParams);
|
||||
|
@ -2403,7 +2408,7 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
|||
state->setUniformTexture("u_cubeTex", _textureCube);
|
||||
|
||||
_teapot->setGLProgramState(state);
|
||||
_teapot->setPosition3D(Vec3(0, -5, -20));
|
||||
_teapot->setPosition3D(Vec3(0, -5, 0));
|
||||
_teapot->setRotation3D(Vec3(-90, 180, 0));
|
||||
|
||||
auto rotate_action = RotateBy::create(1.5, Vec3(0, 30, 0));
|
||||
|
@ -2425,8 +2430,6 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
|||
offset += meshattribute.attribSizeBytes;
|
||||
}
|
||||
addChild(_teapot);
|
||||
addChild(_camera);
|
||||
setCameraMask(2);
|
||||
|
||||
{
|
||||
// config skybox
|
||||
|
@ -2435,8 +2438,13 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
|||
|
||||
_skyBox->setTexture(_textureCube);
|
||||
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)
|
||||
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
||||
[this](EventCustom*)
|
||||
|
@ -2464,3 +2472,17 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
|
|||
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
||||
#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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -514,10 +514,13 @@ public:
|
|||
|
||||
void addNewSpriteWithCoords(Vec2);
|
||||
|
||||
void onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event);
|
||||
|
||||
protected:
|
||||
cocos2d::TextureCube* _textureCube;
|
||||
Skybox* _skyBox;
|
||||
Sprite3D* _teapot;
|
||||
cocos2d::TextureCube* _textureCube;
|
||||
Skybox* _skyBox;
|
||||
Sprite3D* _teapot;
|
||||
cocos2d::Camera *_camera;
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
||||
EventListenerCustom* _backToForegroundListener;
|
||||
|
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 64 KiB |
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 90 KiB |
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 72 KiB |
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 6.7 KiB |