minib3d for FreeBasic

BlitzMax Forums/MiniB3D Module/minib3d for FreeBasic

Stenley(Posted 2010) [#1]
As I have written at
http://www.blitzbasic.com/Community/posts.php?topic=78110
I hoped, that anytime anyone will port minib3d to FreeBasic.

Yesterday I have red, that anybody have done it.
He ported it to C++ and creating headers for FreeBasic:
http://www.freebasic.net/forum/viewtopic.php?t=15409

http://www.freebasic.net/forum/viewtopic.php?t=16085
http://www.freebasic.net/forum/viewtopic.php?t=16132
http://www.freebasic.net/forum/viewtopic.php?t=16133
http://www.freebasic.net/forum/viewtopic.php?t=16767
http://www.freebasic.net/forum/viewtopic.php?t=16798
http://www.freebasic.net/forum/viewtopic.php?t=16833
http://www.freebasic.net/forum/viewtopic.php?t=16873


And it seems, that it runs with GFA32
http://www.freebasic.net/forum/viewtopic.php?t=15794
and RapidQ2
http://www.freebasic.net/forum/viewtopic.php?t=15810
too.


ima747(Posted 2010) [#2]
Very cool, the more minib3d the better.


jhocking(Posted 2010) [#3]
Does this mean we can also use minib3d on desktop in C++?


Stenley(Posted 2010) [#4]
@jhocking: It seems so.

I have downloaded at http://sourceforge.net/projects/minib3d/ the Linux and Windows version.
Both packages have a "src"-Dirctory with the following files:

3ds.cpp, 3ds.h, animation.cpp, animation.h, animation_keys.cpp, animation_keys.h, bank.cpp, bank.h, bone.cpp, bone.h, brush.cpp, brush.h, camera.cpp, camera.h, collision2.cpp, collision2.h, collision.cpp, collision.h, entity.cpp, entity.h, file.cpp, file.h, functions.cpp, geom.cpp, geom.h, global.cpp, global.h, light.cpp, light.h, makefile, maths_helper.cpp, maths_helper.h, matrix.cpp, matrix.h, mesh.cpp, mesh.h, minib3d.cpp, minib3d.h, model.cpp, model.h, pick.cpp, pick.h, pivot.cpp, pivot.h, project.cpp, project.h, quaternion.cpp, quaternion.h, sprite_batch.cpp, sprite_batch.h, sprite.cpp, sprite.h, stb_image.c, stb_image.h, string_helper.cpp, string_helper.h, surface.cpp, surface.h, terrain.cpp, terrain.h, texture.cpp, texture_filter.cpp, texture_filter.h, texture.h, tilt.cpp, tilt.h, touch.cpp, touch.h, tree.cpp, tree.h, turn.cpp

So it seems, you can use it with C++.
In the top directory are only the files
lgpl.txt (the license)
2d.bi (one FB-header)
minib3d.bi (the other FB-header)
And in the Linux package is a libminib3d.so and in the Windows package a minib3d.dll.

So it is really mostly C++. The FreeBasic part are only the two FreeBasic-headers 2d.bi and minib3d.bi


SLotman(Posted 2010) [#5]
3ds? Terrain? This looks like it's minib3dext, not minib3d...


D4NM4N(Posted 2010) [#6]
Interesting...
I had a look at doing this for C# but i figured all the GL stuff was using Max's GL commands so i kind of gave up a bit.


D4NM4N(Posted 2010) [#7]
I downloaded it and attempted to build it in GCC but the functions.cpp threw a load of errors over missing things like "entity" etc. I notice there is no header file for this cpp, which is also a bit odd.

I will do rooting and see what i can find.

Can anyone else build this?

Last edited 2010


ima747(Posted 2010) [#8]
Haven't looked into this, but if you start with iMiniB3D (as this is ported from or so I understand) it's already C++, you just need to strip out a few objective C ties and you should be good to go. It's based on OpenGL ES 1.1 which is basically OpenGL 1.5 on a desktop. If you start with that base and remove the touch and accelerometer files, all you should have to do is create an equivalent Texture2D (something that actually generates openGL textures to reference, the one included is iOS specific) and then silence errors and in theory you should be up and running.

Porting it to android and java the language differences for opengl have been the biggest headaches (java uses it's own buffer types, C++ tends to use primitive arrays) so if you're not jumping languages I would suspect it should go quite smoothly.


D4NM4N(Posted 2010) [#9]
Looks like you are right.

I did an unmanaged interop call to graphics3d but while nothing happened it did not crash/error either. I looked a bit deeper and now i see that there is no 2d renderer in there (or does not appear to be) no flip etc. (probably why nothing happened :D)
I think the 3d is working it is just not being rendered by anything to the screen :/
I am assuming it is that big mush of ASM code in 2d.bi that deals with that, and i would not know where to start trying to put that in C#.

Last edited 2010


ima747(Posted 2010) [#10]
Correct, you will need an OpenGL context . minib3d creates it in an extended uiview that calls the render loop function in it's update event. Minib3d does it in an OpenGL context created by a call to graphics with the OpenGL as the bmax render driver. In on android I'm doing it with an extended openglview and associated extended glrenderer. Every os is going to have it's own way to create an OpenGL context (windowed/fullscreen, resolution, buffer inits etc.). On Mac you should be able to grab the context from just about any view type object since the os runs off OpenGL. No clue on windows. If you dig into the graphics() function with OpenGL drivers in bmax you can probably find some clues. Or you could just google some OpenGL tutorials, step 1 is going to be creating the context.


angros47(Posted 2012) [#11]
The library is now called OpenB3D: I added many features of extended version by Klepto (stencil shadows, and shaders). Also, it supports extensions by Warner (quaternions, terrains).

The library requires an OpenGL context (in FreeBasic, it can be created easily with just one command; in other languages, you should check).

By porting the headers, it can be used in any language that supports external libraries (DLL in windows, .so in linux)


KronosUK(Posted 2012) [#12]
I started writing a blitzmax wrapper for this but it seems texturing doesn't work. I have an example that loads a mesh and rotates it but no textures show.



I have wrapped the texture functions as follows.


The Global LoadTexture_:Int Ptr(file$z, flags:Int) "C" = getprocaddress(lib,"LoadTexture")

Function LoadTexture:Int Ptr(file$, flags:Int=1)
	Return LoadTexture_(file,flags)
End Function

Function EntityTexture:Int(ent:Int Ptr, tex:Int Ptr, frame:Int=0, index:Int=0)
	Return EntityTexture_(ent,tex,frame,index)
End Function



I read on the freebasic forums that "this version uses stb_image.c to load textures: no more OS-specific depandancies. " I am wondering if this requires extra work for blitzmax to communicate with it.

Anybody have any thoughts?


angros47(Posted 2012) [#13]
stb_image.c is a routine that loads images from many file formats in memory. In BlitzMax, there already are built-in routines that do the same (and that were used in the original MiniB3D), but OpenB3D should just ignore them.

You should pass the file name as a null-terminated string. I don't know if BlitzMax will do the conversion automatically... maybe you should do as suggested here:

http://www.blitzbasic.com/Community/posts.php?topic=73286

Since I don't have BlitzMax, I cannot test. I suppose it could be something like:

Function LoadTexture:Int Ptr(file$, flags:Int=1)
	Return LoadTexture_(file.ToCString(),flags)
End Function


Also, you cannot use path info in your file name (also if you load a 3d model with textures, all textures must be in the same directory): in fact, the file name is stripped of path info (otherwise, a model that tries to load a texture from "data\img.jpg" won't work in linux, because it should load image from "data/img.jpg")

Last edited 2012


KronosUK(Posted 2012) [#14]
Loading a texture without path info in filename does work. I guess this means I can only hold textures in the current directory.

Thanks for the pointer Angros (and openb3d of course)


KronosUK(Posted 2012) [#15]
Hi Angros

Would you prefer I post any new questions about OpenB3d here or over in the freebasic forum?

Anyhoo while I am here.

I was having a problem with Camerapick but found the answer in the freebasic thread. (convert camerapick(ent,x,y) to camerapick(ent,x,graphicsheight-y)

Are you aware of any other differences like this which I might need to be aware of for Blitzmax?


angros47(Posted 2012) [#16]
If your question is about OpenB3D in FreeBasic, you should ask in Freebasic forum, while if your question is about OpenB3D in BlitzMax you should ask here: for me, there is no preference, but there are also other people that can help you, and BlitzMax experts are here.

An important difference you need to know is that pitch is reversed (so, you need to use a minus sign in the pitch value for commands RotateEntity, TurnEntity, RotateMesh)

Rotations are quaternion based (as in Blitz3d), so you don't have to fear gimbal lock.

Also, there is no command SetBuffer: you should set frontbuffer or backbuffer when you set up opengl context (in FreeBasic by default backbuffer is set): to draw on a texture: you can use the command BufferToTex (you need to pass a pointer to buffer data, that must be in the format RGBA).

You can use both B3D and 3DS files, but 3DS files have no animation support.


angros47(Posted 2012) [#17]
Version 0.6 is out.

New features:
- CSG (ported from http://www.blitzbasic.com/codearcs/codearcs.php?code=2418 and improved)
- uvw mapping
- stencils (mirrors and portals can be done)
- voxel sprites
- direct rendering to textures (you don't have to render to backbuffer and copy it to a texture anymore if you want to do cubemapping)


KronosUK(Posted 2012) [#18]
Some requests for next release.

More shadow control options such as Link Hide/Showentity to shadowcasting. Currently shadow casting entities still cast shadows when hidden. Separate shadow range from light range - possibly link it to camera instead ie only cast shadow if within range of x from camera. Disable shadow casting from nominated light. By default it appears that all lights cast shadows.

I think there might be a bug with createshadow when static is set to true(1 in blitzmax) as it seems to only create shadows for some objects and not others. I am still researching this though.

Also createplane always creates a plane with only 1 subdivision regardless of what value is entered for divisions.

Last edited 2012


KronosUK(Posted 2012) [#19]
Heres a small prog to show the shadow and createplane problem. If you run the program with createshadows static=false and then createshadows static=true you can hopefully see the difference. I also notice some weirdness when createshadow static= true and you move camera away from scene. A bunch of phantom shadows start appearing on the plane.

(Hopefully wont be too difficult to convert to Freebasic to test!)

This is based on v0.6.



Last edited 2012


angros47(Posted 2013) [#20]
A small patch:

in file shadow.cpp, locate the section:

		for(it2=shadow_list.begin();it2!=shadow_list.end();it2++){
			ShadowObject* S=*it2;
			if (Light->EntityDistance(S->Parent)<1/Light->range){


and replace:
			if (Light->EntityDistance(S->Parent)<1/Light->range){


with:
			if (S->Parent->hide==false && Light->EntityDistance(S->Parent)<1/Light->range){


Hidden entities won't have shadows anymore.


angros47(Posted 2013) [#21]
New version is out; it supports octrees, too.


Hotshot2005(Posted 2013) [#22]
octrees? What the hell is octrees?


angros47(Posted 2014) [#23]
New version is out: this time, it's possible to create spherical terrains (LOD geospheres), from an equirectangular heightmap (the command is loadgeosphere)


Chapman7(Posted 2014) [#24]
Thanks a ton angros37! :D