opengl

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

opengl : goede schaal

Berichtdoor bluatigro » vr jul 17, 2015 10:32 am

update :
- de minion zag er dik uit , nu niet meer

Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

Code: Selecteer alles
[tmr]
  scan
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate .6,.3,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call walk.man angle * 3 , 30
    call minion
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.6,0,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call robot
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : loadObj( file )

Berichtdoor bluatigro » ma jul 20, 2015 1:32 pm

ik probeer *.obj files te lezen

error :
- vtel , vttel , vntel blijven 0

ik ben zo ver
Code: Selecteer alles
''bluatigro 20 jul 2015
''obj file reader

global vtel , vttel , vntel , txt$
open "CUBE.obj" for input as #in
  txt$ = input$( #in , lof( #in ) )
close #in
i = 1
while word$( txt$ , i , chr$( 13 ) ) <> ""
  line$ = word$( txt$ , i , chr$( 13 ) )
  if left$( line$ , 2 ) = "v " then vtel = vtel + 1
  if left$( line$ , 3 ) = "vt " then vttel = vttel + 1
  if left$( line$ , 3 ) = "vn " then vntel = vntel + 1
  i = i + 1
wend
print vtel , vttel , vntel
print txt$

cube.obj
Code: Selecteer alles
#bluatigro 20 jul 2015
#cube obj file
#vertex data
v -1 -1 -1
v -1 -1 1
v -1 1 -1
v -1 1 1
v 1 -1 -1
v 1 -1 1
v 1 1 -1
v 1 1 1
#texture data
vt 0 0
vt 0 1
vt 1 0
vt 1 1
#normal data
vn 1 0 0
vn -1 0 0
vn 0 1 0
vn 0 -1 0
vn 0 0 1
vn 0 0 -1
#faces data v/vt/vn
#left
f 0/0/1 1/1/1 3/3/1 2/2/1
#right
f 7/0/0 6/1/0 4/3/0 5/2/0
#bottom                 
f 0/0/3 1/1/3 5/3/3 4/2/3
#top
f 7/0/2 6/1/2 2/3/2 3/2/2
#front                   
f 0/0/5 2/1/5 6/3/5 4/2/5
#back
f 7/0/4 5/1/4 1/3/4 3/2/4
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : minion met bril

Berichtdoor bluatigro » di jul 21, 2015 9:57 am

update :
- minion heeftv nu een bril
Code: Selecteer alles
sub minion
  call setbox 0 , 1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , yellow
  call setbox 0 , .5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , yellow
  call setbox 0 , -1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , blue
  call setbox 0 , -.5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , blue
  call glPushMatrix
    call glTranslate .4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
  call child .4 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , black
  call glPopMatrix
  call glPushMatrix
  call child -.4 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , black
  call glPopMatrix
  call glPushMatrix
  call child 1 , -.3  , 0 , arm , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , yellow
    call setbox .2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child .2 , -1 , 0 , elbow , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -1 , -.3  , 0 , arm+lr , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , yellow
    call setbox -.2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child -.2 , -1 , 0 , elbow+lr , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist+lr , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child .5 , -1 , 0 , leg , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -.5 , -1 , 0 , leg+lr , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee+lr , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle+lr , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : meer vormen in subs

Berichtdoor bluatigro » di jul 21, 2015 2:06 pm

update :
- meer vormer in de subs
Code: Selecteer alles
''bluatigro 13 jun 2015
''openGL : avatar animation

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2)
global pi : pi = atn( 1 ) * 4

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )
global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "Avatar : robot & minion" for window as #m
else
  open "Avatar : robot & minion" for graphics as #m
end if 
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
  scan
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate .7,.3,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call walk.man angle * 3 , 30
    call minion
  call glPopMatrix
  call glPushMatrix
    call glRotate angle , 0 , 1 , 0
    call setbox 0,0,0 , .3,.3,.3
    call sphere 24 , 24 , 1.1+sin(rad(angle)) , 1.1+cos(rad(angle)) , red
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.7,0,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call robot
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = ( angle + 5 ) mod 360
wait

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr , pend( f - 90 , a ) - a , 0 , 0
end sub

sub robot
''example avatar
  call setbox  0 , 0 , 0  ,  .5 , .1 , .1
  call colorcube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call colorcube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call colorcube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call colorcube
  call glPushMatrix
    call child .45 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.45 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub minion
  call setbox 0 , 1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , yellow
  call setbox 0 , .5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , yellow
  call setbox 0 , -1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , blue
  call setbox 0 , -.5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , blue
  call glPushMatrix
    call glTranslate .4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
  call child .4 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child -.4 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child 1 , -.3  , 0 , arm , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1 , 1 , yellow
    call setbox .2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child .2 , -1 , 0 , elbow , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -1 , -.3  , 0 , arm+lr , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1 , 1 , yellow
    call setbox -.2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child -.2 , -1 , 0 , elbow+lr , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist+lr , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child .5 , -1 , 0 , leg , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -.5 , -1 , 0 , leg+lr , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee+lr , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle+lr , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function
function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function
function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product x
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product y
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product z
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''    3d engine stuf

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub
sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub
function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

''    math

function rad( x )
  rad = x * pi / 180
end function
function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub
sub cylinder s , dx , dz , kl
  if s < 4 then s = 4
  if s > 24 then s = 24
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    for i = 0 to pi * 2 step pi * 2 / s
      i2 = i + pi * 2 / s
      call point 0 , sin( i ) * dx , 1 , cos( i ) * dz
      call point 1 , sin( i2 ) * dx , 1 , cos( i2 ) * dz   
      call point 2 , sin( i2 ) , -1 , cos( i2 )   
      call point 3 , sin( i ) , -1 , cos( i )
      call quad 0 , 1 , 2 , 3 , kl
    next i
  call glPopMatrix
