B3D: text on transparent texture and ?z-fighting?

Blitz3D Forums/Blitz3D Programming/B3D: text on transparent texture and ?z-fighting?

mearrin69(Posted 2012) [#1]
Hi all! It's been a while but I used to frequent these boards many moons ago. I've done a fair bit of 2D programming with Blitz but just a little 3D. I'm working on an app and having a little trouble on a couple of points. Would love some help from you B3D experts!

The app is pretty simple and is designed to help game masters of the old Traveller role-playing game visualize subsector maps...a region of space measuring 8 hexes by 10 hexes. Each hex may contain a world and some other things. If you're interested in some screenshots, I've been posting about it on the Cartographer's Guild here.

The problems I'm having are these:
1) Each world may consist of up to four concentric spheres, each a little larger than the others.

The innermost is textured with a map of the world's surface, no alpha. The next is a sphere with an alpha-mapped texture showing the world's oceans. It's got a 'shininess' factor if that matters. The next sphere is an alpha-mapped texture with clouds. The final sphere is just a colored sphere with an alpha value, representing the atmosphere.

It looks great sitting still but goes randomly haywire when the camera is moved, with layers randomly disappearing or rendering in front of layers they should be behind.

I've tried increasing the distance between the spheres, differing bit depths, 'Wbuffer', etc. to no avail. Is this just a problem with B3D or DX?8? or some other known issue? My graphics card is an NVIDIA GeForce GTX 260 if that helps at all.

2) The hex grid is an alpha-masked texture on a square polygon (two tris) and looks great. I want to write the names of the four neighboring subsectors on each edge of the grid. My plan was to do this with smaller square (or preferably rectangular) polygons with a masked or alpha texture containing the text. It needs to be done at run-time, as the names will change depending on the loaded file, so I can't use pre-generated texture maps.

I've tried to do this in a number of different ways: creating a texture and writing text directly to it, writing text to the backbuffer and copying that to a texture, writing text to an imagebuffer and copying that. I can indeed get a black polygon with white text to appear. When I try to add any flags to the CreateTexture command, however, I just get a transparent texture.

Here's my current code:
	; ------------------------------------
	; Create subsector name entities
	; ------------------------------------
	; Create texture with placeholder text
	imgTmp = CreateImage(1024, 1024)
	SetBuffer ImageBuffer(imgTmp)
	Color 255, 255, 255
	SetFont iFNTSSNames
	Text 512 - StringWidth("Test") / 2, 512 - StringHeight("Test") / 2, "Test", True, True
	texCoreward = CreateTexture(1024, 1024)
	CopyRect 0, 0, 1024, 1024, 0, 0, ImageBuffer(imgTmp), TextureBuffer(texCoreward)	
	FreeImage imgTmp
	; Create mesh
	entCoreward = CreateMesh()
	surf = CreateSurface(entCoreward)
	v0 = AddVertex(surf, -10, 0.1, 10, 0, 0)
	v1 = AddVertex(surf, -10, 0.1, -10, 0, 1)
	v2 = AddVertex(surf, 10, 0.1, -10, 1, 1)
	v3 = AddVertex(surf, 10, 0.1, 10, 1, 0)
	t0 = AddTriangle(surf, v0, v2, v1)
	t1 = AddTriangle(surf, v0, v3, v2)
	EntityTexture entCoreward, texCoreward
	EntityFX entCoreward, 1


This code lives in a function that sets up my other entities (space sphere, grid object, etc.) and entCoreward and texCoreward are global handles (ints) defined during set-up. I've tried CreateTexture with both the 2 and 4 flags. The code as written above generates a black square with a white "Test" centered in it.

Any help you guys can give with these problems will be much appreciated.

Also, if anyone can point me to their favorite frame-limiting code I'd love to check it out. Not sure how to implement this myself but some of the ones I've looked at have seemed a bit complex. I don't need anything fancy or heavy-duty...it's not an FPS or anything! :)

Thanks in advance!
M


Yasha(Posted 2012) [#2]
By the sound of it it's a combination of DX7 and B3D issues (the B3D side being workarounds for DX7's shortcomings).

1) DX7 can't Z-sort alpha-ed polygons at all. It can sort polygons with a masked texture well enough, because they still have pixels that are either-there-or-not, but it can't sort alpha.

2) Because of this, B3D draws alpha'd surfaces - either textured with alpha, or with an alpha brush effect - in a second pass after the solid stuff. Within each surface, the polygons are not sorted, just drawn in order; so any alpha surface that isn't perfectly convex relative to the camera will show weird sorting artifacts in its internal polygons. The entities themselves are ordered strictly by entity distance from the camera - a mesh with its root near the camera and all the polygons distantly offset may be drawn over the top of a mesh whose polygons are nearer the camera. (Alpha'd objects are still Z-ordered correctly against the solid world, but can't "contribute" to the Z-buffer during their drawing pass.)

By the sound of it, your cloud and atmosphere layers are going to be centred on the planet, i.e. in the same place. Therefore, B3D is likely unable to tell which one should be rendered first, even though this "should" be obvious because one is smaller than the other; the distance between polygons is having no effect, because Z-buffering isn't being used to determine draw order here.

Solutions... I guess you could offset the atmosphere layers slightly from the planet centre so that they're "ordered" from the camera's perspective? If you just move them one or two units towards the camera, it shouldn't make a perceptible difference (remember to update this every frame if the camera's position changes relative to the planet), but it might solve the ordering problem.

