Loading Alpha Textures?


Job Open: Blitz3D/C++ DLL RPLidar Programming. View Job Posting
(Posted 1 year ago) RonTek

Hey Mark, I'm having trouble testing out the examples with alpha textures.. Checking out the latest examples, do you need a separate alpha texture file now for that? It seems the LoadAlphaTexture is not available anymore so how do you simply load and apply a texture to a model. I have tried the 1+2 flags on LoadTexture with an alpha png image, but it does not seem to work.

(Posted 1 year ago)

Yeah this is a bug with surface brushes I think it was working so I'll see if I can backtrace it. LoadAlphaTexture was replaced by ApplyAlpha in LoadTexture from Minib3d.

(Posted 1 year ago)

This should be fixed now, it was an issue with LoadTexture (Bmx version).

Long explanation: Openb3d uses it's own STBimageloader and Openb3dmax has it's own version of this, you can actually use that instead of the NG STBimageloader which currently crashes on TGAs. A change to the STB loader caused this no alpha issue, specifically with BMPs which have no alpha channel. LoadTexture assumes an alpha channel exists if it's in RGBA but the STB loader converts RGB to RGBA as I had issues unless I did this. So I added a CheckAlpha to LoadTexture which quickly checks a sqrt amount of pixels to see if there are any valid alpha values in the pixmap, this was a workaround for Assimp as it ignores tex flags, so now it is used as a standard check for ApplyAlpha. It's a bit of a hack but it works.

This leads to different alpha behavior than in Minib3d, if you look at standard/alphamap you'll see tex1 isn't setting tex flag 2 in LoadTexture, if you change it you get quite different results, the black background will be transparent but disappears when alpha flag is set. This won't happen in Minib3d but is actually correct behavior.

(Posted 1 year ago)

I had another look at the STBimageloader to see if it's possible to create RGB images which are then converted to RGBA in LoadTexture but the problem is uncompressed BMP in 32-bit, these return the wrong number of channels from stbi_info_from_memory 3 not 4, so it's necessary to force to RGBA in this case. LoadPixmap doesn't get the url so it can't tell the format either.

I've also just fixed a bug in the B3D loader, there was no transparency for brush alpha textures if brush fx 32 isn't in the B3D file but tex flag 2 is. You can see an example in the roof windows of RomanBath in standard/lightmap.

(Posted 1 year ago)

Hey nice one! Thanks, will try the fix asap. I also noticed with the romanbath lightmap example, there is only this unlit scene without lightmapping effect. Is that supposed to happen? ..will download and check this again.

(Posted 1 year ago)

No, the roman bath should be lightmapped unless loaded by Assimp which doesn't support texture layers. You can specify a loader with MeshLoader "bmx", "cpp" or "assimp".

(Posted 1 year ago)

Ok I see. Here's the part that I have modified from the example, tried both cpp and bmx but apparently still no lightmaps

' load anim mesh
Local loader%=1
Select loader
    Case 1 ' load stream mesh
        TextureLoader "cpp"
        MeshLoader "cpp"

        oldtime=MilliSecs()
        mesh=LoadAnimMesh("../media/bath/RomanBath.b3d")

        debug="b3d time="+(MilliSecs()-oldtime)

I'm also getting this message from the editor console instead even if these files are loaded and already in the /bath folder with the RombanBath.b3d file. I see Default Lightmap.tga in there though and not included in this error message.

perhaps there is a problem how the file is loaded, being the odd one out, space instead of underscore?

(Posted 1 year ago)

For the Alpha textures, my results also shows alphablending (force),

EntityFX(sprite,32)

which works great when I followed the example, but I'm trying to do alpha cutout (not masked) just like what you apply on tree leaves or branch textures.

LoadTexture("../media/branches.png",1+2) 'this does not work
(Posted 1 year ago)

I have extracted the Palm tree from the romanbath model to quickly test alpha textures..

https://www.dropbox.com/s/kbdqs453lmbmquy/palmtree.zip?dl=0

setting the alpha flags on the model (fragmotion) works ok with only Meshloader bmx, but through code somehow it fails at my end..

OpenB3D Alpha Textures - BlitzCoder

loadmeshalpa.bmx

Strict

Framework Openb3dmax.B3dglgraphics

Graphics3D 640,480,0,2

Local Camera:TCamera=CreateCamera() 
CameraClsColor Camera,100,150,200
CameraRange Camera,1,5000
Local Light:TLight=CreateLight() 
RotateEntity Light,90,0,0 

MeshLoader "bmx"

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

'through code
'Local treetex:TTexture=LoadTexture("../media/bath/RB_Palm00.tga",1+2)
'EntityTexture tree,treetex

PositionEntity tree,0,-5,10

While Not KeyDown( KEY_ESCAPE ) 

