## 3d without openGL

User projects written in or related to FreeBASIC.
BasicCoder2
Posts: 3338
Joined: Jan 01, 2009 7:03

### Re: 3d without openGL

Although at this point in time I have been too busy with other non computer related things to sit down and work through Paul Doe's contributions he has certainly made this thread interesting.
And thank you Dodicat for your input as I love your short compact fast code even if I can't at this stage follow all of it.
.
dafhi
Posts: 1231
Joined: Jun 04, 2005 9:51

### Re: 3d without openGL

here's my version

Code: Select all

`/' oop 3d 2017 Oct 29 - by dafhi  framerate-independent physics'/#ifndef piConst Pi = 4 * Atn(1)const TwoPi = 8 * Atn(1)#endiftype v3  as single         x,y,z  declare property  len as double  declare sub       normalize  declare sub       randEnd Typeproperty v3.len as double: return sqr(x*x+y*y+z*z)End Propertysub v3.normalize:  var slen=x*x+y*y+z*z  if slen < .5 then: x=0:y=1:z=0  else: slen = 1/sqr(slen): x*=slen: y*=slen: z*=slen  endifEnd Subsub v3.rand:  y=2*(rnd-.5):  var r=sqr(1-y*y)  z=rnd*twopi: x=r*cos(z): z=r*sin(z)End Suboperator *(l as single, r as v3)as v3:  return type(l*r.x, l*r.y, l*r.z):  end operatoroperator *(l as v3, r as single)as v3:  return type(l.x*r, l.y*r, l.z*r):  end operatoroperator +(l as v3, r as v3)as v3:  return type(l.x+r.x, l.y+r.y, l.z+r.z):  end operatoroperator -(l as v3, r as v3)as v3:  return type(l.x-r.x, l.y-r.y, l.z-r.z):  end operatortype cloudObj  as single         size = 1, _size    ''future: child objects  as v3             ori', _ori  as v3             vx, vy, vz  as single         a0, a1, a2  as long           uchild = -1  as cloudObj ptr   child(any)  declare sub       rot(a as single, byval norm as v3=(0,1,0))  declare sub       reset  declare constructor(ori as v3=(0,0,0), size as single=1)  private:  declare sub       sincos(angle as double)  as double         cosa, sina, dlenEnd Typeconstructor.cloudObj(_ori as v3, size as single): ori=_ori*size: _size=size: resetend constructorsub cloudObj.reset:  size=_size: vx=type(size,0,0): vy=type(0,size,0): vz=type(0,0,size):  end subsub cloudObj.sincos(angle as double):  cosa=cos(angle): sina=sin(angle):  end subsub cloudObj.rot(a as single, byval norm as v3)  dlen=norm.x*norm.x+norm.y*norm.y+norm.z*norm.z  if dlen < .5 then    norm.x=0:norm.y=-1:norm.z=0  else    dlen = -1/sqr(dlen): norm.x*=dlen: norm.y*=dlen: norm.z*=dlen  endif:  sincos a  vx=(1-cosa)*(norm.x*vx.x+norm.y*vx.y+norm.z*vx.z)*norm + cosa*vx +_  sina*type(norm.y*vx.z-norm.z*vx.y, norm.z*vx.x-norm.x*vx.z, norm.x*vx.y-norm.y*vx.x)  vy=(1-cosa)*(norm.x*vy.x+norm.y*vy.y+norm.z*vy.z)*norm + cosa*vy +_  sina*type(norm.y*vy.z-norm.z*vy.y, norm.z*vy.x-norm.x*vy.z, norm.x*vy.y-norm.y*vy.x)  vz=(1-cosa)*(norm.x*vz.x+norm.y*vz.y+norm.z*vz.z)*norm + cosa*vz +_  sina*type(norm.y*vz.z-norm.z*vz.y, norm.z*vz.x-norm.x*vz.z, norm.x*vz.y-norm.y*vz.x)End Subsub incr(byref ret as single, i as single=0, modu as single=1)  ret+=i: ret-=modu*int(ret/modu)End Subsub main    var u = 9999  dim as v3 a(u)  for i as long = 0 to u    a(i) = type(rnd-.5,rnd-.5,rnd-.5)  Next  var w = 800,  midx=w/2  var h = 600,  midy=h/2, y_invert=h-1 - midy  screenres w,h, 32  var scale = sqr(w*w+h*h)/4  dim as cloudObj  axis=type(type<v3>(0,0,2), scale)  var   iphys_fps = 1/67, phys_t = 0f  var   ianim_fps = 1/57, anim_t = 0f  var   tp = timer    while inkey=""    if anim_t <= 0 then      screenlock        cls        for i as long = 0 to u          var p = a(i).x*axis.vx + a(i).y*axis.vy + a(i).z*axis.vz + axis.ori          if p.z > .01 then            var s = scale/p.z            pset (midx + s*p.x, y_invert - s*p.y)          endif        next      screenunlock      anim_t += ianim_fps    endif       sleep 1       var t=timer, dt=t-tp    tp=t       anim_t -= dt    phys_t += dt    var frames=0    while phys_t>0      frames += 1      phys_t -= iphys_fps    wend       var rate = pi*iphys_fps*frames*5    incr axis.a0, .1*rate, twopi    incr axis.a1, .011*rate, twopi       axis.reset    axis.rot axis.a0, type(1,0,0)    axis.rot axis.a1, type(0,1,0)  wend  end subMain`
Last edited by dafhi on Nov 07, 2017 21:32, edited 6 times in total.
dafhi
Posts: 1231
Joined: Jun 04, 2005 9:51

