mirror of https://github.com/axmolengine/axmol.git
Improved samples
This commit is contained in:
parent
a2c4ddf028
commit
02405a8efe
|
@ -227,14 +227,14 @@ void GLProgram::parseVertexAttribs()
|
||||||
for(int i = 0; i < activeAttributes; ++i)
|
for(int i = 0; i < activeAttributes; ++i)
|
||||||
{
|
{
|
||||||
// Query attribute info.
|
// 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';
|
attribName[length] = '\0';
|
||||||
attribute._name = std::string(attribName);
|
attribute._name = std::string(attribName);
|
||||||
|
|
||||||
// Query the pre-assigned attribute location
|
// Query the pre-assigned attribute location
|
||||||
attribute._index = glGetAttribLocation(_program, attribName);
|
attribute._index = glGetAttribLocation(_program, attribName);
|
||||||
|
|
||||||
_attributesDictionary[attribute._name] = attribute;
|
_attributesDictionary[attribute._name] = attribute;
|
||||||
|
attribute.updateTypeAndSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -423,6 +423,10 @@ bool GLProgram::link()
|
||||||
|
|
||||||
glLinkProgram(_program);
|
glLinkProgram(_program);
|
||||||
|
|
||||||
|
|
||||||
|
parseVertexAttribs();
|
||||||
|
parseUniforms();
|
||||||
|
|
||||||
if (_vertShader)
|
if (_vertShader)
|
||||||
{
|
{
|
||||||
glDeleteShader(_vertShader);
|
glDeleteShader(_vertShader);
|
||||||
|
@ -453,9 +457,6 @@ bool GLProgram::link()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
parseVertexAttribs();
|
|
||||||
parseUniforms();
|
|
||||||
|
|
||||||
return (status == GL_TRUE);
|
return (status == GL_TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -790,6 +791,9 @@ void GLProgram::reset()
|
||||||
// VertexAttrib
|
// VertexAttrib
|
||||||
//
|
//
|
||||||
VertexAttrib::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;
|
switch (_originalType) {
|
||||||
GLint elemsize = _size;
|
|
||||||
switch (_type) {
|
|
||||||
case GL_FLOAT_VEC2:
|
case GL_FLOAT_VEC2:
|
||||||
elemtype = GL_FLOAT;
|
_type = GL_FLOAT;
|
||||||
elemsize = 2;
|
_size = 2;
|
||||||
break;
|
break;
|
||||||
case GL_FLOAT_VEC3:
|
case GL_FLOAT_VEC3:
|
||||||
elemtype = GL_FLOAT;
|
_type = GL_FLOAT;
|
||||||
elemsize = 3;
|
_size = 3;
|
||||||
break;
|
break;
|
||||||
case GL_FLOAT_VEC4:
|
case GL_FLOAT_VEC4:
|
||||||
elemtype = GL_FLOAT;
|
_type = GL_FLOAT;
|
||||||
elemsize = 4;
|
_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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -61,13 +61,19 @@ class VertexAttrib
|
||||||
public:
|
public:
|
||||||
VertexAttrib();
|
VertexAttrib();
|
||||||
~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:
|
protected:
|
||||||
GLuint _index;
|
GLuint _index;
|
||||||
GLint _size;
|
GLint _size;
|
||||||
|
GLint _originalSize;
|
||||||
GLenum _type;
|
GLenum _type;
|
||||||
|
GLenum _originalType;
|
||||||
GLboolean _normalized;
|
GLboolean _normalized;
|
||||||
std::string _name;
|
std::string _name;
|
||||||
};
|
};
|
||||||
|
|
|
@ -172,13 +172,7 @@ void ShaderSprite::initShader()
|
||||||
program->release();
|
program->release();
|
||||||
|
|
||||||
CHECK_GL_ERROR_DEBUG();
|
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();
|
program->link();
|
||||||
|
|
||||||
CHECK_GL_ERROR_DEBUG();
|
CHECK_GL_ERROR_DEBUG();
|
||||||
|
@ -190,6 +184,10 @@ void ShaderSprite::initShader()
|
||||||
buildCustomUniforms();
|
buildCustomUniforms();
|
||||||
|
|
||||||
CHECK_GL_ERROR_DEBUG();
|
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)
|
void ShaderSprite::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
||||||
|
@ -220,16 +218,16 @@ void ShaderSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
||||||
|
|
||||||
// vertex
|
// vertex
|
||||||
int diff = offsetof( V3F_C4B_T2F, vertices);
|
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
|
// texCoods
|
||||||
diff = offsetof( V3F_C4B_T2F, texCoords);
|
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
|
// color
|
||||||
diff = offsetof( V3F_C4B_T2F, colors);
|
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);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
||||||
}
|
}
|
||||||
|
@ -769,9 +767,9 @@ void UniformSprite::setCustomUniforms()
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string name = "center";
|
std::string name = "center";
|
||||||
_shaderProgram->getUniformValue(name)->setValue(Vector2(480,320));
|
_shaderProgram->getUniform(name)->setValue(Vector2(480,320));
|
||||||
name = "resolution";
|
name = "resolution";
|
||||||
_shaderProgram->getUniformValue(name)->setValue(Vector2(256,256));
|
_shaderProgram->getUniform(name)->setValue(Vector2(256,256));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
void UniformSprite::onDraw(const Matrix &transform, bool transformUpdated)
|
||||||
|
@ -848,29 +846,13 @@ AttribSprite::~AttribSprite()
|
||||||
void AttribSprite::initShader()
|
void AttribSprite::initShader()
|
||||||
{
|
{
|
||||||
auto shader = new GLProgram();
|
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->link();
|
||||||
shader->updateUniforms();
|
shader->updateUniforms();
|
||||||
this->setShaderProgram(shader);
|
this->setShaderProgram(shader);
|
||||||
|
|
||||||
shader->release();
|
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)
|
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)
|
#define kQuadSize sizeof(_quad.bl)
|
||||||
size_t offset = (size_t)&_quad;
|
size_t offset = (size_t)&_quad;
|
||||||
size_t stride = kQuadSize;
|
size_t stride = kQuadSize;
|
||||||
/*
|
|
||||||
// vertex
|
|
||||||
int diff = offsetof( V3F_C4B_T2F, vertices);
|
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);
|
diff = offsetof( V3F_C4B_T2F, colors);
|
||||||
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
|
program->getVertexAttrib("a_color")->setPointer(4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void*) (offset + diff));
|
||||||
*/
|
|
||||||
// program->setVertexAttrib((void*)offset, false);
|
diff = offsetof( V3F_C4B_T2F, texCoords);
|
||||||
float a = getDisplayedOpacity() / 255.f;
|
program->getVertexAttrib("a_texCoord")->setPointer(2, GL_FLOAT, GL_FALSE, stride, (void*) (offset + diff));
|
||||||
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->getUniformValue("u_diffuseColor")->setValue(Vector4(1,1,1,1));
|
//program->getUniformValue("u_diffuseColor")->setValue(Vector4(1,1,1,1));
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
// glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
||||||
|
|
||||||
CHECK_GL_ERROR_DEBUG();
|
CHECK_GL_ERROR_DEBUG();
|
||||||
|
|
Loading…
Reference in New Issue