Solid color rendering issue in GL

BlitzMax Forums/BlitzMax Programming/Solid color rendering issue in GL

SetGraphicsDriver GLMax2DDriver()


Graphics 800,600


image:TImage=LoadImage("gfx\powerups\powerup1.png")
n:Float=0.4
    SetBlend ALPHABLEND

While Not KeyHit(key_escape)
  Cls
    	
	SetAlpha 1.0
	DrawImage image,100,100
	
	SolidColorOn(0, 127, 0)	
	SetAlpha n
	DrawImage image,100,100
	SolidColorOff()
	
    n:+0.01
    If n>1.0 n=0.1

  Flip
Wend


' -----------------------------------------------------------------------------------------------------------------------------------------------------------
' This function enables solid color rendering.
'
' When rendering with solid colors, the color channels of the images you blit will be overridden with the solid color specified.  The alpha channel will
' remain intact however.
'
' This allows you to render a solid color sprite, white for example, with alpha, over the original, to make it appear to glow. 
' You can vary the strength of the effect by using SetAlpha() before you blit the second pass.
' -----------------------------------------------------------------------------------------------------------------------------------------------------------

	Function SolidColorOn(R%=255, G%=255, B%=255)

		Local TexColorArray#[4]

		SetColor(R, G, B)

		?Win32
			If TD3D7Max2DDriver(_max2dDriver) ' If we are currently rendering with DirectX...
			
				D3D7GraphicsDriver().Direct3DDevice7().SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG2)
				D3D7GraphicsDriver().Direct3DDevice7().SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
			
			Else ' We are using one of the OpenGL drivers.
		?		
				glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND)
				
				TexColorArray[0] = R
				TexColorArray[1] = G
				TexColorArray[2] = B
				
				glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, TexColorArray)
		?Win32		
			EndIf	
		?
		
	End Function


' -----------------------------------------------------------------------------------------------------------------------------------------------------------
' This function disables solid color rendering.
' -----------------------------------------------------------------------------------------------------------------------------------------------------------

	Function SolidColorOff()

		Local TexColorArray#[4]
	
		SetColor(255, 255, 255)
	
		?Win32
			If TD3D7Max2DDriver(_max2dDriver)
				D3D7GraphicsDriver().Direct3DDevice7().SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
				D3D7GraphicsDriver().Direct3DDevice7().SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE)
			Else
		?	

				'TexColorArray[0] = 255
				'TexColorArray[1] = 255
				'TexColorArray[2] = 255

				glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
				glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, TexColorArray)
		?Win32		
			EndIf
		?
	
	End Function 



I posted this code here a while back, but I've just noticed an issue when rendering the effect in OpenGL.

In the above code, whatever image you select should fade to a green siloughette, then return to normal. Every pixel should turn the same shade of green. If you can see any details, other than the siloughette formed by the transparent pixels, then it's wrong. It should also be a dark shade of green.

If you comment out the first line, you should see what I've described above. If you leav it as is however, then you may see white pixels remain white instead of fading to green.

I have also found that if I set the color to have any red in it at all, then the result looks even less like the desired result.

Changing that one line to read SolidColorOn(1, 127, 0) for example, results in the image brightening up as if add blending has been enabled.

In short, something is seriously wrong with the OpenGL code to enable this effect, and I could have sworn that it used to work just fine.

I posted these functions here a while back, and my sprite system uses them. So has anyone encountered this issue when using this code and found a fix for it?

I notice huge changes when I add a tiny number to the other color values, I don't see any white pixels when the image is in full-alpha, but instead black pixels never change color.

This method almost seems to just be negating the color spectrum of the texture (which would explain the black pixels, but not the white).