Improved samples

This commit is contained in:
Ricardo Quesada 2014-05-07 12:41:04 -07:00
parent a2c4ddf028
commit 02405a8efe
3 changed files with 76 additions and 71 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._originalSize, &attribute._originalType, attribName);
attribName[length] = '\0';
attribute._name = std::string(attribName);
// Query the pre-assigned attribute location
attribute._index = glGetAttribLocation(_program, attribName);
_attributesDictionary[attribute._name] = attribute;
attribute.updateTypeAndSize();
}
}
}
@ -423,6 +423,10 @@ bool GLProgram::link()
glLinkProgram(_program);
parseVertexAttribs();
parseUniforms();
if (_vertShader)
{
glDeleteShader(_vertShader);
@ -453,9 +457,6 @@ bool GLProgram::link()
}
#endif
parseVertexAttribs();
parseUniforms();
return (status == GL_TRUE);
}
@ -790,6 +791,9 @@ void GLProgram::reset()
// VertexAttrib
//
VertexAttrib::VertexAttrib()
: _size(-1)
, _type(-1)
, _normalized(false)
{
}
@ -797,32 +801,54 @@ VertexAttrib::~VertexAttrib()
{
}
void VertexAttrib::setPointer(GLsizei stride, void* pointer, GLboolean isNormalized)
void VertexAttrib::updateTypeAndSize()
{
GLenum elemtype = _type;
GLint elemsize = _size;
switch (_type) {
switch (_originalType) {
case GL_FLOAT_VEC2:
elemtype = GL_FLOAT;
elemsize = 2;
_type = GL_FLOAT;
_size = 2;
break;
case GL_FLOAT_VEC3:
elemtype = GL_FLOAT;
elemsize = 3;
_type = GL_FLOAT;
_size = 3;
break;
case GL_FLOAT_VEC4:
elemtype = GL_FLOAT;
elemsize = 4;
_type = GL_FLOAT;
_size = 4;
break;
case GL_FLOAT_MAT2:
_type = GL_FLOAT;
_size = 4;
break;
case GL_FLOAT_MAT3:
_type = GL_FLOAT;
_size = 9;
break;
case GL_FLOAT_MAT4:
_type = GL_FLOAT;
_size = 16;
break;
default:
break;
}
glVertexAttribPointer(_index, elemsize, elemtype, isNormalized, stride, pointer);
}
void VertexAttrib::redefineType(GLenum type, GLint size, GLboolean normalized)
void VertexAttrib::setPointer(GLsizei stride, const GLvoid *pointer)
{
glVertexAttribPointer(_index, _size, _type, _normalized, stride, pointer);
}
void VertexAttrib::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer)
{
glVertexAttribPointer(_index, size, type, normalized, stride, pointer);
}
void VertexAttrib::redefineTypeAndSize(GLenum type, GLint size, GLboolean normalized)
{
_type = type;
_size = size;
_normalized = normalized;
}
//

View File

@ -61,13 +61,19 @@ class VertexAttrib
public:
VertexAttrib();
~VertexAttrib();
void setPointer(GLsizei stride, void* pointer = nullptr, GLboolean isNormalized = GL_FALSE);
void redefineType(GLenum type, GLint size, GLboolean normalized);
void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
void setPointer(GLsizei stride, const GLvoid *pointer);
void redefineTypeAndSize(GLenum type, GLint size, GLboolean normalized);
void updateTypeAndSize();
protected:
GLuint _index;
GLint _size;
GLint _originalSize;
GLenum _type;
GLenum _originalType;
GLboolean _normalized;
std::string _name;
};

View File

@ -173,12 +173,6 @@ void ShaderSprite::initShader()
CHECK_GL_ERROR_DEBUG();
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
CHECK_GL_ERROR_DEBUG();
program->link();
CHECK_GL_ERROR_DEBUG();
@ -190,6 +184,10 @@ void ShaderSprite::initShader()
buildCustomUniforms();
CHECK_GL_ERROR_DEBUG();
program->getVertexAttrib("a_color")->redefineTypeAndSize(GL_UNSIGNED_BYTE, 4, GL_TRUE);
program->getVertexAttrib("a_position")->redefineTypeAndSize(GL_FLOAT, 3, GL_FALSE);
program->getVertexAttrib("a_texCoord")->redefineTypeAndSize(GL_FLOAT, 2, GL_FALSE);
}
void ShaderSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -220,15 +218,15 @@ void ShaderSprite::onDraw(const Matrix &transform, bool transformUpdated)
// vertex
int diff = offsetof( V3F_C4B_T2F, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
shader->getVertexAttrib("a_position")->setPointer(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));
shader->getVertexAttrib("a_texCoord")->setPointer(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));
shader->getVertexAttrib("a_color")->setPointer(kQuadSize, (void*) (offset + diff));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
@ -769,9 +767,9 @@ void UniformSprite::setCustomUniforms()
{
std::string name = "center";
_shaderProgram->getUniformValue(name)->setValue(Vector2(480,320));
_shaderProgram->getUniform(name)->setValue(Vector2(480,320));
name = "resolution";
_shaderProgram->getUniformValue(name)->setValue(Vector2(256,256));
_shaderProgram->getUniform(name)->setValue(Vector2(256,256));
}
void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
@ -848,29 +846,13 @@ AttribSprite::~AttribSprite()
void AttribSprite::initShader()
{
auto shader = new GLProgram();
//shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
shader->initWithFilenames("Shaders/example_attribautobind.vsh", "Shaders/example_attribautobind.fsh");
shader->initWithFilenames(_vertSourceFile, _fragSourceFile);
shader->link();
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)->normalized = GL_TRUE;
// shader->getAttrib(attribname)->index = 1;
//
// attribname ="a_texCoord";
// shader->getAttrib(attribname)->index = 2;
}
void AttribSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
@ -907,29 +889,20 @@ void AttribSprite::onDraw(const Matrix &transform, bool transformUpdated)
#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));
program->getVertexAttrib("a_position")->setPointer(3, GL_FLOAT, GL_FALSE, stride, (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);
float a = getDisplayedOpacity() / 255.f;
GLfloat vertices[] = {0,0,0,1,0,0,a, 30,0,0,0,1,0,a, 30,30,0,0,0,1,a};
stride = sizeof(vertices)/3;
program->getVertexAttrib("a_position")->setPointer(stride, (void*)(vertices));
program->getVertexAttrib("a_color")->setPointer(stride, (void*)(vertices + 3*sizeof(GL_FLOAT)));
program->getVertexAttrib("a_color")->setPointer(4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void*) (offset + diff));
diff = offsetof( V3F_C4B_T2F, texCoords);
program->getVertexAttrib("a_texCoord")->setPointer(2, GL_FLOAT, GL_FALSE, stride, (void*) (offset + diff));
//program->getUniformValue("u_diffuseColor")->setValue(Vector4(1,1,1,1));
glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
CHECK_GL_ERROR_DEBUG();