Last edited 2012


Captain Wicker (crazy hillbilly)(Posted 2012) [#3]
B3D or DX?8?

BB3D is DirectX/Direct3D 7.0 based.
I just get a transparent texture.

What are you adding to the texture?

EDIT: Looks like he beat me to the punch! :P

Last edited 2012


mearrin69(Posted 2012) [#4]
Thanks for the speedy replies, guys. Much appreciated!

* polygon fighting - The off-center sphere concept sounds innovative and I might give that a go.

Does anybody know if DBPro has the same issues? I have that disc around here somewhere and could try to rewrite it in that if it would do a better job. I don't really care for the language but IIRC it has some other features I might put to use...

* text on textures - Seems that whenever I put *any* flags on my CreateTexture it just make the texture go away...i.e. the poly is completely transparent. Not sure what that's all about.

Again, appreciate the help!
M

Last edited 2012


Yasha(Posted 2012) [#5]
DBPro uses DX9, so I think it should be able to properly order alpha'd polygons. Beyond that, couldn't tell you, never used it.

How are you applying flags to CreateTexture?

(This bit may seem patronising but I don't know how much you know:) Flags are a kind of "bitfield", so when you pass them to a function like CreateTexture, you aren't necessarily choosing between e.g. flag 2 and flag 4. You can combine the flags into a single value that has all of the bits you want set (the flags value is treated by the function as an array of 32 bits, each one independently representing a different option). So you could call it like this:
texCoreward = CreateTexture(1024, 1024, 1 + 2 + 8 + 16)


...to request a texture that is both coloured and alpha'd, and mipmapped, and also clamped on the U axis.

Anyway, what this means is always request flag 1. Failing to do this can cause textures, especially alpha textures, to not work on some machines, and may be what is happening here.

Last edited 2012


mearrin69(Posted 2012) [#6]
Ah. I do know how the flag system works...but did not realize I should set flag 1 in all cases. I will try that. Thanks!
M


Guy Fawkes(Posted 2012) [#7]
Can someone tell me why Im not seeing the mesh?



Last edited 2012


PowerPC603(Posted 2012) [#8]
You can try to use EntityOrder to manually set the drawing order instead of letting DX7/Blitz try to figure it out itself what it should draw first.
EntityOrder PlanetSurface, 5
EntityOrder PlanetWater, 4
EntityOrder PlanetClouds, 3
EntityOrder PlanetAtmosphere, 2


Something like this, you can fiddle with the order-values to whatever suits best.

Last edited 2012


Guy Fawkes(Posted 2012) [#9]
That didn't work




mearrin69(Posted 2012) [#10]
I did try entity order but it didn't work because there are multiple planets that sometimes overlap each other...so, for instance, the cloud layer of a planet behind the one you're looking at will render over the one in front. I'll take another look at it, however. I suppose I could do some sort of sorting algorithm based on the camera location and assign entity orders to everything dynamically, but that seems very kludgy.
M


mearrin69(Posted 2012) [#11]
Odd. CreateTexture(x, y, 1) doesn't even work...even though the help file says 1 is the default. If I don't specify a flag at all I can see my polygon with white text on a black background. If I use 1 or any combination of flags it doesn't show up. Not sure what's happening there but I'll keep messing with it.
M

Edit: Any chance it has something to do with non-square textures?
Edit2: Tried it with a square texture/square polygon. I can get flags 1 + 8 to work, but still no transparency using either flag 2 or 4...I should say *total* transparency...nothing renders. I'm not getting what's wrong with it.

What if I did something crazy like saved the image I create to disc and then loaded it back in as a texture? Loading textures with flags seems to work fine, though I haven't tried masked, only alpha.

Last edited 2012


Yasha(Posted 2012) [#12]
Edit: Any chance it has something to do with non-square textures?


Advice is to not use non-square textures unless you have FastExtension turned on and are using the non-square texture extension. Blitz3D doesn't check and "correct" this as far as I can tell, so the result may be machine-dependent. If you have to use a non-square texture, make it square and move the UVs (consider sharing the texture's area with more than one mesh if this means a lot of wasted space).

Can you replicate this problem in a simple piece of demo code? It's starting to sound like legitimate bug-reporting time.


mearrin69(Posted 2012) [#13]
Maybe so. I ran a test using square textures and a square polygon. I created a temporary image with white text on a black background and then saved that to disc. I load it into my texture with the flags 1+2+8 or 1+4+8 and I get the desired effect: a poly with white text and transparent where the black should be.

Sure seems like a bug. Let me get these changes into my app and I'll try to replicate it with some stand-alone code and post the results for you guys to try. Maybe it's something to do with my hardware, not B3D.
M


Captain Wicker (crazy hillbilly)(Posted 2012) [#14]
Does anybody know if DBPro has the same issues?

no it does not but is slower on older graphics cards. :)


mearrin69(Posted 2012) [#15]
I've posted a download link for the app as it stands here if anyone wants to check it out.

Also posted on the Citizens of the Imperium forum. One of the people there tried it out on OSX/WINE and seems all of the images are flipped left-right. it's quite odd. Unless his screenshot is upside down then I guess it's also flipped top-bottom (and the logo gets skewed into italics). Anybody ever seen this before? Know how to fix it?
M