Sign in to follow this  

DirectX8 - Transparacy to both sides of a polygon.

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My polygons are transparent on one side but when it rotates 180 degrees to the other side, the area of the textured polygon that's supposed to be transparent is the same color as the background that's being cleared out, and not transparent. Is there a setting I'm leaving out?

Share this post


Link to post
Share on other sites
No. It is viewed on both sides already. I already was using D3DCULL_NONE for two sided polygons. My problem is that it is transparent on one side, but not on the other. I want both sides transparent. You can still see the texture on both sides. Just that the Area that is supposed to be transparent is the same color as the clearing color. Like if you clear the backbuffer with red, the transparency for the opposite side is red, rather than being transparent. Like I said, the texture already is viewed on both sides. I know I must be missing a setting.

[Edited by - Jacob Roman on October 2, 2004 5:50:26 PM]

Share this post


Link to post
Share on other sites
Okay, just so I understand: your using a color key, as in billboards? On noe side that color is "replaced" by 100% transparancy, on the other side you still see that color (and no transparancy)?

Share this post


Link to post
Share on other sites
Draw the transparent objects last, back to front. What you're seeing is the result of

1) The transparent object drawn first fills the Z buffer.
2a) The objects behind it, drawn after, are not visible because they fail the Z test.
2b) The second half of the object isn't drawn because the first half is drawn in front first, so the second half fails the Z test.

To aid with areas that are solid or invisible you can use alpha test, which will skip writing to Z and color if it fails. For areas with n% transparency you MUST follow a strict back to front order for blending to work correctly. Solid objects are often drawn first sorted by texture/effect. Transparent objects are then drawn in Z order.

Share this post


Link to post
Share on other sites
They are already in back to front order. But it could be a setting I'm missing. Here is what I have as an example:



'Here are my 3D settings

Public Sub Direct3D8_Initialize(Window As Long)

If Direct3D8.CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Direct3D_Display_Mode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16) = D3D_OK Then

Direct3D_Window.AutoDepthStencilFormat = D3DFMT_D16
Direct3D_Window.EnableAutoDepthStencil = 1

End If

If Direct3D_Window.BackBufferFormat <> COLOR_DEPTH_UNKNOWN And Direct3D_Window.BackBufferFormat <> COLOR_DEPTH_8_BIT Then

Set Direct3D_Device = Direct3D8.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)

Else

MsgBox "The color depth needs to be greater than 8 bit color mode (greater than 256 colors.)", vbCritical

End If

Direct3D_Device.SetVertexShader FVF_LIT_VERTEX

Direct3D_Device.SetRenderState D3DRS_ZENABLE, 1

Direct3D_Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE

Direct3D_Device.SetRenderState D3DRS_LIGHTING, 0

Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA

Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA

Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR

Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR

Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, 1

D3DXMatrixIdentity World_Transformation_Matrix
Direct3D_Device.SetTransform D3DTS_WORLD, World_Transformation_Matrix 'commit this matrix to the device

D3DXMatrixLookAtLH Camera_Transformation_Matrix, Create_Vector(0, 0, -20), Create_Vector(0, 0, 0), Create_Vector(0, 1, 0)
Direct3D_Device.SetTransform D3DTS_VIEW, Camera_Transformation_Matrix

D3DXMatrixPerspectiveFovLH Perspective_Transformation_Matrix, PI / 4, 3 / 4, 0.1, 1000
Direct3D_Device.SetTransform D3DTS_PROJECTION, Perspective_Transformation_Matrix

End Sub

'There could possibly be a problem in here too where it
'loads the texture for all I know.

Public Sub Direct3D8_Load_Texture(Texture As Direct3DTexture8, File_Path As String, Width As Long, Height As Long, Transparent_Color As Long)

Set Texture = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, File_Path, Width, Height, _
D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, _
D3DPOOL_MANAGED, D3DX_FILTER_POINT, _
D3DX_FILTER_POINT, Transparent_Color, _
ByVal 0, ByVal 0)

End Sub

'This is where it creates it:

Public Sub Create_Object()

Dim Number_Of_Vertices As Long

Vertex_List(0) = Create_Lit_Vertex(-5, 5, -5, RGB(255, 255, 255), 0, 0, 0)
Vertex_List(1) = Create_Lit_Vertex(5, 5, -5, RGB(255, 255, 255), 0, 1, 0)
Vertex_List(2) = Create_Lit_Vertex(5, -5, -5, RGB(255, 255, 255), 0, 1, 1)
Vertex_List(3) = Create_Lit_Vertex(-5, 5, -5, RGB(255, 255, 255), 0, 0, 0)
Vertex_List(4) = Create_Lit_Vertex(5, -5, -5, RGB(255, 255, 255), 0, 1, 1)
Vertex_List(5) = Create_Lit_Vertex(-5, -5, -5, RGB(255, 255, 255), 0, 0, 1)

Number_Of_Vertices = 6

Set Vertex_Buffer = Direct3D_Device.CreateVertexBuffer(Len(Vertex_List(0)) * Number_Of_Vertices, 0, FVF_LIT_VERTEX, D3DPOOL_MANAGED)

If Vertex_Buffer Is Nothing Then Exit Sub

D3DVertexBuffer8SetData Vertex_Buffer, 0, Len(Vertex_List(0)) * Number_Of_Vertices, 0, Vertex_List(0)

Vertex_List2(0) = Create_Lit_Vertex(-5, 5, 1, RGB(255, 255, 255), 0, 0, 0)
Vertex_List2(1) = Create_Lit_Vertex(5, 5, 1, RGB(255, 255, 255), 0, 1, 0)
Vertex_List2(2) = Create_Lit_Vertex(-5, -5, 1, RGB(255, 255, 255), 0, 0, 1)
Vertex_List2(3) = Create_Lit_Vertex(5, 5, 1, RGB(255, 255, 255), 0, 1, 0)
Vertex_List2(4) = Create_Lit_Vertex(5, -5, 1, RGB(255, 255, 255), 0, 1, 1)
Vertex_List2(5) = Create_Lit_Vertex(-5, -5, 1, RGB(255, 255, 255), 0, 0, 1)

Number_Of_Vertices = 6

Set Vertex_Buffer2 = Direct3D_Device.CreateVertexBuffer(Len(Vertex_List2(0)) * Number_Of_Vertices, 0, FVF_LIT_VERTEX, D3DPOOL_MANAGED)

If Vertex_Buffer2 Is Nothing Then Exit Sub

D3DVertexBuffer8SetData Vertex_Buffer2, 0, Len(Vertex_List2(0)) * Number_Of_Vertices, 0, Vertex_List2(0)

End Sub

'Here is where it draws it. I have 2 polygons as an example
'and as it rotates, it has one side transparent, other side
'not, on both polygons.

Public Sub Draw_Object()

'Note: Must be used in between DirectX_Device.BeginScene and DirectX_Device.EndScene

Direct3D_Device.SetVertexShader FVF_LIT_VERTEX

Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, 1

Direct3D_Device.SetStreamSource 0, Vertex_Buffer, Len(Vertex_List(0))

Direct3D_Device.SetTexture 0, Sprite_Texture(0)

Direct3D_Device.DrawPrimitive D3DPT_TRIANGLELIST, 0, 2

'-----------------------------------------------------------


Direct3D_Device.SetVertexShader FVF_LIT_VERTEX

Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, 1

Direct3D_Device.SetStreamSource 0, Vertex_Buffer2, Len(Vertex_List2(0))

Direct3D_Device.SetTexture 0, Sprite_Texture(2)

Direct3D_Device.DrawPrimitive D3DPT_TRIANGLELIST, 0, 2

End Sub








If I'm missing some crucial code needed please let me know.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jacob Roman
Like if you clear the backbuffer with red, the transparency for the opposite side is red, rather than being transparent. Like I said, the texture already is viewed on both sides. I know I must be missing a setting.

Ok, so if you don't expect to see the red, what do you expect to see? You say you're expecting to see 100% transparent, but what do you mean by that? Are there other meshes? Are you expecting to see the other side of this mesh? What? Perhaps a screenshot would help.

My money is still on the wrong draw order. If it's the other side of the same mesh you're expecting to see, note that you can't guarantee the correct draw order without breaking the mesh into quadrants, or sorting the faces individually. If you don't try to stop it, whichever part of the mesh that's drawn first is drawn first always, whether it's the furthest part or closest part of the mesh isn't D3D's problem.

Share this post


Link to post
Share on other sites
Polygon Quad #1 is at Z --> -5 and drawn first in the Draw_Object() Sub. Here are the coodinates:


Vertex_List(0) = Create_Lit_Vertex(-5, 5, -5, RGB(255, 255, 255), 0, 0, 0)
Vertex_List(1) = Create_Lit_Vertex(5, 5, -5, RGB(255, 255, 255), 0, 1, 0)
Vertex_List(2) = Create_Lit_Vertex(5, -5, -5, RGB(255, 255, 255), 0, 1, 1)
Vertex_List(3) = Create_Lit_Vertex(-5, 5, -5, RGB(255, 255, 255), 0, 0, 0)
Vertex_List(4) = Create_Lit_Vertex(5, -5, -5, RGB(255, 255, 255), 0, 1, 1)
Vertex_List(5) = Create_Lit_Vertex(-5, -5, -5, RGB(255, 255, 255), 0, 0, 1)





Polygon Quad #2 is at Z --> 5 and drawn last in the Draw_Object() Sub. Here are the coodinates:



Vertex_List3(0) = Create_Lit_Vertex(-5, 5, 5, RGB(255, 255, 255), 0, 0, 0)
Vertex_List3(1) = Create_Lit_Vertex(5, 5, 5, RGB(255, 255, 255), 0, 1, 0)
Vertex_List3(2) = Create_Lit_Vertex(-5, -5, 5, RGB(255, 255, 255), 0, 0, 1)
Vertex_List3(3) = Create_Lit_Vertex(5, 5, 5, RGB(255, 255, 255), 0, 1, 0)
Vertex_List3(4) = Create_Lit_Vertex(5, -5, 5, RGB(255, 255, 255), 0, 1, 1)
Vertex_List3(5) = Create_Lit_Vertex(-5, -5, 5, RGB(255, 255, 255), 0, 0, 1)






So back to front doesn't seem to be the problem here. If I were to reverse it, it would still be the same problem. One side transparent, other side not, only reversed. I only have two textures in this example. One for one polygon, and another for the other. That "red" color was just an example color. It's actually black in my program. But the transparency is 0% on one side of my polygons and changing the area that's supposed to be transparent the same color as my clear color. If it didn't affect that opposite side at all, the picture's backcolor in the bitmap file that you want transparent, let's say black, would be black in the program. But it's not. In my program with an example clearing color of red, has that opposite side's transparent area with red with 0% transparency! So it's affecting that opposite side in someway. Just not what I want. I want 100% transparency on both sides. It could be the Z buffer too for all I know, but I don't see how it would affect the transparency colors other than back to front order issues, which isn't really the case here. And yes, a billboard effect is a good example of what I'm talking about, except the only difference is I want mine to be 3D, not stay 2D. If staying 2D where the case, I wouldn't have any problems with the transparency. I'll try to get some screenshots going when I can. What would help me more is if I see an example program written in VB that's based on transparency to help me find out where I went wrong.

[Edited by - Jacob Roman on October 2, 2004 7:35:46 PM]

Share this post


Link to post
Share on other sites
How are you viewing the other side of the objects? Are you rotating the meshes, or rotating the camera around the scene? For the sake of completeness, I'm going to try to describe how moving the camera requires changing the render order. Just because A is at Z=-5 and B is at Z = 5 doesn't mean you always render B then A. It depends on the Z in camera space.

C = Camera, A = Object1 B = Object2. Caps just help show orientation.


Top down, map style view of scene.

Rotate quads, leave camera alone... same Z order

A B
C a b
a b

becomes

a b
C a b
A B

or, rotate camera, leaving quads alone... Z order changes.

A B
C a b
a b

becomes

A B
a b C
a b


Now you have A with Z = -5, and B with Z = 5... so you're drawing B then A... however, if your doing the camera move, in the second position A is now the more distant object in camera space, so you need to draw A then B... however when the camera moves back to the first position you need to draw B then A. It all depends on where the objects are relative to the camera, sorted at runtime.

You might be doing this. Maybe I'm describing things overly simplistic for you. I don't know your skill level. You haven't described what you're doing in enough detail to know whether you're sorting by camera space Z, how your objects are interacting, etc. The reason I'm persisting in the Z buffer problem is because it's the simplest thing that produces this error and you haven't yet convinced me that it's not the problem.

Your render states seem fine, and other than that render order is about the only thing you have to worry about.

When dealing with complex meshes, the render order of the faces of the mesh comes into play, and this is one of the most annoying parts of 3D graphics. When dealing with simple meshes like quads, you just have to sort them in camera space. If you have a particle system, with overlapping, intermixing faces, you're pretty much screwed. You can do things like additive blending, but regular alpha blending math just won't work unless you split the individual particles where two particles intersect, and sort all the fragments of quads you end up with. It's a complex mess, which no game ever tries to deal with.

Share this post


Link to post
Share on other sites
My skill level is probably intermediate, at least in DirectX. As for programming in general. I've been programming for about 13 years since I was 10. Started on Amiga BASIC. Then messed around on AppleIIc BASIC in middle school on this computer that was in one of my classes. Then in high school, I chose Computer Programming as an elective, which had Visual Basic 4.0 Professional Edition. Got VB 5.0 Learning Edition and Borland C++ that same year for Christmas and used it ever since. So I've been working in VB for 8 years. Also in my Senior year of High School, a classmate of mine gave me free burned copies of Visual Studio 5.0 Enterprise Edition. So I have little experience in C++ and a lot of experience in VB. I'm also in my 2nd term of college majoring in Computer Programming and Analysis. VB.Net I'm not really caring for, but it's different, and I'm leanring something. Valencia Community College also plans on having a Game Design degree program next term, so I plan on switching my degree to that. I'm 23 now just to let you know.

This game I'm makin is under a building stage since I just started on it like a month or two ago. I have quite a bit of experience in DirectX, but I'm still learning. I used to use DirectX7 a lot, for 2D stuff. But since DirectX8 no longer supports DirectDraw, I'm forced to learn their 3D. But as I learned more and more on it, I got into DirectX8. After working with DirectX8 for over a year, I'm stuck with this problem, which was a problem to me before I even began to write this game. Just to let you know, the camera is in a static position. Only the Local coordinates of the object is being rotated. Or in DX8 terms, World rather than Local. The object rotates, and shows both polygons transparent on only one side. All I have now are these two polygons displayed in an test sub until I can solve this problem. Then I can add the solution to my engine. I'm taking steps one at a time till I can reach my goal. So everytime I run into a problem, I setup an example of my problem till my problem is solved. That way there I'm not confused. I like to make things easy for myself. I'm now stumpted on my own example program, which is why I posted this problem here to see if you guys can figure it out.

[Edited by - Jacob Roman on October 2, 2004 11:20:50 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Namethatnobodyelsetook
When dealing with simple meshes like quads, you just have to sort them in camera space. If you have a particle system, with overlapping, intermixing faces, you're pretty much screwed. You can do things like additive blending, but regular alpha blending math just won't work unless you split the individual particles where two particles intersect, and sort all the fragments of quads you end up with. It's a complex mess, which no game ever tries to deal with.


?!?!

or leave z-enable on, and turn z-writing - no sorting, no splitting, everything transparent is rendered perfectly; this should be done after all opaque objects. Go ahead, try it, set the destination to D3DBLEND_ONE and source to D3DBLEND_SRCALPHA for best results. And you don't sort the polygons, you just have to do it in a rough front to back order by objects such as models, level data, and skybox - which is last (it's fillrate intensive if done at the beginning of each frame... wastes more ms than you think on high resolution). By the way, the processor is what slows down a game, very rarely is it the modern graphics card.. times have changed.

And Jacob, it's better in this case to just render 2 triangles, one facing "back", one facing "forward". If you add lighting, then the normals can only face one way anyways! Not that this even matters, 3D objects are "skinned" with polygons unless you are meant to go through them (not sure as to why). Changing renderstates is a bad habit, but of course it has to be done every now and then, but not this case, state changes are pretty expensive if called a lot - repetitive ones are ignored by D3D by the way. Changing the stream source is even worse, one vertex buffer is better than dozens. Having 2-3 buffers for minimal vertex sizes is even better, don't send useless junk such as normals if you don't plan on using them, of course you need thousands or millions of triangles until you can see a difference. You're pretty new to this and it doesn't really matter as long as it works in the end, and you're right, it really doesn't. Just keep it up and have some fun.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
Quote:
Original post by Namethatnobodyelsetook
When dealing with simple meshes like quads, you just have to sort them in camera space. If you have a particle system, with overlapping, intermixing faces, you're pretty much screwed. You can do things like additive blending, but regular alpha blending math just won't work unless you split the individual particles where two particles intersect, and sort all the fragments of quads you end up with. It's a complex mess, which no game ever tries to deal with.

or leave z-enable on, and turn z-writing - no sorting, no splitting, everything transparent is rendered perfectly; this should be done after all opaque objects. Go ahead, try it, set the destination to D3DBLEND_ONE and source to D3DBLEND_SRCALPHA for best results.

ONE, SRCALPHA is an additive blend. Please READ what you quoted. I say it's fine for additive blending. For standard blending it's not fine... standard blending the one that looks like SRCALPHA, INVSRCALPHA. Take two quads, overlap then in a X shape. Now get it to render correctly with SRCALPHA and INVSRCALPHA.

Share this post


Link to post
Share on other sites
It has definitely got to be a certain combination of how it's blended. The majority of the DirectX8 tutorials around the internet has this:

D3DDevice.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
D3DDevice.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, True

Which is what I have for transparency and is basic for a beginner at DirectX8, only I'm no beginner at it. To alphablend, the tutorials usually have this:

D3DDevice.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
D3DDevice.SetRenderState D3DRS_DESTBLEND, D3DBLEND_ONE
D3DDevice.SetRenderState D3DRS_ALPHABLENDENABLE, True

Only thing is...it alphablends only on one side on my polygons, and on the other side, it has the same changed color as the first side, only it didn't alphablend. I'm missing a setting. I tried all different kinds of combinations with the constants that I'd think would do it. Note: with the same exact settings I have for DirectX7, it actually does make both sides transparent. Something is up in DirectX8. I don't know.

Share this post


Link to post
Share on other sites

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this