Browse Source

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.

Alex Szpakowski 10 years ago
parent
commit
c82ccbb89b

+ 1 - 1
src/modules/graphics/opengl/Graphics.cpp

@@ -783,7 +783,7 @@ Text *Graphics::newText(Font *font, const std::string &text)
 
 
 void Graphics::setColor(Color c)
 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;
 	states.back().color = c;
 }
 }
 
 

+ 1 - 1
src/modules/graphics/opengl/Mesh.cpp

@@ -654,7 +654,7 @@ void Mesh::draw(float x, float y, float angle, float sx, float sy, float ox, flo
 	{
 	{
 		glDisableVertexAttribArray(attrib);
 		glDisableVertexAttribArray(attrib);
 		if (attrib == ATTRIB_COLOR)
 		if (attrib == ATTRIB_COLOR)
-			gl.setColor(gl.getColor());
+			glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
 	}
 	}
 }
 }
 
 

+ 4 - 16
src/modules/graphics/opengl/OpenGL.cpp

@@ -86,9 +86,9 @@ void OpenGL::setupContext()
 
 
 	initMaxValues();
 	initMaxValues();
 
 
-	state.color = Color(255, 255, 255, 255);
 	GLfloat glcolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
 	GLfloat glcolor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
 	glVertexAttrib4fv(ATTRIB_COLOR, glcolor);
 	glVertexAttrib4fv(ATTRIB_COLOR, glcolor);
+	glVertexAttrib4fv(ATTRIB_CONSTANTCOLOR, glcolor);
 
 
 	// Get the current viewport.
 	// Get the current viewport.
 	glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
 	glGetIntegerv(GL_VIEWPORT, (GLint *) &state.viewport.x);
@@ -317,10 +317,11 @@ void OpenGL::prepareDraw()
 	TempDebugGroup debuggroup("Prepare OpenGL draw");
 	TempDebugGroup debuggroup("Prepare OpenGL draw");
 
 
 	Shader *shader = Shader::current;
 	Shader *shader = Shader::current;
+
 	if (shader != nullptr)
 	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();
 		shader->checkSetScreenParams();
 	}
 	}
 
 
@@ -374,19 +375,6 @@ void OpenGL::drawElements(GLenum mode, GLsizei count, GLenum type, const void *i
 	++stats.drawCalls;
 	++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)
 void OpenGL::setViewport(const OpenGL::Viewport &v)
 {
 {
 	glViewport(v.x, v.y, v.w, v.h);
 	glViewport(v.x, v.y, v.w, v.h);

+ 1 - 13
src/modules/graphics/opengl/OpenGL.h

@@ -56,6 +56,7 @@ enum VertexAttribID
 	ATTRIB_POS = 0,
 	ATTRIB_POS = 0,
 	ATTRIB_TEXCOORD,
 	ATTRIB_TEXCOORD,
 	ATTRIB_COLOR,
 	ATTRIB_COLOR,
+	ATTRIB_CONSTANTCOLOR,
 	ATTRIB_MAX_ENUM
 	ATTRIB_MAX_ENUM
 };
 };
 
 
@@ -196,16 +197,6 @@ public:
 	void drawArrays(GLenum mode, GLint first, GLsizei count);
 	void drawArrays(GLenum mode, GLint first, GLsizei count);
 	void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices);
 	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.
 	 * Sets the OpenGL rendering viewport to the specified rectangle.
 	 * The y-coordinate starts at the top.
 	 * The y-coordinate starts at the top.
@@ -356,9 +347,6 @@ private:
 	// Tracked OpenGL state.
 	// Tracked OpenGL state.
 	struct
 	struct
 	{
 	{
-		// Current constant color.
-		Color color;
-
 		// Texture unit state (currently bound texture for each texture unit.)
 		// Texture unit state (currently bound texture for each texture unit.)
 		std::vector<GLuint> boundTextures;
 		std::vector<GLuint> boundTextures;
 
 

+ 1 - 3
src/modules/graphics/opengl/ParticleSystem.cpp

@@ -854,8 +854,6 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo
 
 
 	OpenGL::TempDebugGroup debuggroup("ParticleSystem draw");
 	OpenGL::TempDebugGroup debuggroup("ParticleSystem draw");
 
 
-	Color curcolor = gl.getColor();
-
 	static Matrix t;
 	static Matrix t;
 	t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
 	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_POS);
 	glDisableVertexAttribArray(ATTRIB_COLOR);
 	glDisableVertexAttribArray(ATTRIB_COLOR);
 
 
-	gl.setColor(curcolor);
+	glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
 }
 }
 
 
 void ParticleSystem::update(float dt)
 void ParticleSystem::update(float dt)

+ 6 - 10
src/modules/graphics/opengl/Polyline.cpp