end sub
sub sphere h , r , da , db , kl
  if h < 4 then h = 4
  if h > 24 then h = 24
  if r < 4 then r = 4
  if r > 24 then r = 24
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    for hi = 0-pi/2 to pi/2 step pi*2/h
      hi2 = hi + pi*2/h
      for ri = 0-pi to pi step pi*2/r
        ri2 = ri + pi*2/r
        x = sin( ri ) * cos( hi )
        y = sin( hi )
        z = cos( ri ) * cos( hi )
        call point 0 _
        , abs( x ) ^ da * sgn( x ) _
        , abs( y ) ^ db * sgn( y ) _
        , abs( z ) ^ da * sgn( z )
        x = sin( ri2 ) * cos( hi )
        y = sin( hi )
        z = cos( ri2 ) * cos( hi )
        call point 1 _
        , abs( x ) ^ da * sgn( x ) _
        , abs( y ) ^ db * sgn( y ) _
        , abs( z ) ^ da * sgn( z )
        x = sin( ri2 ) * cos( hi2 )
        y = sin( hi2 )
        z = cos( ri2 ) * cos( hi2 )
        call point 2 _
        , abs( x ) ^ da * sgn( x ) _
        , abs( y ) ^ db * sgn( y ) _
        , abs( z ) ^ da * sgn( z )
        x = sin( ri ) * cos( hi2 )
        y = sin( hi2 )
        z = cos( ri ) * cos( hi2 )
        call point 3 _
        , abs( x ) ^ da * sgn( x ) _
        , abs( y ) ^ db * sgn( y ) _
        , abs( z ) ^ da * sgn( z )
        call quad 0 , 1 , 2 , 3 , kl
      next ri
    next hi
  call glPopMatrix
end sub
sub colorcube
  call cube red,cyan , green,magenta , blue,yellow
end sub
sub cube left , right , front , back , down , up
''create a cube mesh
''whit 6 colors and whit bodingbox coordinates

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2 , left
    call quad 7 , 6 , 4 , 5 , right
    call quad 0 , 2 , 6 , 4 , front
    call quad 7 , 5 , 1 , 3 , back
    call quad 0 , 1 , 5 , 4 , down
    call quad 7 , 6 , 2 , 3 , up
  call glPopMatrix
end sub
sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub
sub tri p1 , p2 , p3 , kl
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.TRIANGLES
    call glNormal x , y , z
    call setColor kl
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
  call glEnd
end sub
sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.QUADS
    call glNormal x , y , z
    call setColor kl
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub
sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub
sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub
sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub
sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3f" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  a = colorA( clr )
  calldll #gl , "glColor4d" _
  , r as double _
  , g as double _
  , b as double _
  , a as double _
  , ret as long
end sub
sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , PixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : torus

Berichtdoor bluatigro » za jul 25, 2015 12:17 pm

update :
- nu met torus vorm
Code: Selecteer alles
''bluatigro 25 jun 2015
''openGL : avatar animation

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2)
global pi : pi = atn( 1 ) * 4

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )
global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "Avatar : robot & minion" for window as #m
else
  open "Avatar : robot & minion" for graphics as #m
end if
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
  scan
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate .6,.3,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call walk.man angle * 3 , 30
''    call minion
  call glPopMatrix
  call glPushMatrix
    call glRotate angle , 1 , 0 , 0
    call setbox 0,0,0 , .5 , .1 , .1
    call torus 12 , 12 , yellow
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.6,0,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
''    call robot
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr , pend( f - 90 , a ) - a , 0 , 0
end sub

sub robot
''example avatar
  call setbox  0 , 0 , 0  ,  .5 , .1 , .1
  call colorcube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call colorcube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call colorcube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call colorcube
  call glPushMatrix
    call child .45 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.45 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub minion
  call setbox 0 , 1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , yellow
  call setbox 0 , .5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , yellow
  call setbox 0 , -1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , blue
  call setbox 0 , -.5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , blue
  call glPushMatrix
    call glTranslate .4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
  call child .4 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child -.4 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child 1 , -.3  , 0 , arm , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1, 1 , yellow
    call setbox .2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child .2 , -1 , 0 , elbow , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -1 , -.3  , 0 , arm+lr , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1 , 1 , yellow
    call setbox -.2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child -.2 , -1 , 0 , elbow+lr , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist+lr , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child .5 , -1 , 0 , leg , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -.5 , -1 , 0 , leg+lr , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee+lr , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle+lr , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function
function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function
function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product x
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product y
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product z
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''    3d engine stuf

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub

sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub

function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

function rad( x )
  rad = x * pi / 180
end function

function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub

sub cylinder s , dx , dz , kl
  if s < 4 then s = 4
  if s > 24 then s = 24
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    for i = 0 to pi * 2 step pi * 2 / s
      i2 = i + pi * 2 / s
      call point 0 , sin( i ) , 1 , cos( i )
      call point 1 , sin( i2 ) , 1 , cos( i2 )
      call point 2 , sin( i2 ) , -1 , cos( i2 )
      call point 3 , sin( i ) , -1 , cos( i )
      call quad 0 , 1 , 2 , 3 , kl
    next i
  call glPopMatrix
end sub

sub sphere a , b , da , db , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  call glPushMatrix
  call glTranslate box( 0 ) , box( 1 ) , box( 2 )
  call glScale box( 3 ) , box( 4 ) , box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi / 2 to pi / 2 - pi / b * 2 step pi / b * 2
      j2 = j + pi / b * 2

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      call point 0 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      call point 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      call point 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      call point 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      call quad 0 , 1 , 2 , 3 , kl
    next j
  next i
  call glPopMatrix
end sub

sub torus a  , b , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  mx = box( 0 )
  my = box( 1 )
  mz = box( 2 )
  dx = box( 3 )
  dy = box( 4 )
  dz = box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi to pi step pi / b * 2
      j2 = j + pi / b * 2
      call point 0 _
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz
      call point 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz
      call point 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz
      call point 3 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz
      call quad 0 , 1 , 2 , 3 , kl
    next j
  next i
end sub

sub colorcube
  call cube red,cyan , green,magenta , blue,yellow
end sub

sub cube left , right , front , back , down , up
''create a cube mesh
''whit 6 colors and whit bodingbox coordinates

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2 , left
    call quad 7 , 6 , 4 , 5 , right
    call quad 0 , 2 , 6 , 4 , front
    call quad 7 , 5 , 1 , 3 , back
    call quad 0 , 1 , 5 , 4 , down
    call quad 7 , 6 , 2 , 3 , up
  call glPopMatrix
end sub

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub

sub tri p1 , p2 , p3 , kl
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.TRIANGLES
    call glNormal x , y , z
    call setColor kl
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.QUADS
    call glNormal x , y , z
    call setColor kl
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub

sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub

sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub

sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub

sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub

sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub

sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub

sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3f" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  a = colorA( clr )
  calldll #gl , "glColor4d" _
  , r as double _
  , g as double _
  , b as double _
  , a as double _
  , ret as long