If KeyDown(KEY_W) Then MoveEntity Camera,0,0,1
If KeyDown(KEY_A) Then TurnEntity Camera,0,4,0
If KeyDown(KEY_S) Then MoveEntity Camera,0,0,-1
If KeyDown(KEY_D) Then TurnEntity Camera,0,-4,0

RenderWorld 
Flip 
Wend 

End 
(Posted 1 year ago)

Hi Ron,

I've uploaded a few fixes, the "can't find document file" error was being called when the file was found instead of when it wasn't found. Then I added CheckAlpha to the cpp so now it won't ApplyAlpha if it has alpha already, and I added the cpp B3D transparency fx for alpha textures (as in the roman bath roof window).

I doubt the lightmap bug is due to the space in the filename as it works fine on my rig, this bug was due to the bath lightmap being stored in texture layer 7, I fixed it by correcting code that only checked in loops with < 7 instead of <= 7, in the latest version I added a new debug to show the texture layer assigned, enable TGlobal.Log_B3D and you should see this, showing tex0 and tex9 are stored in brush1 in layer0 and layer7:

DebugLog:TEXS file=RB_Marble00.tga flags=9 blend=2 tex_no=0
...
DebugLog:TEXS file=Default Lightmap.bmp flags=65537 blend=2 tex_no=9
...
DebugLog:BRUS name=1 - Default blend=1 fx=1
DebugLog:BRUS brush_no=1 b_tex_id=0 ix=0
DebugLog:BRUS brush_no=1 b_tex_id=9 ix=7

I tried your palm.b3d and it loaded fine when I assigned the texture manually. What kind of fail do you get? Does it crash, is there an error message? I did get a crash recently with the cpp loader so there's certainly something wrong there.

(Posted 1 year ago)

Ah yes, it just fails without the alpha effect. I did not experience any crashes so far though. Thanks, I got to test the latest version, but still fails with the RomanBath lightmap loading and the alpha. It is weird that the other lightmapped level works. Here is the debug log which looks like the same as you posted there. https://pastebin.com/raw/ixm6zNES

It does work if the texture alpha flag is already set in the b3d file and set it again through code. What you can try with the Palm tree model is remove the textures or flags from the file and try to load it again using code only. This is what I'm trying to do if possible. For a quick test, here is the Palm tree model now with the textures/flags removed.

https://www.dropbox.com/s/rse8ll8cshzpnlk/palmtree.b3d?dl=0

(Posted 1 year ago)

With the lightmap could you please try loading it manually and texturing a cube to see if that works, if not then it's a LoadTexture bug. It may be something wrong with the new CheckAlpha function, so you could try removing "Or mask=4" in LoadAnimTexture:

If (flags & 2) And (alpha_present=False Or mask=4) Then tex.pixmap=ApplyAlpha(tex.pixmap)

Also try loading the roman bath in Minib3d, the roof window will not have alpha but it should be lightmapped.

With the alpha you need to also set the correct surface brush or entity fx to 32.

(Posted 1 year ago)

Hey Mark, Ok I did some further testing and one thing I have noticed that it works with models without submeshes (test.b3d) and that is with both direct and manual entity texture. I'm not sure if that is related to this issue and this is what I can test so far.

(Posted 1 year ago)

Hi Ron,

thanks for the help on this! I've added some DebugLogs to the latest Openb3dmax which might help, there's a "No pixmap texture loaded" message which infers an image loader has failed. You need to enable the debug with TGlobal.Log_Texture.

It may also be a bad pointer issue.

(Posted 1 year ago)

Thanks Mark! It would be nice to have if you could squeeze in and include a direct command example as well particularly for these base blitz3d type features.

(Posted 1 year ago)

Hi Ron,

I don't quite follow what you mean, I am planning on porting a lot of the Blitz3d command examples, what command did you mean?

If you could run the standard/lightmap example with TGlobal.Log_Texture=1 and let me know if you get a "No pixmap texture loaded" message, that would rule out if the image loader has failed.

(Posted 1 year ago)

Hey Mark, ok here's the debug log with the latest version.

https://pastebin.com/XhBYkPnC

Another thing that I have noticed with the last few changes is the z-order of the palm trees..

Ah yes, I meant about using them as simple as possible just like on the command examples. As an example, something like this to test out lightmap textures manually and perhaps a switch to test with directly as well..

With alpha textures, I cannot get a working result with a simple alpha cutout texture without using the 32 flag (force alpha blending) which blends in the background. Unless I'm missing something or it is still not implemented, it should be just a smoother cutout version like alpha masking which works great. The 1+2 flag seems to not work with the latest version. As I have mentioned, a simple example equivalent of the one below would help and test out this base blitz3d feature.

Thanks

(Posted 1 year ago)