@@ -379,9 +379,8 @@ void Polyline::draw()
 	if (overdraw)
 	if (overdraw)
 	{
 	{
 		// prepare colors:
 		// prepare colors:
-		Color c = gl.getColor();
 		Color *colors = new Color[overdraw_vertex_count];
 		Color *colors = new Color[overdraw_vertex_count];
-		fill_color_array(colors, c);
+		fill_color_array(colors);
 
 
 		glEnableVertexAttribArray(ATTRIB_COLOR);
 		glEnableVertexAttribArray(ATTRIB_COLOR);
 		glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, colors);
 		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);
 			gl.drawArrays(draw_mode, 0, (int) overdraw_vertex_count);
 
 
 		glDisableVertexAttribArray(ATTRIB_COLOR);
 		glDisableVertexAttribArray(ATTRIB_COLOR);
+		glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
 
 
 		delete[] colors;
 		delete[] colors;
-
-		gl.setColor(c);
 	}
 	}
 
 
 	glDisableVertexAttribArray(ATTRIB_POS);
 	glDisableVertexAttribArray(ATTRIB_POS);
@@ -405,23 +403,21 @@ void Polyline::draw()
 		delete[] indices;
 		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)
 	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;
 		// 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)
 	for (size_t i = 0; i < overdraw_vertex_count; ++i)
 	{
 	{
-		colors[i] = c;
 		// if (i % 4 == 1 || i % 4 == 2) colors[i].a = 0
 		// 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
src/modules/graphics/opengl/Polyline.h

@@ -71,7 +71,7 @@ public:
 
 
 protected:
 protected:
 	virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
 	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.
 	/** Calculate line boundary points.
 	 *
 	 *
@@ -120,7 +120,7 @@ public:
 
 
 protected:
 protected:
 	virtual void render_overdraw(const std::vector<Vector> &normals, float pixel_size, bool is_looping);
 	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,
 	virtual void renderEdge(std::vector<Vector> &anchors, std::vector<Vector> &normals,
 	                        Vector &s, float &len_s, Vector &ns,
 	                        Vector &s, float &len_s, Vector &ns,
 	                        const Vector &q, const Vector &r, float hw);
 	                        const Vector &q, const Vector &r, float hw);

+ 2 - 1
src/modules/graphics/opengl/Shader.cpp

@@ -282,7 +282,7 @@ bool Shader::loadVolatile()
 		throw love::Exception("Cannot link shader program object:\n%s", warnings.c_str());
 		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();
 	mapActiveUniforms();
 
 
 	for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
 	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},
 	{"VertexPosition", ATTRIB_POS},
 	{"VertexTexCoord", ATTRIB_TEXCOORD},
 	{"VertexTexCoord", ATTRIB_TEXCOORD},
 	{"VertexColor", ATTRIB_COLOR},
 	{"VertexColor", ATTRIB_COLOR},
+	{"ConstantColor", ATTRIB_CONSTANTCOLOR},
 };
 };
 
 
 StringMap<VertexAttribID, ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));
 StringMap<VertexAttribID, ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));

+ 1 - 3
src/modules/graphics/opengl/SpriteBatch.cpp

@@ -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);
 	array_buf->unmap(buffer_used_offset, buffer_used_size);
 	buffer_used_offset = buffer_used_size = 0;
 	buffer_used_offset = buffer_used_size = 0;
 
 
-	Color curcolor = gl.getColor();
-
 	// Apply per-sprite color, if a color is set.
 	// Apply per-sprite color, if a color is set.
 	if (color)
 	if (color)
 	{
 	{
@@ -260,7 +258,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 	if (color)
 	if (color)
 	{
 	{
 		glDisableVertexAttribArray(ATTRIB_COLOR);
 		glDisableVertexAttribArray(ATTRIB_COLOR);
-		gl.setColor(curcolor);
+		glVertexAttrib4f(ATTRIB_COLOR, 1.0f, 1.0f, 1.0f, 1.0f);
 	}
 	}
 }
 }
 
 

+ 2 - 1
src/modules/graphics/opengl/wrap_Graphics.lua

@@ -71,6 +71,7 @@ GLSL.VERTEX = {
 attribute vec4 VertexPosition;
 attribute vec4 VertexPosition;
 attribute vec4 VertexTexCoord;
 attribute vec4 VertexTexCoord;
 attribute vec4 VertexColor;
 attribute vec4 VertexColor;
+attribute vec4 ConstantColor;
 
 
 varying vec4 VaryingTexCoord;
 varying vec4 VaryingTexCoord;
 varying vec4 VaryingColor;
 varying vec4 VaryingColor;
@@ -82,7 +83,7 @@ uniform mediump float love_PointSize;
 	FOOTER = [[
 	FOOTER = [[
 void main() {
 void main() {
 	VaryingTexCoord = VertexTexCoord;
 	VaryingTexCoord = VertexTexCoord;
-	VaryingColor = VertexColor;
+	VaryingColor = VertexColor * ConstantColor;
 #ifdef GL_ES
 #ifdef GL_ES
 	gl_PointSize = love_PointSize;
 	gl_PointSize = love_PointSize;
 #endif
 #endif