end sub

sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , pixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : rainbow torus

Berichtdoor bluatigro » zo jul 26, 2015 2:13 pm

update :
- regenboog torus !!
Code: Selecteer alles
''bluatigro 26 jul 2015
''openGL

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2)
global pi : pi = atn( 1 ) * 4

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )
global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "Avatar : robot & minion" for window as #m
else
  open "Avatar : robot & minion" for graphics as #m
end if
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
  scan
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate .6,.3,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
    call walk.man angle * 3 , 30
''    call minion
  call glPopMatrix
  call glPushMatrix
    call glRotate angle , 1 , 0 , 0
    call setbox 0,0,0 , .5 , .1 , .1
    call torus 12 , 12 , -3
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.6,0,0
    call glScale 0.3,0.3,0.3
    call glRotate angle , 0,1,0
''    call robot
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr , pend( f - 90 , a ) - a , 0 , 0
end sub

sub robot
''example avatar
  call setbox  0 , 0 , 0  ,  .5 , .1 , .1
  call colorcube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call colorcube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call colorcube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call colorcube
  call glPushMatrix
    call child .45 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.45 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub minion
  call setbox 0 , 1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , yellow
  call setbox 0 , .5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , yellow
  call setbox 0 , -1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , blue
  call setbox 0 , -.5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , blue
  call glPushMatrix
    call glTranslate .4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
  call child .4 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child -.4 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child 1 , -.3  , 0 , arm , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1, 1 , yellow
    call setbox .2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child .2 , -1 , 0 , elbow , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -1 , -.3  , 0 , arm+lr , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1 , 1 , yellow
    call setbox -.2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child -.2 , -1 , 0 , elbow+lr , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist+lr , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child .5 , -1 , 0 , leg , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -.5 , -1 , 0 , leg+lr , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee+lr , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle+lr , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function rainbow( x )
  r = sin( x ) * 127 + 128
  g = sin( x - pi * 2 / 3 ) * 127 + 128
  b = sin( x + pi * 2 / 3 ) * 127 + 128
  rainbow = rgba( r , g , b , 255 )
end function
function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function
function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function
function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product x
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product y
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product z
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''    3d engine stuf

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub

sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub

function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

function rad( x )
  rad = x * pi / 180
end function

function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub

sub cylinder s , dx , dz , kl
  if s < 4 then s = 4
  if s > 24 then s = 24
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    for i = 0 to pi * 2 step pi * 2 / s
      i2 = i + pi * 2 / s
      call point 0 , sin( i ) , 1 , cos( i )
      call point 1 , sin( i2 ) , 1 , cos( i2 )
      call point 2 , sin( i2 ) , -1 , cos( i2 )
      call point 3 , sin( i ) , -1 , cos( i )
      call quad 0 , 1 , 2 , 3 , kl
    next i
  call glPopMatrix
end sub

sub sphere a , b , da , db , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  call glPushMatrix
  call glTranslate box( 0 ) , box( 1 ) , box( 2 )
  call glScale box( 3 ) , box( 4 ) , box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi / 2 to pi / 2 - pi / b * 2 step pi / b * 2
      j2 = j + pi / b * 2

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      call point 0 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      call point 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      call point 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      call point 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      call quad 0 , 1 , 2 , 3 , kl
    next j
  next i
  call glPopMatrix
end sub

sub torus a  , b , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  mx = box( 0 )
  my = box( 1 )
  mz = box( 2 )
  dx = box( 3 )
  dy = box( 4 )
  dz = box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi to pi step pi / b * 2
      j2 = j + pi / b * 2
      call point 0 _
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz
      call point 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz
      call point 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz
      call point 3 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz
      if kl > 0 then
        call quad 0 , 1 , 2 , 3 , kl
      else
        select case kl
          case -1
            kla = rainbow( i )
            klb = rainbow( i2 )
            call quad4 0 , kla , 1 , kla , 2 , klb , 3 , klb
          case -2
            kla = rainbow( j )
            klb = rainbow( j2 )
            call quad4 0 , kla , 1 , klb , 2 , klb , 3 , kla
          case else
            kla = rainbow( j + i )
            klc = rainbow( ( j + i + j2 + i2 ) / 2 )
            klb = rainbow( j2 + i2 )
            call quad4 0 , kla , 1 , klc , 2 , klb , 3 , klc
        end select
      end if
    next j
  next i
end sub

sub colorcube
  call cube red,cyan , green,magenta , blue,yellow
end sub

sub cube left , right , front , back , down , up
''create a cube mesh
''whit 6 colors and whit bodingbox coordinates

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2 , left
    call quad 7 , 6 , 4 , 5 , right
    call quad 0 , 2 , 6 , 4 , front
    call quad 7 , 5 , 1 , 3 , back
    call quad 0 , 1 , 5 , 4 , down
    call quad 7 , 6 , 2 , 3 , up
  call glPopMatrix
end sub

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub

sub tri p1 , p2 , p3 , kl
  call tri3 p1 , kl , p2 , kl , p3 , kl
end sub

sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.TRIANGLES
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle from points in the swarm
  call quad4 p1 , kl , p2 , kl , p3 , kl , p4 , kl
end sub

sub quad4 p1 , kl1 , p2 , kl2 , p3 , kl3 , p4 , kl4
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.QUADS
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
    call setColor kl4
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub

sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub

sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub

sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub

sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub

sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub

sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub

sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3f" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  a = colorA( clr )
  calldll #gl , "glColor4d" _
  , r as double _
  , g as double _
  , b as double _
  , a as double _
  , ret as long
end sub

sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , pixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : camera

Berichtdoor bluatigro » ma jul 27, 2015 10:42 am

update :
- camera beweging

cursor :
- op , neer , links , rechts
muis :
- draailinks , draairechts , vooruit , achteruit

error :
- ik nis de perspectiviteit
Code: Selecteer alles
''bluatigro 26 jul 2015
''openGL

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2),cam(3)
global pi : pi = atn( 1 ) * 4

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )
global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx , muisx , muisy
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "openGL" for window as #m
else
  open "openGL" for graphics as #m
end if
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "when mouseMove [move]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
  scan
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glRotate 0 - cam( 3 ) , 0 , 1 , 0
  call glTranslate 0 - cam( 0 ) , 0 - cam( 1 ) , 0 - cam( 2 )
