Alphablended texture z-index problem

Everything related to 3D programming
m0
New User
New User
Posts: 8
Joined: Thu Nov 22, 2007 3:38 pm
Location: DE

Alphablended texture z-index problem

Post by m0 »

Hello,

I did search quite a bit here in the forum, but cannot really find an idea why I see this happening to my alpha-blended textures:
Image

The wooden parts are completely opaque, and when i render these cubes without the alpha-blending, their z-order is OK. Did I miss something?
the problem is even worse when disabling culling.

Would be extremely helpful,if anyone of you has any idea to fix this, or event why this is happening.

Thank you very much in advance!!
m0
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Alphablended texture z-index problem

Post by Samuel »

Hello, I experimented with this problem a while back. What's happening is your object rendering order is out of whack. When using alpha, objects need to be rendered from farthest to nearest based on your camera's location. Otherwise, when samples are stored in the buffer, they can be missed. Which causes the weird alpha effects. As far as I know, PB/Ogre doesn't have an automated object rendering order system. Without that, you can't do much to fix this problem.
You could use a transparency shader. viewtopic.php?f=36&t=64081
You'll end up with jagged edges though because it discards the samples. Which means anti-aliasing won't work with it. That's about as far as I could get when dealing with this problem. There are nicer shader solutions out there, but you'd need more access to Ogre's internals to pull them off.
m0
New User
New User
Posts: 8
Joined: Thu Nov 22, 2007 3:38 pm
Location: DE

Re: Alphablended texture z-index problem

Post by m0 »

Well, basically with ogre it is not possible to use alpha textures when there are multiple of them behind each other...
wow this is really sad, then.

Thanks so much for the explanation!
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 289
Joined: Thu Jul 09, 2015 9:07 am

Re: Alphablended texture z-index problem

Post by pf shadoko »

Hello,

your image doesn't work, I don't know if my solution will work for you
maybe you can solve your problem with 2 entities:
one with AntiClockWiseCull, the other with ClockWiseCull
(I think it also works with 2 sub-meshes in the same entity)
(obviously the inside must be defined before the outside)

Code: Select all

Procedure generematiere(num,dx,dy,c1,c2)
  CreateTexture(num,dx,dy)
  StartDrawing(TextureOutput(num))
  DrawingMode(#PB_2DDrawing_AllChannels  )
      Box(0,0,dx,dy,c1)
      Box(1,1,dx-2,dy-2,c2)
  StopDrawing()
  CreateMaterial(num, TextureID(num))
EndProcedure

Define.f yrot,xrot,zrot
Define i,ii,j,c,l,pb=50,dis=0,vdis=2<<dis*5,ex,ey

InitEngine3D():InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, 0,0, "test alphablend - arrow keys - Esc to Quit",#PB_Window_Maximize)
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,10,-20)
CreateLight(0,$888888, 10000, 5000, 2000)
CameraBackColor(0,$ff8888) 
AmbientColor($444444)