On a side note, I think the alpha texture problem should be now considered as a small issue since openb3d is a modern engine and already has nice shader support (and now with GLES2? for portability) that I think should need to take advantage of moving forward. I would pick the shader method anytime since it is much simpler and easily does not have the additional Z-Order Rendering issue that exist on the blitz3d alpha counterpart like the above blitz3d screenshot that I have posted.

Remade the Palm texture to clearly show the alpha (discard) effect..

diffusealpha.frag.glsl


uniform sampler2D texture0;
float cutoff = 0.5;

void main() {

    vec4 color = texture2D(texture0, gl_TexCoord[0].st);
    if (color.a < cutoff)
    {
        discard;
    }   
    gl_FragColor = color;

}
(Posted 1 year ago)

Hi Ron,

thanks, that was helpful, I've a new commit which should fix the z-order issue with palm trees, it seems the lightmap trouble is in the B3D loader but looking at your debuglog it doesn't seem there's anything wrong, because it's also not working with MeshLoader "cpp" then it's probably a CPP bug. I've added a few more debugs to the B3D loader (which might help so could you post the roman bath output again).

About alpha textures, you must set EntityFX to 32 for alpha flag 2 to work, this isn't needed for mask flag 4, this is the way Minib3d works, I'm not sure what B3D does, I'll have to check.

Thanks for the alpha discard example, I've added a new function AlphaDiscard so this can be now be done by the fixed pipeline, it does look better than a masked texture. So here's the code, you've to use NewTexture, set the discard then pass it to LoadTexture:

Strict

Framework Openb3dmax.B3dglgraphics
'Framework sidesign.minib3d

Graphics3D 640,480,0,2

Local Camera:TCamera=CreateCamera() 
CameraClsColor Camera,100,150,200
CameraRange Camera,1,5000
Local Light:TLight=CreateLight() 
RotateEntity Light,90,0,0 

'MeshLoader "cpp"
'TextureLoader "cpp"

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

'through code
'Local treetex:TTexture=LoadTexture("media/RB_Palm00.tga",1+2)
Local treetex:TTexture=NewTexture()
treetex.AlphaDiscard(0.01)
LoadTexture("media/RB_Palm00.tga",1+2,treetex)

EntityTexture tree,treetex
EntityFX tree,32

PositionEntity tree,0,-5,10

While Not KeyDown( KEY_ESCAPE ) 

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

    RenderWorld 
    Flip 
Wend
End
(Posted 1 year ago)

Edit: It could be a bug in rendering the 2nd uv set, however everything seems fine in the render code and when I set the 2nd uv set to 0 the roman bath goes black, so it seems more likely the texture is not getting assigned or stored right.

Btw, what graphics card do you have Ron, Nvidia? Opengl 3?

(Posted 1 year ago)

Hey Mark, Thanks for the update! and yes it does seem to not load when using models with submeshes, single mesh works fine. I have a nvidia card with up to gl 4 support.

The new function is cool, but perhaps and would it be possible that you can still just use the same command and make the discard a new parameter which has a default say 0.5?

Local treetex:TTexture=LoadTexture("media/RB_Palm00.tga",1+2) //defaults to 0.5

Local treetex:TTexture=LoadTexture("media/RB_Palm00.tga",1+2, 0.1) //added parameter to specify alpha flag

Hope this makes sense. cheers.

(Posted 1 year ago)

Hi Ron,

yes, I'm not sure why it doesn't work with child meshes, I'll put a version up with some more debugs, I do need a new laptop anyway so I could wait until then. It may be an opengl deprecated issue, if the roman bath lightmap loads in Minib3d then it's not opengl.

LoadTexture is a legacy command so I don't want to change it, mainly because it's easier to keep track of new features by adding a new function but also it's less confusing to users, new features are also in the right place. Also, the default has to be 1 because that's all alpha values. I'll add an example for new features.

(Posted 1 year ago)

Ok thanks Mark. I'm getting the same result on submeshes with MiniB3D btw (not loaded and applied), no problem though with single mesh lightmapped models.

(Posted 1 year ago)

Oh that's great, that means it's not an Openb3d bug, I'll see if I can find another lightmap model to test with. Thanks!

(Posted 1 year ago)

Ah good to know! I did not notice that and it's now confirmed that this was never ported over to any minib3d version on the BlitzMax side. It will be the first with OpenB3D if this gets through!

(Posted 1 year ago)

update: while checking out warner's minib3d fork, seems like he got this fixed with multiple meshes w/ lightmap textures working on his version, see Mesh.bmx example and that also uses the RomanBath.b3d scene.

(Posted 1 year ago)

Cheers, that'll be something to look at then. Just to point out, Warner isn't a fork of Minib3d, it's quite different internally.

(Posted 1 year ago)

That's great Mark! Ah yes missed that one.. and now I know why with some functions like collisions are totally different from blitz3d standards.

Reply To Topic

Please log in to reply