Improves code a little bit

This commit is contained in:
Ricardo Quesada 2014-05-06 12:46:35 -07:00
parent cf9e8ff0bd
commit 21dd6fef69
3 changed files with 86 additions and 82 deletions

View File

@ -225,9 +225,9 @@ void GLProgram::setUniformsForUserDef()
void GLProgram::setAttribForUserDef()
{
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
for(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
{
GLProgramData::VertexAttrib* attrib = _programData->getAttrib(i);
GLProgramData::VertexAttrib* attrib = _programData->getVertexAttribByIndex(i);
if(i == 0)
{
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
@ -249,9 +249,9 @@ 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)
for(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
{
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(i);
if(_attrib->_type == GL_UNSIGNED_BYTE)
vertexsize += _attrib->_size ;
@ -263,10 +263,10 @@ void GLProgram::setVertexAttrib(const GLvoid* vertex, bool isTight)
vertexsize = 0;
size_t offset = 0;
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
for(ssize_t i = 0, count = _programData->getAttribCount(); i < count; ++i)
{
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(i);
std::string name = _attrib->_name;
GLint size = _attrib->_size;
GLenum type = _attrib->_type;
@ -374,7 +374,7 @@ void GLProgram::autoParse()
attrib->_index = i;
//vertexsize +=size;
_programData->addAttrib(name, attrib);
_programData->addVertexAttrib(name, attrib);
//_user_vertAttributes[attribName] = attribLocation;
// bindAttribLocation(attribName,attribLocation);
@ -434,7 +434,7 @@ void GLProgram::autoParse()
for(unsigned int i = 0, count = _programData->getAttribCount(); i < count; ++i)
{
GLProgramData::VertexAttrib* _attrib = _programData->getAttrib(i);
GLProgramData::VertexAttrib* _attrib = _programData->getVertexAttribByIndex(i);
std::string name = _attrib->_name;
GLint attribLocation = getAttribLocation(name.c_str());
bindAttribLocation( name.c_str(),attribLocation);
@ -444,13 +444,13 @@ void GLProgram::autoParse()
UniformValue* GLProgram::getUniformValue(const std::string &name)
{
GLProgramData::Uniform* uniform = _programData->getUniform(name);
GLProgramData::Uniform* uniform = _programData->getUniformByName(name);
return uniform->_uniformvalue;
}
GLProgramData::VertexAttrib* GLProgram::getAttrib(std::string &name)
GLProgramData::VertexAttrib* GLProgram::getAttrib(const std::string &name)
{
GLProgramData::VertexAttrib* attrib = _programData->getAttrib(name);
GLProgramData::VertexAttrib* attrib = _programData->getVertexAttribByName(name);
return attrib;
}
@ -950,60 +950,80 @@ _vertexsize(0)
GLProgramData::~GLProgramData()
{
for(std::map<std::string, Uniform*>::iterator itr = _uniforms.begin(); itr != _uniforms.end(); itr++)
for(auto itr : _uniforms)
{
CC_SAFE_DELETE(itr->second);
CC_SAFE_DELETE(itr.second);
}
for(std::map<std::string, VertexAttrib*>::iterator itr = _vertAttributes.begin(); itr != _vertAttributes.end(); itr++)
for(auto itr : _vertAttributes)
{
CC_SAFE_DELETE(itr->second);
CC_SAFE_DELETE(itr.second);
}
}
//
// Uniform
//
void GLProgramData::addUniform(const std::string &name, Uniform* uniform)
{
_uniforms[name] = uniform;
}
void GLProgramData::addAttrib(const std::string &name, VertexAttrib* attrib)
GLProgramData::Uniform* GLProgramData::getUniformByLocation(GLint location)
{
for (const auto &itr : _uniforms)
{
if (itr.second->_location == location)
{
return itr.second;
}
}
return nullptr;
}
GLProgramData::Uniform* GLProgramData::getUniformByName(const std::string& name)
{
const auto& itr = _uniforms.find(name);
if(itr != _uniforms.end())
return itr->second;
return nullptr;
}
ssize_t GLProgramData::getUniformCount()
{
return _uniforms.size();
}
//
// VertexAttrib
//
void GLProgramData::addVertexAttrib(const 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(const 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)
GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByIndex(unsigned int index)
{
//unsigned int i = 0;
for (std::map<std::string, VertexAttrib*>::const_iterator itr = _vertAttributes.begin(); itr != _vertAttributes.end(); itr++)
for (const auto& itr : _vertAttributes )
{
if (itr->second->_index == index)
if (itr.second->_index == index)
{
return itr->second;
return itr.second;
}
}
return NULL;
return nullptr;
}
GLProgramData::VertexAttrib* GLProgramData::getVertexAttribByName(const std::string& name)
{
const auto &itr = _vertAttributes.find(name);
if(itr != _vertAttributes.end())
return itr->second;
return nullptr;
}
std::vector<GLProgramData::VertexAttrib*> GLProgramData::getVertexAttributes(const std::string* attrNames, int count)
@ -1017,21 +1037,7 @@ std::vector<GLProgramData::VertexAttrib*> GLProgramData::getVertexAttributes(con
return attribs;
}
GLProgramData::VertexAttrib* GLProgramData::getAttrib(const 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()
ssize_t GLProgramData::getAttribCount()
{
return _vertAttributes.size();
}

View File

@ -35,6 +35,7 @@ THE SOFTWARE.
#include "CCGL.h"
#include "math/CCMath.h"
#include <set>
#include <unordered_map>
NS_CC_BEGIN
@ -60,12 +61,15 @@ typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length,
*/
class GLProgramData
{
friend class GLProgram;
public:
typedef struct _VertexAttrib
{
GLuint _index;
GLint _size;
GLenum _type;
GLboolean _normalized;
std::string _name;
} VertexAttrib;
@ -77,34 +81,27 @@ public:
GLenum _type;
UniformValue* _uniformvalue;
} Uniform;
public:
GLProgramData();
~GLProgramData();
void addUniform(const std::string &name, Uniform* uniform);
void addAttrib(const std::string &name, VertexAttrib* attrib);
Uniform* getUniform(unsigned int index);
Uniform* getUniform(const std::string& name);
VertexAttrib* getAttrib(unsigned int index);
VertexAttrib* getAttrib(const std::string& name);
Uniform* getUniformByLocation(GLint location);
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);
unsigned int getUniformCount();
unsigned int getAttribCount();
void setVertexSize(GLint size) { _vertexsize = size;}
ssize_t getUniformCount();
ssize_t getAttribCount();
GLint getVertexSize() {return _vertexsize;}
private:
protected:
void addUniform(const std::string &name, Uniform* uniform);
void addVertexAttrib(const std::string &name, VertexAttrib* attrib);
void setVertexSize(GLint size) { _vertexsize = size;}
GLint _vertexsize;
std::map<std::string, Uniform*> _uniforms;
std::map<std::string, VertexAttrib*> _vertAttributes;
std::unordered_map<std::string, Uniform*> _uniforms;
std::unordered_map<std::string, VertexAttrib*> _vertAttributes;
};
@ -214,7 +211,7 @@ public:
//void bindUniformValue(std::string uniformName, int value);
UniformValue* getUniformValue(const std::string &name);
GLProgramData::VertexAttrib* getAttrib(std::string& name);
GLProgramData::VertexAttrib* getAttrib(const std::string& name);
void bindAllAttrib();

View File

@ -867,6 +867,7 @@ void AttribSprite::initShader()
attribname ="a_color";
shader->getAttrib(attribname)->_type = GL_UNSIGNED_BYTE;
shader->getAttrib(attribname)->_normalized = GL_TRUE;
shader->getAttrib(attribname)->_index = 1;
attribname ="a_texCoord";