Skip to content

Instantly share code, notes, and snippets.

@RustyMoyher
Last active November 23, 2016 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RustyMoyher/63c02251ab4e0b3d4153a5b17dd42cbf to your computer and use it in GitHub Desktop.
Save RustyMoyher/63c02251ab4e0b3d4153a5b17dd42cbf to your computer and use it in GitHub Desktop.
Sprite3D with RenderTexture doesn't work - example - Cocos2d-x 3.11.1
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate() {}
AppDelegate::~AppDelegate() {}
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("CocosVanilla311", cocos2d::Rect(0, 0, 1280, 720));
#else
glview = GLViewImpl::create("CocosVanilla311");
#endif
director->setOpenGLView(glview);
}
director->setDisplayStats(true);
director->setAnimationInterval(1.0f / 60);
glview->setDesignResolutionSize(1280, 720, ResolutionPolicy::NO_BORDER);
auto frameSize = glview->getFrameSize();
register_all_packages();
auto scene = HelloWorld::createScene();
director->runWithScene(scene);
return true;
}
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
}
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
}
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
Private inheritance here hides part of interface from Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief Called when the application moves to the background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief Called when the application reenters the foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init() {
if (!Layer::init() ) return false;
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// Ship - Model is from cocos2d-x test project
ship3D = Sprite3D::create("boss.c3b");
ship3D->setScale(17);
ship3D->setRotation3D(Vec3(180,45,0));
ship3D->setPosition(Vec2(1280/4 + origin.x, visibleSize.height/2 + origin.y));
addChild(ship3D, 1);
ship3D->setForce2DQueue(true);
// Rotate Ship
auto spin = RotateBy::create(4, Vec3(0,180,0));
auto repeatspin = RepeatForever::create(spin);
ship3D->runAction(repeatspin);
// RenderTextures
renderTexDefault = RenderTexture::create(1280, 720, Texture2D::PixelFormat::RGBA8888);
addChild(renderTexDefault);
renderTexDefault->setPosition(1280/4 * 3, 720/2);
renderTexWithBuffer = RenderTexture::create(1280, 720, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);
addChild(renderTexWithBuffer);
renderTexWithBuffer->setPosition(1280/4 * 4, 720/2);
// Update
scheduleUpdate();
// Labels
auto label = Label::createWithTTF("Sprite3D with RenderTextures", "fonts/arial.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
auto label1 = Label::createWithTTF("Normal Sprite3D\n", "fonts/arial.ttf", 24);
label1->setPosition(Vec2(1280/4 * 1, 100));
this->addChild(label1, 1);
auto label2 = Label::createWithTTF("RenderTexture\nDefault - No depth buffer", "fonts/arial.ttf", 24);
label2->setPosition(Vec2(1280/4 * 2, 100));
this->addChild(label2, 1);
auto label3 = Label::createWithTTF("RenderTexture\nGL_DEPTH24_STENCIL8", "fonts/arial.ttf", 24);
label3->setPosition(Vec2(1280/4 * 3, 100));
this->addChild(label3, 1);
return true;
}
void HelloWorld::update(float delta)
{
renderTexDefault->beginWithClear(0, 0, 0, 0, 0, 0);
ship3D->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0);
renderTexDefault->end();
renderTexWithBuffer->beginWithClear(0, 0, 0, 0, 0, 0);
ship3D->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0);
renderTexWithBuffer->end();
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
cocos2d::Sprite3D *ship3D;
cocos2d::RenderTexture *renderTexDefault;
cocos2d::RenderTexture *renderTexWithBuffer;
int frame;
void update(float delta);
};
#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment