i get a image
but it is a bit dark
main
Code: Select all
'' bluatigro 8 aug 2021
'' raserizor
#include "t3s.bas"
#include "matix.bas"
dim shared as integer scrnw , scrnh
screen 20 , 32 , , fb.gfx_fullscreen
screeninfo scrnw , scrnh
dim shared as t3s pnt( 255 )
dim shared as t3s light = t3s( 1 , 1 , 1 )
sub setpoint( no as integer _
, x as single , y as single , z as single )
spot x , y , z
pnt( no ) = t3s( x , y , z )
end sub
dim shared as integer tritel = 0
type ttriangle
dim as t3s p( 2 ) , led , normal , kleur
declare sub fill( p1 as integer , p2 as integer _
, p3 as integer , kl as t3s )
declare sub show()
end type
sub ttriangle.fill( p1 as integer , p2 as integer _
, p3 as integer , kl as t3s )
p( 0 ) = pnt( p1 )
p( 1 ) = pnt( p2 )
p( 2 ) = pnt( p3 )
led = ( p( 0 ) + p( 1 ) + p( 2 ) ) / 3
normal = cross( p( 0 ) - led , p( 1 ) - led )
kleur = kl
end sub
sub ttriangle.show()
dim as single x1,y1,x2,y2,x3,y3,a,b,i,angle
x1 = scrnw / 2 + p( 0 ).x / ( p( 0 ).z + 1000 ) * 1000
y1 = scrnh / 2 - p( 0 ).y / ( p( 0 ).z + 1000 ) * 1000
x2 = scrnw / 2 + p( 1 ).x / ( p( 1 ).z + 1000 ) * 1000
y2 = scrnh / 2 - p( 1 ).y / ( p( 1 ).z + 1000 ) * 1000
x3 = scrnw / 2 + p( 2 ).x / ( p( 2 ).z + 1000 ) * 1000
y3 = scrnh / 2 - p( 2 ).y / ( p( 2 ).z + 1000 ) * 1000
if y1 = y2 then y1 = y1 - 1e-7
if y2 = y3 then y3 = y3 + 1e-7
if y1 > y3 then
swap y1 , y3
swap x1 , x3
end if
if y1 > y2 then
swap y1 , y2
swap x1 , x2
end if
if y2 > y3 then
swap y2 , y3
swap x2 , x3
end if
for i = y1 to y3
a = x1 + ( x3 - x1 ) * (i-y1) / ( y3 - y1 )
if i < y2 then
b = x1 + ( x2 - x1 ) * (i-y1) / ( y2 - y1 )
else
b = x2 + ( x3 - x2 ) * (i-y2) / ( y3 - y2 )
end if
angle = getangle( light , normal )
kleur = kleur * ( cos( angle ) / 4 + .75 )
line ( a , i ) - ( b , i ) , kleur.toColor()
next i
end sub
dim shared as ttriangle triangles( 1000 )
sub tri( p1 as integer , p2 as integer _
, p3 as integer , kl as t3s )
triangles( tritel ).fill p1 , p2 , p3 , kl
tritel += 1
end sub
sub quad(p1 as integer,p2 as integer,p3 as integer _
,p4 as integer,kl as t3s)
tri p1,p2,p3 , kl
tri p1,p3,p4 , kl
end sub
dim shared as single box(5)
sub setbox( x as single , y as single , z as single _
, dx as single , dy as single , dz as single )
box(0)=x
box(1)=y
box(2)=z
box(3)=dx
box(4)=dy
box(5)=dz
end sub
sub sphere( a as integer , b as integer , kl as t3s )
dim as single i , j , i2 , j2
dim as single x , y , z
if a < 3 then a = 3
if a > 64 then a = 64
if b < 3 then b = 3
if b > 64 then b = 6
for i = -PI to PI step PI / a * 2
i2 = i + PI / a * 2
for j = -PI / 2 to PI / 2 - pi / b * 2 step PI / b * 2
j2 = j + PI / b * 2
x = sin( i ) * cos( j ) * box(3)+box(0)
y = sin( j ) * box(4)+box(1)
z = cos( i ) * cos( j ) * box(5)+box(2)
setpoint 0 , x,y,z
x = sin( i2 ) * cos( j ) * box(3)+box(0)
y = sin( j ) * box(4)+box(1)
z = cos( i2 ) * cos( j ) * box(5)+box(2)
setpoint 1 ,x,y,z
x = sin( i2 ) * cos( j2 ) * box(3)+box(0)
y = sin( j2 ) * box(4)+box(1)
z = cos( i2 ) * cos( j2 ) * box(5)+box(2)
setpoint 2 , x,y,z
x = sin( i ) * cos( j2 ) * box(3)+box(0)
y = sin( j2 ) * box(4)+box(1)
z = cos( i ) * cos( j2 ) * box(5)+box(2)
setpoint 3 , x,y,z
quad 0 , 1 , 2 , 3 , kl
next j
next i
end sub
dim as single angle
dim as integer hi , lo
''do
cls
paint (1,1),rgb(0,100,100)
tritel = 0
camera 0,0,0 , 0,0,0 , 1
link 1 , -300,0,0 , angle,angle,0 , xyz , 0
setpoint 0 , 100 , 100 , 100
setpoint 1 , -100 , 100 , -100
setpoint 2 , -100 , -100 , 100
setpoint 3 , 100 , -100 , -100
tri 0 , 1 , 2 , red
tri 0 , 2 , 3 , yellow
tri 0 , 1 , 3 , blue
tri 1 , 2 , 3 , green
link 1 , 200,0,0 , angle,angle,0 , xyz , 0
setbox 0,0,0 , 100,100,100
sphere 12 , 12 , yellow
for hi = 1 to tritel
for lo = 0 to hi - 1
if triangles(lo).led.z > triangles(hi).led.z then
swap triangles(lo) , triangles(hi)
end if
next lo
next hi
for lo = 0 to tritel - 1
triangles(lo).show
next lo
angle += 5
sleep
''loop while inkey = ""
Code: Select all
'''bluatigro 7 aug 2021
''t3s : 3dvector class
type t3s
dim as single x,y,z
declare constructor()
declare constructor( a as single _
, b as single , c as single )
declare function toColor() as ulong
end type
constructor t3s()
end constructor
constructor t3s( a as single , b as single , c as single )
x = a
y = b
z = c
end constructor
function t3s.toColor() as ulong
dim as integer r , g , b
r = cint( x * 255 ) and 255
g = cint( y * 255 ) and 255
b = cint( z * 255 ) and 255
return rgb( r , g , b )
end function
operator +( a as t3s , b as t3s ) as t3s
return t3s( a.x + b.x , a.y + b.y , a.z + b.z )
end operator
operator -( a as t3s , b as t3s ) as t3s
return t3s( a.x - b.x , a.y - b.y , a.z - b.z )
end operator
operator /( a as t3s , b as single ) as t3s
return t3s( a.x / b , a.y / b , a.z / b )
end operator
operator *( a as t3s , b as single ) as t3s
return t3s( a.x * b , a.y * b , a.z * b )
end operator
function dot( a as t3s , b as t3s ) as single
return a.x * b.x + a.y * b.y + a.z * b.z
end function
function lenght( a as t3s ) as single
return sqr( dot( a , a ) )
end function
function getangle( a as t3s , b as t3s ) as single
dim as single la , lb , d
d = dot( a , b )
la = lenght( a )
lb = lenght( b )
return acos( d / ( la * lb ) )
end function
function cross( a as t3s , b as t3s ) as t3s
return t3s( a.y * b.z - a.z * b.y _
, a.z * b.x - a.x * b.z _
, a.x * b.y - a.y * b.x )
end function
dim shared as t3s black = t3s( 0 , 0 , 0 )
dim shared as t3s red = t3s( 1 , 0 , 0 )
dim shared as t3s green = t3s( 0 , 1 , 0 )
dim shared as t3s yellow = t3s( 1 , 1 , 0 )
dim shared as t3s blue = t3s( 0 , 0 , 1 )
dim shared as t3s magenta = t3s( 1 , 0 , 1 )
dim shared as t3s cyan = t3s( 0 , 1 , 1 )
dim shared as t3s white = t3s( 1 , 1 , 1 )
Code: Select all
''bluatigro 30-dec-2013
''graphics lib : 3d engine
#ifndef BASIC3D_H
#define BASIC3D_H
#include "math.bas"
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif
''create matrix type to hold position & orentation
type matrix
dim as double m( 3 , 3 )
end type
''create multyplycation operator for matrix
operator * ( a as matrix , b as matrix ) as matrix
dim as integer i , j , k
dim uit as matrix
for i = 0 to 3
for j = 0 to 3
'' uit.m( i , j ) = 0
for k = 0 to 3
'' uit.m( i , j ) += a.m( i , k ) * b.m( k , j )
uit.m( j , k ) += a.m( j , i ) * b.m( i , k )
next k
next j
next i
return uit
end operator
function inverse( q as matrix ) as matrix
dim as single d
dim as matrix uit
d = q.m(0,0)*q.m(1,1)*q.m(2,2) _
- q.m(0,0)*q.m(2,1)*q.m(1,2) _
+ q.m(1,0)*q.m(2,1)*q.m(0,2) _
- q.m(1,0)*q.m(0,1)*q.m(2,2) _
+ q.m(2,0)*q.m(0,1)*q.m(1,2) _
- q.m(2,0)*q.m(1,1)*q.m(0,2)
uit.m(0,0)=(q.m(1,1)*q.m(2,2)-q.m(1,2)*q.m(2,1))/d
uit.m(1,0)=(q.m(0,1)*q.m(2,2)-q.m(0,2)*q.m(2,1))/d
uit.m(2,0)=(q.m(0,1)*q.m(1,2)-q.m(0,2)*q.m(1,1))/d
uit.m(0,1)=(q.m(1,0)*q.m(2,2)-q.m(1,2)*q.m(2,0))/d
uit.m(1,1)=(q.m(0,0)*q.m(2,2)-q.m(0,2)*q.m(2,0))/d
uit.m(2,1)=(q.m(0,0)*q.m(1,2)-q.m(0,2)*q.m(1,0))/d
uit.m(0,2)=(q.m(1,0)*q.m(2,1)-q.m(1,1)*q.m(2,0))/d
uit.m(1,2)=(q.m(0,0)*q.m(2,1)-q.m(0,1)*q.m(2,0))/d
uit.m(2,2)=(q.m(0,0)*q.m(1,1)-q.m(0,1)*q.m(1,2))/d
return uit
end function
''create array of matrix's
dim shared as matrix v( 20 )
''create unity matrix
v( 0 ).m( 0 , 0 ) = 1
v( 0 ).m( 1 , 1 ) = 1
v( 0 ).m( 2 , 2 ) = 1
v( 0 ).m( 3 , 3 ) = 1
''create array to hold skeletal angles
dim shared as single sk( 64 , 2 )
''create some stuf for 3D engine
declare sub link( no as integer , x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , ax as integer , p as integer )
declare sub child( no as integer , x as single , y as single , z as single _
, lim as integer , ax as integer , p as integer )
declare sub spot( byref x as single , byref y as single , byref z as single )
declare sub rotate( byref k as single , byref l as single , deg as single )
dim shared as integer number
dim shared as single cam( 6 )
declare sub camara( x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , zoom as single )
declare function pend( f as single , a as single ) as single
declare sub skelet( no as integer , x as single , y as single , z as single )
const as integer xyz = 0
const as integer xzy = 1
const as integer yxz = 2
const as integer yzx = 3
const as integer zxy = 4
const as integer zyx = 5
function pend( f as single , a as single ) as single
''pendular motion for animation
''looks verry natural
return sin( rad( f ) ) * a
end function
sub skelet( lim as integer , x as single , y as single , z as single )
''set angles of skeletal lim
''for animated avatars
if lim < 0 or lim > 64 then exit sub
sk( lim , 0 ) = x
sk( lim , 1 ) = y
sk( lim , 2 ) = z
end sub
sub camera( x as single , y as single , z as single _
, pan as single , tilt as single , rol as single , zoom as single )
''set look from point & look angles
cam( 0 ) = x
cam( 1 ) = y
cam( 2 ) = z
cam( 3 ) = pan
cam( 4 ) = tilt
cam( 5 ) = rol
cam( 6 ) = zoom
end sub
sub moveCamera( x as single , y as single , z as single _
, pan as single )
rotate x , z , cam( 3 )
cam( 0 ) += x
cam( 1 ) += y
cam( 2 ) += z
cam( 3 ) = ( cam( 3 ) + pan ) mod 360
end sub
Sub link( no As Integer, x As single, y As single, z As single, pan As single, tilt As single, rol As single, ax As Integer, p As Integer )
''set curent matrix wil afect folowing drawing comands
If no < 1 Or no > 20 Then Exit Sub
If p < 0 Or p > 20 Then Exit Sub
If p = no Then Exit Sub
''create some lokal matrix's and fill them
Dim As matrix mp, rotx, roty, rotz, translate
mp = v( p )
rotx = v( 0 )
roty = v( 0 )
rotz = v( 0 )
translate = v( 0 )
rotz.m( 0, 0 ) = Cos( rad( rol ))
rotz.m( 0, 1 ) = -Sin( rad( rol ))
rotz.m( 1, 0 ) = Sin( rad( rol ))
rotz.m( 1, 1 ) = Cos( rad( rol ))
roty.m( 0, 0 ) = Cos( rad( pan ))
roty.m( 0, 2 ) = -Sin( rad( pan ))
roty.m( 2, 0 ) = Sin( rad( pan ))
roty.m( 2, 2 ) = Cos( rad( pan ))
rotx.m( 1, 1 ) = Cos( rad( tilt ))
rotx.m( 1, 2 ) = -Sin( rad( tilt ))
rotx.m( 2, 1 ) = Sin( rad( tilt ))
rotx.m( 2, 2 ) = Cos( rad( tilt ))
translate.m( 3, 0 ) = x
translate.m( 3, 1 ) = y
translate.m( 3, 2 ) = z
''angles can permutate 6 ways
Select Case ax
Case xyz
v( no ) = rotx * roty * rotz * translate * mp
Case xzy
v( no ) = rotx * rotz * roty * translate * mp
Case yxz
v( no ) = roty * rotx * rotz * translate * mp
Case yzx
v( no ) = roty * rotz * rotx * translate * mp
Case zxy
v( no ) = rotz * rotx * roty * translate * mp
Case zyx
v( no ) = rotz * roty * rotx * translate * mp
Case Else
End Select
number = no
End Sub
sub child( no as integer , x as single , y as single , z as single _
, lim as integer , ax as integer , p as integer )
''set curent matrix for lim of animated avatar
''wil efect folowing drawings
if lim < 0 or lim > 64 then exit sub
link no , x , y , z _
, sk( lim , 1 ) , sk( lim , 0 ) , sk( lim , 2 ), ax , p
end sub
sub spot( byref x as single , byref y as single , byref z as single )
''calulate world coordinates from lokal coordinates
''using curent matrix
dim as single hx , hy , hz
dim as integer i
''use curent matrix
i = number
hx = x * v( i ).m( 0 , 0 ) + y * v( i ).m( 1 , 0 ) _
+ z * v( i ).m( 2 , 0 ) + v( i ).m( 3 , 0 )
hy = x * v( i ).m( 0 , 1 ) + y * v( i ).m( 1 , 1 ) _
+ z * v( i ).m( 2 , 1 ) + v( i ).m( 3 , 1 )
hz = x * v( i ).m( 0 , 2 ) + y * v( i ).m( 1 , 2 ) _
+ z * v( i ).m( 2 , 2 ) + v( i ).m( 3 , 2 )
x = hx
y = hy
z = hz
''use camara matrix
x += - cam( 0 )
y += - cam( 1 )
z += - cam( 2 )
x *= cam( 6 )
y *= cam( 6 )
z *= cam( 6 )
rotate x , z , -cam( 3 )
rotate y , z , -cam( 4 )
rotate x , y , -cam( 5 )
end sub
sub lokal( byref x as single , byref y as single _
, byref z as single , q as matrix )
dim as single hx,hy,hz
hx = q.m(0,0)*x+q.m(1,0)*y+q.m(2,0)*z
hy = q.m(0,1)*x+q.m(1,1)*y+q.m(2,1)*z
hz = q.m(0,2)*x+q.m(1,2)*y+q.m(2,2)*z
x = hx
y = hy
z = hz
end sub
sub rotate( byref k as single , byref l as single , deg as single )
''help sub for rotating coordinates
dim as single hk , hl , s , c
s = sin( rad( deg ) )
c = cos( rad( deg ) )
hk = k * c - l * s
hl = k * s + l * c
k = hk
l = hl
end sub
#endif
Code: Select all
''bluatigro 6-nov-2013
''game lib : math helper functions + connstands
#ifndef MATH_H
#define MATH_H
const as single pi = atn( 1 ) * 4
const as single golden_ratio = ( sqr( 5 ) - 1 ) / 2
function rad( x as single ) as single
''help function degrees to radians
return x * pi / 180
end function
function range( l as single , h as single ) as single
return rnd * ( h - l ) + l
end function
function nr( no as integer , m as integer ) as string
return right( "0000000000" + str( no ) , m )
end function
#endif