love.graphics.setColor no longer uses the main per-vertex color attribute. This lets it affect the color of ParticleSystems, and SpriteBatches and Meshes even when they use custom per-vertex colors.

This commit is contained in:
Alex Szpakowski
2015-06-21 11:20:26 -03:00
parent 42ae9c9585
commit c82ccbb89b
10 changed files with 21 additions and 51 deletions
+1 -1
View File
@@ -783,7 +783,7 @@ Text *Graphics::newText(Font *font, const std::string &text)
void Graphics::setColor(Color c)
{
gl.setColor(c);
glVertexAttrib4f(ATTRIB_CONSTANTCOLOR, c.r/255.0f, c.g/255.0f, c.b/255.0f, c.a/255.0f);
states.back().color = c;
}
+1 -1
View File
@@ -654,7 +654,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
{
glDisableVertexAttribArray(attrib);
if (attrib == ATTRIB_COLOR)
gl.setColor(gl.getColor());
glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
}
}
+4 -16
View File
@@ -86,9 +86,9 @@ void OpenGL::setupContext()
initMaxValues();
state.color = Color(255, 255, 255, 255);
GLfloat glcolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
glVertexAttrib4fv(ATTRIB_COLOR, glcolor);
glVertexAttrib4fv(ATTRIB_CONSTANTCOLOR, glcolor);
// Get the current viewport.
glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
@@ -317,10 +317,11 @@ void OpenGL::prepareDraw()
TempDebugGroup debuggroup("Prepare OpenGL draw");
Shader *shader = Shader::current;
if (shader != nullptr)
{
// Make sure the active shader has the correct values for its
// love-provided uniforms.
// Make sure the active shader has the correct values for its love-
// provided uniforms.
shader->checkSetScreenParams();
}
@@ -374,19 +375,6 @@ void OpenGL::drawElements(GLenum mode, GLsizei count, GLenum type, const void *i
++stats.drawCalls;
}
void OpenGL::setColor(const Color &c)
{
GLfloat glc[] = {c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f};
glVertexAttrib4fv(ATTRIB_COLOR, glc);
state.color = c;
}
Color OpenGL::getColor() const
{
return state.color;
}
void OpenGL::setViewport(const OpenGL::Viewport &v)
{
glViewport(v.x, v.y, v.w, v.h);
+1 -13
View File
@@ -56,6 +56,7 @@ enum VertexAttribID
ATTRIB_POS = 0,
ATTRIB_TEXCOORD,
ATTRIB_COLOR,
ATTRIB_CONSTANTCOLOR,
ATTRIB_MAX_ENUM
};
@@ -196,16 +197,6 @@ public:
void drawArrays(GLenum mode, GLint first, GLsizei count);
void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices);
/**
* Sets the current constant color.
**/
void setColor(const Color &c);
/**
* Gets the current constant color.
**/
Color getColor() const;
/**
* Sets the OpenGL rendering viewport to the specified rectangle.
* The y-coordinate starts at the top.
@@ -356,9 +347,6 @@ private:
// Tracked OpenGL state.
struct
{
// Current constant color.
Color color;
// Texture unit state (currently bound texture for each texture unit.)
std::vector<GLuint> boundTextures;
@@ -854,8 +854,6 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo
OpenGL::TempDebugGroup debuggroup("ParticleSystem draw");
Color curcolor = gl.getColor();
static Matrix t;
t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
@@ -916,7 +914,7 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo
glDisableVertexAttribArray(ATTRIB_POS);
glDisableVertexAttribArray(ATTRIB_COLOR);
gl.setColor(curcolor);
glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
}
void ParticleSystem::update(float dt)
+6 -10
View File
@@ -379,9 +379,8 @@ void Polyline::draw()
if (overdraw)
{
// prepare colors:
Color c = gl.getColor();
Color *colors = new Color[overdraw_vertex_count];
fill_color_array(colors, c);
fill_color_array(colors);
glEnableVertexAttribArray(ATTRIB_COLOR);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
@@ -393,10 +392,9 @@ void Polyline::draw()
gl.drawArrays(draw_mode, 0, (int) overdraw_vertex_count);
glDisableVertexAttribArray(ATTRIB_COLOR);
glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
delete[] colors;
gl.setColor(c);
}
glDisableVertexAttribArray(ATTRIB_POS);
@@ -405,23 +403,21 @@ void Polyline::draw()
delete[] indices;
}
void Polyline::fill_color_array(Color *colors, const Color &c)
void Polyline::fill_color_array(Color *colors)
{
for (size_t i = 0; i < overdraw_vertex_count; ++i)
{
colors[i] = c;
// avoids branching. equiv to if (i%2 == 1) colors[i].a = 0;
colors[i].a *= GLubyte((i+1) % 2);
colors[i] = {255, 255, 255, GLubyte(255 * ((i+1) % 2))};
}
}
void NoneJoinPolyline::fill_color_array(Color *colors, const Color &c)
void NoneJoinPolyline::fill_color_array(Color *colors)
{
for (size_t i = 0; i < overdraw_vertex_count; ++i)
{
colors[i] = c;
// if (i % 4 == 1 || i % 4 == 2) colors[i].a = 0
colors[i].a *= GLubyte((i+1) % 4 < 2);
colors[i] = {255, 255, 255, GLubyte(255 * ((i+1) % 4 < 2))};
}
}
+2 -2
View File
@@ -71,7 +71,7 @@ public:
protected:
virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
virtual void fill_color_array(Color *colors, const Color &c);
virtual void fill_color_array(Color *colors);
/** Calculate line boundary points.
*
@@ -120,7 +120,7 @@ public:
protected:
virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
virtual void fill_color_array(Color *colors, const Color &c);
virtual void fill_color_array(Color *colors);
virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
Vector &s, float &len_s, Vector &ns,
const Vector &q, const Vector &r, float hw);
+2 -1
View File
@@ -282,7 +282,7 @@ bool Shader::loadVolatile()
throw love::Exception("Cannot link shader program object:\n%s", warnings.c_str());
}
// Retreive all active uniform variables in this shader from OpenGL.
// Get all active uniform variables in this shader from OpenGL.
mapActiveUniforms();
for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
@@ -869,6 +869,7 @@ StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry Shader::attribNameEntries[] =
{"VertexPosition", ATTRIB_POS},
{"VertexTexCoord", ATTRIB_TEXCOORD},
{"VertexColor", ATTRIB_COLOR},
{"ConstantColor", ATTRIB_CONSTANTCOLOR},
};
StringMap<VertexAttribID, ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));
+1 -3
View File
@@ -236,8 +236,6 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
array_buf->unmap(buffer_used_offset, buffer_used_size);
buffer_used_offset = buffer_used_size = 0;
Color curcolor = gl.getColor();
// Apply per-sprite color, if a color is set.
if (color)
{
@@ -260,7 +258,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
if (color)
{
glDisableVertexAttribArray(ATTRIB_COLOR);
gl.setColor(curcolor);
glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
}
}
@@ -71,6 +71,7 @@ GLSL.VERTEX = {
attribute vec4 VertexPosition;
attribute vec4 VertexTexCoord;
attribute vec4 VertexColor;
attribute vec4 ConstantColor;
varying vec4 VaryingTexCoord;
varying vec4 VaryingColor;
@@ -82,7 +83,7 @@ uniform mediump float love_PointSize;
FOOTER = [[
void main() {
VaryingTexCoord = VertexTexCoord;
VaryingColor = VertexColor;
VaryingColor = VertexColor * ConstantColor;
#ifdef GL_ES
gl_PointSize = love_PointSize;
#endif