beautify Uniform and VertexAttrib

This commit is contained in:
Ricardo Quesada 2014-05-07 11:26:26 -07:00
parent b1ed7178fe
commit a2c4ddf028
3 changed files with 57 additions and 46 deletions

View File

@ -227,14 +227,14 @@ void GLProgram::parseVertexAttribs()
for(int i = 0; i < activeAttributes; ++i)
{
// Query attribute info.
glGetActiveAttrib(_program, i, length, NULL, &attribute.size, &attribute.type, attribName);
glGetActiveAttrib(_program, i, length, NULL, &attribute._size, &attribute._type, attribName);
attribName[length] = '\0';
attribute.name = std::string(attribName);
attribute._name = std::string(attribName);
// Query the pre-assigned attribute location
attribute.index = glGetAttribLocation(_program, attribName);
attribute._index = glGetAttribLocation(_program, attribName);
_attributesDictionary[attribute.name] = attribute;
_attributesDictionary[attribute._name] = attribute;
}
}
}
@ -260,10 +260,10 @@ void GLProgram::parseUniforms()
for(int i = 0; i < activeUniforms; ++i)
{
// Query uniform info.
glGetActiveUniform(_program, i, length, NULL, &uniform.size, &uniform.type, uniformName);
glGetActiveUniform(_program, i, length, NULL, &uniform._size, &uniform._type, uniformName);
uniformName[length] = '\0';
// remove possible array '[]' from uniform name
if(uniform.size > 1 && length > 3)
if(uniform._size > 1 && length > 3)
{
char* c = strrchr(uniformName, '[');
if(c)
@ -271,13 +271,13 @@ void GLProgram::parseUniforms()
*c = '\0';
}
}
uniform.name = std::string(uniformName);
uniform.location = glGetUniformLocation(_program, uniformName);
uniform._name = std::string(uniformName);
uniform._location = glGetUniformLocation(_program, uniformName);
//something wrong, uniform is an object not a pointer, may be released soon
uniform.init(this);
_uniformsDictionary[uniform.name] = uniform;
_uniformsDictionary[uniform._name] = uniform;
}
}
}
@ -799,9 +799,9 @@ VertexAttrib::~VertexAttrib()
void VertexAttrib::setPointer(GLsizei stride, void* pointer, GLboolean isNormalized)
{
GLenum elemtype = type;
GLint elemsize = size;
switch (type) {
GLenum elemtype = _type;
GLint elemsize = _size;
switch (_type) {
case GL_FLOAT_VEC2:
elemtype = GL_FLOAT;
elemsize = 2;
@ -818,7 +818,11 @@ void VertexAttrib::setPointer(GLsizei stride, void* pointer, GLboolean isNormali
default:
break;
}
glVertexAttribPointer(index, elemsize, elemtype, isNormalized, stride, pointer);
glVertexAttribPointer(_index, elemsize, elemtype, isNormalized, stride, pointer);
}
void VertexAttrib::redefineType(GLenum type, GLint size, GLboolean normalized)
{
}
//
@ -838,82 +842,82 @@ Uniform::~Uniform()
bool Uniform::init(GLProgram* program)
{
_program = program;
return program && program && location != -1;
return program && program && _location != -1;
}
bool Uniform::setValue(float value)
{
CCASSERT (type == GL_FLOAT, "");
_program->setUniformLocationWith1f(location, value);
CCASSERT (_type == GL_FLOAT, "");
_program->setUniformLocationWith1f(_location, value);
return true;
}
bool Uniform::setValue(int value)
{
CCASSERT ((type == GL_INT || type == GL_SAMPLER_2D), "");
_program->setUniformLocationWith1i(location, value);
CCASSERT ((_type == GL_INT || _type == GL_SAMPLER_2D), "");
_program->setUniformLocationWith1i(_location, value);
return true;
}
bool Uniform::setValue(const Vector2& value)
{
CCASSERT (type == GL_FLOAT_VEC2, "");
_program->setUniformLocationWith2f(location, value.x, value.y);
CCASSERT (_type == GL_FLOAT_VEC2, "");
_program->setUniformLocationWith2f(_location, value.x, value.y);
return true;
}
bool Uniform::setValue(const Vector3& value)
{
CCASSERT (type == GL_FLOAT_VEC3, "");
_program->setUniformLocationWith3f(location, value.x, value.y, value.z);
CCASSERT (_type == GL_FLOAT_VEC3, "");
_program->setUniformLocationWith3f(_location, value.x, value.y, value.z);
return true;
}
bool Uniform::setValue(const Vector4& value)
{
CCASSERT (type == GL_FLOAT_VEC4, "");
_program->setUniformLocationWith4f(location, value.x, value.y, value.z, value.w);
CCASSERT (_type == GL_FLOAT_VEC4, "");
_program->setUniformLocationWith4f(_location, value.x, value.y, value.z, value.w);
return true;
}
bool Uniform::setValue(const Matrix& value)
{
CCASSERT(type == GL_FLOAT_MAT4, "");
_program->setUniformLocationWithMatrix4fv(location, value.m, 1);
CCASSERT(_type == GL_FLOAT_MAT4, "");
_program->setUniformLocationWithMatrix4fv(_location, value.m, 1);
return true;
}
bool Uniform::setValue(const Vector2* value, int count)
{
CCASSERT (type == GL_FLOAT_VEC2 && size == count, "");
_program->setUniformLocationWith2fv(location, (GLfloat*)value, count);
CCASSERT (_type == GL_FLOAT_VEC2 && _size == count, "");
_program->setUniformLocationWith2fv(_location, (GLfloat*)value, count);
return true;
}
bool Uniform::setValue(const Vector3* value, int count)
{
CCASSERT (type == GL_FLOAT_VEC3 && size == count, "");
_program->setUniformLocationWith3fv(location, (GLfloat*)value, count);
CCASSERT (_type == GL_FLOAT_VEC3 && _size == count, "");
_program->setUniformLocationWith3fv(_location, (GLfloat*)value, count);
return true;
}
bool Uniform::setValue(const Vector4* value, int count)
{
CCASSERT (type == GL_FLOAT_VEC4 && size == count, "");
_program->setUniformLocationWith4fv(location, (GLfloat*)value, count);
CCASSERT (_type == GL_FLOAT_VEC4 && _size == count, "");
_program->setUniformLocationWith4fv(_location, (GLfloat*)value, count);
return true;
}
bool Uniform::setValue(const Matrix* value, int count)
{
CCASSERT (type == GL_FLOAT_MAT4 && size == count, "");
_program->setUniformLocationWithMatrix4fv(location, (GLfloat*)value, count);
CCASSERT (_type == GL_FLOAT_MAT4 && _size == count, "");
_program->setUniformLocationWithMatrix4fv(_location, (GLfloat*)value, count);
return true;
}

View File

@ -55,20 +55,27 @@ typedef void (*GLLogFunction) (GLuint program, GLsizei bufsize, GLsizei* length,
class VertexAttrib
{
friend class GLProgram;
friend class VertexAttribBind;
public:
VertexAttrib();
~VertexAttrib();
void setPointer(GLsizei stride, void* pointer = nullptr, GLboolean isNormalized = GL_FALSE);
void redefineType(GLenum type, GLint size, GLboolean normalized);
GLuint index;
GLint size;
GLenum type;
GLboolean normalized;
std::string name;
protected:
GLuint _index;
GLint _size;
GLenum _type;
GLboolean _normalized;
std::string _name;
};
class Uniform
{
friend class GLProgram;
public:
Uniform();
~Uniform();
@ -84,11 +91,11 @@ public:
bool setValue(const Vector4* value, int count);
bool setValue(const Matrix* value, int count);
GLint location;
GLint size;
std::string name;
GLenum type;
protected:
GLint _location;
GLint _size;
std::string _name;
GLenum _type;
GLProgram* _program; // weak ref
};

View File

@ -93,8 +93,8 @@ 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());