MiniB3d for FreeBasic

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
veggie
Posts: 75
Joined: May 17, 2009 12:52

Post by veggie »

In the way of a request Angros, could you keep a change-log? This would be very helpful for me personally.
oog
Posts: 124
Joined: Jul 08, 2011 20:34

Post by oog »

angros47 wrote:To be able to map a different texture to every face of a cube, a solution could be the use of VertexU and VertexW....
Thank you, that works.
To be able to access the Vertices of every surface, I defined my own objects (CreateTile() and CreatePlatform()), instead of using CreateCube().
The platform is similar to a cube, but the bottom is open and the texture does fit to the top and sides as I need it.
On creation, the bottom of a platform can be scaled to be larger than the top face.

Now I have a new problem (run example program to see).
The texture does not "stretch" as I need to.

The reason is clear: I pull the vertex of a triangle to the left and another vertex of the next triangle to the right. So the texture gets a buckling.

But what can I do? I tried some web- and forum-searches, but maybe I did not use the right keywords for the search.

Here is the example program:

Code: Select all

' example:
' street element

#include "openb3d.bi"

screen 18, 32, , &h02
Graphics3d 640,480,32,1,1

var cam=createcamera()
CameraClsColor cam,100,100,100

Function CreateTile(parent As Any Ptr=0) As Any Ptr
  var brush = CreateBrush(128,128,128)
  var mesh = CreateMesh(parent)
  var surface = CreateSurface(mesh, brush)
  var v0 = AddVertex(surface, -1, 0, -1, 0.0, 1.0)  'Bottom Left
  var v1 = AddVertex(surface, -1, 0, 1,  0.0, 0.0)  'Top Left
  var v2 = AddVertex(surface, 1, 0, 1,   1.0, 0.0)  'Top Right
  var v3 = AddVertex(surface, 1, 0, -1,  1.0, 1.0)  'Bottom Right
  AddTriangle(surface, v0, v1, v2)
  AddTriangle(surface, v0, v2, v3)
  UpdateNormals mesh
  Return mesh
End Function

Function CreatePlatform(basescale As Double = 1.0, parent As Any Ptr=0) As Any Ptr
'Triangles share vertices of the same face/surface
  var brush = CreateBrush(128,128,128)
  var mesh = CreateMesh(parent)
  var surface = CreateSurface(mesh, brush)
  
  'top
  var tv0 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
  var tv1 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
  var tv2 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
  var tv3 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
  AddTriangle(surface, tv0, tv1, tv2)
  AddTriangle(surface, tv0, tv2, tv3)
  
  'south side
  var sv0 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
  var sv1 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
  var sv2 = AddVertex(surface,  1*basescale, -1, -1*basescale, 15/16, 16/16)
  var sv3 = AddVertex(surface, -1*basescale, -1, -1*basescale,  1/16, 16/16)
  AddTriangle(surface, sv0, sv1, sv2)
  AddTriangle(surface, sv0, sv2, sv3)
  
  'west side
  var wv0 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
  var wv1 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
  var wv2 = AddVertex(surface, -1*basescale, -1, -1*basescale,  0/16, 15/16)
  var wv3 = AddVertex(surface, -1*basescale, -1,  1*basescale,  0/16,  1/16)
  AddTriangle(surface, wv0, wv1, wv2)
  AddTriangle(surface, wv0, wv2, wv3)
  
  'north side
  var nv0 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
  var nv1 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
  var nv2 = AddVertex(surface, -1*basescale, -1,  1*basescale,  1/16,  0/16)  ' Left
  var nv3 = AddVertex(surface,  1*basescale, -1,  1*basescale, 15/16,  0/16)  ' Right
  AddTriangle(surface, nv0, nv1, nv2)
  AddTriangle(surface, nv0, nv2, nv3)
  
  'east side
  var ev0 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
  var ev1 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
  var ev2 = AddVertex(surface,  1*basescale, -1,  1*basescale, 16/16,  1/16)  'Top Left
  var ev3 = AddVertex(surface,  1*basescale, -1, -1*basescale, 16/16, 15/16)  'Bottom Left
  AddTriangle(surface, ev0, ev1, ev2)
  AddTriangle(surface, ev0, ev2, ev3)

  UpdateNormals mesh
  Return mesh
End Function


