GLProgramState fixes

It is possible to submit to the Renderer a Sprite with custom uniforms
(although it won't be batched).
It is possible to use a custom shader + uniforms on Sprite with
subclassing Sprite
This commit is contained in:
Ricardo Quesada 2014-05-12 19:12:56 -07:00
parent 5d24b3d99a
commit 012f1020ca
45 changed files with 163 additions and 643 deletions

View File

@ -106,7 +106,7 @@ bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeigh
_quadsToDraw = itemsToRender;
// shader stuff
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
return true;
}
@ -138,7 +138,7 @@ void AtlasNode::draw(Renderer *renderer, const Matrix &transform, bool transform
_quadCommand.init(
_globalZOrder,
_textureAtlas->getTexture()->getName(),
getGLProgram(),
getGLProgramState(),
_blendFunc,
_textureAtlas->getQuads(),
_quadsToDraw,

View File

@ -158,7 +158,7 @@ bool DrawNode::init()
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
ensureCapacity(512);

View File

@ -314,7 +314,7 @@ void Grid3D::blit(void)
{
int n = _gridSize.width * _gridSize.height;
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();;
@ -536,7 +536,7 @@ void TiledGrid3D::blit(void)
//
// Attributes
//
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
#ifdef EMSCRIPTEN
int numQuads = _gridSize.width * _gridSize.height;

View File

@ -339,21 +339,21 @@ void Label::updateShaderProgram()
{
case cocos2d::LabelEffect::NORMAL:
if (_useDistanceField)
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL));
else if (_useA8Shader)
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_LABEL_NORMAL));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_NORMAL));
else
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
break;
case cocos2d::LabelEffect::OUTLINE:
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_LABEL_OUTLINE));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_OUTLINE));
_uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "v_effectColor");
break;
case cocos2d::LabelEffect::GLOW:
if (_useDistanceField)
{
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW));
_uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "v_effectColor");
}
break;

View File

@ -534,7 +534,7 @@ bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
updateColor();
setContentSize(Size(w, h));
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
return true;
}
return false;

View File

@ -128,7 +128,7 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
// shader state
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setTexture(texture);
setColor(color);

View File

@ -647,7 +647,7 @@ void Node::setGLProgram(GLProgram *glProgram)
if (_glProgramState == nullptr || (_glProgramState && _glProgramState->getGLProgram() != glProgram))
{
CC_SAFE_RELEASE(_glProgramState);
_glProgramState = GLProgramState::get(glProgram);
_glProgramState = GLProgramState::getOrCreate(glProgram);
_glProgramState->retain();
}
}

View File

@ -103,7 +103,7 @@ bool ParticleBatchNode::initWithTexture(Texture2D *tex, int capacity)
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true;
}

View File

@ -131,7 +131,7 @@ bool ParticleSystemQuad::initWithTotalParticles(int numberOfParticles)
setupVBO();
}
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
#if CC_ENABLE_CACHE_TEXTURE_DATA
// Need to listen the event only when not use batchnode, because it will use VBO
@ -374,7 +374,7 @@ void ParticleSystemQuad::draw(Renderer *renderer, const Matrix &transform, bool
//quad command
if(_particleIdx > 0)
{
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgram(), _blendFunc, _quads, _particleIdx, transform);
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, _quads, _particleIdx, transform);
renderer->addCommand(&_quadCommand);
}
}

View File

@ -87,7 +87,7 @@ bool ProgressTimer::initWithSprite(Sprite* sp)
setSprite(sp);
// shader state
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true;
}

View File

@ -249,7 +249,7 @@ bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect, bool rotated)
_quad.tr.colors = Color4B::WHITE;
// shader state
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
// update texture (calls updateBlendFunc)
setTexture(texture);
@ -590,7 +590,7 @@ void Sprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpd
if(_insideBounds)
{
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgram(), _blendFunc, &_quad, 1, transform);
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transform);
renderer->addCommand(&_quadCommand);
#if CC_SPRITE_DEBUG_DRAW
_customDebugDrawCommand.init(_globalZOrder);

View File

@ -100,7 +100,7 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity)
_descendants.reserve(capacity);
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true;
}

View File