''  call glPushMatrix
''    call glTranslate .6,.3,0
''    call glScale 0.3,0.3,0.3
''    call glRotate angle , 0,1,0
''    call walk.man angle * 3 , 30
''    call minion
''  call glPopMatrix
  call glPushMatrix
    for x = -5 to 5
      for z = -5 to 5
        call setbox x,0,z , .1 , .1 , .1
        call colorcube
      next z
    next x
  call glPopMatrix
''  call glPushMatrix
''    call glTranslate -.6,0,0
''    call glScale 0.3,0.3,0.3
''    call glRotate angle , 0,1,0
''    call robot
''  call glPopMatrix
  select case key$
    case chr$( _VK_UP )
      call moveCam 0 , .1 , 0 , 0
    case chr$( _VK_DOWN )
      call moveCam 0 , -.1 , 0 , 0
    case chr$( _VK_LEFT )
      call moveCam -.1 , 0 , 0 , 0
    case chr$( _VK_RIGHT )
      call moveCam .1 , 0 , 0 , 0
    case else
  end select
  key$ = ""
  if muisx < winx / 3 then
    call moveCam 0 , 0 , 0 , 1
  end if
  if muisx > winx * 2 / 3 then
    call moveCam 0 , 0 , 0 , -1
  end if
  if muisy < winy / 3 then
    call moveCam 0 , 0 , .1 , 0
  end if
  if muisy > winy * 2 / 3 then
    call moveCam 0 , 0 , -.1 , 0
  end if

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
[move]
  muisx = MouseX
  muisy = MouseY
wait
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr , pend( f - 90 , a ) - a , 0 , 0
end sub

sub robot
''example avatar
  call setbox  0 , 0 , 0  ,  .5 , .1 , .1
  call colorcube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call colorcube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call colorcube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call colorcube
  call glPushMatrix
    call child .45 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.45 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        call setbox 0 , -.3 , 0 , .05 , .2 , .15
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub minion
  call setbox 0 , 1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , yellow
  call setbox 0 , .5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , yellow
  call setbox 0 , -1 , 0 , 1 , 1 , 1
  call sphere 24 , 24 , 1 , 1 , blue
  call setbox 0 , -.5 , 0 , 1 , .5 , 1
  call cylinder 24 , 1 , 1 , blue
  call glPushMatrix
    call glTranslate .4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
    call glTranslate -.4 , .7 , -.8
    call glRotate 90 , 1 , 0 , 0
    call setbox 0,0,0 , .4,.4,.4
    call cylinder 12 , 1 , 1 , gray
  call glPopMatrix
  call glPushMatrix
  call child .4 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child -.4 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .3,.3,.3
    call sphere 12 , 12 , 1 , 1 , white
    call setbox 0,0,-.3 , .1,.1,.1
    call sphere 12 , 12 , 1 , 1 , black
  call glPopMatrix
  call glPushMatrix
  call child 1 , -.3  , 0 , arm , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1, 1 , yellow
    call setbox .2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child .2 , -1 , 0 , elbow , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -1 , -.3  , 0 , arm+lr , xzy
    call setbox 0,0,0 , .2,.2,.2
    call sphere 12 , 12 , 1 , 1 , yellow
    call setbox -.2,-.5,0 , .1,.5,.1
    call cylinder 12 , 1 , 1 , yellow
    call glPushMatrix
    call child -.2 , -1 , 0 , elbow+lr , xyz
      call setbox 0,-.5,0 , .1,.5,.1
      call cylinder 12 , 1 , 1 , yellow
      call glPushMatrix
      call child 0 , -1 , 0 , wrist+lr , yzx
        call setbox 0,-.3,0 , .15,.3,.15
        call sphere 12 , 12 , 1 , 1 , yellow
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child .5 , -1 , 0 , leg , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
  call child -.5 , -1 , 0 , leg+lr , yxz
    call setbox 0,-.5,0 , .2,.5,.2
    call cylinder 12 , 1 , 1 , blue
    call glPushMatrix
    call child 0 , -1 , 0 , knee+lr , xyz
      call cylinder 12 , 1 , 1 , blue
      call glPushMatrix
      call child 0 , -1 , 0 , enkle+lr , zxy
        call setbox 0,0,-.3 , .2,.2,.4
        call cube gray,gray,gray,gray,gray,gray
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function rainbow( x )
  r = sin( x ) * 127 + 128
  g = sin( x - pi * 2 / 3 ) * 127 + 128
  b = sin( x + pi * 2 / 3 ) * 127 + 128
  rainbow = rgba( r , g , b , 255 )
end function
function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function
function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function
function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product x
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product y
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product z
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''    3d engine stuf

sub setCam x , y , z , a
  cam( 0 ) = x
  cam( 1 ) = y
  cam( 2 ) = z
  cam( 3 ) = a
end sub

sub moveCam x , y , z , a
  call rotate x , z , 0 - cam( 3 )
  cam( 0 ) = cam( 0 ) + x
  cam( 1 ) = cam( 1 ) + y
  cam( 2 ) = cam( 2 ) + z
  cam( 3 ) = ( cam( 3 ) + a ) mod 360 
end sub

sub rotate byref k , byref l , deg
  s = sin( rad( deg ) )
  c = cos( rad( deg ) )
  hk = k * c - l * s
  hl = k * s + l * c
  k = hk
  l = hl
end sub

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub

sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub

function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

function rad( x )
  rad = x * pi / 180
end function

function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub

sub cylinder s , dx , dz , kl
  if s < 4 then s = 4
  if s > 24 then s = 24
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    for i = 0 to pi * 2 step pi * 2 / s
      i2 = i + pi * 2 / s
      call point 0 , sin( i ) , 1 , cos( i )
      call point 1 , sin( i2 ) , 1 , cos( i2 )
      call point 2 , sin( i2 ) , -1 , cos( i2 )
      call point 3 , sin( i ) , -1 , cos( i )
      call quad 0 , 1 , 2 , 3 , kl
    next i
  call glPopMatrix
end sub

sub sphere a , b , da , db , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  call glPushMatrix
  call glTranslate box( 0 ) , box( 1 ) , box( 2 )
  call glScale box( 3 ) , box( 4 ) , box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi / 2 to pi / 2 - pi / b * 2 step pi / b * 2
      j2 = j + pi / b * 2

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      call point 0 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      call point 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      call point 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      call point 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )

      call quad 0 , 1 , 2 , 3 , kl
    next j
  next i
  call glPopMatrix
end sub

