2014-05-07 02:19:51 +08:00
|
|
|
#include "ShaderTest2.h"
|
2013-09-09 17:49:13 +08:00
|
|
|
#include "ShaderTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
#include "cocos2d.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderCommand.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
namespace ShaderTest2
|
|
|
|
{
|
|
|
|
static std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
2014-05-07 03:07:42 +08:00
|
|
|
CL(AttribShaderTest),
|
2013-09-09 17:49:13 +08:00
|
|
|
CL(NormalSpriteTest),
|
|
|
|
CL(GreyScaleSpriteTest),
|
|
|
|
CL(BlurSpriteTest),
|
|
|
|
CL(NoiseSpriteTest),
|
|
|
|
CL(EdgeDetectionSpriteTest),
|
|
|
|
CL(BloomSpriteTest),
|
2013-09-12 10:25:55 +08:00
|
|
|
CL(CelShadingSpriteTest),
|
2014-04-14 11:52:17 +08:00
|
|
|
CL(LensFlareSpriteTest),
|
2014-05-07 02:19:51 +08:00
|
|
|
CL(OutlineShadingSpriteTest),
|
|
|
|
CL(UniformShaderTest),
|
|
|
|
|
|
|
|
CL(UniformAttribShaderTest)
|
|
|
|
|
2013-09-09 17:49:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int TEST_CASE_COUNT = sizeof(ShaderTest2::createFunctions) / sizeof(ShaderTest2::createFunctions[0]);
|
|
|
|
|
|
|
|
static int sceneIdx=-1;
|
|
|
|
Layer* createTest(int index)
|
|
|
|
{
|
|
|
|
auto layer = (createFunctions[index])();;
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* nextAction();
|
|
|
|
Layer* backAction();
|
|
|
|
Layer* restartAction();
|
|
|
|
|
|
|
|
Layer* nextAction()
|
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % TEST_CASE_COUNT;
|
|
|
|
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* backAction()
|
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx = TEST_CASE_COUNT -1;
|
|
|
|
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* restartAction()
|
|
|
|
{
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderTestDemo2::ShaderTestDemo2()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ShaderTestDemo2::backCallback(Ref* sender)
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2013-11-14 09:36:33 +08:00
|
|
|
auto s = ShaderTestScene2::create();
|
2013-09-09 17:49:13 +08:00
|
|
|
s->addChild( ShaderTest2::backAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ShaderTestDemo2::nextCallback(Ref* sender)
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2013-11-14 09:36:33 +08:00
|
|
|
auto s = ShaderTestScene2::create();
|
2013-09-09 17:49:13 +08:00
|
|
|
s->addChild( ShaderTest2::nextAction() );
|
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ShaderTestDemo2::restartCallback(Ref* sender)
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2013-11-14 09:36:33 +08:00
|
|
|
auto s = ShaderTestScene2::create();
|
|
|
|
s->addChild(ShaderTest2::restartAction());
|
2013-09-09 17:49:13 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderTestScene2::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = ShaderTest2::nextAction();
|
|
|
|
addChild(layer);
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class spriteType>
|
|
|
|
class ShaderSpriteCreator
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 09:36:33 +08:00
|
|
|
static spriteType* createSprite(const std::string& filename)
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2013-11-14 07:55:36 +08:00
|
|
|
spriteType* ret = spriteType::create();
|
2013-11-14 09:36:33 +08:00
|
|
|
ret->setTexture(filename);
|
|
|
|
ret->initShader();
|
|
|
|
ret->setBackgroundNotification();
|
2013-11-14 07:55:36 +08:00
|
|
|
return ret;
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderSprite : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShaderSprite();
|
|
|
|
~ShaderSprite();
|
|
|
|
|
2013-11-14 09:36:33 +08:00
|
|
|
virtual void initShader();
|
|
|
|
void setBackgroundNotification();
|
|
|
|
|
2014-04-08 22:07:35 +08:00
|
|
|
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
|
2014-02-20 10:53:49 +08:00
|
|
|
void listenBackToForeground(Ref *obj);
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms() = 0;
|
|
|
|
virtual void setCustomUniforms() = 0;
|
|
|
|
protected:
|
|
|
|
std::string _fragSourceFile;
|
2014-04-14 11:52:17 +08:00
|
|
|
std::string _vertSourceFile;
|
2013-12-30 15:53:36 +08:00
|
|
|
protected:
|
|
|
|
CustomCommand _renderCommand;
|
2014-04-11 18:03:07 +08:00
|
|
|
void onDraw(const Matrix &transform, bool transformUpdated);
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ShaderSprite::ShaderSprite()
|
2014-04-14 11:52:17 +08:00
|
|
|
:_vertSourceFile("")
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ShaderSprite::~ShaderSprite()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-14 09:36:33 +08:00
|
|
|
void ShaderSprite::setBackgroundNotification()
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2013-12-31 10:54:37 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
|
|
|
|
this->setShaderProgram(nullptr);
|
|
|
|
this->initShader();
|
|
|
|
});
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
2013-11-14 09:36:33 +08:00
|
|
|
void ShaderSprite::initShader()
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2014-04-14 13:48:17 +08:00
|
|
|
auto fileUtiles = FileUtils::getInstance();
|
|
|
|
auto fragmentFilePath = fileUtiles->fullPathForFilename(_fragSourceFile);
|
|
|
|
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
|
|
|
|
std::string vertSource;
|
2014-04-14 11:52:17 +08:00
|
|
|
if (_vertSourceFile.empty()) {
|
|
|
|
vertSource = ccPositionTextureColor_vert;
|
|
|
|
}else{
|
2014-04-14 13:48:17 +08:00
|
|
|
std::string vertexFilePath = fileUtiles->fullPathForFilename(_vertSourceFile);
|
|
|
|
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
|
2014-04-14 11:52:17 +08:00
|
|
|
}
|
|
|
|
|
2013-11-15 07:44:22 +08:00
|
|
|
auto program = new GLProgram();
|
2014-04-14 13:48:17 +08:00
|
|
|
program->initWithByteArrays(vertSource.c_str(), fragSource.c_str());
|
2013-11-15 07:44:22 +08:00
|
|
|
setShaderProgram(program);
|
|
|
|
program->release();
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2014-03-05 05:51:43 +08:00
|
|
|
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-11-15 07:44:22 +08:00
|
|
|
program->link();
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2013-11-15 07:44:22 +08:00
|
|
|
program->updateUniforms();
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
buildCustomUniforms();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2014-04-08 22:07:35 +08:00
|
|
|
void ShaderSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
2013-09-09 17:49:13 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_renderCommand.init(_globalZOrder);
|
2014-03-06 07:49:08 +08:00
|
|
|
_renderCommand.func = CC_CALLBACK_0(ShaderSprite::onDraw, this, transform, transformUpdated);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_renderCommand);
|
|
|
|
|
2013-12-30 15:53:36 +08:00
|
|
|
}
|
2013-11-15 07:44:22 +08:00
|
|
|
|
2014-04-11 18:03:07 +08:00
|
|
|
void ShaderSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
2013-12-30 15:53:36 +08:00
|
|
|
{
|
2014-03-06 07:49:08 +08:00
|
|
|
auto shader = getShaderProgram();
|
|
|
|
shader->use();
|
|
|
|
shader->setUniformsForBuiltins(transform);
|
|
|
|
|
2013-09-09 17:49:13 +08:00
|
|
|
setCustomUniforms();
|
2013-12-30 15:53:36 +08:00
|
|
|
|
2013-11-15 07:44:22 +08:00
|
|
|
GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
2013-09-09 17:49:13 +08:00
|
|
|
GL::bindTexture2D( getTexture()->getName());
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
2014-01-23 03:19:03 +08:00
|
|
|
#define kQuadSize sizeof(_quad.bl)
|
|
|
|
size_t offset = (size_t)&_quad;
|
2013-09-09 17:49:13 +08:00
|
|
|
|
|
|
|
// vertex
|
|
|
|
int diff = offsetof( V3F_C4B_T2F, vertices);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
|
|
|
|
|
|
|
// texCoods
|
|
|
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
// color
|
|
|
|
diff = offsetof( V3F_C4B_T2F, colors);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2014-03-01 14:03:36 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class NormalSprite : public ShaderSprite, public ShaderSpriteCreator<NormalSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(NormalSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
NormalSprite();
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
NormalSprite::NormalSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_normal.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalSprite::buildCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void NormalSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class GreyScaleSprite : public ShaderSprite, public ShaderSpriteCreator<GreyScaleSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(GreyScaleSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
GreyScaleSprite();
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
GreyScaleSprite::GreyScaleSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_greyScale.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void GreyScaleSprite::buildCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GreyScaleSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class BlurSprite : public ShaderSprite, public ShaderSpriteCreator<BlurSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(BlurSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
BlurSprite();
|
|
|
|
void setBlurSize(float f);
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
protected:
|
2014-03-28 20:53:14 +08:00
|
|
|
int _blurRadius;
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2 _pixelSize;
|
2013-09-09 17:49:13 +08:00
|
|
|
|
2014-03-28 20:53:14 +08:00
|
|
|
int _samplingRadius;
|
|
|
|
float _scale;
|
|
|
|
float _cons;
|
|
|
|
float _weightSum;
|
|
|
|
|
|
|
|
GLuint pixelSizeLocation;
|
|
|
|
GLuint coefficientLocation;
|
2013-09-09 17:49:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
BlurSprite::BlurSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_Blur.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlurSprite::buildCustomUniforms()
|
|
|
|
{
|
|
|
|
auto s = getTexture()->getContentSizeInPixels();
|
2014-03-28 20:53:14 +08:00
|
|
|
_blurRadius = 0;
|
2014-04-15 18:23:40 +08:00
|
|
|
_pixelSize = Vector2(1/s.width, 1/s.height);
|
2014-03-28 20:53:14 +08:00
|
|
|
_samplingRadius = 0;
|
2014-03-05 05:51:43 +08:00
|
|
|
|
2014-03-31 10:44:48 +08:00
|
|
|
setBlurSize(3.0f);
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
2014-03-28 20:53:14 +08:00
|
|
|
pixelSizeLocation = program->getUniformLocation("onePixelSize");
|
|
|
|
coefficientLocation = program->getUniformLocation("gaussianCoefficient");
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlurSprite::setCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
2014-03-28 20:53:14 +08:00
|
|
|
program->setUniformLocationWith2f(pixelSizeLocation, _pixelSize.x, _pixelSize.y);
|
|
|
|
program->setUniformLocationWith4f(coefficientLocation, _samplingRadius, _scale,_cons,_weightSum);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlurSprite::setBlurSize(float f)
|
|
|
|
{
|
2014-03-28 20:53:14 +08:00
|
|
|
if(_blurRadius == (int)f)
|
|
|
|
return;
|
|
|
|
_blurRadius = (int)f;
|
2013-09-09 17:49:13 +08:00
|
|
|
|
2014-03-28 20:53:14 +08:00
|
|
|
_samplingRadius = _blurRadius;
|
|
|
|
if (_samplingRadius > 10)
|
|
|
|
{
|
|
|
|
_samplingRadius = 10;
|
|
|
|
}
|
|
|
|
if (_blurRadius > 0)
|
|
|
|
{
|
|
|
|
float sigma = _blurRadius / 2.0f;
|
|
|
|
_scale = -0.5f / (sigma * sigma);
|
|
|
|
_cons = -1.0f * _scale / 3.141592f;
|
|
|
|
_weightSum = -_cons;
|
|
|
|
|
|
|
|
float weight;
|
|
|
|
int squareX;
|
|
|
|
for(int dx = 0; dx <= _samplingRadius; ++dx)
|
|
|
|
{
|
|
|
|
squareX = dx * dx;
|
|
|
|
weight = _cons * exp(squareX * _scale);
|
|
|
|
_weightSum += 2.0 * weight;
|
|
|
|
for (int dy = 1; dy <= _samplingRadius; ++dy)
|
|
|
|
{
|
|
|
|
weight = _cons * exp((squareX + dy * dy) * _scale);
|
|
|
|
_weightSum += 4.0 * weight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log("_blurRadius:%d",_blurRadius);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class NoiseSprite : public ShaderSprite, public ShaderSpriteCreator<NoiseSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(NoiseSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
NoiseSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLfloat _resolution[2];
|
|
|
|
GLuint _resolutionLoc;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
NoiseSprite::NoiseSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_Noisy.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseSprite::buildCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
|
|
|
_resolutionLoc = program->getUniformLocation("resolution");
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
_resolution[0] = getTexture()->getContentSizeInPixels().width;
|
|
|
|
_resolution[1] = getTexture()->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class EdgeDetectionSprite : public ShaderSprite, public ShaderSpriteCreator<EdgeDetectionSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(EdgeDetectionSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
EdgeDetectionSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLfloat _resolution[2];
|
|
|
|
GLuint _resolutionLoc;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
EdgeDetectionSprite::EdgeDetectionSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_edgeDetection.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void EdgeDetectionSprite::buildCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
|
|
|
_resolutionLoc = program->getUniformLocation("resolution");
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EdgeDetectionSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
_resolution[0] = getTexture()->getContentSizeInPixels().width;
|
|
|
|
_resolution[1] = getTexture()->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class BloomSprite : public ShaderSprite, public ShaderSpriteCreator<BloomSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(BloomSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
BloomSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLfloat _resolution[2];
|
|
|
|
GLuint _resolutionLoc;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
BloomSprite::BloomSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_bloom.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void BloomSprite::buildCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
|
|
|
_resolutionLoc = program->getUniformLocation("resolution");
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BloomSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
_resolution[0] = getTexture()->getContentSizeInPixels().width;
|
|
|
|
_resolution[1] = getTexture()->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class CelShadingSprite : public ShaderSprite, public ShaderSpriteCreator<CelShadingSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(CelShadingSprite);
|
2013-09-09 17:49:13 +08:00
|
|
|
CelShadingSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLfloat _resolution[2];
|
|
|
|
GLuint _resolutionLoc;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
CelShadingSprite::CelShadingSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_celShading.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CelShadingSprite::buildCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
|
|
|
_resolutionLoc = program->getUniformLocation("resolution");
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CelShadingSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
_resolution[0] = getTexture()->getContentSizeInPixels().width;
|
|
|
|
_resolution[1] = getTexture()->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
|
|
|
|
}
|
|
|
|
|
2013-09-12 10:25:55 +08:00
|
|
|
class LensFlareSprite : public ShaderSprite, public ShaderSpriteCreator<LensFlareSprite>
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
CREATE_FUNC(LensFlareSprite);
|
2013-09-12 10:25:55 +08:00
|
|
|
LensFlareSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLfloat _resolution[2];
|
|
|
|
GLfloat _textureResolution[2];
|
|
|
|
GLuint _resolutionLoc;
|
|
|
|
GLuint _textureResolutionLoc;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
LensFlareSprite::LensFlareSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_lensFlare.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LensFlareSprite::buildCustomUniforms()
|
|
|
|
{
|
2014-03-05 05:51:43 +08:00
|
|
|
auto program = getShaderProgram();
|
|
|
|
_resolutionLoc = program->getUniformLocation("resolution");
|
|
|
|
_textureResolutionLoc = program->getUniformLocation("textureResolution");
|
2013-09-12 10:25:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LensFlareSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
_textureResolution[0] = getTexture()->getContentSizeInPixels().width;
|
|
|
|
_textureResolution[1] = getTexture()->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
_resolution[0] = getContentSize().width;
|
|
|
|
_resolution[1] = getContentSize().height;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
|
|
|
|
getShaderProgram()->setUniformLocationWith2fv(_textureResolutionLoc, _textureResolution, 1);
|
|
|
|
}
|
|
|
|
|
2013-09-09 17:49:13 +08:00
|
|
|
NormalSpriteTest::NormalSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
NormalSprite* sprite = NormalSprite::createSprite("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width/2, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GreyScaleSpriteTest::GreyScaleSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
GreyScaleSprite* sprite = GreyScaleSprite::createSprite("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BlurSpriteTest::BlurSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
BlurSprite* sprite = BlurSprite::createSprite("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NoiseSpriteTest::NoiseSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
NoiseSprite* sprite = NoiseSprite::createSprite("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EdgeDetectionSpriteTest::EdgeDetectionSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
EdgeDetectionSprite* sprite = EdgeDetectionSprite::createSprite("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/powered.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BloomSpriteTest::BloomSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2013-09-10 11:45:38 +08:00
|
|
|
BloomSprite* sprite = BloomSprite::createSprite("Images/stone.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/stone.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CelShadingSpriteTest::CelShadingSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2013-09-10 11:45:38 +08:00
|
|
|
CelShadingSprite* sprite = CelShadingSprite::createSprite("Images/stone.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
2013-09-10 11:45:38 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/stone.png");
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
2013-09-09 17:49:13 +08:00
|
|
|
addChild(sprite);
|
2013-09-10 11:45:38 +08:00
|
|
|
addChild(sprite2);
|
2013-09-09 17:49:13 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-12 10:25:55 +08:00
|
|
|
|
|
|
|
LensFlareSpriteTest::LensFlareSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
LensFlareSprite* sprite = LensFlareSprite::createSprite("Images/noise.png");
|
|
|
|
Rect rect = Rect::ZERO;
|
|
|
|
rect.size = Size(480,320);
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(Vector2(s.width * 0.5, s.height/2));
|
2013-09-12 10:25:55 +08:00
|
|
|
addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 11:52:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OutlineSprite : public ShaderSprite, public ShaderSpriteCreator<OutlineSprite>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CREATE_FUNC(OutlineSprite);
|
|
|
|
OutlineSprite();
|
|
|
|
|
|
|
|
private:
|
|
|
|
GLuint _outlineColorUniformLocation;
|
|
|
|
GLuint _thresdholdUniformLocation;
|
|
|
|
GLuint _radiusUniformLocation;
|
|
|
|
protected:
|
|
|
|
virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutlineSprite::OutlineSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_outline.fsh";
|
|
|
|
_vertSourceFile = "Shaders/example_outline.vsh";
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutlineSprite::buildCustomUniforms()
|
|
|
|
{
|
|
|
|
auto program = getShaderProgram();
|
|
|
|
_outlineColorUniformLocation = program->getUniformLocation("u_outlineColor");
|
|
|
|
_thresdholdUniformLocation = program->getUniformLocation("u_threshold");
|
|
|
|
_radiusUniformLocation = program->getUniformLocation("u_radius");
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutlineSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
GLfloat color[3] = {1.0, 0.2, 0.3};
|
2014-04-14 12:03:37 +08:00
|
|
|
GLfloat radius = 0.01;
|
2014-04-14 11:52:17 +08:00
|
|
|
GLfloat threshold = 1.75;
|
|
|
|
|
|
|
|
getShaderProgram()->setUniformLocationWith3fv(_outlineColorUniformLocation, color, 1);
|
|
|
|
getShaderProgram()->setUniformLocationWith1f(_radiusUniformLocation, radius);
|
|
|
|
getShaderProgram()->setUniformLocationWith1f(_thresdholdUniformLocation, threshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OutlineShadingSpriteTest::OutlineShadingSpriteTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init()) {
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
OutlineSprite* sprite = OutlineSprite::createSprite("Images/grossini_dance_10.png");
|
2014-05-07 02:19:51 +08:00
|
|
|
sprite->setPosition(Point(s.width * 0.75, s.height/2));
|
2014-04-14 11:52:17 +08:00
|
|
|
auto sprite2 = Sprite::create("Images/grossini_dance_10.png");
|
2014-05-07 02:19:51 +08:00
|
|
|
sprite2->setPosition(Point(s.width * 0.25, s.height/2));
|
2014-04-14 11:52:17 +08:00
|
|
|
addChild(sprite);
|
|
|
|
addChild(sprite2);
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 02:19:51 +08:00
|
|
|
|
|
|
|
class UniformSprite : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UniformSprite();
|
|
|
|
~UniformSprite();
|
|
|
|
CREATE_FUNC(UniformSprite);
|
|
|
|
|
|
|
|
virtual void initShader();
|
|
|
|
// void setBackgroundNotification();
|
|
|
|
|
|
|
|
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
|
|
|
|
// void listenBackToForeground(Ref *obj);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
protected:
|
|
|
|
std::string _fragSourceFile;
|
|
|
|
std::string _vertSourceFile;
|
|
|
|
protected:
|
|
|
|
CustomCommand _renderCommand;
|
|
|
|
void onDraw(const Matrix &transform, bool transformUpdated);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
UniformSprite::UniformSprite()
|
|
|
|
{
|
|
|
|
_vertSourceFile = "Shaders/example_Heart.vsh";
|
|
|
|
_fragSourceFile = "Shaders/example_Heart.fsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformSprite::~UniformSprite()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformSprite::initShader()
|
|
|
|
{
|
|
|
|
auto shader = new GLProgram();
|
|
|
|
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
|
|
|
|
shader->autoParse();
|
|
|
|
//shader->link();
|
|
|
|
|
|
|
|
shader->updateUniforms();
|
|
|
|
|
|
|
|
this->setShaderProgram(shader);
|
|
|
|
|
|
|
|
std::string attribname ="a_position";
|
|
|
|
shader->getAttrib(attribname)->_size = 2;
|
|
|
|
|
|
|
|
shader->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
|
|
|
{
|
|
|
|
_renderCommand.init(_globalZOrder);
|
|
|
|
_renderCommand.func = CC_CALLBACK_0(UniformSprite::onDraw, this, transform, transformUpdated);
|
|
|
|
renderer->addCommand(&_renderCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string name = "center";
|
2014-05-07 03:07:42 +08:00
|
|
|
_shaderProgram->getUniformValue(name)->setValue(Vector2(480,320));
|
2014-05-07 02:19:51 +08:00
|
|
|
name = "resolution";
|
2014-05-07 03:07:42 +08:00
|
|
|
_shaderProgram->getUniformValue(name)->setValue(Vector2(256,256));
|
2014-05-07 02:19:51 +08:00
|
|
|
|
|
|
|
_shaderProgram->setUniformsForUserDef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
|
|
|
{
|
|
|
|
// Set Shader GLProgram
|
|
|
|
auto program = getShaderProgram();
|
|
|
|
program->use();
|
|
|
|
program->setUniformsForBuiltins(transform);
|
|
|
|
|
|
|
|
setCustomUniforms();
|
|
|
|
//GL::bindTexture2D( getTexture()->getName());
|
|
|
|
program->setAttribForUserDef();
|
|
|
|
|
|
|
|
|
|
|
|
float w = 256, h = 256;
|
|
|
|
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
|
|
|
|
program->setVertexAttrib(vertices, true);
|
|
|
|
|
|
|
|
// Draw Call Test
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UniformShaderTest::UniformShaderTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init()) {
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
UniformSprite* sprite = UniformSprite::create();
|
|
|
|
setContentSize(Size(256, 256));
|
|
|
|
setAnchorPoint(Vector2(0.5f, 0.5f));
|
|
|
|
sprite->initShader();
|
|
|
|
sprite->setPosition(64,64);
|
|
|
|
addChild(sprite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AttribSprite : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AttribSprite();
|
|
|
|
~AttribSprite();
|
|
|
|
CREATE_FUNC(AttribSprite);
|
|
|
|
|
|
|
|
virtual void initShader();
|
|
|
|
// void setBackgroundNotification();
|
|
|
|
|
|
|
|
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
|
|
|
|
// void listenBackToForeground(Ref *obj);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// virtual void buildCustomUniforms();
|
|
|
|
virtual void setCustomUniforms();
|
|
|
|
protected:
|
|
|
|
std::string _fragSourceFile;
|
|
|
|
std::string _vertSourceFile;
|
|
|
|
protected:
|
|
|
|
CustomCommand _renderCommand;
|
|
|
|
void onDraw(const Matrix &transform, bool transformUpdated);
|
|
|
|
};
|
|
|
|
|
|
|
|
AttribSprite::AttribSprite()
|
|
|
|
{
|
|
|
|
_fragSourceFile = "Shaders/example_normal.fsh";
|
|
|
|
_vertSourceFile = "Shaders/example_normal.vsh";
|
|
|
|
}
|
|
|
|
|
|
|
|
AttribSprite::~AttribSprite()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttribSprite::initShader()
|
|
|
|
{
|
|
|
|
auto shader = new GLProgram();
|
|
|
|
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
|
|
|
|
|
|
|
|
shader->autoParse();
|
|
|
|
shader->updateUniforms();
|
|
|
|
this->setShaderProgram(shader);
|
|
|
|
|
|
|
|
shader->release();
|
|
|
|
|
|
|
|
std::string attribname ="a_position";
|
|
|
|
shader->getAttrib(attribname)->_size = 3;
|
|
|
|
shader->getAttrib(attribname)->_index = 0;
|
|
|
|
|
|
|
|
attribname ="a_color";
|
|
|
|
shader->getAttrib(attribname)->_type = GL_UNSIGNED_BYTE;
|
2014-05-07 03:46:35 +08:00
|
|
|
shader->getAttrib(attribname)->_normalized = GL_TRUE;
|
2014-05-07 02:19:51 +08:00
|
|
|
shader->getAttrib(attribname)->_index = 1;
|
|
|
|
|
|
|
|
attribname ="a_texCoord";
|
|
|
|
shader->getAttrib(attribname)->_index = 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
|
|
|
{
|
|
|
|
_renderCommand.init(_globalZOrder);
|
|
|
|
_renderCommand.func = CC_CALLBACK_0(AttribSprite::onDraw, this, transform, transformUpdated);
|
|
|
|
renderer->addCommand(&_renderCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttribSprite::setCustomUniforms()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttribSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Set Shader GLProgram
|
|
|
|
auto program = getShaderProgram();
|
|
|
|
program->use();
|
|
|
|
program->setUniformsForBuiltins(transform);
|
|
|
|
|
|
|
|
setCustomUniforms();
|
|
|
|
|
|
|
|
// Set
|
|
|
|
//glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
//GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
GL::bindTexture2D( getTexture()->getName());
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
#define kQuadSize sizeof(_quad.bl)
|
|
|
|
size_t offset = (size_t)&_quad;
|
|
|
|
size_t stride = kQuadSize;
|
|
|
|
/*
|
|
|
|
// vertex
|
|
|
|
int diff = offsetof( V3F_C4B_T2F, vertices);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
|
|
|
|
|
|
|
|
// texCoods
|
|
|
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
|
|
|
|
|
|
|
|
// color
|
|
|
|
diff = offsetof( V3F_C4B_T2F, colors);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
|
|
|
*/
|
|
|
|
program->setVertexAttrib((void*)offset, false);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
AttribShaderTest::AttribShaderTest()
|
|
|
|
{
|
|
|
|
if (ShaderTestDemo2::init())
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
AttribSprite* sprite = AttribSprite::create();
|
|
|
|
sprite->setTexture("Images/powered.png");
|
|
|
|
sprite->initShader();
|
|
|
|
sprite->setPosition(Vector2(s.width/2, s.height/2));
|
|
|
|
addChild(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformAttribShaderTest::UniformAttribShaderTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|