@ -205,7 +205,7 @@ void TMXLayer::parseInternalProperties()
_useAutomaticVertexZ = true;
auto alphaFuncVal = getProperty("cc_alpha_func");
float alphaFuncValue = alphaFuncVal.asFloat();
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
GLint alphaValueLocation = glGetUniformLocation(getGLProgram()->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);

View File

@ -1159,7 +1159,7 @@ void Texture2D::drawAtPoint(const Vector2& point)
point.x, height + point.y,
width + point.x, height + point.y };
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
@ -1193,7 +1193,7 @@ void Texture2D::drawInRect(const Rect& rect)
rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();

View File

@ -848,7 +848,7 @@ CC_DEPRECATED_ATTRIBUTE extern const char* kCCAttributeNameTexCoord;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_None = GL::VERTEX_ATTRIB_FLAG_NONE;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_Position = GL::VERTEX_ATTRIB_FLAG_POSITION;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_Color = GL::VERTEX_ATTRIB_FLAG_COLOR;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_TexCoords = GL::VERTEX_ATTRIB_FLAG_TEX_COORDS;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_TexCoords = GL::VERTEX_ATTRIB_FLAG_TEX_COORD;
CC_DEPRECATED_ATTRIBUTE const int kCCVertexAttribFlag_PosColorTex = GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX;
CC_DEPRECATED_ATTRIBUTE const ProgressTimer::Type kCCProgressTimerTypeRadial = ProgressTimer::Type::RADIAL;

View File

@ -182,7 +182,7 @@ bool Armature::init(const std::string& name)
}
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setCascadeOpacityEnabled(true);
setCascadeColorEnabled(true);

View File

@ -61,7 +61,7 @@ BatchNode::~BatchNode()
bool BatchNode::init()
{
bool ret = Node::init();
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return ret;
}

View File

@ -224,7 +224,7 @@ void Skin::draw(Renderer *renderer, const Matrix &transform, bool transformUpdat
Matrix mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
//TODO implement z order
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgram(), _blendFunc, &_quad, 1, mv);
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, mv);
renderer->addCommand(&_quadCommand);
}

View File

