mirror of https://github.com/axmolengine/axmol.git
Improvements for Attributes and Uniform API
This commit is contained in:
parent
21dd6fef69
commit
db759005e0
|
@ -34,6 +34,7 @@ THE SOFTWARE.
|
|||
#include "2d/uthash.h"
|
||||
#include "deprecated/CCString.h"
|
||||
#include "CCGL.h"
|
||||
#include <alloca.h>
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
||||
#include "CCPrecompiledShaders.h"
|
||||
|
@ -89,10 +90,8 @@ GLProgram::GLProgram()
|
|||
, _fragShader(0)
|
||||
, _hashForUniforms(nullptr)
|
||||
, _flags()
|
||||
,_isTight(false)
|
||||
{
|
||||
memset(_uniforms, 0, sizeof(_uniforms));
|
||||
_programData = new GLProgramData();
|
||||
}
|
||||
|
||||
GLProgram::~GLProgram()
|
||||
|
@ -117,8 +116,6 @@ GLProgram::~GLProgram()
|
|||
free(current_element->value);
|
||||
free(current_element);
|
||||
}
|
||||
|
||||
CC_SAFE_DELETE(_programData);
|
||||
}
|
||||
|
||||
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
||||
|
@ -210,200 +207,63 @@ bool GLProgram::initWithFilenames(const std::string &vShaderFilename, const std:
|
|||
return initWithByteArrays(vertexSource.c_str(), fragmentSource.c_str());
|
||||
}
|
||||
|
||||
void GLProgram::setUniformsForUserDef()
|
||||
void GLProgram::parseVertexAttribs()
|
||||
{
|
||||
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
|
||||
}
|
||||
_attributesDictionary.clear();
|
||||
|
||||
void GLProgram::setAttribForUserDef()
|
||||
{
|
||||
for(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* attrib = _programData->getVertexAttribByIndex(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(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(i);
|
||||
|
||||
if(_attrib->_type == GL_UNSIGNED_BYTE)
|
||||
vertexsize += _attrib->_size ;
|
||||
else
|
||||
vertexsize += _attrib->_size * 4;
|
||||
}
|
||||
}
|
||||
if(isTight)
|
||||
vertexsize = 0;
|
||||
|
||||
size_t offset = 0;
|
||||
for(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(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)
|
||||
{
|
||||
VertexAttrib attribute;
|
||||
|
||||
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;
|
||||
|
||||
GLchar* attribName = (GLchar*) alloca(length + 1);
|
||||
|
||||
for(int i = 0; i < activeAttributes; ++i)
|
||||
{
|
||||
// Query attribute info.
|
||||
glGetActiveAttrib(_program, i, length, NULL, &attribSize, &attribType, attribName);
|
||||
glGetActiveAttrib(_program, i, length, NULL, &attribute.size, &attribute.type, attribName);
|
||||
attribName[length] = '\0';
|
||||
|
||||
attribute.name = std::string(attribName);
|
||||
|
||||
// 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->addVertexAttrib(name, attrib);
|
||||
|
||||
//_user_vertAttributes[attribName] = attribLocation;
|
||||
// bindAttribLocation(attribName,attribLocation);
|
||||
attribute.index = glGetAttribLocation(_program, attribName);
|
||||
|
||||
_attributesDictionary[attribute.name] = attribute;
|
||||
}
|
||||
CC_SAFE_DELETE_ARRAY(attribName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GLProgram::parseUniforms()
|
||||
{
|
||||
_uniformsDictionary.clear();
|
||||
|
||||
// Query and store uniforms from the program.
|
||||
GLint activeUniforms;
|
||||
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &activeUniforms);
|
||||
if(activeUniforms > 0)
|
||||
{
|
||||
GLint length;
|
||||
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;
|
||||
Uniform uniform;
|
||||
|
||||
GLchar* uniformName = (GLchar*)alloca(length + 1);
|
||||
|
||||
for(int i = 0; i < activeUniforms; ++i)
|
||||
{
|
||||
// Query uniform info.
|
||||
glGetActiveUniform(_program, i, length, NULL, &uniformSize, &uniformType, uniformName);
|
||||
glGetActiveUniform(_program, i, length, NULL, &uniform.size, &uniform.type, uniformName);
|
||||
uniformName[length] = '\0';
|
||||
|
||||
if(uniformSize > 1 && length > 3)
|
||||
// remove possible array '[]' from uniform name
|
||||
if(uniform.size > 1 && length > 3)
|
||||
{
|
||||
char* c = strrchr(uniformName, '[');
|
||||
if(c)
|
||||
|
@ -411,47 +271,30 @@ void GLProgram::autoParse()
|
|||
*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(this, uniform);
|
||||
|
||||
_programData->addUniform(name, uniform);
|
||||
uniform.name = std::string(uniformName);
|
||||
uniform.location = glGetUniformLocation(_program, uniformName);
|
||||
uniform.value.init(this, &uniform);
|
||||
|
||||
_uniformsDictionary[uniform.name] = uniform;
|
||||
}
|
||||
CC_SAFE_DELETE_ARRAY(uniformName);
|
||||
}
|
||||
}
|
||||
|
||||
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
|
||||
{
|
||||
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(i);
|
||||
std::string name = _attrib->_name;
|
||||
GLint attribLocation = getAttribLocation(name.c_str());
|
||||
bindAttribLocation( name.c_str(),attribLocation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
UniformValue* GLProgram::getUniformValue(const std::string &name)
|
||||
{
|
||||
GLProgramData::Uniform* uniform = _programData->getUniformByName(name);
|
||||
return uniform->_uniformvalue;
|
||||
const auto itr = _uniformsDictionary.find(name);
|
||||
if( itr != _uniformsDictionary.end())
|
||||
return &itr->second.value;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgram::getAttrib(const std::string &name)
|
||||
VertexAttrib* GLProgram::getVertexAttrib(const std::string &name)
|
||||
{
|
||||
GLProgramData::VertexAttrib* attrib = _programData->getVertexAttribByName(name);
|
||||
return attrib;
|
||||
const auto itr = _attributesDictionary.find(name);
|
||||
if( itr != _attributesDictionary.end())
|
||||
return &itr->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string GLProgram::getDescription() const
|
||||
|
@ -516,19 +359,19 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source
|
|||
return (status == GL_TRUE);
|
||||
}
|
||||
|
||||
GLint GLProgram::getAttribLocation(const char* attributeName) const
|
||||
GLint GLProgram::getAttribLocation(const std::string &attributeName) const
|
||||
{
|
||||
return glGetAttribLocation(_program, attributeName);
|
||||
return glGetAttribLocation(_program, attributeName.c_str());
|
||||
}
|
||||
|
||||
GLint GLProgram::getUniformLocation(const char* attributeName) const
|
||||
GLint GLProgram::getUniformLocation(const std::string &attributeName) const
|
||||
{
|
||||
return glGetUniformLocation(_program, attributeName);
|
||||
return glGetUniformLocation(_program, attributeName.c_str());
|
||||
}
|
||||
|
||||
void GLProgram::bindAttribLocation(const char* attributeName, GLuint index) const
|
||||
void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint index) const
|
||||
{
|
||||
glBindAttribLocation(_program, index, attributeName);
|
||||
glBindAttribLocation(_program, index, attributeName.c_str());
|
||||
}
|
||||
|
||||
void GLProgram::updateUniforms()
|
||||
|
@ -608,6 +451,9 @@ bool GLProgram::link()
|
|||
}
|
||||
#endif
|
||||
|
||||
parseVertexAttribs();
|
||||
parseUniforms();
|
||||
|
||||
return (status == GL_TRUE);
|
||||
}
|
||||
|
||||
|
@ -970,11 +816,11 @@ void GLProgramData::addUniform(const std::string &name, Uniform* uniform)
|
|||
_uniforms[name] = uniform;
|
||||
}
|
||||
|
||||
GLProgramData::Uniform* GLProgramData::getUniformByLocation(GLint location)
|
||||
Uniform* GLProgramData::getUniformByLocation(GLint location)
|
||||
{
|
||||
for (const auto &itr : _uniforms)
|
||||
{
|
||||
if (itr.second->_location == location)
|
||||
if (itr.second->location == location)
|
||||
{
|
||||
return itr.second;
|
||||
}
|
||||
|
@ -982,7 +828,7 @@ GLProgramData::Uniform* GLProgramData::getUniformByLocation(GLint location)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
GLProgramData::Uniform* GLProgramData::getUniformByName(const std::string& name)
|
||||
Uniform* GLProgramData::getUniformByName(const std::string& name)
|
||||
{
|
||||
const auto& itr = _uniforms.find(name);
|
||||
if(itr != _uniforms.end())
|
||||
|
@ -1005,12 +851,12 @@ void GLProgramData::addVertexAttrib(const std::string &name, VertexAttrib* attri
|
|||
_vertAttributes[name] = attrib;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByIndex(unsigned int index)
|
||||
VertexAttrib* GLProgramData::getVertexAttribByIndex(unsigned int index)
|
||||
{
|
||||
//unsigned int i = 0;
|
||||
for (const auto& itr : _vertAttributes )
|
||||
{
|
||||
if (itr.second->_index == index)
|
||||
if (itr.second->index == index)
|
||||
{
|
||||
return itr.second;
|
||||
}
|
||||
|
@ -1018,7 +864,7 @@ GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByIndex(unsigned int
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByName(const std::string& name)
|
||||
VertexAttrib* GLProgramData::getVertexAttribByName(const std::string& name)
|
||||
{
|
||||
const auto &itr = _vertAttributes.find(name);
|
||||
if(itr != _vertAttributes.end())
|
||||
|
@ -1026,9 +872,9 @@ GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByName(const std::str
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<GLProgramData::VertexAttrib*> GLProgramData::getVertexAttributes(const std::string* attrNames, int count)
|
||||
std::vector<VertexAttrib*> GLProgramData::getVertexAttributes(const std::string* attrNames, int count)
|
||||
{
|
||||
std::vector<GLProgramData::VertexAttrib*> attribs;
|
||||
std::vector<VertexAttrib*> attribs;
|
||||
for (auto i = 0; i < count; i++) {
|
||||
auto it = _vertAttributes.find(attrNames[i]);
|
||||
CCASSERT(it != _vertAttributes.end(), "attribute not find");
|
||||
|
@ -1054,112 +900,86 @@ UniformValue::~UniformValue()
|
|||
{
|
||||
}
|
||||
|
||||
bool UniformValue::init(GLProgram* program, GLProgramData::Uniform* uniform)
|
||||
bool UniformValue::init(GLProgram* program, Uniform* uniform)
|
||||
{
|
||||
_program = program;
|
||||
_uniform = uniform;
|
||||
return program && program && uniform->_location != -1;
|
||||
return program && program && uniform->location != -1;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(float value)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT, "");
|
||||
_program->setUniformLocationWith1f(_uniform->_location, value);
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT, "");
|
||||
_program->setUniformLocationWith1f(_uniform->location, value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(int value)
|
||||
{
|
||||
CCASSERT (_uniform && (_uniform->_type == GL_INT || _uniform->_type == GL_SAMPLER_2D), "");
|
||||
_program->setUniformLocationWith1i(_uniform->_location, value);
|
||||
CCASSERT (_uniform && (_uniform->type == GL_INT || _uniform->type == GL_SAMPLER_2D), "");
|
||||
_program->setUniformLocationWith1i(_uniform->location, value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector2& value)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC2, "");
|
||||
_program->setUniformLocationWith2f(_uniform->_location, value.x, value.y);
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC2, "");
|
||||
_program->setUniformLocationWith2f(_uniform->location, value.x, value.y);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector3& value)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC3, "");
|
||||
_program->setUniformLocationWith3f(_uniform->_location, value.x, value.y, value.z);
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC3, "");
|
||||
_program->setUniformLocationWith3f(_uniform->location, value.x, value.y, value.z);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector4& value)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC4, "");
|
||||
_program->setUniformLocationWith4f(_uniform->_location, value.x, value.y, value.z, value.w);
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC4, "");
|
||||
_program->setUniformLocationWith4f(_uniform->location, value.x, value.y, value.z, value.w);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Matrix& value)
|
||||
{
|
||||
CCASSERT(_uniform && _uniform->_type == GL_FLOAT_MAT4, "");
|
||||
_program->setUniformLocationWithMatrix4fv(_uniform->_location, value.m, 1);
|
||||
CCASSERT(_uniform && _uniform->type == GL_FLOAT_MAT4, "");
|
||||
_program->setUniformLocationWithMatrix4fv(_uniform->location, value.m, 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector2* value, int count)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC2 && _uniform->_size == count, "");
|
||||
GLfloat* array = new GLfloat[count * 2];
|
||||
for (auto i = 0; i < count; i++) {
|
||||
array[2*i] = value[i].x;
|
||||
array[2*i + 1] = value[i].y;
|
||||
}
|
||||
_program->setUniformLocationWith2fv(_uniform->_location, array, count);
|
||||
delete[] array;
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC2 && _uniform->size == count, "");
|
||||
_program->setUniformLocationWith2fv(_uniform->location, (GLfloat*)value, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector3* value, int count)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC3 && _uniform->_size == count, "");
|
||||
GLfloat* array = new GLfloat[count * 3];
|
||||
for (auto i = 0; i < count; i++) {
|
||||
array[3*i] = value[i].x;
|
||||
array[3*i + 1] = value[i].y;
|
||||
array[3*i + 2] = value[i].z;
|
||||
}
|
||||
_program->setUniformLocationWith3fv(_uniform->_location, array, count);
|
||||
delete[] array;
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC3 && _uniform->size == count, "");
|
||||
_program->setUniformLocationWith3fv(_uniform->location, (GLfloat*)value, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Vector4* value, int count)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_VEC4 && _uniform->_size == count, "");
|
||||
GLfloat* array = new GLfloat[count * 4];
|
||||
for (auto i = 0; i < count; i++) {
|
||||
array[4*i] = value[i].x;
|
||||
array[4*i + 1] = value[i].y;
|
||||
array[4*i + 2] = value[i].z;
|
||||
array[4*i + 3] = value[i].w;
|
||||
}
|
||||
_program->setUniformLocationWith4fv(_uniform->_location, array, count);
|
||||
delete[] array;
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_VEC4 && _uniform->size == count, "");
|
||||
_program->setUniformLocationWith4fv(_uniform->location, (GLfloat*)value, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UniformValue::setValue(const Matrix* value, int count)
|
||||
{
|
||||
CCASSERT (_uniform && _uniform->_type == GL_FLOAT_MAT4 && _uniform->_size == count, "");
|
||||
GLfloat* array = new GLfloat[count * 16];
|
||||
for (auto i = 0; i < count; i++) {
|
||||
memcpy(&array[i * 16], value[i].m, 16 * sizeof(GLfloat));
|
||||
}
|
||||
_program->setUniformLocationWithMatrix4fv(_uniform->_location, array, count);
|
||||
delete[] array;
|
||||
CCASSERT (_uniform && _uniform->type == GL_FLOAT_MAT4 && _uniform->size == count, "");
|
||||
_program->setUniformLocationWithMatrix4fv(_uniform->location, (GLfloat*)value, count);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -49,12 +49,64 @@ USING_NS_CC_MATH;
|
|||
struct _hashUniformEntry;
|
||||
class GLProgramData;
|
||||
class UniformValue;
|
||||
struct VertexAttrib;
|
||||
class GLProgram;
|
||||
struct _Uniform;
|
||||
|
||||
typedef void (*GLInfoFunction)(GLuint program, GLenum pname, GLint* params);
|
||||
typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length, GLchar* infolog);
|
||||
|
||||
|
||||
class UniformValue
|
||||
{
|
||||
public:
|
||||
UniformValue();
|
||||
~UniformValue();
|
||||
|
||||
bool init(GLProgram* program, struct _Uniform* uniform);
|
||||
|
||||
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);
|
||||
|
||||
bool setValue(const Vector2* value, int count);
|
||||
|
||||
bool setValue(const Vector3* value, int count);
|
||||
|
||||
bool setValue(const Vector4* value, int count);
|
||||
|
||||
bool setValue(const Matrix* value, int count);
|
||||
|
||||
protected:
|
||||
GLProgram* _program; // weak ref
|
||||
struct _Uniform* _uniform; // weak ref
|
||||
};
|
||||
|
||||
typedef struct _VertexAttrib
|
||||
{
|
||||
GLuint index;
|
||||
GLint size;
|
||||
GLenum type;
|
||||
GLboolean normalized;
|
||||
std::string name;
|
||||
} VertexAttrib;
|
||||
|
||||
typedef struct _Uniform
|
||||
{
|
||||
GLint location;
|
||||
GLint size;
|
||||
std::string name;
|
||||
GLenum type;
|
||||
UniformValue value;
|
||||
} Uniform;
|
||||
|
||||
|
||||
/** GLProgramData
|
||||
Class store user defined vertexAttributes and uniforms
|
||||
|
@ -64,23 +116,6 @@ class GLProgramData
|
|||
friend class GLProgram;
|
||||
|
||||
public:
|
||||
typedef struct _VertexAttrib
|
||||
{
|
||||
GLuint _index;
|
||||
GLint _size;
|
||||
GLenum _type;
|
||||
GLboolean _normalized;
|
||||
std::string _name;
|
||||
} VertexAttrib;
|
||||
|
||||
typedef struct _Uniform
|
||||
{
|
||||
GLint _location;
|
||||
GLint _size;
|
||||
std::string _name;
|
||||
GLenum _type;
|
||||
UniformValue* _uniformvalue;
|
||||
} Uniform;
|
||||
|
||||
GLProgramData();
|
||||
~GLProgramData();
|
||||
|
@ -89,7 +124,7 @@ public:
|
|||
Uniform* getUniformByName(const std::string& name);
|
||||
VertexAttrib* getVertexAttribByIndex(unsigned int index);
|
||||
VertexAttrib* getVertexAttribByName(const std::string& name);
|
||||
std::vector<GLProgramData::VertexAttrib*> getVertexAttributes(const std::string* attrNames, int count);
|
||||
std::vector<VertexAttrib*> getVertexAttributes(const std::string* attrNames, int count);
|
||||
ssize_t getUniformCount();
|
||||
ssize_t getAttribCount();
|
||||
GLint getVertexSize() {return _vertexsize;}
|
||||
|
@ -201,28 +236,19 @@ 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* getUniformValue(const std::string &name);
|
||||
GLProgramData::VertexAttrib* getAttrib(const std::string& name);
|
||||
|
||||
void bindAllAttrib();
|
||||
UniformValue* getUniformValue(const std::string& name);
|
||||
VertexAttrib* getVertexAttrib(const std::string& name);
|
||||
|
||||
/** It will add a new attribute to the shader by calling glBindAttribLocation */
|
||||
void bindAttribLocation(const char* attributeName, GLuint index) const;
|
||||
void bindAttribLocation(const std::string& attributeName, GLuint index) const;
|
||||
|
||||
/** calls glGetAttribLocation */
|
||||
GLint getAttribLocation(const char* attributeName) const;
|
||||
GLint getAttribLocation(const std::string& attributeName) const;
|
||||
|
||||
/** calls glGetUniformLocation() */
|
||||
GLint getUniformLocation(const char* attributeName) const;
|
||||
GLint getUniformLocation(const std::string& attributeName) const;
|
||||
|
||||
/** links the glProgram */
|
||||
bool link();
|
||||
|
@ -313,6 +339,11 @@ public:
|
|||
void setUniformsForBuiltins();
|
||||
void setUniformsForBuiltins(const Matrix &modelView);
|
||||
|
||||
void setUniformByName(const std::string& uniformName, const UniformValue &value);
|
||||
|
||||
|
||||
// Attribute
|
||||
|
||||
/** returns the vertexShader error log */
|
||||
std::string getVertexShaderLog() const;
|
||||
|
||||
|
@ -327,9 +358,7 @@ public:
|
|||
void reset();
|
||||
|
||||
inline const GLuint getProgram() const { return _program; }
|
||||
|
||||
|
||||
GLProgramData* getProgramData() { return _programData; }
|
||||
// DEPRECATED
|
||||
CC_DEPRECATED_ATTRIBUTE bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray)
|
||||
{ return initWithByteArrays(vShaderByteArray, fShaderByteArray); }
|
||||
|
@ -337,27 +366,24 @@ public:
|
|||
{ return initWithFilenames(vShaderByteArray, fShaderByteArray); }
|
||||
CC_DEPRECATED_ATTRIBUTE void addAttribute(const char* attributeName, GLuint index) const { return bindAttribLocation(attributeName, index); }
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
bool updateUniformLocation(GLint location, const GLvoid* data, unsigned int bytes);
|
||||
virtual std::string getDescription() const;
|
||||
void parseVertexAttribs();
|
||||
void parseUniforms();
|
||||
|
||||
bool compileShader(GLuint * shader, GLenum type, const GLchar* source);
|
||||
std::string logForOpenGLObject(GLuint object, GLInfoFunction infoFunc, GLLogFunction logFunc) const;
|
||||
|
||||
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
|
||||
|
@ -372,49 +398,14 @@ private:
|
|||
// handy way to initialize the bitfield
|
||||
flag_struct() { memset(this, 0, sizeof(*this)); }
|
||||
} _flags;
|
||||
public:
|
||||
static const GLuint _maxMaterialIDNumber;
|
||||
|
||||
std::unordered_map<std::string, Uniform> _uniformsDictionary;
|
||||
std::unordered_map<std::string, VertexAttrib> _attributesDictionary;
|
||||
};
|
||||
|
||||
// end of shaders group
|
||||
/// @}
|
||||
|
||||
|
||||
|
||||
class UniformValue
|
||||
{
|
||||
public:
|
||||
UniformValue();
|
||||
~UniformValue();
|
||||
|
||||
bool init(GLProgram* program, GLProgramData::Uniform* uniform);
|
||||
|
||||
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);
|
||||
|
||||
bool setValue(const Vector2* value, int count);
|
||||
|
||||
bool setValue(const Vector3* value, int count);
|
||||
|
||||
bool setValue(const Vector4* value, int count);
|
||||
|
||||
bool setValue(const Matrix* value, int count);
|
||||
|
||||
protected:
|
||||
GLProgram* _program;
|
||||
GLProgramData::Uniform* _uniform;
|
||||
};
|
||||
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* __CCGLPROGRAM_H__ */
|
||||
|
|
|
@ -89,12 +89,12 @@ VertexAttribBind::VertexAttribBind(const std::vector<VertexAttribType>& elems)
|
|||
setVertexAttribElems(&elems[0], elems.size());
|
||||
}
|
||||
|
||||
VertexAttribBind::VertexAttribBind(const std::vector<GLProgramData::VertexAttrib*>& attribs)
|
||||
VertexAttribBind::VertexAttribBind(const std::vector<VertexAttrib*>& attribs)
|
||||
{
|
||||
std::vector<VertexAttribType> elems;
|
||||
for (auto it = attribs.begin(); it != attribs.end(); it++) {
|
||||
VertexAttribType type((*it)->_type, (*it)->_size);
|
||||
type._location = (*it)->_index;
|
||||
VertexAttribType type((*it)->type, (*it)->size);
|
||||
type._location = (*it)->index;
|
||||
elems.push_back(type);
|
||||
}
|
||||
setVertexAttribElems(&elems[0], elems.size());
|
||||
|
|
|
@ -63,7 +63,7 @@ public:
|
|||
|
||||
VertexAttribBind(const std::vector<VertexAttribType>& elems);
|
||||
|
||||
VertexAttribBind(const std::vector<GLProgramData::VertexAttrib*>& attribs);
|
||||
VertexAttribBind(const std::vector<VertexAttrib*>& attribs);
|
||||
|
||||
virtual ~VertexAttribBind();
|
||||
|
||||
|
|
|
@ -746,7 +746,6 @@ void UniformSprite::initShader()
|
|||
{
|
||||
auto shader = new GLProgram();
|
||||
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
|
||||
shader->autoParse();
|
||||
//shader->link();
|
||||
|
||||
shader->updateUniforms();
|
||||
|
@ -754,7 +753,7 @@ void UniformSprite::initShader()
|
|||
this->setShaderProgram(shader);
|
||||
|
||||
std::string attribname ="a_position";
|
||||
shader->getAttrib(attribname)->_size = 2;
|
||||
shader->getAttrib(attribname)->size = 2;
|
||||
|
||||
shader->release();
|
||||
}
|
||||
|
@ -791,8 +790,8 @@ void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
|||
|
||||
float w = 256, h = 256;
|
||||
GLfloat vertices[12] = {0,0, w,0, w,h, 0,0, 0,h, w,h};
|
||||
program->setVertexAttrib(vertices, true);
|
||||
|
||||
// program->setVertexAttrib(vertices, true);
|
||||
|
||||
// Draw Call Test
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,6);
|
||||
|
@ -855,23 +854,22 @@ 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;
|
||||
shader->getAttrib(attribname)->size = 3;
|
||||
shader->getAttrib(attribname)->index = 0;
|
||||
|
||||
attribname ="a_color";
|
||||
shader->getAttrib(attribname)->_type = GL_UNSIGNED_BYTE;
|
||||
shader->getAttrib(attribname)->_normalized = GL_TRUE;
|
||||
shader->getAttrib(attribname)->_index = 1;
|
||||
shader->getAttrib(attribname)->type = GL_UNSIGNED_BYTE;
|
||||
shader->getAttrib(attribname)->normalized = GL_TRUE;
|
||||
shader->getAttrib(attribname)->index = 1;
|
||||
|
||||
attribname ="a_texCoord";
|
||||
shader->getAttrib(attribname)->_index = 2;
|
||||
shader->getAttrib(attribname)->index = 2;
|
||||
|
||||
}
|
||||
|
||||
|
@ -923,7 +921,7 @@ void AttribSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
|||
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);
|
||||
// program->setVertexAttrib((void*)offset, false);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
||||
|
||||
|
|
Loading…
Reference in New Issue