mirror of https://github.com/axmolengine/axmol.git
fix the include error
This commit is contained in:
parent
a4ce0687f4
commit
69fd841b30
|
@ -33,6 +33,7 @@ THE SOFTWARE.
|
|||
#include "2d/platform/CCFileUtils.h"
|
||||
#include "2d/uthash.h"
|
||||
#include "deprecated/CCString.h"
|
||||
#include "CCGL.h"
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
||||
#include "CCPrecompiledShaders.h"
|
||||
|
@ -88,8 +89,10 @@ GLProgram::GLProgram()
|
|||
, _fragShader(0)
|
||||
, _hashForUniforms(nullptr)
|
||||
, _flags()
|
||||
,_isTight(false)
|
||||
{
|
||||
memset(_uniforms, 0, sizeof(_uniforms));
|
||||
_programData = new GLProgramData();
|
||||
}
|
||||
|
||||
GLProgram::~GLProgram()
|
||||
|
@ -114,6 +117,8 @@ GLProgram::~GLProgram()
|
|||
free(current_element->value);
|
||||
free(current_element);
|
||||
}
|
||||
|
||||
CC_SAFE_DELETE(_programData);
|
||||
}
|
||||
|
||||
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
||||
|
@ -205,6 +210,256 @@ bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std:
|
|||
return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str());
|
||||
}
|
||||
|
||||
void GLProgram::setUniformsForUserDef()
|
||||
{
|
||||
Director* director = Director::getInstance();
|
||||
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
||||
|
||||
Matrix matrixMV;
|
||||
matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
||||
|
||||
Matrix matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
||||
|
||||
// Set Uniform value
|
||||
|
||||
for(unsigned int i = 0, count = _programData->getUniformCount(); i < count; ++i)
|
||||
{
|
||||
_programData->getUniform(i)->_uniformvalue->bindUniform(this);
|
||||
}
|
||||
}
|
||||
|
||||
void GLProgram::setAttribForUserDef()
|
||||
{
|
||||
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* attrib = _programData->getAttrib(i);
|
||||
if(i == 0)
|
||||
{
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
||||
}
|
||||
else if(attrib->_type == GL_FLOAT_VEC4)
|
||||
{
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
||||
}
|
||||
else if(attrib->_type == GL_FLOAT_VEC2)
|
||||
{
|
||||
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// have some questions.
|
||||
void GLProgram::setVertexAttrib(const GLvoid* vertex, bool isTight)
|
||||
{
|
||||
static GLint vertexsize = 0;
|
||||
if(vertexsize == 0)
|
||||
{
|
||||
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
|
||||
|
||||
if(_attrib->_type == GL_UNSIGNED_BYTE)
|
||||
vertexsize += _attrib->_size ;
|
||||
else
|
||||
vertexsize += _attrib->_size * 4;
|
||||
}
|
||||
}
|
||||
if(isTight)
|
||||
vertexsize = 0;
|
||||
|
||||
size_t offset = 0;
|
||||
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
|
||||
std::string name = _attrib->_name;
|
||||
GLint size = _attrib->_size;
|
||||
GLenum type = _attrib->_type;
|
||||
bool normalized = GL_FALSE;
|
||||
|
||||
glVertexAttribPointer(i, size, type, normalized, vertexsize, (size_t*)(vertex)+ offset);
|
||||
if(_attrib->_type != GL_UNSIGNED_BYTE)
|
||||
offset += size * 4 ;
|
||||
else
|
||||
offset += size;
|
||||
}
|
||||
}
|
||||
|
||||
void GLProgram::autoParse()
|
||||
{
|
||||
// Link program
|
||||
GLint status = GL_TRUE;
|
||||
glLinkProgram(_program);
|
||||
|
||||
// Delete shaders after linking
|
||||
if (_vertShader)
|
||||
{
|
||||
glDeleteShader(_vertShader);
|
||||
}
|
||||
|
||||
if (_fragShader)
|
||||
{
|
||||
glDeleteShader(_fragShader);
|
||||
}
|
||||
|
||||
_vertShader = _fragShader = 0;
|
||||
|
||||
// Query and store vertex attribute meta-data from the program.
|
||||
GLint activeAttributes;
|
||||
GLint length;
|
||||
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTES, &activeAttributes);
|
||||
if(activeAttributes > 0)
|
||||
{
|
||||
glGetProgramiv(_program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &length);
|
||||
if(length > 0)
|
||||
{
|
||||
GLchar* attribName = new GLchar[length + 1];
|
||||
GLint attribSize;
|
||||
GLenum attribType;
|
||||
GLint attribLocation;
|
||||
GLint vertexsize = 0;
|
||||
|
||||
for(int i = 0; i < activeAttributes; ++i)
|
||||
{
|
||||
// Query attribute info.
|
||||
glGetActiveAttrib(_program, i, length, NULL, &attribSize, &attribType, attribName);
|
||||
attribName[length] = '\0';
|
||||
|
||||
// Query the pre-assigned attribute location
|
||||
attribLocation = glGetAttribLocation(_program, attribName);
|
||||
std::string name = attribName;
|
||||
|
||||
GLuint size;
|
||||
switch (attribType) {
|
||||
case GL_FLOAT:
|
||||
size = 1;
|
||||
attribType = GL_FLOAT;
|
||||
break;
|
||||
case GL_FLOAT_VEC2:
|
||||
attribType = GL_FLOAT;
|
||||
size = 2;
|
||||
break;
|
||||
case GL_FLOAT_VEC3:
|
||||
attribType = GL_FLOAT;
|
||||
size = 3;
|
||||
break;
|
||||
case GL_FLOAT_VEC4:
|
||||
attribType = GL_FLOAT;
|
||||
size = 4;
|
||||
break;
|
||||
case GL_FLOAT_MAT4:
|
||||
attribType = GL_FLOAT;
|
||||
size = 16;
|
||||
break;
|
||||
case GL_INT:
|
||||
size = 1;
|
||||
attribType = GL_INT;
|
||||
break;
|
||||
case GL_INT_VEC2:
|
||||
size = 2;
|
||||
attribType = GL_INT;
|
||||
break;
|
||||
case GL_INT_VEC3:
|
||||
size = 3;
|
||||
attribType = GL_INT;
|
||||
break;
|
||||
case GL_INT_VEC4:
|
||||
size = 4;
|
||||
attribType = GL_INT;
|
||||
break;
|
||||
default:
|
||||
size = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
GLProgramData::_VertexAttrib* attrib = new GLProgramData::_VertexAttrib();
|
||||
attrib->_name = name;
|
||||
attrib->_size = size;
|
||||
attrib->_type = attribType;
|
||||
attrib->_index = i;
|
||||
//vertexsize +=size;
|
||||
|
||||
_programData->addAttrib(name, attrib);
|
||||
|
||||
//_user_vertAttributes[attribName] = attribLocation;
|
||||
// bindAttribLocation(attribName,attribLocation);
|
||||
}
|
||||
CC_SAFE_DELETE_ARRAY(attribName);
|
||||
}
|
||||
}
|
||||
|
||||
// Query and store uniforms from the program.
|
||||
GLint activeUniforms;
|
||||
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &activeUniforms);
|
||||
if(activeUniforms > 0)
|
||||
{
|
||||
glGetProgramiv(_program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &length);
|
||||
if(length > 0)
|
||||
{
|
||||
GLchar* uniformName = new char[length + 1];
|
||||
GLint uniformSize;
|
||||
GLenum uniformType;
|
||||
GLint uniformLocation;
|
||||
|
||||
unsigned int sampleIndex = 0;
|
||||
for(int i = 0; i < activeUniforms; ++i)
|
||||
{
|
||||
// Query uniform info.
|
||||
glGetActiveUniform(_program, i, length, NULL, &uniformSize, &uniformType, uniformName);
|
||||
uniformName[length] = '\0';
|
||||
|
||||
if(uniformSize > 1 && length > 3)
|
||||
{
|
||||
char* c = strrchr(uniformName, '[');
|
||||
if(c)
|
||||
{
|
||||
*c = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
uniformLocation = glGetUniformLocation(_program, uniformName);
|
||||
//_user_uniforms[uniformName] = uniformLocation;
|
||||
std::string name = uniformName;
|
||||
GLProgramData::_Uniform* uniform = new GLProgramData::_Uniform();
|
||||
|
||||
uniform->_location = uniformLocation;
|
||||
uniform->_name = uniformName;
|
||||
uniform->_size = uniformSize;
|
||||
uniform->_type = uniformType;
|
||||
|
||||
UniformValue* uniformvalue = new UniformValue();
|
||||
uniform->_uniformvalue = uniformvalue;
|
||||
uniformvalue->init(uniform);
|
||||
|
||||
_programData->addUniform(name, uniform);
|
||||
}
|
||||
CC_SAFE_DELETE_ARRAY(uniformName);
|
||||
}
|
||||
}
|
||||
|
||||
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
|
||||
std::string name = _attrib->_name;
|
||||
GLint attribLocation = getAttribLocation(name.c_str());
|
||||
bindAttribLocation( name.c_str(),attribLocation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
UniformValue* GLProgram::getUniform(std::string &name)
|
||||
{
|
||||
GLProgramData::Uniform* uniform = _programData->getUniform(name);
|
||||
return uniform->_uniformvalue;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgram::getAttrib(std::string &name)
|
||||
{
|
||||
GLProgramData::VertexAttrib* attrib = _programData->getAttrib(name);
|
||||
return attrib;
|
||||
}
|
||||
|
||||
|
||||
std::string GLProgram::getDescription() const
|
||||
{
|
||||
return StringUtils::format("<GLProgram = "
|
||||
|
@ -326,7 +581,7 @@ bool GLProgram::link()
|
|||
#endif
|
||||
|
||||
GLint status = GL_TRUE;
|
||||
|
||||
|
||||
glLinkProgram(_program);
|
||||
|
||||
if (_vertShader)
|
||||
|
@ -689,4 +944,287 @@ void GLProgram::reset()
|
|||
_hashForUniforms = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
GLProgramData::GLProgramData():
|
||||
_vertexsize(0)
|
||||
{
|
||||
_uniforms.clear();
|
||||
_vertAttributes.clear();
|
||||
}
|
||||
|
||||
GLProgramData::~GLProgramData()
|
||||
{
|
||||
for(std::map<std::string, Uniform*>::iterator itr = _uniforms.begin(); itr != _uniforms.end(); itr++)
|
||||
{
|
||||
CC_SAFE_DELETE(itr->second);
|
||||
}
|
||||
|
||||
for(std::map<std::string, VertexAttrib*>::iterator itr = _vertAttributes.begin(); itr != _vertAttributes.end(); itr++)
|
||||
{
|
||||
CC_SAFE_DELETE(itr->second);
|
||||
}
|
||||
}
|
||||
|
||||
void GLProgramData::addUniform(std::string &name, Uniform* uniform)
|
||||
{
|
||||
_uniforms[name] = uniform;
|
||||
}
|
||||
|
||||
void GLProgramData::addAttrib(std::string &name, VertexAttrib* attrib)
|
||||
{
|
||||
_vertAttributes[name] = attrib;
|
||||
}
|
||||
|
||||
GLProgramData::Uniform* GLProgramData::getUniform(unsigned int index)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for (std::map<std::string, Uniform*>::const_iterator itr = _uniforms.begin(); itr != _uniforms.end(); itr++, i++)
|
||||
{
|
||||
if (i == index)
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GLProgramData::Uniform* GLProgramData::getUniform(std::string& name)
|
||||
{
|
||||
std::map<std::string, Uniform*>::const_iterator itr = _uniforms.find(name);
|
||||
if(itr != _uniforms.end())
|
||||
return itr->second;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgramData::getAttrib(unsigned int index)
|
||||
{
|
||||
//unsigned int i = 0;
|
||||
for (std::map<std::string, VertexAttrib*>::const_iterator itr = _vertAttributes.begin(); itr != _vertAttributes.end(); itr++)
|
||||
{
|
||||
if (itr->second->_index == index)
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgramData::getAttrib(std::string& name)
|
||||
{
|
||||
std::map<std::string, VertexAttrib*>::const_iterator itr = _vertAttributes.find(name);
|
||||
if(itr != _vertAttributes.end())
|
||||
return itr->second;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int GLProgramData::getUniformCount()
|
||||
{
|
||||
return _uniforms.size();
|
||||
}
|
||||
|
||||
unsigned int GLProgramData::getAttribCount()
|
||||
{
|
||||
return _vertAttributes.size();
|
||||
}
|
||||
|
||||
|
||||
UniformValue::UniformValue(): _count(0),_type(UniformValue::NONE),_update(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
UniformValue::~UniformValue()
|
||||
{
|
||||
resetValue();
|
||||
}
|
||||
|
||||
bool UniformValue::init(GLProgramData::Uniform* uniform)
|
||||
{
|
||||
resetValue();
|
||||
_uniform = uniform;
|
||||
return true;
|
||||
}
|
||||
|
||||
void UniformValue::bindUniform(GLProgram* program)
|
||||
{
|
||||
switch(_type){
|
||||
case UniformValue::FLOAT:
|
||||
program->setUniformLocationWith1f(_uniform->_location, *_value.floatPtrValue);
|
||||
break;
|
||||
case UniformValue::INT:
|
||||
program->setUniformLocationWith1i(_uniform->_location, *_value.intPtrValue);
|
||||
break;
|
||||
case UniformValue::VECTOR2:
|
||||
program->setUniformLocationWith2f(_uniform->_location, _value.vec2PtrValue->x, _value.vec2PtrValue->y);
|
||||
break;
|
||||
case UniformValue::VECTOR3:
|
||||
program->setUniformLocationWith3f(_uniform->_location, _value.vec3PtrValue->x, _value.vec3PtrValue->y, _value.vec3PtrValue->z);
|
||||
break;
|
||||
case UniformValue::VECTOR4:
|
||||
program->setUniformLocationWith4f(_uniform->_location, _value.vec4PtrValue->x, _value.vec4PtrValue->y, _value.vec4PtrValue->z, _value.vec4PtrValue->w);
|
||||
break;
|
||||
case UniformValue::MATRIX:
|
||||
program->setUniformLocationWithMatrix4fv(_uniform->_location, _value.matPtrValue->m, _count);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(float value)
|
||||
{
|
||||
_update = false;
|
||||
if(*(_value.floatPtrValue) == value)
|
||||
return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_FLOAT)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::FLOAT;
|
||||
if(_value.floatPtrValue == nullptr)
|
||||
_value.floatPtrValue = new float[_count];
|
||||
|
||||
*(_value.floatPtrValue) = value;
|
||||
_update = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(int value)
|
||||
{
|
||||
_update = false;
|
||||
if(_value.intPtrValue != nullptr)
|
||||
if(*(_value.intPtrValue) == value)
|
||||
return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_INT)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::INT;
|
||||
if(_value.floatPtrValue == nullptr)
|
||||
_value.floatPtrValue = new float[_count];
|
||||
|
||||
*(_value.floatPtrValue) = value;
|
||||
_update = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector2& value)
|
||||
{
|
||||
_update = false;
|
||||
if(_value.vec2PtrValue != nullptr)
|
||||
if(*(_value.vec2PtrValue) == value)
|
||||
return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_FLOAT_VEC2)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::VECTOR2;
|
||||
if(_value.vec2PtrValue == nullptr)
|
||||
_value.vec2PtrValue = new Vector2[_count];
|
||||
|
||||
*(_value.vec2PtrValue) = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector3& value)
|
||||
{
|
||||
_update = false;
|
||||
if(_value.vec3PtrValue != nullptr)
|
||||
if(*(_value.vec3PtrValue) == value)
|
||||
return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_FLOAT_VEC3)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::VECTOR3;
|
||||
if(_value.vec3PtrValue == nullptr)
|
||||
_value.vec3PtrValue = new Vector3[_count];
|
||||
|
||||
*(_value.vec3PtrValue) = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector4& value)
|
||||
{
|
||||
_update = false;
|
||||
if(_value.vec4PtrValue != nullptr)
|
||||
if(*(_value.vec4PtrValue) == value)
|
||||
return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_FLOAT_VEC4)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::VECTOR4;
|
||||
if(_value.vec4PtrValue == nullptr)
|
||||
_value.vec4PtrValue = new Vector4[_count];
|
||||
|
||||
*(_value.vec4PtrValue) = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Matrix& value)
|
||||
{
|
||||
_update = false;
|
||||
// if(_value.matPtrValue != nullptr)
|
||||
// if(*(_value.matPtrValue) == value)
|
||||
// return true;
|
||||
|
||||
if(_uniform && _uniform->_type == GL_FLOAT_MAT4)
|
||||
{
|
||||
_count = 1;
|
||||
_type = UniformValue::MATRIX;
|
||||
if(_value.matPtrValue == nullptr)
|
||||
_value.matPtrValue = new Matrix[_count];
|
||||
|
||||
*(_value.matPtrValue) = value;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void UniformValue::resetValue()
|
||||
{
|
||||
switch(_type)
|
||||
{
|
||||
case UniformValue::FLOAT :
|
||||
CC_SAFE_DELETE(_value.floatPtrValue);
|
||||
break;
|
||||
case UniformValue::INT:
|
||||
CC_SAFE_DELETE(_value.intPtrValue);
|
||||
break;
|
||||
case UniformValue::VECTOR2:
|
||||
CC_SAFE_DELETE(_value.vec2PtrValue);
|
||||
break;
|
||||
case UniformValue::VECTOR3:
|
||||
CC_SAFE_DELETE(_value.vec3PtrValue);
|
||||
break;
|
||||
case UniformValue::VECTOR4:
|
||||
CC_SAFE_DELETE(_value.vec4PtrValue);
|
||||
break;
|
||||
case UniformValue::MATRIX:
|
||||
CC_SAFE_DELETE(_value.matPtrValue);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
memset(&_value,0, sizeof(_value));
|
||||
_count = 0;
|
||||
_type = UniformValue::NONE;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -46,10 +46,65 @@ USING_NS_CC_MATH;
|
|||
*/
|
||||
|
||||
struct _hashUniformEntry;
|
||||
class GLProgramData;
|
||||
class UniformValue;
|
||||
struct VertexAttrib;
|
||||
|
||||
typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params);
|
||||
typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
|
||||
|
||||
|
||||
|
||||
/** GLProgramData
|
||||
Class store user defined vertexAttributes and uniforms
|
||||
*/
|
||||
class GLProgramData
|
||||
{
|
||||
public:
|
||||
typedef struct _VertexAttrib
|
||||
{
|
||||
GLuint _index;
|
||||
GLint _size;
|
||||
GLenum _type;
|
||||
std::string _name;
|
||||
} VertexAttrib;
|
||||
|
||||
typedef struct _Uniform
|
||||
{
|
||||
GLint _location;
|
||||
GLint _size;
|
||||
std::string _name;
|
||||
GLenum _type;
|
||||
UniformValue* _uniformvalue;
|
||||
} Uniform;
|
||||
|
||||
public:
|
||||
GLProgramData();
|
||||
~GLProgramData();
|
||||
|
||||
|
||||
void addUniform(std::string &name, Uniform* uniform);
|
||||
void addAttrib(std::string &name, VertexAttrib* attrib);
|
||||
|
||||
Uniform* getUniform(unsigned int index);
|
||||
Uniform* getUniform(std::string& name);
|
||||
VertexAttrib* getAttrib(unsigned int index);
|
||||
VertexAttrib* getAttrib(std::string& name);
|
||||
|
||||
unsigned int getUniformCount();
|
||||
unsigned int getAttribCount();
|
||||
|
||||
void setVertexSize(GLint size) { _vertexsize = size;}
|
||||
GLint getVertexSize() {return _vertexsize;}
|
||||
|
||||
private:
|
||||
GLint _vertexsize;
|
||||
|
||||
std::map<std::string, Uniform*> _uniforms;
|
||||
std::map<std::string, VertexAttrib*> _vertAttributes;
|
||||
};
|
||||
|
||||
|
||||
/** GLProgram
|
||||
Class that implements a glProgram
|
||||
|
||||
|
@ -146,6 +201,18 @@ public:
|
|||
* @lua init
|
||||
*/
|
||||
bool initWithFilenames(const std::string& vShaderFilename, const std::string& fShaderFilename);
|
||||
|
||||
//void bindAttirbForUserdef();
|
||||
void setUniformsForUserDef();
|
||||
void setAttribForUserDef();
|
||||
void setVertexAttrib(const GLvoid* vertex, bool isTight);
|
||||
void autoParse();
|
||||
|
||||
//void bindUniformValue(std::string uniformName, int value);
|
||||
UniformValue* getUniform(std::string &name);
|
||||
GLProgramData::VertexAttrib* getAttrib(std::string& name);
|
||||
|
||||
void bindAllAttrib();
|
||||
|
||||
/** It will add a new attribute to the shader by calling glBindAttribLocation */
|
||||
void bindAttribLocation(const char* attributeName, GLuint index) const;
|
||||
|
@ -274,13 +341,20 @@ private:
|
|||
bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
|
||||
std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const;
|
||||
|
||||
private:
|
||||
protected:
|
||||
GLuint _program;
|
||||
|
||||
private:
|
||||
|
||||
GLuint _vertShader;
|
||||
GLuint _fragShader;
|
||||
GLint _uniforms[UNIFORM_MAX];
|
||||
struct _hashUniformEntry* _hashForUniforms;
|
||||
bool _hasShaderCompiler;
|
||||
bool _isTight;
|
||||
|
||||
GLProgramData* _programData;
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
||||
std::string _shaderId;
|
||||
#endif
|
||||
|
@ -302,6 +376,60 @@ public:
|
|||
// end of shaders group
|
||||
/// @}
|
||||
|
||||
|
||||
|
||||
class UniformValue
|
||||
{
|
||||
public:
|
||||
UniformValue();
|
||||
~UniformValue();
|
||||
|
||||
bool init(GLProgramData::Uniform* uniform);
|
||||
|
||||
void bindUniform(GLProgram* program);
|
||||
|
||||
bool setValue(float value);
|
||||
|
||||
bool setValue(int value);
|
||||
|
||||
bool setValue(const Vector2& value);
|
||||
|
||||
bool setValue(const Vector3& value);
|
||||
|
||||
bool setValue(const Vector4& value);
|
||||
|
||||
bool setValue(const Matrix& value);
|
||||
|
||||
void resetValue();
|
||||
|
||||
protected:
|
||||
GLProgramData::Uniform* _uniform;
|
||||
bool _update;
|
||||
int _count;
|
||||
union
|
||||
{
|
||||
float* floatPtrValue;
|
||||
int* intPtrValue;
|
||||
Vector2* vec2PtrValue;
|
||||
Vector3* vec3PtrValue;
|
||||
Vector4* vec4PtrValue;
|
||||
Matrix* matPtrValue;
|
||||
|
||||
//const Texture2D* textureValue;
|
||||
}_value;
|
||||
enum
|
||||
{
|
||||
NONE,
|
||||
FLOAT,
|
||||
INT,
|
||||
VECTOR2,
|
||||
VECTOR3,
|
||||
VECTOR4,
|
||||
MATRIX,
|
||||
}_type;
|
||||
};
|
||||
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* __CCGLPROGRAM_H__ */
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "ShaderTest.h"
|
||||
#include "../testResource.h"
|
||||
#include "cocos2d.h"
|
||||
#include "renderer/CCCustomCommand.h"
|
||||
#include "renderer/CCRenderer.h"
|
||||
|
||||
static int sceneIdx = -1;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "ShaderTest2.h"
|
||||
#include "ShaderTest2.h"
|
||||
#include "ShaderTest.h"
|
||||
#include "../testResource.h"
|
||||
#include "cocos2d.h"
|
||||
|
@ -9,6 +9,7 @@ namespace ShaderTest2
|
|||
{
|
||||
static std::function<Layer*()> createFunctions[] =
|
||||
{
|
||||
CL(AttribShaderTest),
|
||||
CL(NormalSpriteTest),
|
||||
CL(GreyScaleSpriteTest),
|
||||
CL(BlurSpriteTest),
|
||||
|
@ -17,7 +18,11 @@ namespace ShaderTest2
|
|||
CL(BloomSpriteTest),
|
||||
CL(CelShadingSpriteTest),
|
||||
CL(LensFlareSpriteTest),
|
||||
CL(OutlineShadingSpriteTest)
|
||||
CL(OutlineShadingSpriteTest),
|
||||
CL(UniformShaderTest),
|
||||
|
||||
CL(UniformAttribShaderTest)
|
||||
|
||||
};
|
||||
|
||||
static unsigned int TEST_CASE_COUNT = sizeof(ShaderTest2::createFunctions) / sizeof(ShaderTest2::createFunctions[0]);
|
||||
|
@ -693,10 +698,252 @@ OutlineShadingSpriteTest::OutlineShadingSpriteTest()
|
|||
if (ShaderTestDemo2::init()) {
|
||||
auto s = Director::getInstance()->getWinSize();
|
||||
OutlineSprite* sprite = OutlineSprite::createSprite("Images/grossini_dance_10.png");
|
||||
sprite->setPosition(Vector2(s.width * 0.75, s.height/2));
|
||||
sprite->setPosition(Point(s.width * 0.75, s.height/2));
|
||||
auto sprite2 = Sprite::create("Images/grossini_dance_10.png");
|
||||
sprite2->setPosition(Vector2(s.width * 0.25, s.height/2));
|
||||
sprite2->setPosition(Point(s.width * 0.25, s.height/2));
|
||||
addChild(sprite);
|
||||
addChild(sprite2);
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
_shaderProgram->getUniform(name)->setValue(Vector2(480,320));
|
||||
name = "resolution";
|
||||
_shaderProgram->getUniform(name)->setValue(Vector2(256,256));
|
||||
|
||||
_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;
|
||||
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()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -102,4 +102,28 @@ public:
|
|||
virtual std::string subtitle() const {return "OutlineShadingSpriteTest";}
|
||||
};
|
||||
|
||||
class UniformShaderTest : public ShaderTestDemo2
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(UniformShaderTest);
|
||||
UniformShaderTest();
|
||||
virtual std::string subtitle() const {return "UniformShaderTest";}
|
||||
};
|
||||
|
||||
class AttribShaderTest : public ShaderTestDemo2
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(AttribShaderTest);
|
||||
AttribShaderTest();
|
||||
virtual std::string subtitle() const {return "AttribShaderTest";}
|
||||
};
|
||||
|
||||
class UniformAttribShaderTest : public ShaderTestDemo2
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(UniformAttribShaderTest);
|
||||
UniformAttribShaderTest();
|
||||
virtual std::string subtitle() const {return "UniformAndShaderTest";}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
attribute vec4 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 * a_position;
|
||||
v_fragmentColor = a_color;
|
||||
v_texCoord = a_texCoord;
|
||||
|
||||
}
|
Loading…
Reference in New Issue