generematiere(0,8,8,$ff008800,$88000044):MaterialBlendingMode(0,#PB_Material_AlphaBlend):MaterialCullingMode(0,#PB_Material_AntiClockWiseCull)
generematiere(1,8,8,$ff880000,$88000044):MaterialBlendingMode(1,#PB_Material_AlphaBlend)
generematiere(10,64,64,$008888,$00ffff)

CreateCube(0,9)
CreateEntity(i*2+0,MeshID(0),MaterialID(0))
CreateEntity(i*2+1,MeshID(0),MaterialID(1))
AttachEntityObject(i*2,"",EntityID(i*2+1))

CreatePlane(10,100,100,1,1,20,20)
CreateEntity(10,MeshID(10),MaterialID(10),0,-10,0)

Repeat     
  WindowEvent()
  ExamineMouse()
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Space):action=0:EndIf
  If KeyboardReleased(#PB_Key_F2):dis=(dis+1)%3:vdis=2<<dis*5:EndIf
  If KeyboardReleased(#PB_Key_F1):EntityAngularFactor(0,0,0,0):EndIf
  xrot+(Bool(KeyboardPushed(#PB_Key_Up)<>0)-Bool(KeyboardPushed(#PB_Key_Down)<>0))
  yrot+(Bool(KeyboardPushed(#PB_Key_Left)<>0)-Bool(KeyboardPushed(#PB_Key_Right)<>0))
  zrot+(Bool(KeyboardPushed(#PB_Key_PageUp)<>0)-Bool(KeyboardPushed(#PB_Key_PageDown)<>0))
  RotateEntity(0,xrot,yrot,zrot,0)
  ;MoveCamera(0,Cos(yrot/180*#PI)*40,10,Sin(yrot/180*#PI)*40,0)
  CameraLookAt(0,EntityX(0),EntityY(0),EntityZ(0))
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
m0
New User
New User
Posts: 8
Joined: Thu Nov 22, 2007 3:38 pm
Location: DE

Re: Alphablended texture z-index problem

Post by m0 »

hello,
thank you very much for the reply, although i must say your solution should result in the same behaviour than using "#pb_material_noculling". As far as i understand it's really a problem of orge because you have to disable the distance evaluation as soon as you want alpha textures. but I'll definitely give your solution a try and report the outcome as soon as i find time :)

thanks so much for putting together this good example.

m0
m0
New User
New User
Posts: 8
Joined: Thu Nov 22, 2007 3:38 pm
Location: DE

Re: Alphablended texture z-index problem

Post by m0 »

Well I must admit: Your sample really seems to work, so far the problem has disappeared, although I cannot understand why, to be honest, would you mind telling the trick? What's the difference than just putting NoCulling?
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 289
Joined: Thu Jul 09, 2015 9:07 am

Re: Alphablended texture z-index problem

Post by pf shadoko »

the explanations would be a bit long (moreover I'm not sure to master everything)
it is related to the functioning of graphic cards
I think you'll find the explanations on the internet (alphablend, transparent sorting, depthbuffer)

fortunately the sorting is done between submesh (and consequently between entities).

rather than going through a 2nd entity, the best is to double the mesh
I have created a "meshdoubleside" procedure for this purpose.
it works for convex shapes, for concave shapes, you have to cut the mesh in as many submesh as necessary.
(not necessarily possible for curved shapes...)
for example for a car, you need a submesh for the top (roof and windows) and another one for the bottom.

Code: Select all

Procedure meshdoubleside(mesh,inversenormal=1)
  Protected Dim MV.PB_MeshVertex(0)
  Protected Dim MF.PB_MeshFace(0)
  Protected i,j,inv=-1
  
  GetMeshData(Mesh,0, MV(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate| #PB_Mesh_Normal |#PB_Mesh_Color, 0, MeshVertexCount(Mesh, 0)-1)
  GetMeshData(Mesh,0, MF(), #PB_Mesh_Face, 0, MeshIndexCount(Mesh, 0)-1)
  
  CreateMesh(mesh)
  For j=0 To 1
      AddSubMesh()
      If j=1 And inversenormal:inv=1:EndIf
      For i=0 To ArraySize(MV())
          With MV(i)
              MeshVertex(\x,\y,\z,\u,\v,\Color,\NormalX*inv,\NormalY*inv,\NormalZ*inv)
          EndWith
      Next     
      Select j
      Case 1:For i=0 To ArraySize(MF()) Step 3:MeshFace(MF(i)\Index, MF(i+1)\Index, MF(i+2)\Index):Next
      Case 0:For i=0 To ArraySize(MF()) Step 3:MeshFace(MF(i)\Index, MF(i+2)\Index, MF(i+1)\Index):Next
      EndSelect
  Next
  FinishMesh(1)
EndProcedure

Procedure generematiere(num,dx,dy,c1,c2,scalex.f=1,scaley.f=1,AlphaBlend=0)
  CreateTexture(num,dx,dy)
  StartDrawing(TextureOutput(num))
  DrawingMode(#PB_2DDrawing_AllChannels  )
      Box(0,0,dx,dy,c1)
      Box(1,1,dx-2,dy-2,c2)
  StopDrawing()
  CreateMaterial(num, TextureID(num))
  ScaleMaterial(num,scalex,scaley)
  MaterialShininess(num,64):SetMaterialColor(num,#PB_Material_SpecularColor,$ffffff)
  If AlphaBlend:MaterialBlendingMode(num,#PB_Material_AlphaBlend):EndIf
EndProcedure

InitEngine3D():InitSprite():InitKeyboard():InitMouse()
OpenWindow(0, 0, 0, 0,0, "test alphablend - arrow keys - Esc to Quit",#PB_Window_Maximize)
ex=WindowWidth (0,#PB_Window_InnerCoordinate)
ey=WindowHeight(0,#PB_Window_InnerCoordinate)
OpenWindowedScreen(WindowID(0), 0, 0, ex, ey, 0, 0, 0)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,10,-20)
CreateLight(0,$ffffff, 5000, 5000, 0)
CameraBackColor(0,$ff8888)
AmbientColor($444444)


CreateSphere(0,8,64,64)
meshdoubleside(0)
generematiere(0,8,8,$ff00ff00,$88444444,0.1,0.1,1):SetMeshMaterial(0,MaterialID(0),0)
generematiere(1,8,8,$ff0000ff,$88444444,0.1,0.1,1):SetMeshMaterial(0,MaterialID(1),1); !!! second submesh is outside !!!
CreateEntity(0,MeshID(0),#PB_Material_None)

CreatePlane(10,100,100,1,1,20,20)
generematiere(10,64,64,$008888,$00ffff)
CreateEntity(10,MeshID(10),MaterialID(10),0,-10,0)

Repeat     
  WindowEvent()
  ExamineMouse()
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Space):action=0:EndIf
  If KeyboardReleased(#PB_Key_F2):dis=(dis+1)%3:vdis=2<<dis*5:EndIf
  If KeyboardReleased(#PB_Key_F1):EntityAngularFactor(0,0,0,0):EndIf
  RotateEntity(0,0.1,0.2,0.3,#PB_Relative)
  yrot.f+0.5:MoveCamera(0,Cos(yrot/180*#PI)*40,10,Sin(yrot/180*#PI)*40,0)
  CameraLookAt(0,EntityX(0),EntityY(0),EntityZ(0))
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Post Reply