ravi.joshi53's picture

Texture Color is mixing with other colors

Hi,

I am rendering some textures and objects in 2D plane. I noticed that texture color is mixing with other objects color. I mean originally the texture is in yellow color and it renders fine but when I draw it together with an object which is in green color, the texture becomes green. Below is the sample code-

int YellowTexture;
const int Top = 0;
const int Left = 0;
const int Far = 1;
const int Near = -1;
const int Right = 800;
const int Bottom = 600;
 
public TextureMappingTest() : base(800, 600){}
 
protected override void OnLoad(EventArgs e)
{
    GL.ClearColor(Color.Aqua);
    YellowTexture = LoadTexture(@"G:\textures\Yellow.PNG");
}
 
protected override void OnResize(EventArgs e)
{
    GL.Viewport(ClientRectangle);
 
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(Left, Right, Bottom, Top, Near, FarZ);
}
 
protected override void OnRenderFrame(FrameEventArgs e)
{
    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    GL.MatrixMode(MatrixMode.Modelview);
    GL.LoadIdentity();
 
    PointF TopLeft = new PointF(250, 10);
    DrawBitmap(TopLeft, new Size(200, 80), YellowTexture); // This is yellow texture
 
    TopLeft = new PointF(300, 100);
    DrawRectangle(TopLeft, new Size(100, 80), Color.Red);
 
    TopLeft = new PointF(400, 100);
    DrawRectangle(TopLeft, new Size(100, 80), Color.Green); // Because of this Green color, the above texture also become green
 
    SwapBuffers();
}
 
int LoadTexture(string File)
{
    Bitmap Bitmap = new Bitmap(File);
    int TextureId = GL.GenTexture();
 
    GL.BindTexture(TextureTarget.Texture2D, TextureId);
    System.Drawing.Imaging.BitmapData bitmapData = Bitmap.LockBits(new System.Drawing.Rectangle(0, 0, Bitmap.Width, Bitmap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
 
    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmapData.Width, bitmapData.Height, 0,
        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bitmapData.Scan0);
 
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Linear);
 
    Bitmap.UnlockBits(bitmapData);
    Bitmap.Dispose();
 
    return TextureId;
}
 
void DrawBitmap(PointF TopLeft, Size Size, int Texture)
{
    GL.PushMatrix();
    GL.Enable(EnableCap.Blend);
    GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
    GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
    GL.Enable(EnableCap.Texture2D);
    GL.BindTexture(TextureTarget.Texture2D, Texture);
 
    GL.Begin(PrimitiveType.Quads);
    GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(TopLeft.X, TopLeft.Y);
    GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y);
    GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y + Size.Height);
    GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(TopLeft.X, TopLeft.Y + Size.Height);
    GL.End();
 
    GL.Disable(EnableCap.Blend);
    GL.Disable(EnableCap.Texture2D);
    GL.MatrixMode(MatrixMode.Modelview);
    GL.PopMatrix();
}
 
void DrawRectangle(PointF TopLeft, Size Size, Color Color)
{
    GL.PushMatrix();
    GL.Begin(PrimitiveType.Quads);
    GL.Color3(Color);
    GL.Vertex2(TopLeft.X, TopLeft.Y);
    GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y);
    GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y + Size.Height);
    GL.Vertex2(TopLeft.X, TopLeft.Y + Size.Height);
    GL.End();
    GL.PopMatrix();
}

Please see the attached screenshot showing the output of above code. Texture file is also attached.

I believe I have disabled the blending after drawing the texture.
Why this is happening? How to prevent it?

-
Thanks

Inline Images
Output of above code
Used Texture File in above code

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

The texture is being influenced by the last drawn color. You can fix that by resetting the color to white in DrawBitmap:

    GL.Begin(PrimitiveType.Quads);
    GL.Color4(Color4.White);
    GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(TopLeft.X, TopLeft.Y);
    GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y);
    GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(TopLeft.X + Size.Width, TopLeft.Y + Size.Height);
    GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(TopLeft.X, TopLeft.Y + Size.Height);
    GL.End();
ravi.joshi53's picture

Great thanks Fiddler. It works. But why this is happening? Can you recommend me better way to do it if exists?

the Fiddler's picture

I thought that vertex color (GL.Color4()) wouldn't have an effect when using textures, but it appears I remembered incorrectly. I'm not sure what the interaction is here, but the specification should have more information (or maybe someone could explain what is going on.)

Frassle's picture

Colors are always turned on for the fixed-function pipeline.

ravi.joshi53's picture
Frassle wrote:

Colors are always turned on for the fixed-function pipeline.

Frassle, It would be better, if you explain little more. By the way, your trick works. Thanks for that.

Frassle's picture

The fixed function pipeline (i.e. the old style of OpenGL without shaders) defines a number of states. Color, lighting, texture etc. These states all have default values, and can be modified by GL functions (e.g. glDisable, glColor, glFog). Some of the states can be turned off entirely, such as lighting and textures. Some states such as color can never be turned off. The fixed function pipeline will ALWAYS color your vertices, but the default color of white will have no effect so you can use that to kinda turn off colors.

If you want to know exactly how it all works together read the specification.

ravi.joshi53's picture
Frassle wrote:

the old style of OpenGL

Then what is the new one? VBO? Please guide me.... Thanks for the above explanation.

Frassle's picture

Old style is using glBegin/glEnd and related functions (glColor, glVertex, glTexCoord). New style GL (and when the next version of OpenGL comes out, it will be the ONLY style) is using buffer objects and shader programs. It's also known by some other names:
old | new
immediate | retained
fixed-function pipeline | programmable pipeline
deprecated functions | core functions

There are lots of tutorials on how to use the new style, most are written for C/C++ but the OpenGL functions used are the same for C#.

OpenGL 4 core tutorial
OpenGL core sample pack

marcusqueen's picture

Colors are always turned on for the fixed-function pipeline.

http://www.angularcheilitisgoneforever.com

flopoloco's picture

I had created an application like that the other day. Now I have uploaded it in the blog section since it's now a good code for study.
http://www.opentk.com/node/3789