sub torus a  , b , kl
  if a < 4 then a = 4
  if a > 24 then a = 24
  if b < 4 then b = 4
  if b > 24 then b = 24
  mx = box( 0 )
  my = box( 1 )
  mz = box( 2 )
  dx = box( 3 )
  dy = box( 4 )
  dz = box( 5 )
  for i = 0-pi to pi  step pi / a * 2
    i2 = i + pi / a * 2
    for j = 0-pi to pi step pi / b * 2
      j2 = j + pi / b * 2
      call point 0 _
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz
      call point 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz
      call point 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz
      call point 3 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz
      if kl > 0 then
        call quad 0 , 1 , 2 , 3 , kl
      else
        select case kl
          case -1
            kla = rainbow( i )
            klb = rainbow( i2 )
            call quad4 0 , kla , 1 , kla , 2 , klb , 3 , klb
          case -2
            kla = rainbow( j )
            klb = rainbow( j2 )
            call quad4 0 , kla , 1 , klb , 2 , klb , 3 , kla
          case else
            kla = rainbow( j + i ) 
            klc = rainbow( ( j + i + j2 + i2 ) / 2 )
            klb = rainbow( j2 + i2 )               
            call quad4 0 , kla , 1 , klc , 2 , klb , 3 , klc
        end select
      end if
    next j
  next i
end sub

sub colorcube
  call cube red,cyan , green,magenta , blue,yellow
end sub

sub cube left , right , front , back , down , up
''create a cube mesh
''whit 6 colors and whit bodingbox coordinates

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2 , left
    call quad 7 , 6 , 4 , 5 , right
    call quad 0 , 2 , 6 , 4 , front
    call quad 7 , 5 , 1 , 3 , back
    call quad 0 , 1 , 5 , 4 , down
    call quad 7 , 6 , 2 , 3 , up
  call glPopMatrix
end sub

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub

sub tri p1 , p2 , p3 , kl
  call tri3 p1 , kl , p2 , kl , p3 , kl
end sub

sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.TRIANGLES
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle from points in the swarm
  call quad4 p1 , kl , p2 , kl , p3 , kl , p4 , kl
end sub

sub quad4 p1 , kl1 , p2 , kl2 , p3 , kl3 , p4 , kl4
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.QUADS
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
    call setColor kl4
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub

sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub

sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub

sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub

sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub

sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub

sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub

sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3f" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  a = colorA( clr )
  calldll #gl , "glColor4d" _
  , r as double _
  , g as double _
  , b as double _
  , a as double _
  , ret as long
end sub

sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , pixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » di apr 18, 2017 10:02 am

update : licht en materialen

error :
robot is groen

Code: Selecteer alles
''bluatigro 15 mrt 2017
''openGL

dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2)
global pi : pi = atn( 1 ) * 4
global tel

struct d4d _
  , a as double _
  , b as double _
  , c as double _
  , d as double

struct d3d _
  , x as double _
  , y as double _
  , z as double

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
global lgray , dgray
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
lgray   = rgba( 181 , 181 , 181 , 255 )
dgray   = rgba( 064 , 064 , 064 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )

global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
global thumb,index.finger,mid.finger,ring.finger
thumb = 7
index.finger = 10
mid.finger = 13
ring.finger = 16
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
global GL.FRONT: GL.FRONT = 1028
global GL.SMOOTH : GL.SMOOTH = 7425
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
nomainwin
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "openGL" for window as #m
else
  open "openGL" for graphics as #m
end if
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  calldll #gl , "glShadeModel" _
  , GL.SMOOTH as ulong _
  , ret as ulong
  call glEnable GL.LIGHTING
  call glLight GL.LIGHT0 , GL.DIFFUSE , lgray
  call glLight GL.LIGHT0 , GL.AMBIENT , dgray
  call d4d.fill 0 , 1 , 0 , 1
  call glLight GL.LIGHT0 , GL.POSITION , dummy
  call glEnable GL.LIGHT0

  timer 40 , [tmr]
wait
[tmr]
  scan

  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate 0,0,0
    call glScale 0.3,0.3,0.3
    call glRotate 30 , 0,1,0
    call walk.man angle , 30
    call glMaterial GL.FRONT , GL.DIFFUSE , white
    call glMaterial GL.FRONT , GL.AMBIENT , white
    call robot 1
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr _
  , pend( f - 90 , a ) - a , 0 , 0
  call skelet thumb , pend( f , 15 ) + 15 , 0 , 0
  for i = 0 to 2
    call skelet index.finger + i _
    , 0 , 0 , pend( f , 15 ) - 15
    call skelet mid.finger + i _
    , 0 , 0 , pend( f - 120 , 15 ) - 15
    call skelet ring.finger + i _
    , 0 , 0 , pend( f + 120 , 15 ) - 15
    call skelet index.finger + i + lr _
    , 0 , 0 , pend( f , 15 ) + 15
    call skelet mid.finger + i + lr _
    , 0 , 0 , pend( f - 120 , 15 ) + 15
    call skelet ring.finger + i + lr _
    , 0 , 0 , pend( f + 120 , 15 ) + 15
  next i
end sub

sub finger i
  call glPushMatrix
    call child 0,-.2,0 , i , xyz
    call setbox 0,-.1,0 , .1,.1,.1
    call cube
    call glPushMatrix
      call child 0,-.2,0 , i+1 , xyz
      call cube
      call glPushMatrix
        call child 0,-.2,0 , i+2 , xyz
        call cube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub hand i
  call setbox 0,-.3,0 , .1,.3,.3
  call cube
  call glPushMatrix
    call child 0,-.2,-.5 , thumb + i , xyz
    call finger thumb + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,-.3 , index.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,0 , mid.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,.3 , ring.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
end sub

sub robot h
''example avatar
  call setbox  0 , 0 , 0  ,  .3 , .1 , .1
  call cube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call cube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call cube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call cube
  call glPushMatrix
    call child .25 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call cube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call cube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call cube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.25 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call cube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call cube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call cube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call cube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call cube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        if h then
          call hand 0
        else
          call setbox 0 , -.3 , 0 , .05 , .2 , .15
          call cube
        end if
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call cube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call cube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        if h then
          call hand lr
        else
          call setbox 0 , -.3 , 0 , .05 , .2 , .15
          call cube
        end if
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function rainbow( x )
  r = sin( rad( x ) ) * 127 + 128
  g = sin( rad( x - 120 ) ) * 127 + 128
  b = sin( rad( x + 120 ) ) * 127 + 128
  rainbow = rgba( r , g , b , 255 )
