Skip to main content
GameDev.net gamedev.net
🔒 Locked

Color Modulation

Started by BlueMonk Jan 31, 2008 at 6:51 PM 6 replies 8.5k views
Original Post
BlueMonk
BlueMonk
I'm converting an application from DirectX to OpenGL and I was using a color modulation feature in DirectX that allowed me, for example, to scale down the red channel of an image to 50% by specifying a color modulation value like 0xFF80FFFF. Is there a similar feature which I could use as I convert this to OpenGL? The image is being drawn as a textured quad in the new environment. D3DSprite was the old method.
"All you need to do to learn circular logic is learn circular logic"
V-man
V-man
If you are not using lighting
glColor4ub(color, color, color, color)

For the tex environment mode :
glTexEnvi(..., ...., GL_MODULATE)
ndhb
ndhb
And if you want to store the modified image permanently in GL (instead of drawing it blended with another colour) you can use glPixelTransfer (http://www.opengl.org/sdk/docs/man/xhtml/glPixelTransfer.xml).
BlueMonk
BlueMonk
I'm using OpenTK, so I tried adding the following code before GL.Begin in my test program:
GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate);GL.Color4(255, 255, 128, 255);

And now none of the textured quads seem to be drawing. I expected output with half the green. This occurs even when I use GL.Color4(255,255,255,255) which I expected to have no effect on the output. And if I merely comment out the Color4 line, everything works again. Would any of these statements be interfering?
GL.PushAttrib(AttribMask.DepthBufferBit | AttribMask.LightingBit);GL.Disable(EnableCap.DepthTest);GL.Disable(EnableCap.Lighting);GL.Disable(EnableCap.Dither);GL.Enable(EnableCap.Blend);GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);GL.TexParameter(TextureTarget.TextureRectangleNv, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);GL.TexParameter(TextureTarget.TextureRectangleNv, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
"All you need to do to learn circular logic is learn circular logic"
V-man
V-man
GL.Color4 takes ubyte?

And don't call GL.TexParameter randomly. You only need to call it when you create the texture. It becomes part of the texture object and there is no need to change it again. GL is not like D3D.
BlueMonk
BlueMonk
Quote:
Original post by V-man
GL.Color4 takes ubyte?


Yes, there are 26 overloads of this function in OpenTK, and one of them takes 4 bytes (which, in C#, are unsigned).

Quote:

And don't call GL.TexParameter randomly. You only need to call it when you create the texture. It becomes part of the texture object and there is no need to change it again. GL is not like D3D.


It's only being called when creating the texture before the image is loaded into the texture.

Any ideas why the textures would be failing completely when I call GL.Color4? I've used Color4 to set the color for other things in the same way (drawing colored triangles works).

Oh, but it appears that when I called Color4 elsewhere, I was using floats instead of bytes. When I switch to using floats (between 0 and 1) everything works fine. I also see now that it works fine if explicitly cast the parameters that I'm passing to bytes so that the correct overload gets called.
"All you need to do to learn circular logic is learn circular logic"
BlueMonk
BlueMonk
DirectX has a function to modulate two colors with respect to each other:
color = Microsoft.DirectX.Direct3D.ColorOperator.Modulate(frame.Color, color)

Does OpenGL have anything like this, or is there a way to efficiently calculate the result?

The reason: I have a design environment in which the user is allowed to specify a color modulation value in an individual frame within a sprite, as well as an overall color modulation on the whole sprite, and if they specify both, I need to combine the values.
"All you need to do to learn circular logic is learn circular logic"
V-man
V-man
According to Microsoft.DirectX.Direct3D.ColorOperator.Modulate

it does

result.red = frame.Color.red * color.red
result.green = frame.Color.green * color.green
etc.

That's kind of weird. I would expect such a function to be in D3DX, not D3D.

To answer your question, no GL doesn't offer such a function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.