'Function CreatePlatform(basescale As Double = 1.0, parent As Any Ptr=0) As Any Ptr
''Every triangle hat it's own vertices
'  var bs=basescale
'  var brush = CreateBrush(128,128,128)
'  var mesh = CreateMesh(parent)
'  var surface = CreateSurface(mesh, brush)
'  'top
'  var tv0 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
'  var tv1 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
'  var tv2 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
'  
'  var tv3 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
'  var tv4 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
'  var tv5 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
'  
'  AddTriangle(surface, tv0, tv1, tv2)
'  AddTriangle(surface, tv3, tv4, tv5)
'  
'  'south side
'  var sv0 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
'  var sv1 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
'  var sv2 = AddVertex(surface,  1*bs, -1, -1*bs, 15/16, 16/16)
'  
'  var sv3 = AddVertex(surface,  1*bs, -1, -1*bs, 15/16, 16/16)
'  var sv4 = AddVertex(surface, -1*bs, -1, -1*bs,  1/16, 16/16)
'  var sv5 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
'  
'  AddTriangle(surface, sv0, sv1, sv2)
'  AddTriangle(surface, sv3, sv4, sv5)
'  
'  'west side
'  var wv0 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
'  var wv1 = AddVertex(surface, -1,  0, -1,  1/16, 15/16)  'Bottom Left
'  var wv2 = AddVertex(surface, -1*bs, -1, -1*bs,  0/16, 15/16)
'  
'  var wv3 = AddVertex(surface, -1*bs, -1, -1*bs,  0/16, 15/16)
'  var wv4 = AddVertex(surface, -1*bs, -1,  1*bs,  0/16,  1/16)
'  var wv5 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
'  
'  AddTriangle(surface, wv0, wv1, wv2)
'  AddTriangle(surface, wv3, wv4, wv5)
'  
'  'north side
'  var nv0 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
'  var nv1 = AddVertex(surface, -1,  0,  1,  1/16,  1/16)  'Top Left
'  var nv2 = AddVertex(surface, -1*bs, -1,  1*bs,  1/16,  0/16)  ' Left
'  
'  var nv3 = AddVertex(surface, -1*bs, -1,  1*bs,  1/16,  0/16)  ' Left
'  var nv4 = AddVertex(surface,  1*bs, -1,  1*bs, 15/16,  0/16)  ' Right
'  var nv5 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
'  
'  AddTriangle(surface, nv0, nv1, nv2)
'  AddTriangle(surface, nv3, nv4, nv5)
'  
'  'east side
'  var ev0 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
'  var ev1 = AddVertex(surface,  1,  0,  1, 15/16,  1/16)  'Top Right
'  var ev2 = AddVertex(surface,  1*bs, -1,  1*bs, 16/16,  1/16)  'Top Left
'  
'  var ev3 = AddVertex(surface,  1*bs, -1,  1*bs, 16/16,  1/16)  'Top Left
'  var ev4 = AddVertex(surface,  1*bs, -1, -1*bs, 16/16, 15/16)  'Bottom Left
'  var ev5 = AddVertex(surface,  1,  0, -1, 15/16, 15/16)  'Bottom Right
'  
'  AddTriangle(surface, ev0, ev1, ev2)
'  AddTriangle(surface, ev3, ev4, ev5)
'
'  UpdateNormals mesh
'  Return mesh
'End Function


var light = CreateLight(2)


'texture
'  var testtex = LoadTexture("str1.bmp")
  
  var img=imagecreate(64,64,32)
  Line img,(0,0)-(63,63),RGB(0,100,0),BF
  Line img,(24,0)-(40,63),RGB(40,40,40),BF
  Line img,(0,24)-(63,40),RGB(40,40,40),BF
  'print text on texture
  For x As Integer=0 To 7
    Draw String img,(x*8,24), mid("wes  eas",x+1,1),&Hffffff
  Next x
  For z As Integer=0 To 7
    Draw String img,(28,z*8-4), mid("nor  sou",z+1,1),&Hffffff
  Next z
  'convert image to texture
  var testtex=CreateTexture(64,64,1,1)
  BufferToTex testtex,img+32,1 



var Tile = CreateTile()
  EntityTexture Tile,testtex
  PositionEntity Tile, 0, 0, 6
  RotateEntity Tile, 30, 0, 0


var Platform1 = CreatePlatform(1.0)
  EntityTexture Platform1,testtex
  PositionEntity Platform1, -2, -1, 5
  RotateEntity Platform1, 30, 0, 0


var Platform2 = CreatePlatform(1.5)
  EntityTexture Platform2,testtex
  PositionEntity Platform2, 2, -1, 5
  RotateEntity Platform2, 30, 0, 0