@ -69,7 +69,7 @@ void Skeleton::initialize () {
setOpacityModifyRGB(true);
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
}
void Skeleton::setSkeletonData (spSkeletonData *skeletonData, bool isOwnsSkeletonData) {

View File

@ -33,8 +33,6 @@ NS_CC_BEGIN
class TextureAtlas;
#define CC_NO_TEXTURE 0
class BatchCommand : public RenderCommand
{
public:

View File

@ -166,12 +166,14 @@ void UniformValue::setMat4(const Matrix& value)
VertexAttribValue::VertexAttribValue()
: _useCallback(false)
, _vertexAttrib(nullptr)
, _enabled(false)
{
}
VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
: _useCallback(false)
, _vertexAttrib(vertexAttrib)
, _enabled(false)
{
}
@ -183,6 +185,7 @@ VertexAttribValue::~VertexAttribValue()
void VertexAttribValue::apply()
{
if(_enabled) {
if(_useCallback) {
(*_value.callback)(_vertexAttrib);
}
@ -195,6 +198,7 @@ void VertexAttribValue::apply()
_value.pointer.stride,
_value.pointer.pointer);
}
}
}
void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback)
@ -202,6 +206,7 @@ void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &ca
_value.callback = new std::function<void(VertexAttrib*)>();
*_value.callback = callback;
_useCallback = true;
_enabled = true;
}
void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
@ -211,6 +216,7 @@ void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized
_value.pointer.normalized = normalized;
_value.pointer.stride = stride;
_value.pointer.pointer = pointer;
_enabled = true;
}
//
@ -229,18 +235,18 @@ GLProgramState* GLProgramState::create(GLProgram *glprogram)
return ret;
}
GLProgramState* GLProgramState::getWithGLProgramName(const std::string &glProgramName )
GLProgramState* GLProgramState::getOrCreateWithGLProgramName(const std::string &glProgramName )
{
GLProgram *glProgram = GLProgramCache::getInstance()->getGLProgram(glProgramName);
if( glProgram )
return get(glProgram);
return getOrCreate(glProgram);
CCLOG("cocos2d: warning: GLProgram '%s' not found", glProgramName.c_str());
return nullptr;
}
GLProgramState* GLProgramState::get(GLProgram *glprogram)
GLProgramState* GLProgramState::getOrCreate(GLProgram *glprogram)
{
GLProgramState* ret = GLProgramStateCache::getInstance()->getGLProgramState(glprogram);
return ret;
@ -294,6 +300,9 @@ void GLProgramState::apply(const Matrix& modelView)
_glprogram->setUniformsForBuiltins(modelView);
// quick hack:
// Don't deal with attributes if they were set
if(_vertexAttribsFlags) {
// enable/disable vertex attribs
GL::enableVertexAttribs(_vertexAttribsFlags);
@ -301,6 +310,7 @@ void GLProgramState::apply(const Matrix& modelView)
for(auto &attribute : _attributes) {
attribute.second.apply();
}
}
// set uniforms
for(auto& uniform : _uniforms) {

View File

@ -108,6 +108,7 @@ public:
protected:
VertexAttrib* _vertexAttrib; // weak ref
bool _useCallback;
bool _enabled;
union U{
struct {
@ -143,19 +144,22 @@ public:
static GLProgramState* create(GLProgram* glprogram);
/** gets-or-creates an instance of GLProgramState for a given GLProgram */
static GLProgramState* get(GLProgram* glprogram);
static GLProgramState* getOrCreate(GLProgram* glprogram);
/** gets-or-creates an instance of GLProgramState for a given GLProgramName */
static GLProgramState* getWithGLProgramName(const std::string &glProgramName );
static GLProgramState* getOrCreateWithGLProgramName(const std::string &glProgramName );
void apply(const Matrix& modelView);
void setGLProgram(GLProgram* glprogram);
GLProgram* getGLProgram() const { return _glprogram; }
uint32_t getVertexAttribsFlags() const { return _vertexAttribsFlags; }
ssize_t getVertexAttribCount() const { return _attributes.size(); }
void setVertexAttribCallback(const std::string &name, const std::function<void(VertexAttrib*)> &callback);
void setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
ssize_t getUniformCount() const { return _uniforms.size(); }
void setUniformFloat(const std::string &uniformName, float value);
void setUniformInt(const std::string &uniformName, int value);
void setUniformVec2(const std::string &uniformName, const Vector2& value);
@ -164,6 +168,7 @@ public:
void setUniformMat4(const std::string &uniformName, const Matrix& value);
void setUniformCallback(const std::string &uniformName, const std::function<void(Uniform*)> &callback);
protected:
GLProgramState();
~GLProgramState();
@ -176,7 +181,6 @@ protected:
std::unordered_map<std::string, VertexAttribValue> _attributes;
uint32_t _vertexAttribsFlags;
GLProgram *_glprogram;
};

View File

@ -25,20 +25,17 @@
#include "renderer/CCQuadCommand.h"
#include "renderer/ccGLStateCache.h"
#include "renderer/CCGLProgram.h"
#include "renderer/CCGLProgramState.h"
#include "xxhash.h"
NS_CC_BEGIN
static void convertIntToByteArray(int value, int* output)
{
*output = value;
}
QuadCommand::QuadCommand()
:_materialID(0)
,_textureID(0)
,_shader(nullptr)
,_glProgramState(nullptr)
,_blendType(BlendFunc::DISABLE)
,_quads(nullptr)
,_quadsCount(0)
@ -46,8 +43,11 @@ QuadCommand::QuadCommand()
_type = RenderCommand::Type::QUAD_COMMAND;
}
void QuadCommand::init(float globalOrder, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const Matrix &mv)
void QuadCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const Matrix &mv)
{
CCASSERT(glProgramState, "Invalid GLProgramState");
CCASSERT(glProgramState->getVertexAttribsFlags() == 0, "No custom attributes are supported in QuadCommand");
_globalOrder = globalOrder;
_quadsCount = quadCount;
@ -55,11 +55,11 @@ void QuadCommand::init(float globalOrder, GLuint textureID, GLProgram* shader, B
_mv = mv;
if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _shader != shader) {
if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _glProgramState != glProgramState) {
_textureID = textureID;
_blendType = blendType;
_shader = shader;
_glProgramState = glProgramState;
generateMaterialID();
}
@ -71,48 +71,29 @@ QuadCommand::~QuadCommand()
void QuadCommand::generateMaterialID()
{
//TODO fix blend id generation
int blendID = 0;
if(_blendType == BlendFunc::DISABLE)
if( _glProgramState->getUniformCount() > 0 )
{
blendID = 0;
}
else if(_blendType == BlendFunc::ALPHA_PREMULTIPLIED)
{
blendID = 1;
}
else if(_blendType == BlendFunc::ALPHA_NON_PREMULTIPLIED)
{
blendID = 2;
}
else if(_blendType == BlendFunc::ADDITIVE)
{
blendID = 3;
_materialID = QuadCommand::MATERIAL_ID_IGNORE;
}
else
{
blendID = 4;
}
// convert program id, texture id and blend id into byte array
int intArray[3];
convertIntToByteArray(_shader->getProgram(), intArray);
convertIntToByteArray(blendID, intArray+1);
convertIntToByteArray(_textureID, intArray+2);
int glProgram = (int)_glProgramState->getGLProgram()->getProgram();
int intArray[4] = { glProgram, (int)_textureID, (int)_blendType.src, (int)_blendType.dst};
_materialID = XXH32((const void*)intArray, sizeof(intArray), 0);
}
}
void QuadCommand::useMaterial() const
{
_shader->use();
_shader->setUniformsForBuiltins(_mv);
//Set texture
GL::bindTexture2D(_textureID);
//set blend mode
GL::blendFunc(_blendType.src, _blendType.dst);
_glProgramState->apply(_mv);
}
NS_CC_END

View File

@ -25,61 +25,47 @@
#ifndef _CC_QUADCOMMAND_H_
#define _CC_QUADCOMMAND_H_
#include "CCRenderCommand.h"
#include "renderer/CCGLProgram.h"
#include "CCRenderCommandPool.h"
#include "renderer/CCRenderCommand.h"
#include "renderer/CCGLProgramState.h"
#include "renderer/CCRenderCommandPool.h"
NS_CC_BEGIN
#define CC_NO_TEXTURE 0
/** Command used to render one or more Quads */
class QuadCommand : public RenderCommand
{
public:
static const int MATERIAL_ID_IGNORE = 0;
QuadCommand();
~QuadCommand();
/** Initializes the command with a globalZOrder, a texture ID, a `GLProgram`, a blending function, a pointer to quads,
* quantity of quads, and the Model View transform to be used for the quads */
void init(float globalOrder, GLuint texutreID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quads, ssize_t quadCount,
void init(float globalOrder, GLuint texutreID, GLProgramState* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quads, ssize_t quadCount,
const Matrix& mv);
void useMaterial() const;
//TODO use material to decide if it is translucent
inline bool isTranslucent() const { return true; }
inline uint32_t getMaterialID() const { return _materialID; }
inline GLuint getTextureID() const { return _textureID; }
inline V3F_C4B_T2F_Quad* getQuads() const { return _quads; }
inline ssize_t getQuadCount() const { return _quadsCount; }
inline GLProgram* getShader() const { return _shader; }
inline GLProgramState* getGLProgramState() const { return _glProgramState; }
inline BlendFunc getBlendType() const { return _blendType; }
inline const Matrix& getModelView() const { return _mv; }
private:
void generateMaterialID();
protected:
void generateMaterialID();
uint32_t _materialID;
GLuint _textureID;
GLProgram* _shader;
GLProgramState* _glProgramState;
BlendFunc _blendType;
V3F_C4B_T2F_Quad* _quads;
ssize_t _quadsCount;
Matrix _mv;
};
NS_CC_END

View File

@ -450,10 +450,10 @@ void Renderer::drawBatchedQuads()
}
//Start drawing verties in batch
//for(auto i = _batchedQuadCommands.begin(); i != _batchedQuadCommands.end(); ++i)
for(const auto& cmd : _batchedQuadCommands)
{
if(_lastMaterialID != cmd->getMaterialID())
auto newMaterialID = cmd->getMaterialID();
if(_lastMaterialID != newMaterialID || newMaterialID == QuadCommand::MATERIAL_ID_IGNORE)
{
//Draw quads
if(quadsToDraw > 0)
@ -468,7 +468,7 @@ void Renderer::drawBatchedQuads()
//Use new material
cmd->useMaterial();
_lastMaterialID = cmd->getMaterialID();
_lastMaterialID = newMaterialID;
}
quadsToDraw += cmd->getQuadCount();

View File

@ -50,9 +50,9 @@ enum {
VERTEX_ATTRIB_FLAG_POSITION = 1 << 0,
VERTEX_ATTRIB_FLAG_COLOR = 1 << 1,
VERTEX_ATTRIB_FLAG_TEX_COORDS = 1 << 2,
VERTEX_ATTRIB_FLAG_TEX_COORD = 1 << 2,
VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = (VERTEX_ATTRIB_FLAG_POSITION | VERTEX_ATTRIB_FLAG_COLOR | VERTEX_ATTRIB_FLAG_TEX_COORDS),
VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = (VERTEX_ATTRIB_FLAG_POSITION | VERTEX_ATTRIB_FLAG_COLOR | VERTEX_ATTRIB_FLAG_TEX_COORD),
};
/** @file ccGLStateCache.h

View File

@ -748,7 +748,7 @@ void RawStencilBufferTest3::setupStencilForDrawingOnPlane(GLint plane)
void RawStencilBufferTestAlphaTest::setup()
{
RawStencilBufferTest::setup();
auto programState = GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
auto programState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
for(int i = 0; i < _planeCount; ++i)
{
_spritesStencil.at(i)->setGLProgramState(programState);

View File

@ -185,7 +185,7 @@ void ArmatureTestLayer::onEnter()
addChild(menu, 100);
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
}
void ArmatureTestLayer::onExit()

View File

@ -150,7 +150,7 @@ void SceneEditorTestLayer::onEnter()
addChild(menu, 100);
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
}
void SceneEditorTestLayer::onExit()

View File

@ -14,7 +14,7 @@ class TouchPoint : public Node
public:
TouchPoint()
{
setGLProgramState(GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
}
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)

View File

@ -989,7 +989,7 @@ public:
sprite->initWithFile(spritefilename);
sprite->autorelease();
auto shaderState = GLProgramState::getWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
auto shaderState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
sprite->setGLProgramState(shaderState);
return sprite;
}

View File

@ -120,7 +120,7 @@ ShaderNode::~ShaderNode()
{
}
ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
ShaderNode* ShaderNode::shaderNodeWithVertex(const std::string &vert, const std::string& frag)
{
auto node = new ShaderNode();
node->initWithVertex(vert, frag);
@ -129,17 +129,20 @@ ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
return node;
}
bool ShaderNode::initWithVertex(const char *vert, const char *frag)
bool ShaderNode::initWithVertex(const std::string &vert, const std::string &frag)
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
this->setGLProgram(nullptr);
loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str());
this->setGLProgramState(nullptr);
loadShaderVertex(_vertFileName, _fragFileName);
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
_vertFileName = vert;
_fragFileName = frag;
loadShaderVertex(vert, frag);
_time = 0;
@ -151,16 +154,30 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
setContentSize(Size(SIZE_X, SIZE_Y));
setAnchorPoint(Vector2(0.5f, 0.5f));
_vertFileName = vert;
_fragFileName = frag;
return true;
}
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &frag)
{
auto shader = GLProgram::createWithFilenames(vert, frag);
this->setGLProgram(shader);
auto fileUtiles = FileUtils::getInstance();
// frag
auto fragmentFilePath = fileUtiles->fullPathForFilename(frag);
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
// vert
std::string vertSource;
if (vert.empty()) {
vertSource = ccPositionTextureColor_vert;
} else {
std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}
auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
auto glprogramstate = GLProgramState::getOrCreate(glprogram);
setGLProgramState(glprogramstate);
}
void ShaderNode::update(float dt)
@ -208,7 +225,7 @@ bool ShaderMonjori::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Monjori.vsh", "Shaders/example_Monjori.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Monjori.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -242,7 +259,7 @@ bool ShaderMandelbrot::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Mandelbrot.vsh", "Shaders/example_Mandelbrot.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Mandelbrot.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -275,7 +292,7 @@ bool ShaderJulia::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Julia.vsh", "Shaders/example_Julia.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Julia.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -309,7 +326,7 @@ bool ShaderHeart::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/example_Heart.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Heart.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -342,7 +359,7 @@ bool ShaderFlower::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Flower.vsh", "Shaders/example_Flower.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Flower.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -375,7 +392,7 @@ bool ShaderPlasma::init()
{
if (ShaderTestDemo::init())
{
auto sn = ShaderNode::shaderNodeWithVertex("Shaders/example_Plasma.vsh", "Shaders/example_Plasma.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/example_Plasma.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -406,13 +423,11 @@ public:
~SpriteBlur();
void setBlurSize(float f);
bool initWithTexture(Texture2D* texture, const Rect& rect);
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
void initProgram();
void initGLProgram();
static SpriteBlur* create(const char *pszFileName);
protected:
void onDraw(const Matrix &transform, bool transformUpdated);
int _blurRadius;
Vector2 _pixelSize;
@ -422,8 +437,6 @@ protected:
float _scale;
float _cons;
float _weightSum;
CustomCommand _customCommand;
};
SpriteBlur::~SpriteBlur()
@ -453,7 +466,7 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
setGLProgram(nullptr);
initProgram();
initGLProgram();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
@ -464,7 +477,7 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
_pixelSize = Vector2(1/s.width, 1/s.height);
_samplingRadius = 0;
this->initProgram();
this->initGLProgram();
getGLProgramState()->setUniformVec2("onePixelSize", _pixelSize);
@ -474,45 +487,14 @@ bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
return false;
}
void SpriteBlur::initProgram()
void SpriteBlur::initGLProgram()
{
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_vert, fragSource);
setGLProgram(program);
auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource);
auto glProgramState = getGLProgramState();
#define kQuadSize sizeof(_quad.bl)
size_t offset = (size_t)&_quad;
// position
int diff = offsetof( V3F_C4B_T2F, vertices);
glProgramState->setVertexAttribPointer("a_position", 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texcoord
diff = offsetof( V3F_C4B_T2F, texCoords);
glProgramState->setVertexAttribPointer("a_texCoord", 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( V3F_C4B_T2F, colors);
glProgramState->setVertexAttribPointer("a_color", 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
}
void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(SpriteBlur::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
}
void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated)
{
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
GL::bindTexture2D(_texture->getName());
auto glProgramState = getGLProgramState();
glProgramState->apply(transform);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
auto glProgramState = GLProgramState::getOrCreate(program);
setGLProgramState(glProgramState);
}
void SpriteBlur::setBlurSize(float f)
@ -614,8 +596,8 @@ bool ShaderBlur::init()
void ShaderBlur::sliderAction(Ref* sender, Control::EventType controlEvent)
{
ControlSlider* pSlider = (ControlSlider*)sender;
_blurSprite->setBlurSize(pSlider->getValue());
ControlSlider* slider = (ControlSlider*)sender;
_blurSprite->setBlurSize(slider->getValue());
}
// ShaderRetroEffect
@ -680,101 +662,6 @@ std::string ShaderRetroEffect::subtitle() const
}
UniformShaderNode::UniformShaderNode()
:_center(Vector2(0.0f, 0.0f))
,_resolution(Vector2(0.0f, 0.0f))
,_time(0.0f)
{
}
UniformShaderNode::~UniformShaderNode()
{
}
bool UniformShaderNode::initWithVertex(const char *vert, const char *frag)
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
this->setGLProgram(nullptr);
loadShaderVertex(_vertFileName.c_str(), _fragFileName.c_str());
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
loadShaderVertex(vert, frag);
_time = 0;
auto s = Director::getInstance()->getWinSize();
_resolution = Vector2(s.width , s.height );
getGLProgramState()->setUniformVec2("resolution", _resolution);
scheduleUpdate();
//setContentSize(Size(getContentSize().width, getContentSize().height));
setAnchorPoint(Vector2(0.5f, 0.5f));
_vertFileName = vert;
_fragFileName = frag;
return true;
}
void UniformShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
auto shader = GLProgram::createWithFilenames(vert, frag);
this->setGLProgram(shader);
}
void UniformShaderNode::update(float dt)
{
_time += dt;
}
void UniformShaderNode::setPosition(const Vector2 &newPosition)
{
Node::setPosition(newPosition);
auto position = getPosition();
_center = Vector2(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
getGLProgramState()->setUniformVec2("center", _center);
}
void UniformShaderNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(UniformShaderNode::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
}
UniformShaderNode* UniformShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
{
auto node = new UniformShaderNode();
node->initWithVertex(vert, frag);
node->autorelease();
return node;
}
void UniformShaderNode::onDraw(const Matrix &transform, bool transformUpdated)
{
float w = getContentSize().width, h = getContentSize().height;
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
auto glProgramState = getGLProgramState();
glProgramState->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices);
glProgramState->apply(transform);
glDrawArrays(GL_TRIANGLES, 0, 6);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
}
ShaderLensFlare::ShaderLensFlare()
{
init();
@ -794,7 +681,7 @@ bool ShaderLensFlare::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_LensFlare.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_LensFlare.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -826,7 +713,7 @@ bool ShaderFireBall::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_FireBall.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_FireBall.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));
@ -859,7 +746,7 @@ bool ShaderGlow::init()
{
if (ShaderTestDemo::init())
{
auto sn = UniformShaderNode::shaderNodeWithVertex("Shaders/example_Heart.vsh", "Shaders/shadertoy_Glow.fsh");
auto sn = ShaderNode::shaderNodeWithVertex("", "Shaders/shadertoy_Glow.fsh");
auto s = Director::getInstance()->getWinSize();
sn->setPosition(Vector2(s.width/2, s.height/2));

View File

@ -113,19 +113,19 @@ protected:
class ShaderNode : public Node
{
public:
ShaderNode();
~ShaderNode();
bool initWithVertex(const char *vert, const char *frag);
void loadShaderVertex(const char *vert, const char *frag);
static ShaderNode* shaderNodeWithVertex(const std::string &vert, const std::string &frag);
virtual void update(float dt);
virtual void setPosition(const Vector2 &newPosition);
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
static ShaderNode* shaderNodeWithVertex(const char *vert, const char *frag);
protected:
ShaderNode();
~ShaderNode();
bool initWithVertex(const std::string &vert, const std::string &frag);
void loadShaderVertex(const std::string &vert, const std::string &frag);
void onDraw(const Matrix &transform, bool transformUpdated);
Vector2 _center;
@ -142,34 +142,6 @@ public:
virtual void runThisTest();
};
class UniformShaderNode : public Node
{
public:
UniformShaderNode();
~UniformShaderNode();
bool initWithVertex(const char *vert, const char *frag);
void loadShaderVertex(const char *vert, const char *frag);
virtual void update(float dt);
virtual void setPosition(const Vector2 &newPosition);
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
static UniformShaderNode* shaderNodeWithVertex(const char *vert, const char *frag);
protected:
void onDraw(const Matrix &transform, bool transformUpdated);
Vector2 _center;
Vector2 _resolution;
float _time;
std::string _vertFileName;
std::string _fragFileName;
CustomCommand _customCommand;
};
class ShaderLensFlare : public ShaderTestDemo
{
public:

View File

@ -136,7 +136,6 @@ public:
virtual void initShader();
void setBackgroundNotification();
virtual void draw(Renderer *renderer, const Matrix &transform, bool transformUpdated) override;
void listenBackToForeground(Ref *obj);
protected:
@ -144,9 +143,6 @@ protected:
std::string _fragSourceFile;
std::string _vertSourceFile;
CustomCommand _renderCommand;
void onDraw(const Matrix &transform, bool transformUpdated);
};
ShaderSprite::ShaderSprite()
@ -162,7 +158,7 @@ void ShaderSprite::setBackgroundNotification()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event){
this->setGLProgram(nullptr);
this->setGLProgramState(nullptr);
this->initShader();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
@ -176,49 +172,17 @@ void ShaderSprite::initShader()
auto fragSource = fileUtiles->getStringFromFile(fragmentFilePath);
std::string vertSource;
if (_vertSourceFile.empty()) {
vertSource = ccPositionTextureColor_vert;
vertSource = ccPositionTextureColor_noMVP_vert;
}else{
std::string vertexFilePath = fileUtiles->fullPathForFilename(_vertSourceFile);
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}
auto program = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
setGLProgram(program);
auto glprogramState = getGLProgramState();
auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
auto glprogramState = GLProgramState::getOrCreate(glprogram);
this->setGLProgramState(glprogramState);
setCustomUniforms();
#define kQuadSize sizeof(_quad.bl)
size_t offset = (size_t)&_quad;
// vertex
int diff = offsetof( V3F_C4B_T2F, vertices);
glprogramState->setVertexAttribPointer("a_position", 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( V3F_C4B_T2F, texCoords);
glprogramState->setVertexAttribPointer("a_texCoord", 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// color
diff = offsetof( V3F_C4B_T2F, colors);
glprogramState->setVertexAttribPointer("a_color", 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*) (offset + diff));
}
void ShaderSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
{
_renderCommand.init(_globalZOrder);
_renderCommand.func = CC_CALLBACK_0(ShaderSprite::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_renderCommand);
}
void ShaderSprite::onDraw(const Matrix &transform, bool transformUpdated)
{
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
GL::bindTexture2D(_texture->getName());
getGLProgramState()->apply(transform);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
}
class NormalSprite : public ShaderSprite, public ShaderSpriteCreator<NormalSprite>
@ -588,7 +552,7 @@ protected:
OutlineSprite::OutlineSprite()
{
_fragSourceFile = "Shaders/example_outline.fsh";
_vertSourceFile = "Shaders/example_outline.vsh";
_vertSourceFile = ""; //"Shaders/example_outline.vsh";
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}
@ -619,157 +583,3 @@ OutlineShadingSpriteTest::OutlineShadingSpriteTest()
}
}
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 setCustomUniforms();
std::string _fragSourceFile;
std::string _vertSourceFile;
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 = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
this->setGLProgram(shader);
}
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()
{
auto glprogramstate = getGLProgramState();
glprogramstate->setUniformVec2("center", Vector2(480,320));
glprogramstate->setUniformVec2("resolution", Vector2(256,256));
}
void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
{
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
GL::bindTexture2D(_texture->getName());
float w = 256, h = 256;
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
auto glprogramstate = getGLProgramState();
glprogramstate->setVertexAttribPointer("a_position", 2, GL_FLOAT, GL_FALSE, 0, vertices);
glprogramstate->apply(transform);
// Draw Call Test
glDrawArrays(GL_TRIANGLES, 0, 6);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
CHECK_GL_ERROR_DEBUG();
}
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 setCustomUniforms();
std::string _fragSourceFile;
std::string _vertSourceFile;
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 glProgram = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
setGLProgram(glProgram);
auto glProgramState = getGLProgramState();
#define kQuadSize sizeof(_quad.bl)
size_t offset = (size_t)&_quad;
// vertex
int diff = offsetof( V3F_C4B_T2F, vertices);
glProgramState->setVertexAttribPointer("a_position", 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( V3F_C4B_T2F, texCoords);
glProgramState->setVertexAttribPointer("a_texCoord", 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// color
diff = offsetof( V3F_C4B_T2F, colors);
glProgramState->setVertexAttribPointer("a_color", 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*) (offset + diff));
//program->getUniformValue("u_diffuseColor")->setValue(Vector4(1,1,1,1));
}
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)
{
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
GL::bindTexture2D(_texture->getName());
getGLProgramState()->apply(transform);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
CHECK_GL_ERROR_DEBUG();
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,8 +0,0 @@
// http://www.cocos2d-iphone.org
attribute vec4 a_position;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
}

View File

@ -1,13 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec4 v_diffColor;
uniform sampler2D CC_Texture0;
void main(void)
{
gl_FragColor = v_fragmentColor;// * v_diffColor;
}

View File

@ -1,19 +0,0 @@
attribute vec3 a_position;
attribute vec4 a_color;
uniform vec4 u_diffuseColor;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec4 v_diffColor;
#else
varying vec4 v_fragmentColor;
varying vec4 v_diffColor;
#endif
void main()
{
gl_Position = CC_MVPMatrix * vec4(a_position, 1);
v_fragmentColor = a_color;
v_diffColor = u_diffuseColor;
}

View File

@ -1,20 +0,0 @@
attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * vec4(a_position, 1);
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}

View File

@ -1,20 +0,0 @@
/*
Created by guanghui on 4/8/14.
*/
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
varying vec4 v_fragmentColor;
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}