0
\$\begingroup\$

In my 2D game I use glColor4f() to set the color and draw my screen border rectangles but when I try to start drawing my game objects, made up of a texture on a rectangular "quad", using GL10.GL_TEXTURE_2D, GL10.GL_VERTEX_ARRAY and GL10.GL_TEXTURE_COORD_ARRAY no GL10.GL_COLOR_ARRAY, it uses the "current color" from glColor4f().

My question is what do I need to set/reset in OpenGl ES to re-enable it to use the colors from the texture when I try to draw them? If I comment out the border drawing, so glColor4f() never gets called, the colors are fine.

I'm not using double buffering. I read that once you set the color with glColor4f() it stays set, unless you possibly use glPushAttrib(GL_CURRENT_BIT), but I heard that that's expensive, time wise.

\$\endgroup\$
2
  • \$\begingroup\$ Is the texture that is drawn entirely a single color(the color you set with glColor4f) or are different parts of the texture different colors, just not what they are meant to be? if it is the second case try setting your color to white with glColor4f(1,1,1,1) before you draw your textures. \$\endgroup\$ Feb 4 '14 at 23:11
  • \$\begingroup\$ @user1572522: Please do not post answers into original posts body. \$\endgroup\$
    – Kromster
    Feb 20 '15 at 13:47
2
\$\begingroup\$

Full explanation:

OpenGL's (and GLES') default behavior is to modulate the currently set color with the texture with a simple multiplication. That is, it multiplies the color with the value at the texture.

The default color is white. White is defined as <1.0f, 1.0f, 1.0f, 1.0f>. One times anything is itself, so modulating white with the texture color results in the texture color itself.

If you were to change the color to red (<1.0f, 0.0f, 0.0f, 1.0f>) then the result would be red times the texture color; since zero times anything is zero, the result would be <1.0f * Texture.Red, 0.0f * Texture.Green, 0.0f * Texture.Blue, 1.0f * Texture.Alpha> = <Texture.Red, 0.0f, 0.0f, Texture.Alpha>. In other words, you'd see just the red component of the texture color at the texture's opacity.

That's useful behavior to have for some effects, especially back before more complicated masking and blending operations became available or the advent of shaders. The effect doesn't necessarily look all that good, but it works. (The problem is that you'd lose all the countours of green and blue objects, so what you probably want is a color modulation of luminocity values computed from all three color channels.)

You can't get too much control without switching to GLES 2+ and using shaders.

The result of all this math on your situation is what's already been suggested: reset GL's current color to white before drawing in order to see textured objects 'properly'.

\$\endgroup\$
1
\$\begingroup\$

You should set the color to white glColor4f(1,1,1,1) before drawing textured geometry.

\$\endgroup\$
0
\$\begingroup\$

Quoting the author:


I got it working:

The textures are made up of several colors. I went ahead and changed it to use a color array, and not use glColor4f(), when drawing the solid color borders, adding them with glColorPointer() and in beginDrawing() it does glEnableClientState() or glDisableClientState() with GL10.GL_COLOR_ARRAY and it's working pretty well.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.