Was there ever a fix for the alpha ordering issue?

BlitzMax Forums/MiniB3D Module/Was there ever a fix for the alpha ordering issue?

Chapman7(Posted 2013) [#1]
Backstory: Bought a couple models to mess with and I used Milkshape to convert the .obj to .b3d


Import sidesign.minib3d

Strict

Local width=640,height=480,depth=0,mode=0

Graphics3D width,height,depth,mode
ClearTextureFilters


Local cam:TCamera=CreateCamera()
CameraRange cam,50,10000
PositionEntity cam,10,100,-100

Local light:TLight=CreateLight(1)
Local tex:TTexture=LoadTexture("media/test.png")
Local treetex:TTexture=LoadTexture("media/tree.png",2)


Local tree:TMesh=LoadMesh("media/tree.b3d")

PositionEntity tree,0,100,250

EntityTexture tree,treetex

' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	' control camera
	MoveEntity cam,KeyDown(KEY_D)*10-KeyDown(KEY_A)*10,0,KeyDown(KEY_W)*10-KeyDown(KEY_S)*10
	TurnEntity cam,KeyDown(KEY_DOWN)*2-KeyDown(KEY_UP)*2,KeyDown(KEY_LEFT)*2-KeyDown(KEY_RIGHT)*2,0

	RenderWorld
	renders=renders+1

	' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
	
	Text 0,0,"FPS: "+fps

	Flip
	
Wend
End


The result:



If I just load the textures without alpha, everything is fine except the textures look weird (and I have tried masking but it looks like garbage). So then when I do Alpha, surfaces show up even though they are behind others.


Kryzon(Posted 2013) [#2]
Break all the foliage into a second surface. You do that in Milkshape by making a second material (you can use the same texture) and applying it only to foliage triangles.

If that doesn't work, there are other ways.


Chapman7(Posted 2013) [#3]
I went ahead and made the foliage a whole separate model but it still has some of the foliage in front of the rest.


simonh(Posted 2013) [#4]
Try making each section of foliage a separate model.


Chapman7(Posted 2013) [#5]
Okay maybe I am getting somewhere. I was checking out AdamRedWoods talking about this and he gave two fixes that didnt seem to do much but FBEpyon posted this code that goes in TMesh.bmx

			alpha#=alpha#-fade_alpha#

			If surf.alpha_enable=True
				glEnable(GL_BLEND)
				glDepthMask(GL_FALSE)
			Else
				glDisable(GL_BLEND)
			EndIf

			' blend modes
			Select blend
				Case 0
					glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) ' alpha
				Case 1
					glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) ' alpha
				Case 2
					glBlendFunc(GL_DST_COLOR,GL_ZERO) ' multiply
				Case 3
					glBlendFunc(GL_SRC_ALPHA,GL_ONE) ' additive and alpha
			End Select
			
			glDepthMask(GL_TRUE)



I no longer have issues where something is behind another and you can see it. The new issue (and I hope to christ this is an easier fix) is this:



Making each section of foliage a seperate model is incredibly tedious (especially when I am looking at different trees with many sections that require alpha and I don't know how to (re)texture)

If anyone could fix this I would be more than thrilled!


Chapman7(Posted 2013) [#6]
If anyone wanted I could give the model out so you guys could play with it (not sure if the alpha issue was specific to this model)


Kryzon(Posted 2013) [#7]
If you can pack it with this code you're using to view the tree, I can give it a look. E-mail is in my profile.

It's one of those age-old problems that you always need to compromise at some point, so don't expect much.
Most games get away with it by using the alpha-test (masking, which you're not particularly interested in), others get away because they only look at trees from a specific angle etc.


simonh(Posted 2013) [#8]
It looks like the 'fix' just disables the alpha, which means the mesh gets drawn in the correct order.


Chapman7(Posted 2013) [#9]
It worked for the twigs though. Idk this stuff is beyond my skill level.

I told Kryzon even getting it so I could mask would be amazing but it seems any color I use shows up on the mesh a little bit when its textured. Maybe I am using a bad graphics program? (Pain.NET) I think its changing some of the pixels (that I dont want it to) to compress the image

Ill play around with masking some more. I just really need this thing to texture and render how its supposed to regardless of the method (unless its dismantling each foliage :P)


AdamRedwoods(Posted 2013) [#10]
so is this an alpha test issue rather than an alpha texture issue? i'm guessing since this is what foliage uses to sort properly.

you need to enable alpha testing, or TEXFLAG 4 (2+4 in your case) when loading the texture. this should all be on a separate surface from the main tree.
(see here in tmesh.bmx)
					' masked texture flag
					If tex_flags&4<>0
						glEnable(GL_ALPHA_TEST)
						glDepthMask(GL_TRUE) ''add this line
						'glAlphaFunc( GL_LEQUAL, 0.5 ) ''optional
					Else
						glDisable(GL_ALPHA_TEST)
					EndIf


but then in this code, we also need to make sure depth testing is enabled to sort Z order properly. see the above line i added.

...your edges of the leaves will be a little funky, but this can be tweaked by using
glAlphaFunc( GL_LEQUAL, 0.5 )

where 0.5 is an arbitrary number to fiddle with between 0.0-1.0.


Kryzon(Posted 2013) [#11]
You don't need that additional line if you just use flag 4.

glDepthMask(GL_FALSE) is only called if there's a flag 2 involved.


Chapman7(Posted 2013) [#12]

this should all be on a separate surface from the main tree.



I need it to work without me going through and changing the models (wouldn't even know where to begin) =/

Im just going to do masking and edit the textures since it seems it would be the easiest.

~~~~
If anyone stumbles across this thread when they try and find a solution, the best way it seems is to get rid of alpha and to use masking. Kryzon sent me a nice email explaining how to do it and I hope he is okay with me posting it here:


Instead of filling the transparent parts by merging the texture layer with a background color (which causes the background color to show through the semi-transparent pixels), rather "remove" the alpha information from these semi-transparent pixels.
You can do that easily in GIMP. Open your texture, then in the top menu click Layers -> Transparency -> Threshold Alpha. Then control with the slider an appropriate value (I think 32 is good). This will make pixels with an alpha value bigger than 32 be set at 255 alpha (so from semi-transparent they become fully opaque).
Then you can safely composite this texture with a layer below it, filled with black, and the pixel will remain their original color.



Also you will want to make sure your texture does not have black in it (since I believe that is the default maskcolor) so I would make a 0,0,0 pixel in gimp, go to Select -> By Color, set the Threshold to 0.00 and select the black. It will select all the black pixels in the image and from there you can do Colors -> Colorize and maybe bump the brightness from 0 to like 5 and hit Okay. Then delete your dead pixel and follow what Kryzon said above! :)


Chapman7(Posted 2013) [#13]
:_)




RemiD(Posted 2016) [#14]
the answer is to load your texture with the mask flag so that only black texels are alpha0 or to load your texture with the alpha flag but then rewrite the alpha property of each texel with your own procedure so that only black or darkgrey texels are alpha0 (if you load the texture with the alpha flag and don't rewrite the alpha value of each texel yourself, Blitz3d will automatically set the alpha value of each texel depending on its color and some texels which should not be transparent will be slightly transparent, and we don't want that...)

1: Color - colour map, what you see is what you get.

2: Alpha - alpha map. If an image contains an alpha map, this will be used to make certain areas of the texture transparent. Otherwise, the colour map will be used as an alpha map. With alpha maps, the dark areas always equal high-transparency, light areas equal low-transparency.

4: Masked - all areas of a texture coloured 0,0,0 will not be drawn to the screen.




RemiD(Posted 2016) [#15]
my previous post is totally off topic... :P


to solve the z-ordering problems due to the use of variable alpha texels or variable alpha vertices, i have noticed that if you separate your mesh in smaller parts ( surfaces) so that each variable alpha part "bounding box" does not intersect with the others surfaces, there is no more z-ordering problems. probably because the z-ordering is done per surface (considering the "bounding box" of each surface) instead of per triangle...