end function

function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function

function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function

function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function

function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function

function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 )
end function

sub cross x1 , y1 , z1 , x2 , y2 , z2
''normalized cross product
  x = y1 * z2 - y2 * z1
  d3d.x.struct = x / lenght(x1,y1,z1,x2,y2,z2)
  y = z1 * x2 - z2 * x1
  d3d.y.struct = y / lenght(x1,y1,z1,x2,y2,z2)
  z = x1 * y2 - x2 * y1
  d3d.z.struct = z / lenght(x1,y1,z1,x2,y2,z2)
end sub

''    blua3d engine

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub

sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub

function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

function rad( x )
  rad = x * pi / 180
end function

function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub

sub cube
''create a cube mesh

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2
    call quad 7 , 6 , 4 , 5
    call quad 0 , 2 , 6 , 4
    call quad 7 , 5 , 1 , 3
    call quad 0 , 1 , 5 , 4
    call quad 7 , 6 , 2 , 3
  call glPopMatrix
end sub

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub

sub tri p1 , p2 , p3
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  call cross x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1
  call glBegin GL.TRIANGLES
    call glNormal d3d.x.struct , d3d.y.struct , d3d.z.struct
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  call cross x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1
  call glBegin GL.QUADS
    call glNormal d3d.x.struct , d3d.y.struct , d3d.z.struct
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub

sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub

sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub

sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub

sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub

sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub

sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub

sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub setColor kl
''set color of polygon
  r = colorR( kl )
  g = colorG( kl )
  b = colorB( kl )
  a = colorA( kl )
  call d4d.fill r , g , b , a
end sub

sub d4d.fill a , b , c , d
''fill d4d.struct
  d4d.a.struct = a
  d4d.b.struct = b
  d4d.c.struct = c
  d4d.d.struct = d
end sub

function glGetError()
  calldll #gl , "glGetError" _
  , glGetError as ulong
end function

sub glLight no , e , kl
  select case e
    case GL.DIFFUSE
      call setColor kl
      calldll #gl , "glLightfv" _
      , no as long _
      , e as long _
      , d4d as struct _
      , ret as long
      print "light.diffuse = " ; dechex$( kl )
      print "light.ret = " ; dechex$( ret )
      print "error = " ; dechex$( glGetError() )
    case GL.POSITION
      calldll #gl , "glLightfv" _
      , no as long _
      , e as long _
      , d4d as struct _
      , ret as long
    case else
  end select
end sub

sub glMaterial face , e , kl
  if e = GL.SHININESS then
    calldll #gl , "glMaterialf" _
    , face as long _
    , e as long _
    , kl as single _
    , ret as long
  else
    call setColor kl
    calldll #gl , "glMaterialfv" _
    , face as long _
    , e as long _
    , d4d as struct _
    , ret as long
    if tel = 0 then
      print "mat.color = " ; dechex$( kl )
      print "mat.ret = " ; dechex$( ret )
      print "error = " ; dechex$( glGetError() )
      tel = 1
    end if
  end if
end sub

sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , pixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » za mei 13, 2017 11:53 am

update :
tree sub added
Code: Selecteer alles
''bluatigro 11 feb 2015
''openGL

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5),sk(64,2)
global pi : pi = atn( 1 ) * 4

''    color objects

global black , red , green , yellow
global blue , magenta , cyan , white
global gray , pink , purple , orange
black   = rgba( 000 , 000 , 000 , 255 )
red     = rgba( 255 , 000 , 000 , 255 )
green   = rgba( 000 , 255 , 000 , 255 )
yellow  = rgba( 255 , 255 , 000 , 255 )
blue    = rgba( 000 , 000 , 255 , 255 )
magenta = rgba( 255 , 000 , 255 , 255 )
cyan    = rgba( 000 , 255 , 255 , 255 )
white   = rgba( 255 , 255 , 255 , 255 )
gray    = rgba( 127 , 127 , 127 , 255 )
pink    = rgba( 255 , 127 , 127 , 255 )
purple  = rgba( 127 , 000 , 127 , 255 )
orange  = rgba( 255 , 127 , 000 , 255 )
global xyz , xzy , yxz , yzx , zxy , zyx
xyz = 0
xzy = 1
yxz = 2
yzx = 3
zxy = 4
zyx = 5
global arm,elbow,wrist,leg,knee,enkle,eye,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
eye = 6
global thumb,index.finger,mid.finger,ring.finger
thumb = 7
index.finger = 10
mid.finger = 13
ring.finger = 16
lr = 32

''    opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global GL.DEPTH.TEST : GL.DEPTH.TEST = 2929
'     primatifs
global GL.TRIANGLES : GL.TRIANGLES = 4
global GL.QUADS : GL.QUADS = 7
'     light
Global GL.LIGHTING: GL.LIGHTING = 2896
Global GL.LIGHT0: GL.LIGHT0 = 16384
Global GL.AMBIENT: GL.AMBIENT = 4608
Global GL.DIFFUSE: GL.DIFFUSE = 4609
Global GL.SPECULAR: GL.SPECULAR = 4610
Global GL.SHININESS: GL.SHININESS = 5633
Global GL.EMISSION: GL.EMISSION = 5632
Global GL.POSITION: GL.POSITION = 4611
Global GL.FRONT.AND.BACK: GL.FRONT.AND.BACK = 103
''    Fog
Global GL.FOG: GL.FOG = 2912
Global GL.FOG.MODE: GL.FOG.MODE = 2917
Global GL.FOG.DENSITY: GL.FOG.DENSITY = 2914
Global GL.FOG.COLOR: GL.FOG.COLOR = 2918
Global GL.FOG.INDEX: GL.FOG.INDEX = 2913
Global GL.FOG.START: GL.FOG.START = 2915
Global GL.FOG.END: GL.FOG.END = 2916
Global GL.LINEAR: GL.LINEAR = 9729
Global GL.EXP: GL.EXP = 2048
Global GL.EXP2: GL.EXP2 = 2049

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy , winyx
winx = WindowWidth
winy = WindowHeight
winyx = winy / winx

global MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "openGL" for window as #m
else
  open "openGL" for graphics as #m
