Transparency wierdness

Blitz3D Forums/Blitz3D Programming/Transparency wierdness

Shifty Geezer(Posted 2005) [#1]
Following on from my bottomless cube, I've got a wall assembled from four boxes. I'm using custom UV coords for a texture. In the top-left corner of this 128x128 texture I've a semi-transparent pixel.

My cunning plan to create a wall that's semi-transparent from the outside is to set the UV coords of the back rectangle to point to this semi-transparent pixel and with texture Alpha on...Bob's your uncle.

It works, but only properly for two walls. The other two walls show through the transparency and I don't understand why.


The code is...
Graphics3D 1024,768,32,0
pivot=CreatePivot()
camera=CreateCamera(pivot)
PositionEntity camera,0,50,-500
PointEntity camera,pivot
CameraClsColor camera,0,128,255

AmbientLight 120,120,120
light1=CreateLight()
LightColor light1,40,100,255
TurnEntity light1,45,-205,0	
light2=CreateLight()
LightColor light2,255,230,180
TurnEntity light2,45,35,0	

	tex_wall=LoadTexture("Textures\wall.png",3)
	;#Region Create Block
		mdl_block=CreateMesh()
		surface=CreateSurface(mdl_block)
		; Face A
		vA1=AddVertex(surface, 1,-1,-1,		0,1)
		vA2=AddVertex(surface, 1, 1,-1,			0,0.02)
		vA3=AddVertex(surface,-1, 1,-1,		1,0.02)
		vA4=AddVertex(surface,-1,-1, -1,		1,1)
		VertexNormal  surface,vA1,0,0,-1
		VertexNormal  surface,vA2,0,0,-1
		VertexNormal  surface,vA3,0,0,-1
		VertexNormal  surface,vA4,0,0,-1
		AddTriangle (surface,vA1, vA2, vA3)
		AddTriangle (surface,vA1, vA3, vA4)
	
		; Face B
		vB1=AddVertex(surface,-1,-1,-1,	0.01,0.01)
		vB2=AddVertex(surface,-1, 1,-1,		0.01,0.01)
		vB3=AddVertex(surface,-1, 1, 1,		0.01,0.01)
		vB4=AddVertex(surface,-1,-1, 1,		0.01,0.01)
		VertexNormal  surface,vB1,-1,0,0
		VertexNormal  surface,vB2,-1,0,0
		VertexNormal  surface,vB3,-1,0,0
		VertexNormal  surface,vB4,-1,0,0
		AddTriangle (surface,vB1, vB2, vB3)
		AddTriangle (surface,vB1, vB3, vB4)
	
		; Face C
		vC1=AddVertex(surface, 1,1, 1,		1,1);0.01,0.01)
		vC2=AddVertex(surface,-1,1, 1,		1,1);0.01,0.01)
		vC3=AddVertex(surface,-1,1,-1,		1,1);0.01,0.01)
		vC4=AddVertex(surface, 1,1,-1,		1,1);0.01,0.01)
		VertexNormal  surface,vC1,0,1,0
		VertexNormal  surface,vC2,0,1,0
		VertexNormal  surface,vC3,0,1,0
		VertexNormal  surface,vC4,0,1,0
		AddTriangle (surface,vC1, vC2, vC3)
		AddTriangle (surface,vC1, vC3, vC4)
	
		; Face D
		vD1=AddVertex(surface,1,-1, 1,		0.01,0.01)
		vD2=AddVertex(surface,1, 1, 1,		0.01,0.01)
		vD3=AddVertex(surface,1, 1,-1,		0.01,0.01)
		vD4=AddVertex(surface,1,-1,-1,		0.01,0.01)
		VertexNormal  surface,vD1,1,0,0
		VertexNormal  surface,vD2,1,0,0
		VertexNormal  surface,vD3,1,0,0
		VertexNormal  surface,vD4,1,0,0
		AddTriangle (surface,vD1, vD2, vD3)
		AddTriangle (surface,vD1, vD3, vD4)
	
		; Face E
		vE1=AddVertex(surface,-1,-1,1,		1,0)
		vE2=AddVertex(surface,-1, 1,1,		1,0)
		vE3=AddVertex(surface, 1, 1,1,		1,0)
		vE4=AddVertex(surface, 1,-1,1,		1,0)
		VertexNormal  surface,vE1,0,0,1
		VertexNormal  surface,vE2,0,0,1
		VertexNormal  surface,vE3,0,0,1
		VertexNormal  surface,vE4,0,0,1
		AddTriangle (surface,vE1, vE2, vE3)
		AddTriangle (surface,vE1, vE3, vE4)
		HideEntity mdl_block
	;#End Region
	;#Region Create walls
		mdl_wall=CreateMesh()
		length=500 : height=30 : depth = 5

		mdl_block1=CopyMesh(mdl_block)
		FitMesh mdl_block1,-length/2, 0,0, length, height, depth
		PositionMesh mdl_block1,0,0,length/2
		AddMesh mdl_block1,mdl_wall

		mdl_block2=CopyMesh(mdl_block1) : RotateMesh mdl_block2,0,90,0 :	AddMesh mdl_block2,mdl_wall
		mdl_block3=CopyMesh(mdl_block1) : RotateMesh mdl_block3,0,180,0 : AddMesh mdl_block3,mdl_wall
		mdl_block4=CopyMesh(mdl_block1) : RotateMesh mdl_block4,0,-90,0 : AddMesh mdl_block4,mdl_wall
		 FreeEntity mdl_block1 : FreeEntity mdl_block2 : FreeEntity mdl_block3 : 	FreeEntity mdl_block4
		
		EntityTexture mdl_wall,tex_wall
		ScaleTexture tex_wall,0.0769,1.0
