Adds GLProgram::createXXX()

This commit is contained in:
Ricardo Quesada 2014-05-07 14:38:41 -07:00
parent 02405a8efe
commit 98454eeb69
7 changed files with 60 additions and 63 deletions

View File

@ -84,6 +84,30 @@ const char* GLProgram::ATTRIBUTE_NAME_POSITION = "a_position";
const char* GLProgram::ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
GLProgram* GLProgram::createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithByteArrays(vShaderByteArray, fShaderByteArray)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithFilenames(vShaderFilename, fShaderFilename)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
GLProgram::GLProgram()
: _program(0)
, _vertShader(0)
@ -178,6 +202,18 @@ bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar*
}
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
GLProgram* GLProgram::createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
auto ret = new (std::nothrow) GLProgram();
if(ret && ret->initWithPrecompiledProgramByteArray(vShaderByteArray, fShaderByteArray)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool GLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
{
bool haveProgram = false;

View File

@ -172,14 +172,8 @@ public:
static const char* ATTRIBUTE_NAME_COLOR;
static const char* ATTRIBUTE_NAME_POSITION;
static const char* ATTRIBUTE_NAME_TEX_COORD;
/**
* @js ctor
*/
GLProgram();
/**
* @js NA
* @lua NA
*/
virtual ~GLProgram();
/** Initializes the GLProgram with a vertex and fragment with bytes array
* @js initWithString
@ -188,6 +182,7 @@ public:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
/** Initializes the CCGLProgram with precompiled shader program */
static GLProgram* createWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
bool initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
#endif
@ -195,12 +190,14 @@ public:
* @js initWithString
* @lua initWithString
*/
static GLProgram* createWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
bool initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray);
/** Initializes the GLProgram with a vertex and fragment with contents of filenames
* @js init
* @lua init
*/
static GLProgram* createWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
//void bindUniform(std::string uniformName, int value);

View File

@ -256,7 +256,7 @@ __String* __String::createWithFormat(const char* format, ...)
return ret;
}
__String* __String::createWithContentsOfFile(const char* filename)
__String* __String::createWithContentsOfFile(const std::string &filename)
{
std::string str = FileUtils::getInstance()->getStringFromFile(filename);
return __String::create(std::move(str));

View File

@ -174,7 +174,7 @@ public:
* it means that you needn't do a release operation unless you retain it.
* @js NA
*/
static __String* createWithContentsOfFile(const char* filename);
static __String* createWithContentsOfFile(const std::string& filename);
/**
* @js NA
* @lua NA

View File

@ -1,8 +1,6 @@
#include "ShaderTest.h"
#include "../testResource.h"
#include "cocos2d.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h"
static int sceneIdx = -1;
@ -113,9 +111,6 @@ ShaderNode::ShaderNode()
:_center(Vector2(0.0f, 0.0f))
,_resolution(Vector2(0.0f, 0.0f))
,_time(0.0f)
,_uniformCenter(0)
,_uniformResolution(0)
,_uniformTime(0)
{
}
@ -161,21 +156,14 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag)
void ShaderNode::loadShaderVertex(const char *vert, const char *frag)
{
auto shader = new GLProgram();
shader->initWithFilenames(vert, frag);
auto shader = GLProgram::createWithFilenames(vert, frag);
shader->bindAttribLocation("aVertex", GLProgram::VERTEX_ATTRIB_POSITION);
shader->link();
shader->updateUniforms();
_uniformCenter = shader->getUniformLocation("center");
_uniformResolution = shader->getUniformLocation("resolution");
_uniformTime = shader->getUniformLocation("time");
this->setShaderProgram(shader);
shader->release();
}
void ShaderNode::update(float dt)
@ -202,11 +190,9 @@ void ShaderNode::onDraw(const Matrix &transform, bool transformUpdated)
auto shader = getShaderProgram();
shader->use();
shader->setUniformsForBuiltins(transform);
shader->setUniformLocationWith2f(_uniformCenter, _center.x, _center.y);
shader->setUniformLocationWith2f(_uniformResolution, _resolution.x, _resolution.y);
// time changes all the time, so it is Ok to call OpenGL directly, and not the "cached" version
glUniform1f(_uniformTime, _time);
shader->getUniform("center")->setValue(_center);
shader->getUniform("resolution")->setValue(_resolution);
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
@ -447,9 +433,6 @@ protected:
float _cons;
float _weightSum;
GLuint pixelSizeLocation;
GLuint coefficientLocation;
CustomCommand _customCommand;
};
@ -502,10 +485,8 @@ void SpriteBlur::initProgram()
{
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
auto program = new GLProgram();
program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_vert, fragSource);
setShaderProgram(program);
program->release();
CHECK_GL_ERROR_DEBUG();
@ -522,11 +503,6 @@ void SpriteBlur::initProgram()
program->updateUniforms();
CHECK_GL_ERROR_DEBUG();
pixelSizeLocation = program->getUniformLocation("onePixelSize");
coefficientLocation = program->getUniformLocation("gaussianCoefficient");
CHECK_GL_ERROR_DEBUG();
}
void SpriteBlur::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -545,8 +521,9 @@ void SpriteBlur::onDraw(const Matrix &transform, bool transformUpdated)
auto program = getShaderProgram();
program->use();
program->setUniformsForBuiltins(transform);
program->setUniformLocationWith2f(pixelSizeLocation, _pixelSize.x, _pixelSize.y);
program->setUniformLocationWith4f(coefficientLocation, _samplingRadius, _scale,_cons,_weightSum);
program->getUniform("onePixelSize")->setValue(_pixelSize);
program->getUniform("gaussianCoefficient")->setValue(Vector4(_samplingRadius, _scale, _cons, _weightSum));
GL::bindTexture2D( getTexture()->getName());
@ -687,9 +664,8 @@ bool ShaderRetroEffect::init()
{
if( ShaderTestDemo::init() ) {
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh").c_str())->getCString();
auto p = new GLProgram();
p->initWithByteArrays(ccPositionTexture_vert, fragSource);
GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"))->getCString();
auto p = GLProgram::createWithByteArrays(ccPositionTexture_vert, fragSource);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
@ -704,8 +680,6 @@ bool ShaderRetroEffect::init()
_label->setAnchorPoint(Vector2::ANCHOR_MIDDLE);
_label->setShaderProgram(p);
p->release();
_label->setPosition(Vector2(s.width/2,s.height/2));
addChild(_label);

View File

@ -131,7 +131,6 @@ protected:
Vector2 _center;
Vector2 _resolution;
float _time;
GLuint _uniformCenter, _uniformResolution, _uniformTime;
std::string _vertFileName;
std::string _fragFileName;
CustomCommand _customCommand;

View File

@ -166,10 +166,8 @@ void ShaderSprite::initShader()
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}
auto program = new GLProgram();
program->initWithByteArrays(vertSource.c_str(), fragSource.c_str());
auto program = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
setShaderProgram(program);
program->release();
CHECK_GL_ERROR_DEBUG();
@ -742,8 +740,7 @@ UniformSprite::~UniformSprite()
void UniformSprite::initShader()
{
auto shader = new GLProgram();
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
shader->link();
shader->updateUniforms();
@ -752,8 +749,6 @@ void UniformSprite::initShader()
// std::string attribname ="a_position";
// shader->getAttrib(attribname)->size = 2;
shader->release();
}
void UniformSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -845,14 +840,10 @@ AttribSprite::~AttribSprite()
void AttribSprite::initShader()
{
auto shader = new GLProgram();
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
auto shader = GLProgram::createWithFilenames(_vertSourceFile, _fragSourceFile);
shader->link();
shader->updateUniforms();
this->setShaderProgram(shader);
shader->release();
}
void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)