### Re: 3d without openGL

hi paul - i simplified your dependencies. you can accept or discard as you see fit

core.bi

Code: Select all

`   #include once "platform.bi"   #include once "crt.bi" '' needed for memcpy`

math.bi

Code: Select all

`   #include once "core.bi"`

vec4.bi

Code: Select all

`   #include once "math.bi"`

mat4.bi

Code: Select all

`   '#include once "core.bi"   '#include once "platform.bi"   #include once "vec4.bi"`

arrayList.bi

Code: Select all

`   '#include once "platform.bi"   '#include once "core.bi"   #include once "math.bi"   '#include once "crt.bi" '' needed for memcpy`

object.bi

Code: Select all

`'#include once "platform.bi"'#include once "core.bi"'#include once "math.bi"'#include once "vec4.bi"#include once "mat4.bi"#include once "arrayList.bi"`

camera.bi

Code: Select all

`'#include once "platform.bi" '' platform specific definitions'#include once "core.bi" '' core functions and defines'#include once "math.bi" '' math functions and constants'#include once "vec4.bi" '' vec4 type and operations'#include once "mat4.bi" '' mat4 type and operations#include once "object.bi"    '' the camera class definition '' 2017 Oct 21 - by dafhi`

utility.bi

Code: Select all

`#include once "camera.bi"    '' the camera class definition  '' 2017 Oct 21 - by dafhi`

camera test.bas

Code: Select all

`#include once "fbgfx.bi"'#include once "platform.bi" '' platform specific definitions'#include once "core.bi"      '' core functions and defines'#include once "math.bi"      '' math functions and constants'#include once "vec4.bi"      '' vec4 type and operations'#include once "mat4.bi"      '' mat4 type and operations'#include once "arrayList.bi" '' convenience'#include once "camera.bi"    '' the camera class definition'#include once "object.bi"    '' the object definition#include once "utility.bi"   '' to draw lines an' stuff directly in 3D space`

vec2h.bi

Code: Select all

`   '#include once "core.bi"   #include once "math.bi"`

mat3.bi

Code: Select all

`   #include once "vec2h.bi"   '#include once "math.bi"`

grid.bi

Code: Select all

`   '#include once "core.bi"   '#include once "vec2h.bi"   #include once "mat3.bi"`

grid test.bas

Code: Select all

`'#include once "platform.bi"'#include once "core.bi"#include once "grid.bi"'#include once "vec2h.bi"'#include once "mat3.bi"`

shape.bi

Code: Select all

`   '#include once "core.bi"   '#include once "vec2h.bi"   #include once "mat3.bi"   #include once "arrayList.bi"`

main.bas

Code: Select all

`'#include once "platform.bi"'#include once "core.bi"'#include once "mat3.bi"'#include once "vec2h.bi"#include once "shape.bi"#include once "fbgfx.bi" '' needed for the constants used by multiKey()`
paul doe
Posts: 846
Joined: Jul 25, 2017 17:22
Location: Argentina

### Re: 3d without openGL

Hi dafhi

You just had to tinker with it, don't you? ;)

If you prefer it like that, no prob. The reason they're laid out like that, is that every file should be able to compile itself, with the dependencies it needs. It's easier to isolate the errors in each file (syntactic or otherwise) that way. In small code snippets like this, that's a non-issue, but in bigger projects it makes all the difference.
Anyway, work with it however you like. I'm preparing another code snippet that you (and also dodicat, perhaps) will find interesting. It deals with how I usually manage things like the inner loop of the code and other issues. And it even resembles real code XD
I was considering a GitHub repository for these things as a convenience (as they use many files). Or is it better that I post them here, the way I'm doing? What do you think?

Paul

PS: Has the code 'settled' yet? ;) If so, you'll probably like the next one. Catch you later.
dafhi
Posts: 1231
Joined: Jun 04, 2005 9:51

### Re: 3d without openGL

regarding dependency - in their current form they don't throw errors (self-contained)

as far as git, i have no opinion at this point; i haven't been coding much. Looking forward to seeing any new code from you regardless