end if
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
  scan

  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glScale winyx , 1 , 1
  call glPushMatrix
    call glTranslate 0,0,0
    call glScale 0.1,0.1,0.1
    call glRotate angle , 0,1,0
    call tree 5 , "f[xyb]Xyb" , 0.7,45,90,20
  call glPopMatrix

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
wait
[key]
  key$ = right$( Inkey$ , 1 )
  if key$ <> chr$( 27 ) then wait
[quit]
  calldll #gl,"wglMakeCurrent" _
  , 0 as ulong, 0 as ulong, ret as long
  calldll #gl,"wglDeleteContext" _
  , GLContext as ulong, ret as long
  calldll #user32, "ReleaseDC" _
  , MainH as ulong, MainDC as ulong,ret as long
  close #m
  close #gl
end

sub walk.man f , a
''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , abs( a ) , 0 , 0
  call skelet leg , pend( f + 180 , a ) , 0 , 0
  call skelet knee , pend( f + 90 , a ) - a , 0 , 0
  call skelet leg + lr , pend( f , a ) , 0 , 0
  call skelet knee + lr _
  , pend( f - 90 , a ) - a , 0 , 0
  call skelet thumb , pend( f , 15 ) + 15 , 0 , 0
  for i = 0 to 2
    call skelet index.finger + i _
    , 0 , 0 , pend( f , 15 ) - 15
    call skelet mid.finger + i _
    , 0 , 0 , pend( f - 120 , 15 ) - 15
    call skelet ring.finger + i _
    , 0 , 0 , pend( f + 120 , 15 ) - 15
    call skelet index.finger + i + lr _
    , 0 , 0 , pend( f , 15 ) + 15
    call skelet mid.finger + i + lr _
    , 0 , 0 , pend( f - 120 , 15 ) + 15
    call skelet ring.finger + i + lr _
    , 0 , 0 , pend( f + 120 , 15 ) + 15
  next i
end sub

sub tree l , prog$ , f,x,y,z
  if l > 0 then
    for i = 1 to len( prog$ )
      select case mid$( prog$ , i , 1 )
        case "f"
          call setbox 0,1,0 , .1,1,.1
          call colorcube
          call glTranslate 0,2,0
        case "g"
          call glTranslate 0,2,0
        case "["
          call glPushMatrix
        case "]"
          call glPopMatrix
        case "X"
          call glRotate 0-x , 1,0,0
        case "x"
          call glRotate x , 1,0,0
        case "Y"
          call glRotate 0-y , 0,1,0
        case "y"
          call glRotate y , 0,1,0
        case "Z"
          call glRotate 0-z , 0,0,1
        case "z"
          call glRotate z , 0,0,1
        case "b"
          call glScale f,f,f
          call tree l - 1 , prog$ , f,x,y,z
        case else
      end select
    next i
  end if
end sub

sub finger i
  call glPushMatrix
    call child 0,-.2,0 , i , xyz
    call setbox 0,-.1,0 , .1,.1,.1
    call graycube
    call glPushMatrix
      call child 0,-.2,0 , i+1 , xyz
      call graycube
      call glPushMatrix
        call child 0,-.2,0 , i+2 , xyz
        call graycube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

sub hand i
  call setbox 0,-.3,0 , .1,.3,.3
  call graycube
  call glPushMatrix
    call child 0,-.2,-.5 , thumb + i , xyz
    call finger thumb + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,-.3 , index.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,0 , mid.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
  call glPushMatrix
    call child 0,-.6,.3 , ring.finger + i , xyz
    call finger index.finger + i
  call glPopMatrix
end sub

sub robot h
''example avatar
  call setbox  0 , 0 , 0  ,  .3 , .1 , .1
  call colorcube
  call setbox 0 , .75 , 0 , .1 , .5 , .1
  call colorcube
  call setbox 0 , 1.8 , 0 , .2 , .2 , .2
  call colorcube
  call setbox 0 , 1.4 , 0 , .7 , .1 , .1
  call colorcube
  call glPushMatrix
    call child .25 , 0 , 0 , leg , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.25 , 0 , 0 , leg + lr , zyx
    call setbox 0 , -.6 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , knee + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1.2 , 0 , enkle + lr , xyz
        call setbox 0 , 0 , -.2 , .1 , .1 , .3
        call colorcube
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child .65 , 1.3 , 0 , arm , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist , zyx
        if h then
          call hand 0
        else
          call setbox 0 , -.3 , 0 , .05 , .2 , .15
          call colorcube
        end if
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
  call glPushMatrix
    call child -.65 , 1.3 , 0 , arm + lr , xyz
    call setbox 0 , -.5 , 0 , .1 , .4 , .1
    call colorcube
    call glPushMatrix
      call child 0 , -1 , 0 , elbow + lr , xyz
      call colorcube
      call glPushMatrix
        call child 0 , -1 , 0 , wrist + lr , zyx
        if h then
          call hand lr
        else
          call setbox 0 , -.3 , 0 , .05 , .2 , .15
          call colorcube
        end if
      call glPopMatrix
    call glPopMatrix
  call glPopMatrix
end sub

''    color object math

function grijs( x )
  grijs = rgba( x , x , x , 255 )
end function

function rainbow( x )
  r = sin( x ) * 127 + 128
  g = sin( x - pi * 2 / 3 ) * 127 + 128
  b = sin( x + pi * 2 / 3 ) * 127 + 128
  rainbow = rgba( r , g , b , 255 )
end function

function rgba( r , g , b , a )
''create color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgba = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function

function colorR( clr )
''get red channel from color object
  colorR = int( clr and 255 ) / 256
end function

function colorG( clr )
''get green channel from color object
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function

function colorB( clr )
''get blue channel from color object
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function

function colorA( clr )
''get alpha channel from color object
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function

''    vector math

function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function

function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product x
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function

function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product y
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function

function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''normalized cross product z
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''    blua3d engine

sub child x , y , z , lim , ax
  if lim < 0 or lim > 64 then exit sub
  select case ax
    case xyz
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
    case xzy
      call glTranslate x , y , z
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
    case yxz
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,2) , 0,0,1
    case yzx
      call glTranslate x , y , z
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
    case zxy
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,0) , 1,0,0
      call glRotate sk(lim,1) , 0,1,0
    case zyx
      call glTranslate x , y , z
      call glRotate sk(lim,2) , 0,0,1
      call glRotate sk(lim,1) , 0,1,0
      call glRotate sk(lim,0) , 1,0,0
    case else
  end select
end sub

sub skelet no , x , y , z
  if no < 0 or no > 64 then exit sub
  sk( no , 0 ) = x
  sk( no , 1 ) = y
  sk( no , 2 ) = z