;		EntityFX mdl_wall,16
		FlipMesh mdl_wall
	;#End Region

cube=CreateCube()
EntityColor cube,0,128,0
ScaleEntity cube,255,2,255
PositionEntity cube,0,-2,0
texScale#=0.0769

While Not KeyHit(1)
	If KeyDown(205)
		TurnEntity pivot,0,1.0,0
	ElseIf KeyDown(203)
		TurnEntity pivot,0,-1.0,0
	EndIf
	If KeyDown(200)
		TurnEntity pivot,1.0,0,0
	ElseIf KeyDown(208)
		TurnEntity pivot,-1.0,0,0
	EndIf
	If KeyDown(209)
		MoveEntity camera,0,0,1
	ElseIf KeyDown(201)
		MoveEntity camera,0,0,-1
	EndIf
	If KeyDown(13)
		texScale=texScale+0.0001
		ScaleTexture tex_wall,texScale,1.0
	ElseIf KeyDown(12)
		texScale=texScale-0.0001
		ScaleTexture tex_wall,texScale,1.0
	EndIf

	RenderWorld
	
	Text 10,10,texScale
	
	Flip
Wend

I feel a bit bad not using the box routines provided me but my simple brain was happier dealing with each vertex one by one with the UV coords!


markcw(Posted 2005) [#2]
functions 'dCreateBox' & 'dGeomSetPosition' not found.

Edit: I commented out those lines and seems it's working on my pc.




Shifty Geezer(Posted 2005) [#3]
I inadvertantly left in some ODE functions.

Are the back walls fully opaque? They look to me like they're alpha'd in your pic, as though they're a transparent grey over a blue background. Here's the texture I was using if anyone wants to try it out



Right-Click and Save As to preserve the alpha.


Ross C(Posted 2005) [#4]
It seems like a z ordering problem to me. i'll try the code :o)


Ross C(Posted 2005) [#5]
Yep, def looks like it. To solve this, make each wall section a seperate mesh. Basically, because all the wall is the one surface/mesh, alphaed tri's aren't sorted by distance, instead, they are sorted by order of creation. You get similar problems with single surface grass system, further away grass being draw in front of closer pieces of grass. There is code in the code archives to sort this problem, by constant re-ordering the tri's, but i dunno if it's limited to single quads.

I'd just make each section a seperate mesh :o)


Shifty Geezer(Posted 2005) [#6]
And there was me trying to be clever and consolidate them all into one mesh!


Shifty Geezer(Posted 2005) [#7]
Nope, that doesn't fix it. Same problem with four separate meshes as 1 consolidated mesh. My code for this is...
	;#Region Create walls
;		mdl_wall=CreateMesh()
		length=500 : height=30 : depth = 5

		mdl_block1=CopyMesh(mdl_block)
		FitMesh mdl_block1,-length/2, 0,0, length, height, depth
		PositionMesh mdl_block1,0,0,length/2
;		AddMesh mdl_block1,mdl_wall

		mdl_block2=CopyMesh(mdl_block1) : RotateMesh mdl_block2,0,90,0; :	AddMesh mdl_block2,mdl_wall
		mdl_block3=CopyMesh(mdl_block1) : RotateMesh mdl_block3,0,180,0; : AddMesh mdl_block3,mdl_wall
		mdl_block4=CopyMesh(mdl_block1) : RotateMesh mdl_block4,0,-90,0; : AddMesh mdl_block4,mdl_wall
;		 FreeEntity mdl_block1 : FreeEntity mdl_block2 : FreeEntity mdl_block3 : 	FreeEntity mdl_block4
		
		EntityTexture mdl_block1,tex_wall
		EntityTexture mdl_block2,tex_wall
		EntityTexture mdl_block3,tex_wall
		EntityTexture mdl_block4,tex_wall
		ScaleTexture tex_wall,0.0769,1.0
;		EntityFX mdl_wall,16
;		FlipMesh mdl_wall
	;#End Region



VIP3R(Posted 2005) [#8]
I get the same problems as Shifty with both examples.

The first example half works as Shifty mentions.

The second example doesn't work well at all and the textures are on the outer sides instead of the inner sides.


Shifty Geezer(Posted 2005) [#9]
Oh, that second example is slightly wrong in it's use (or lack of) flip mesh. FlipMesh needs to be applied to all mdl_blocks.
Here's a more direct example



VIP3R(Posted 2005) [#10]
Yeh, that's better.

In the first example, two of the walls connected at a corner have correct alpha. But in the last example, two opposing walls have correct alpha instead. In both cases the remaining walls don't alpha correctly, weird.

Swapping the walls around doesn't seem to help either, it's always the same walls that work or don't work.


Shifty Geezer(Posted 2005) [#11]
I was just thinking if it's a graphics card, but I see your ATi and I'm GF4 Ti4200, so that can't be it either.


Ross C(Posted 2005) [#12]
Your adding the meshes into one mesh. So, your back with the same problem. Make each wall, a seperate mesh :o)


VIP3R(Posted 2005) [#13]
Look again Ross ;)


Ross C(Posted 2005) [#14]
well, what i see is this:

FitMesh mdl_block1,-length/2, 0,0, length, height, depth
		PositionMesh mdl_block1,0,0,length/2
		mdl_block2=CopyMesh(mdl_block1) : RotateMesh mdl_block2,0,90,0; : AddMesh mdl_block2,mdl_wall
		mdl_block3=CopyMesh(mdl_block1) : RotateMesh mdl_block3,0,180,0; : AddMesh mdl_block3,mdl_wall
		mdl_block4=CopyMesh(mdl_block1) : RotateMesh mdl_block4,0,-90,0; : AddMesh mdl_block4,mdl_wall




VIP3R(Posted 2005) [#15]
Yeh... the AddMesh functions are commented out ;)


Ross C(Posted 2005) [#16]
lol, sorry guys

*me embarrased*


Ross C(Posted 2005) [#17]
Dam, i dunno. I've had a good look through the code and tried many different things. not using copy mesh... etc. Just seems to be a Z-ordering issue. You can't really sort that. I think it's because of the solid side and the alphaed side... You could try vertexalpha instead of the textures alpha channel.


Shifty Geezer(Posted 2005) [#18]
VertexAlpha eh? That's probably a better way anyway. I'll look into.


Ross C(Posted 2005) [#19]
Your probably better, creating this kind of shape in a modeller. Ultimate Unwrap is very good for this stuff.


Shifty Geezer(Posted 2005) [#20]
Using VertexAlpha fixed the problem, and all's hunky-dorey now. Thanks all.