Dim As Double i
Dim As String ks

Do
  ks=inkey
  
  'animation
  turnentity Tile,0,1,0
  turnentity Platform1,0,1,0
  turnentity Platform2,0,1,0

  renderworld
  flip
  Sleep 10
Loop Until ks=chr(27)
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Post by angros47 »

Texture mapping is not an easy task (that's why there are dedicated programs).

Try replacing your CreatePlatform function with this:

Code: Select all

Function CreatePlatform(basescale As Double = 1.0, parent As Any Ptr=0) As Any Ptr
'Triangles share vertices of the same face/surface
  var brush = CreateBrush(128,128,128)
  var mesh = CreateMesh(parent)
  var surface = CreateSurface(mesh, brush)
  var bscale=.5-.5/basescale
 
  'top
  var tv0 = AddVertex(surface, -1,  0, -1,  bscale, 1-bscale)  'Bottom Left
  var tv1 = AddVertex(surface, -1,  0,  1,  bscale,  bscale)  'Top Left
  var tv2 = AddVertex(surface,  1,  0,  1, 1-bscale,  bscale)  'Top Right
  var tv3 = AddVertex(surface,  1,  0, -1, 1-bscale, 1-bscale)  'Bottom Right
  AddTriangle(surface, tv0, tv1, tv2)
  AddTriangle(surface, tv0, tv2, tv3)
 
  'south side
  var sv0 = AddVertex(surface, -1,  0, -1,  bscale, 1-bscale)  'Bottom Left
  var sv1 = AddVertex(surface,  1,  0, -1, 1-bscale, 1-bscale)  'Bottom Right
  var sv2 = AddVertex(surface,  1*basescale, -1, -1*basescale, 1, 1)
  var sv3 = AddVertex(surface, -1*basescale, -1, -1*basescale,  0, 1)
  AddTriangle(surface, sv0, sv1, sv2)
  AddTriangle(surface, sv0, sv2, sv3)
 
  'west side
  var wv0 = AddVertex(surface, -1,  0,  1,  bscale,  bscale)  'Top Left
  var wv1 = AddVertex(surface, -1,  0, -1,  bscale, 1-bscale)  'Bottom Left
  var wv2 = AddVertex(surface, -1*basescale, -1, -1*basescale,  0, 1)
  var wv3 = AddVertex(surface, -1*basescale, -1,  1*basescale,  0,  0)
  AddTriangle(surface, wv0, wv1, wv2)
  AddTriangle(surface, wv0, wv2, wv3)
 
  'north side
  var nv0 = AddVertex(surface,  1,  0,  1, 1-bscale,  bscale)  'Top Right
  var nv1 = AddVertex(surface, -1,  0,  1,  bscale,  bscale)  'Top Left
  var nv2 = AddVertex(surface, -1*basescale, -1,  1*basescale,  0,  0)  ' Left
  var nv3 = AddVertex(surface,  1*basescale, -1,  1*basescale, 1,  0)  ' Right
  AddTriangle(surface, nv0, nv1, nv2)
  AddTriangle(surface, nv0, nv2, nv3)
 
  'east side
  var ev0 = AddVertex(surface,  1,  0, -1, 1-bscale, 1-bscale)  'Bottom Right
  var ev1 = AddVertex(surface,  1,  0,  1, 1-bscale,  bscale)  'Top Right
  var ev2 = AddVertex(surface,  1*basescale, -1,  1*basescale, 1,  0)  'Top Left
  var ev3 = AddVertex(surface,  1*basescale, -1, -1*basescale, 1, 1)  'Bottom Left
  AddTriangle(surface, ev0, ev1, ev2)
  AddTriangle(surface, ev0, ev2, ev3)

  UpdateNormals mesh
  Return mesh
End Function
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

Post by creek23 »

where can I find a simple "hello world" for this lib?

~creek23
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Post by angros47 »

There is no point in doing a "hello world" demo, since an "hello world" demo does not require 3d.

Here is a demo that shows a cube:

Code: Select all

#include "openb3d.bi"



screen 18, 32, , &h10002 

graphics3d 640,480

var cube=createcube()
var camera=createcamera()

moveentity cube,0,0,5

renderworld
flip
sleep

If you really want an "hello world":

Code: Select all

#include "openb3d.bi"

#define Render_OpenGL
#include "2d.bi"




screenres 640,480,32

graphics3d 640,480

print "hello world"


screensync
sleep
It's a bit useless, isn't it?
oog
Posts: 124
Joined: Jul 08, 2011 20:34

Post by oog »

angros47 wrote:Texture mapping is not an easy task (that's why there are dedicated programs).
Try replacing your CreatePlatform function with this...
Yes, that works.
So when I change the geometry of a triangle, I also have to move the U and W to fit on the texture bitmap, to avoid optical distortion.
OK , I understood, thank you.
oog
Posts: 124
Joined: Jul 08, 2011 20:34

Post by oog »

I made some simple examples for 3D programming, using OpenB3D.
The programs have been developed, starting with the simple cube demo, then added color, terrain, textures ...
I'm still learning 3D stuff, so don't expect the programs to be perfect, but they may be useful for beginners.

Image

Download at proog.de
http://www.proog.de/joomla/index.php/projekte/72-cube
Tio Bit
Posts: 27
Joined: May 28, 2005 12:06

Post by Tio Bit »

Hi Angros

When I try to compile OpenB3D v0.4 using MinGW (g++ v4.6.1 from Nuwen.net MinGW Distro v7.2), I get the following error :

.
.
.
g++ -O3 -c touch.cpp -o touch.o
g++ -O3 -c texture_filter.cpp -o texture_filter.o
g++ -O3 -c tree.cpp -o tree.o
tree.cpp: In member function 'bool MeshCollider::collide(const Line&, float, const Transform&, Collision*)':
tree.cpp:55:12: error: 'NULL' was not declared in this scope
make: *** [tree.o] Error 1

Any clue how to fix the error? (I'm trying to use OpenB3D from C++)

Thx
Tio Bit
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Post by angros47 »

Try replacing, in tree.cpp, the line:

Code: Select all

	if( tree==NULL ) return false;
with:

Code: Select all

	if( tree==0 ) return false;
(it should produce exactly the same code, since NULL and 0 should be equivalent, but maybe some versions of the compiler don't like NULL)
Tio Bit
Posts: 27
Joined: May 28, 2005 12:06

Post by Tio Bit »

angros47 wrote:Try replacing, in tree.cpp, the line:

Code: Select all

	if( tree==NULL ) return false;
with:

Code: Select all

	if( tree==0 ) return false;
(it should produce exactly the same code, since NULL and 0 should be equivalent, but maybe some versions of the compiler don't like NULL)
Yeps, it works
Thx
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

Post by creek23 »

angros47 wrote:There is no point in doing a "hello world" demo, since an "hello world" demo does not require 3d.

Here is a demo that shows a cube:

Code: Select all

#include "openb3d.bi"



screen 18, 32, , &h10002 

graphics3d 640,480

var cube=createcube()
var camera=createcamera()

moveentity cube,0,0,5

renderworld
flip
sleep
now didn't you just wrote a "hello world" for this 3d engine? showcasing what libs are required, how to initialize the engine, how to create a 3d scene and how to have it rendered...

Btw, thanks for the "hello world". ;)

~creek23
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Post by joseywales72 »

Hi TioBit,

Your code is very useful. These are just the examples I need to get started. Thank you. A very big thanks to angros47 for making this a reality.

Anil
Tio Bit
Posts: 27
Joined: May 28, 2005 12:06

Post by Tio Bit »

joseywales72 wrote:Hi TioBit,

Your code is very useful. These are just the examples I need to get started. Thank you. A very big thanks to angros47 for making this a reality.

Anil
The examples are from oog, not mine :)
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Post by joseywales72 »

joseywales72 wrote:
Hi TioBit,

Your code is very useful. These are just the examples I need to get started. Thank you. A very big thanks to angros47 for making this a reality.

Anil


The examples are from oog, not mine :)
Oops... I'm very sorry. Thanks all, and thanks for the examples Oog.
chung
Posts: 648
Joined: Jan 16, 2010 20:52
Location: France
Contact:

Post by chung »

i tryed openb3d with gui_chung and it works !
heres a sample demo with tilecity_gui (openGL window + some buttons)

=> http://chungswebsite.blogspot.com/searc ... City_chung

+maxfps set to 30 (ancient value was 0, default to 10 ...)
+inkey edit map bug corrected (21/11/2011)
+performances boost x3 (25/11/2011)
+gui edit map window (26/11/2011)

Image
Last edited by chung on Dec 18, 2011 13:14, edited 14 times in total.
Post Reply