Color method changing color of something already drawn

Mar 20, 2013 at 7:09 PM
I am using the WPF SharpGL control in my application to do some surface plotting, and I'm to the point where I am using a gradient in an image to color the map. I am seeing something really odd though. It seems as though the first frame gets the gradient applied correctly, but after that the gradient gets replaced by black fading to red and then fading back to black. So, to make things simpler I removed my code that was rendering and copied the code from the winforms texture example and sure enough I'm seeing the same problem with my image that I'm using for the gradient. However, if I use crate.bmp everything works fine. Here are screenshots to help illustrate, and my gradient image. In the example, the left side with the coloring is the first frame, and the window hasn't loaded yet which is where the transparency comes from.
http://i444.photobucket.com/albums/qq170/gizmodoMN/behavior_zps9ba6ca82.png

The gradient is actually a bmp, but photobucket doesn't support that so I converted it to png for uploading.
http://i444.photobucket.com/albums/qq170/gizmodoMN/gradient_zpsba1dc681.png

What am I doing wrong?
Apr 10, 2013 at 6:18 PM
Revisiting this. It turns out this wasn't a problem with textures at all. I have two methods, one to render the surface plot and the other to render the x, y and z axes. The axes call sets the color to black to draw the axes.

Why would having two different methods cause this behavior? What am I missing here? For now what I've done is remove the code that sets the color to black in the axis rendering method. That results in the axes having the color of the last point rendered. What do I need to do to be able to have the axes black? The surface map rendering is wrapped in a begin/end, and the axes are in a different begin/end pair.
Apr 10, 2013 at 8:45 PM
You need to set the color within the begin and end block.

so you would do this:

gl.Begin( ... );
gl.Color(set desired color);
render objects
gl.Color(set another color);
render other objects
gl.End();
Apr 10, 2013 at 8:51 PM
I have the rendering broken out into different methods. The code to render the plot uses BeginMode.Triangles and the code to render the axes uses BeginMode.Lines. From my understanding I can't change the rendering mode with a begin/end call. This is why I am confused, I thought gl.End effectively renders the objects, so I should be able to go back and change colors in a different begin/end section without affecting what is already rendered.
Apr 10, 2013 at 9:08 PM
You should still be able to do that. You can still set the color in between the begin and the end calls.
Apr 11, 2013 at 6:33 PM
That's what I thought. I don't understand what is happening. Here is my code. Both of these functions are called from the OpenGL draw event handler. One other thing I think is odd is that it doesn't matter whether I call RenderSurfaceMap first or RenderAxes first, it always results in an all black surface.
private void RenderSurfaceMap(OpenGL gl)
{
    if (this.Triangles.Count > 2)
    {
        // Determine the range of values so we can get the proper texture coordinates.
        double range = this.PlotData.HighestValue - this.PlotData.LowestValue;

        gl.BindTexture(OpenGL.GL_TEXTURE_2D, this.mTextures[0]);
        gl.Begin(BeginMode.Triangles);

        // Draw the map.
        foreach (Triangle t in this.Triangles)
        {
            gl.TexCoord(0.0f, (float)this.PlotData.Data[t.PointOne].Value / range);
            gl.Vertex((float)this.PlotData.Data[t.PointOne].X - this.XAxisOffset, (float)(this.PlotData.Data[t.PointOne].Value * this.ValueScale) - this.YAxisOffset, (float)this.PlotData.Data[t.PointOne].Y - this.ZAxisOffset);

            gl.TexCoord(0.0f, (float)this.PlotData.Data[t.PointTwo].Value / range);
            gl.Vertex((float)this.PlotData.Data[t.PointTwo].X - this.XAxisOffset, (float)(this.PlotData.Data[t.PointTwo].Value * this.ValueScale) - this.YAxisOffset, (float)this.PlotData.Data[t.PointTwo].Y - this.ZAxisOffset);

            gl.TexCoord(0.0f, (float)this.PlotData.Data[t.PointThree].Value / range);
            gl.Vertex((float)this.PlotData.Data[t.PointThree].X - this.XAxisOffset, (float)(this.PlotData.Data[t.PointThree].Value * this.ValueScale) - this.YAxisOffset, (float)this.PlotData.Data[t.PointThree].Y - this.ZAxisOffset);
        }

        gl.End();
    }
}

private void RenderAxes(OpenGL gl)
{
    gl.LineWidth(3.0f);

    gl.Begin(BeginMode.Lines);
    
    gl.Color(0.0f, 0.0f, 0.0f, 0.0f);

    // X Value axis
    gl.Vertex(0, 0, 0);
    gl.Vertex(this.PlotData.HighestXAxisValue, 0, 0);

    // Value axis
    gl.Vertex(0, 0, 0);
    gl.Vertex(0, this.PlotData.HighestValue * 2, 0);

    // Z value Axis
    gl.Vertex(0, 0, 0);
    gl.Vertex(0, 0, this.PlotData.HighestZAxisValue);

    gl.End();

    // Reset the line width
    gl.LineWidth(1.0f);
}
May 20, 2013 at 3:57 PM
It looks like I'm not the only person with this problem.
http://stackoverflow.com/questions/14234256/gl-color-affects-the-object-has-already-been-drawn-between-gl-begin-and-gl-end

Can someone please help?