end sub

function pend( f , a )
  pend = sin( rad( f ) ) * a
end function

function rad( x )
  rad = x * pi / 180
end function

function sgn( x )
  uit = 0
  if x < 0 then uit = -1
  if x > 0 then uit = 1
  sgn = uit
end function

''    graphics wrapper

sub setbox mx , my , mz , dx , dy , dz
''set bodinging box coordinates
  box(0) = mx
  box(1) = my
  box(2) = mz
  box(3) = dx
  box(4) = dy
  box(5) = dz
end sub

sub colorcube
  call cube red,cyan , green,magenta , blue,yellow
end sub

sub graycube
  kl1 = grijs( 64 )
  kl2 = grijs( 185 )
  call cube white,white,kl1,kl1,kl2,kl2
end sub

sub cube left , right , front , back , down , up
''create a cube mesh
''whit 6 colors and whit bodingbox coordinates

''first fil swarm whit points
  call point 0 , -1 , -1 , -1
  call point 1 , -1 , -1 ,  1
  call point 2 , -1 ,  1 , -1
  call point 3 , -1 ,  1 ,  1
  call point 4 ,  1 , -1 , -1
  call point 5 ,  1 , -1 ,  1
  call point 6 ,  1 ,  1 , -1
  call point 7 ,  1 ,  1 ,  1

''then use points in swarm to draw quads
  call glPushMatrix
    call glTranslate box(0) , box(1) , box(2)
    call glScale box(3) , box(4) , box(5)
    call quad 0 , 1 , 3 , 2 , left
    call quad 7 , 6 , 4 , 5 , right
    call quad 0 , 2 , 6 , 4 , front
    call quad 7 , 5 , 1 , 3 , back
    call quad 0 , 1 , 5 , 4 , down
    call quad 7 , 6 , 2 , 3 , up
  call glPopMatrix
end sub

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub

sub tri p1 , p2 , p3 , kl
  call tri3 p1 , kl , p2 , kl , p3 , kl
end sub

sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
''draw a triangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.TRIANGLES
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle from points in the swarm
  call quad4 p1 , kl , p2 , kl , p3 , kl , p4 , kl
end sub

sub quad4 p1 , kl1 , p2 , kl2 , p3 , kl3 , p4 , kl4
''draw a quadangle from points in the swarm
  if p1 < 0 or p1 > 256 then exit sub
  if p2 < 0 or p2 > 256 then exit sub
  if p3 < 0 or p3 > 256 then exit sub
  if p4 < 0 or p4 > 256 then exit sub
  x1 = pntx( p1 )
  y1 = pnty( p1 )
  z1 = pntz( p1 )
  x2 = pntx( p2 )
  y2 = pnty( p2 )
  z2 = pntz( p2 )
  x3 = pntx( p3 )
  y3 = pnty( p3 )
  z3 = pntz( p3 )
  x4 = pntx( p4 )
  y4 = pnty( p4 )
  z4 = pntz( p4 )
  x = nx( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  y = ny( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  z = nz( x2-x1 , y2-y1 , z2-z1 , x3-x1 , y3-y1 , z3-z1 )
  call glBegin GL.QUADS
    call glNormal x , y , z
    call setColor kl1
    call glVertex x1 , y1 , z1
    call setColor kl2
    call glVertex x2 , y2 , z2
    call setColor kl3
    call glVertex x3 , y3 , z3
    call setColor kl4
    call glVertex x4 , y4 , z4
  call glEnd
end sub

''    openGl

sub glClear code
''clear the openGL screen
  calldll #gl,"glClear" _
  , code as long _
  , ret as long
end sub

sub glLoadIdentity
''set drawingmatrix to standert
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub

sub glPushMatrix
''to new drawingmatrix
  calldll #gl , "glPushMatrix" _
  , ret as long
end sub


sub glPopMatrix
''to old drawingmatrix
  calldll #gl , "glPopMatrix" _
  , ret as long
end sub

sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub

sub glEnable i
''set a item
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub

sub glBegin i
''set polygon mode
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub

sub glScale x , y , z
''scale drawingmatrix
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glTranslate x , y , z
''move drawingmatrix
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glRotate a , x , y , z
''rotate drawinmatrix
  calldll #gl , "glRotated" _
  , a as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glVertex x , y , z
''add a point to a polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glNormal x , y , z
''set normal of point[s]
  calldll #gl , "glNormal3f" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  a = colorA( clr )
  calldll #gl , "glColor4d" _
  , r as double _
  , g as double _
  , b as double _
  , a as double _
  , ret as long
end sub

sub openglInit
  struct PFD _
  , Size as word _
  , Version as word _
  , Flags as long _
  , pixelType as char[1] _
  , ColorBits as char[1] _
  , RedBits as char[1] _
  , RedShift as char[1] _
  , GreenBits as char[1] _
  , GreenShift as char[1] _
  , BlueBits as char[1] _
  , BlueShift as char[1] _
  , AlphaBits as char[1] _
  , AlphaShift as char[1] _
  , AccumBits as char[1] _
  , AccumRedBits as char[1] _
  , AccumGreenBits as char[1] _
  , AccumBlueBits as char[1] _
  , AccumAlphaBits as char[1] _
  , DepthBits as char[1] _
  , StencilBits as char[1] _
  , AuxBuffers as char[1] _
  , LayerType as char[1] _
  , Reserved as char[1] _
  , LayerMask as long _
  , VisibleMask as long _
  , DamageMask as long

  PFD.Version.struct=1
  PFD.ColorBits.struct=24
  PFD.DepthBits.struct=16
  PFD.Size.struct=len(PFD.struct)
  PFD.Flags.struct=37

  calldll #user32,"GetDC" _
  , MainH as ulong, MainDC as ulong
  calldll #gdi32,"ChoosePixelFormat" _
  , MainDC as ulong, PFD as struct, ret as long
  calldll #gdi32, "SetPixelFormat" _
  , MainDC as ulong, ret as long _
  , PFD as struct, t as long
  calldll #gl,"wglCreateContext" _
  , MainDC as ulong, GLContext as ulong
  calldll #gl,"wglMakeCurrent" _
  , MainDC as ulong, GLContext as ulong _
  , ret as long
  call glEnable GL.DEPTH.TEST
end sub
bluatigro
 
Berichten: 304
Geregistreerd: za sep 27, 2008 6:01 pm

Vorige

Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Geen geregistreerde gebruikers. en 1 gast

cron