axmol/extensions/CCArmature/display/CCShaderNode.cpp

137 lines
4.0 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCShaderNode.h"
namespace cocos2d { namespace extension { namespace armature {
2013-06-06 12:02:54 +08:00
2013-06-07 10:52:32 +08:00
enum
2013-06-06 12:02:54 +08:00
{
SIZE_X = 128,
SIZE_Y = 128,
};
ShaderNode::ShaderNode()
: _center(Vertex2F(0.0f, 0.0f))
, _resolution(Vertex2F(0.0f, 0.0f))
, _time(0.0f)
, _uniformCenter(0)
, _uniformResolution(0)
, _uniformTime(0)
2013-06-06 12:02:54 +08:00
{
}
ShaderNode *ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag)
2013-06-06 12:02:54 +08:00
{
ShaderNode *node = new ShaderNode();
2013-06-06 12:02:54 +08:00
node->initWithVertex(vert, frag);
node->autorelease();
return node;
}
bool ShaderNode::initWithVertex(const char *vert, const char *frag)
2013-06-06 12:02:54 +08:00
{
loadShaderVertex(vert, frag);
_time = 0;
_resolution = Vertex2F(SIZE_X, SIZE_Y);
2013-06-06 12:02:54 +08:00
scheduleUpdate();
setContentSize(CCSizeMake(SIZE_X, SIZE_Y));
2013-07-12 14:11:55 +08:00
setAnchorPoint(Point(0.5f, 0.5f));
2013-06-06 12:02:54 +08:00
return true;
}
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
2013-06-06 12:02:54 +08:00
{
GLProgram *shader = new GLProgram();
2013-06-06 12:02:54 +08:00
shader->initWithVertexShaderFilename(vert, frag);
shader->addAttribute("aVertex", kVertexAttrib_Position);
2013-06-06 12:02:54 +08:00
shader->link();
shader->updateUniforms();
_uniformCenter = glGetUniformLocation(shader->getProgram(), "center");
_uniformResolution = glGetUniformLocation(shader->getProgram(), "resolution");
_uniformTime = glGetUniformLocation(shader->getProgram(), "time");
2013-06-06 12:02:54 +08:00
this->setShaderProgram(shader);
shader->release();
}
void ShaderNode::update(float dt)
2013-06-06 12:02:54 +08:00
{
_time += dt;
2013-06-06 12:02:54 +08:00
}
void ShaderNode::translateFormOtherNode(AffineTransform &transform)
2013-06-06 12:02:54 +08:00
{
Node::setAdditionalTransform(transform);
2013-06-06 12:02:54 +08:00
_center = Vertex2F(_additionalTransform.tx * CC_CONTENT_SCALE_FACTOR(), _additionalTransform.ty * CC_CONTENT_SCALE_FACTOR());
_resolution = Vertex2F( SIZE_X * _additionalTransform.a, SIZE_Y * _additionalTransform.d);
2013-06-06 12:02:54 +08:00
}
void ShaderNode::setPosition(const Point &newPosition)
2013-06-06 12:02:54 +08:00
{
Node::setPosition(newPosition);
Point position = getPosition();
_center = Vertex2F(position.x * CC_CONTENT_SCALE_FACTOR(), position.y * CC_CONTENT_SCALE_FACTOR());
2013-06-06 12:02:54 +08:00
}
void ShaderNode::draw()
2013-06-06 12:02:54 +08:00
{
CC_NODE_DRAW_SETUP();
float w = SIZE_X, h = SIZE_Y;
2013-06-07 10:52:32 +08:00
GLfloat vertices[12] = {0, 0, w, 0, w, h, 0, 0, 0, h, w, h};
2013-06-06 12:02:54 +08:00
//
// Uniforms
//
getShaderProgram()->setUniformLocationWith2f(_uniformCenter, _center.x, _center.y);
getShaderProgram()->setUniformLocationWith2f(_uniformResolution, _resolution.x, _resolution.y);
2013-06-06 12:02:54 +08:00
// time changes all the time, so it is Ok to call OpenGL directly, and not the "cached" version
glUniform1f(_uniformTime, _time);
2013-06-06 12:02:54 +08:00
ccGLEnableVertexAttribs( kVertexAttribFlag_Position );
2013-06-06 12:02:54 +08:00
glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
2013-06-06 12:02:54 +08:00
glDrawArrays(GL_TRIANGLES, 0, 6);
2013-06-07 10:52:32 +08:00
2013-06-06 12:02:54 +08:00
CC_INCREMENT_GL_DRAWS(1);
}
2013-06-07 10:52:32 +08:00
}}} // namespace cocos2d { namespace extension { namespace armature {