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

Berichtdoor bluatigro » za okt 12, 2013 11:50 am

ik ben begonnen met t ombouwen van
Code: Selecteer alles
   nomainwin
    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
    GlColorBufferBit=16384
    open "opengl32.dll" for dll as #gl
    WindowWidth=500
    WindowHeight=500
    UpperLeftX=1
    UpperLeftY=1
    graphicbox #main.gr, 1, 1, 480, 480
    open "Triangle 1.0" for window as #main
    print #main,"trapclose [quit]"
    MainH=hwnd(#main.gr)
    #main.gr, "down"
    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
    calldll #gl,"glClear", GlColorBufferBit as long,  ret as long
    calldll #gl,"glRotated", 0 as double, 0 as double, 0 as double, 0 as double, ret as long
    calldll #gl,"glBegin", 4 as long, ret as long
    calldll #gl,"glColor3d", 0 as double, 0 as double, 255 as double, ret as long
    calldll #gl,"glVertex3i", -1 as long, -1 as long, 0 as long, ret as long
    calldll #gl,"glColor3d", 255 as double, 0 as double, 0 as double, ret as long
    calldll #gl,"glVertex3i", 0 as long, 1 as long, 0 as long, ret as long
    calldll #gl,"glColor3d", 0 as double, 255 as double, 0 as double, ret as long
    calldll #gl,"glVertex3i", 1 as long, -1 as long, 0 as long, ret as long
    calldll #gl,"glEnd", ret as void
    calldll #gdi32,"SwapBuffers", MainDC as ulong, ret as long
    #main.gr, "place 220 220"
    #main.gr, "\LB Graphics and OpenGL"
    #main.gr, "color red"
    #main.gr, "circle 100"
    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 #main
    close #gl
    end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » za okt 12, 2013 11:57 am

nu heb ik dit
Code: Selecteer alles
nomainwin
global GLColorBufferBit , MainH , MainDC , GLTRIANGLES
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 )
GlColorBufferBit = 16384
GLTRIANGLES = 4
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global angle
open "opengl32.dll" for dll as #gl
open "Triangle 1.1" for window as #m
  #m , "trapclose [quit]"
  MainH = hwnd( #m )
''  #m , "down"
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
    calldll #gl,"glClear", GlColorBufferBit as long,  ret as long

    call glLoadIdentity
    call glRotate angle , 0 , 0 , 1
    call colortriangle

    calldll #gdi32,"SwapBuffers", MainDC as ulong, ret as long
    angle = angle + 1
wait
function rgba( r , g , b , a )
  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 )
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function
sub colortriangle
  call glBegin GLTRIANGLES
    call setcolor red
    call glVertex 1 , 1 , 0
    call setcolor blue
    call glVertex 0 , -1 , 0
    call setcolor green
    call glVertex -1 , 0 , 0
  call glEnd
end sub
sub glLoadIdentity
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glEnd
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glBegin i
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScalef x , y , z
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
  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
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setcolor clr
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  calldll #gl , "glColor3d" _
  , r as double _
  , g as double _
  , b 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
end sub
[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


om dit verder uit te brijden heb ik meer constanten van opengl nodig
zoals :
- gl_light , glLight0
- gl_diffuse , gl_ambient
- gl_quad

- etc...

waar vind ik die ?
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » zo okt 13, 2013 2:31 pm

wat constanten gevonden in n ander LB prog
Code: Selecteer alles
nomainwin
dim pntx(256),pnty(256),pntz(256)
'=========================================
' 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 )
'===========================================
' opengl consts
'===========================================
global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
'===========================================
' 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 = 1032
'===========================================
' 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 MainH , MainDC ''wil be filled by code
struct D4 _
, x as double _
, y as double _
, z as double _
, w as double
global angle
open "opengl32.dll" for dll as #gl
open "Triangle 1.3" for window as #m
  #m , "trapclose [quit]"
  MainH = hwnd( #m )
''  #m , "down"
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
    calldll #gl,"glClear" _
    , GL.COLOR.BUFFER.BIT as long _
    ,  ret as long

    call glLoadIdentity
    call glRotate angle , 0 , 0 , 1
    call colortriangle

    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long
    angle = angle + 1
wait
function rgba( r , g , b , a )
  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 )
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function
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 )
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function
sub colortriangle
  call glBegin GL.TRIANGLES
    call setcolor red
    call glVertex 1 , 1 , 0
    call setcolor blue
    call glVertex 0 , -1 , 0
    call setcolor green
    call glVertex -1 , 0 , 0
  call glEnd
end sub
sub point no , x , y , z
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub
sub triangle p1 , p2 , p3
  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
  call tri pntx( p1 ) , pnty( p1 ) , pntz( p1 ) _
         , pntx( p2 ) , pnty( p2 ) , pntz( p2 ) _
         , pntx( p3 ) , pnty( p3 ) , pntz( p3 )
end sub
sub tri x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub
sub glLoadIdentity
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glEnd
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glEnable i
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub
sub glBegin i
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScale x , y , z
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
  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
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setcolor clr
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  calldll #gl , "glColor3d" _
  , r as double _
  , g as double _
  , b 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
end sub
[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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » zo okt 13, 2013 2:41 pm

dit werkt echter niet

WAARSCHUWING : code doet vremde dingen

Code: Selecteer alles
nomainwin
dim pntx(256),pnty(256),pntz(256)
'=========================================
' 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 )
'===========================================
' opengl consts
'===========================================
global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global SCREEN
SCREEN = GL.COLOR.BUFFER.BIT or GL.DEPT.BUFFER.BIT
'===========================================
' 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 = 1032
'===========================================
' 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 MainH , MainDC ''will be filled by code

struct D4 _
, x as double _
, y as double _
, z as double _
, w as double

global angle
open "opengl32.dll" for dll as #gl
open "Cube 1.0" for window as #m
  #m , "trapclose [quit]"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]

    calldll #gl,"glClear" _
    , SCREEN as long _
    ,  ret as long

    call glLoadIdentity'
    call glTranslate 0 , 0 , 5
    call glRotate angle , 0 , 1 , 1
    call setMaterial red , red
    call cube 0,0,0 , 1,1,1

    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long
    angle = angle + 1
wait
function rgba( r , g , b , a )
  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 )
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function
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 )
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function
sub colortriangle
  call glBegin GL.TRIANGLES
    call setcolor red
    call glVertex 1 , 1 , 0
    call setcolor blue
    call glVertex 0 , -1 , 0
    call setcolor green
    call glVertex -1 , 0 , 0
  call glEnd
end sub
sub point no , x , y , z
  if no < 0 or no > 256 then exit sub
  pntx( no ) = x
  pnty( no ) = y
  pntz( no ) = z
end sub
sub triangle p1 , p2 , p3
  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
  call tri pntx( p1 ) , pnty( p1 ) , pntz( p1 ) _
         , pntx( p2 ) , pnty( p2 ) , pntz( p2 ) _
         , pntx( p3 ) , pnty( p3 ) , pntz( p3 )
end sub
sub tri x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub
sub cube mx , my , mz , dx , dy , dz
  call point 0 , mx + dx , my + dy , mz + dz
  call point 1 , mx + dx , my + dy , mz - dz
  call point 2 , mx + dx , my - dy , mz + dz
  call point 3 , mx + dx , my - dy , mz - dz
  call point 4 , mx - dx , my + dy , mz + dz
  call point 5 , mx - dx , my + dy , mz - dz
  call point 6 , mx - dx , my - dy , mz + dz
  call point 7 , mx - dx , my - dy , mz - dz
  call quad 0 , 1 , 3 , 2
  call quad 7 , 6 , 4 , 5
  call quad 0 , 1 , 5 , 4
  call quad 7 , 6 , 2 , 3
  call quad 0 , 2 , 6 , 4
  call quad 7 , 5 , 1 , 3
end sub
sub glLoadIdentity
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glEnd
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glEnable i
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub
sub glBegin i
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScale x , y , z
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
  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
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setcolor clr
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  calldll #gl , "glColor3d" _
  , r as double _
  , g as double _
  , b as double _
  , ret as long
end sub
sub setMaterial ambient , diffuse

  D4.x.struct = colorR( ambient )
  D4.y.struct = colorG( ambient )
  D4.z.struct = colorB( ambient )
  D4.w.struct = colorA( ambient )

  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.AMBIENT as long _
  , D4 as struct _
  , ret as long

  D4.x.struct = colorR( diffuse )
  D4.y.struct = colorG( diffuse )
  D4.z.struct = colorB( diffuse )
  D4.w.struct = colorA( diffuse )

  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.DIFFUSE as long _
  , D4 as struct _
  , 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.LIGHTING
  call glEnable GL.LIGHT0
  D4.x.struct = 0
  D4.y.struct = 1
  D4.z.struct = 0
  D4.w.struct = 1
  calldll #gl , "glLightfv" _
  , GL.LIGHT0 as long _
  , GL.POSITION as long _
  , D4 as struct _
  , ret as long
end sub
[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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » di okt 15, 2013 1:39 pm

update :
- ander shape objecten syteem

werkt helaas niet
Code: Selecteer alles
nomainwin
dim pntx(256),pnty(256),pntz(256)

' 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 )

' box object

global bmx , bmy , bmz , bdx , bdy , bdz

' opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global SCREEN
SCREEN = GL.COLOR.BUFFER.BIT or GL.DEPT.BUFFER.BIT

' 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 = 1032

' 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 MainH , MainDC ''will be filled by code

struct D4 _
, x as double _
, y as double _
, z as double _
, w as double

global angle
open "opengl32.dll" for dll as #gl
open "Cube 1.1" for window as #m
  #m , "trapclose [quit]"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]

    calldll #gl,"glClear" _
    , SCREEN as long _
    ,  ret as long

    call glLoadIdentity'
    call glTranslate 0 , 0 , 5
    call glRotate angle , 0 , 1 , 1
    call setMaterial red , red
    call setbox 0,0,0 , 1,1,1
    call cube

    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long

    angle = angle + 1
wait
function rgba( r , g , b , a )
  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 )
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function
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 )
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function
sub point no , x , y , z
  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
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
  call glEnd
end sub
sub setbox mx , my , mz , dx , dy , dz
  bmx = mx
  bmy = my
  bmz = mz
  bdx = dx
  bdy = dy
  bdz = dz
end sub
sub cube
  call glPushMatrix
  call glTranslate bmx , bmy , bmz
  call glScale bdx , bdy , bdz
  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
  call quad 0 , 1 , 3 , 2
  call quad 7 , 6 , 4 , 5
  call quad 0 , 1 , 5 , 4
  call quad 7 , 6 , 2 , 3
  call quad 0 , 2 , 6 , 4
  call quad 7 , 5 , 1 , 3
  call glPopMatrix
end sub
sub glPushMatrix
  calldll #gl , "glPushMatrix" , ret as long
end sub
sub glPopMatrix
  caldll #gl , "glPopMatrix" , ret as long
end sub
sub glLoadIdentity
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glEnd
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glEnable i
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub
sub glBegin i
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScale x , y , z
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
  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
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setcolor clr
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  calldll #gl , "glColor3d" _
  , r as double _
  , g as double _
  , b as double _
  , ret as long
end sub
sub setMaterial ambient , diffuse
''this is a first try
''wil be expanded

  D4.x.struct = colorR( ambient )
  D4.y.struct = colorG( ambient )
  D4.z.struct = colorB( ambient )
  D4.w.struct = colorA( ambient )

  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.AMBIENT as long _
  , D4 as struct _
  , ret as long

  D4.x.struct = colorR( diffuse )
  D4.y.struct = colorG( diffuse )
  D4.z.struct = colorB( diffuse )
  D4.w.struct = colorA( diffuse )

  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.DIFFUSE as long _
  , D4 as struct _
  , 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.LIGHTING
  call glEnable GL.LIGHT0
  D4.x.struct = 0
  D4.y.struct = 1
  D4.z.struct = 0
  D4.w.struct = 1
  calldll #gl , "glLightfv" _
  , GL.LIGHT0 as long _
  , GL.POSITION as long _
  , D4 as struct _
  , ret as long
end sub
[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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » do okt 17, 2013 9:39 am

ik heb wat remarks toegevoegt
zodat jullie kunnen zien wat t zou moeten doen
laat me weten als iets nog niet duidelijk is

t werkt helaas niet
ik denk dat n paar ret as long ret as void moeten worden
Code: Selecteer alles
nomainwin
' swarm of 256 points
' for creating meshes
dim pntx(256),pnty(256),pntz(256)

' color objects
' for use whit lights and material

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 )

' boindigbox object
' for easy center and border of meshes

global bmx , bmy , bmz , bdx , bdy , bdz

' opengl consts

global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
global SCREEN
SCREEN = GL.COLOR.BUFFER.BIT or GL.DEPT.BUFFER.BIT

' primatifs
' to start polygons type

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 = 1032

' 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

'fulscreen

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight

global MainH , MainDC ''will be filled by code

struct D4 _
, x as double _
, y as double _
, z as double _
, w as double

global angle
'load opengl libary
open "opengl32.dll" for dll as #gl
'open a window
open "Cube 1.1" for window as #m
  #m , "trapclose [quit]"
  'get handle of window
  MainH = hwnd( #m )
  'start opengl
  call openglInit
  timer 40 , [tmr]
wait
[tmr]

    'clear opengl window
    calldll #gl,"glClear" _
    , SCREEN as long _
    ,  ret as long

    'reset drawmatrix
    call glLoadIdentity
    'change position of folowing polygons
    call glTranslate 0 , 0 , 5
    'change orientation of folowing polygons
    call glRotate angle , 0 , 1 , 1
    'change material colorobjects of folowing polygons
    call setMaterial red , red
    'set center and border of folowing mesh
    call setbox 0,0,0 , 1,1,1
    'draw a mesh made of polygons
    call cube

    'drawing is on backbuffer
    'put backbuffer on frondbuffer
    'to make drawing visible
    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long

    angle = angle + 1
wait
function rgba( r , g , b , a )
''create a colorobject
  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 part of colorobject
  colorR = int( clr and 255 ) / 256
end function
function colorG( clr )
''get green part of colorobject
  colorG = ( int( clr / 256 ) and 255 ) / 256
end function
function colorB( clr )
''get blue part of colorobject
  colorB = ( int( clr / 256 ^ 2 ) and 255 ) / 256
end function
function colorA( clr )
''get alpha | transparent part of colorobject
  colorA = ( int( clr / 256 ^ 3 ) and 255 ) / 256
end function
function lenght( x1 , y1 , z1 , x2 , y2 , z2 )
''get distance between 2 points
  lenght = sqr( (x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2 ) + 1e-10
end function
function nx( x1 , y1 , z1 , x2 , y2 , z2 )
''get normalized x part of crossproduct of 2 points
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''get normalized y part of crossproduct of 2 points
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''get normalized z part of crossproduct of 2 points
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function
sub point no , x , y , z
''set a point in the swarm
''for creating a mesh
  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 polygon
''use 3 points out of the swarm to draw a triangle
''first some savegarding from iligal points
  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
''get coordinates from swarm
  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 )
''get crosproduct for normal of triangle
  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 )
''set polygon as triangle
  call glBegin GL.TRIANGLES
''set normal of polygon
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
''add 3 points to polygon
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
''end polygon
  call glEnd
end sub
sub quad p1 , p2 , p3 , p4
''draw a polygon
''use 4 points out of the swarm draw a quadangle
''first some savegarding from iligal points
  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
''get coordinates from swarm
  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 )
''get crossproduct for normal quadangle
  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 )
''set polygon as quadangles
  call glBegin GL.QUADS
''set normal of polygon
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
''add 4 points to polygon
    call glVertex x1 , y1 , z1
    call glVertex x2 , y2 , z2
    call glVertex x3 , y3 , z3
    call glVertex x4 , y4 , z4
''end polygon
  call glEnd
end sub
sub setbox mx , my , mz , dx , dy , dz
''set center and border of boudingbox
''for use whit a mesh
  bmx = mx
  bmy = my
  bmz = mz
  bdx = dx
  bdy = dy
  bdz = dz
end sub
sub cube
''draw a cube mesh
''uses boundinbox for center and size
''to a new drawmatrix
  call glPushMatrix
''change position of folowing polygons
  call glTranslate bmx , bmy , bmz
''change size of folowing polygons
  call glScale bdx , bdy , bdz
''set some swarm 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
''use points from the swarm to draw polygons
  call quad 0 , 1 , 3 , 2
  call quad 7 , 6 , 4 , 5
  call quad 0 , 1 , 5 , 4
  call quad 7 , 6 , 2 , 3
  call quad 0 , 2 , 6 , 4
  call quad 7 , 5 , 1 , 3
''go the old drawmatrix
  call glPopMatrix
end sub
sub glPushMatrix
''to a new drawmatrix
  calldll #gl , "glPushMatrix" , ret as long
end sub
sub glPopMatrix
''to the old drawmatrix
  caldll #gl , "glPopMatrix" , ret as long
end sub
sub glLoadIdentity
''rest drawmatrix
  calldll #gl , "glLoadIdentity" _
  , ret as long
end sub
sub glEnd
''end of polygon[s]
  calldll #gl , "glEnd" _
  , ret as void
end sub
sub glEnable i
''make openglobject i actif
  calldll #gl , "glEnable" _
  , i as long _
  , ret as long
end sub
sub glBegin i
''set polygon to mode i
  calldll #gl , "glBegin" _
  , i as long _
  , ret as long
end sub
sub glScale x , y , z
''change folowing poligons in size
  calldll #gl , "glScaled" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glTranslate x , y , z
''change folowing polygons in place
  calldll #gl , "glTranslated" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glRotate a , x , y , z
''change folowing polygons in orientation
''angle in degrees
  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 the polygon
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub setMaterial ambient , diffuse
''set the material colors of the folowing polygons
''get the parts of the color object
  D4.x.struct = colorR( ambient )
  D4.y.struct = colorG( ambient )
  D4.z.struct = colorB( ambient )
  D4.w.struct = colorA( ambient )
''set ambient of material
  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.AMBIENT as long _
  , D4 as struct _
  , ret as long
''get the parts of the colorobject
  D4.x.struct = colorR( diffuse )
  D4.y.struct = colorG( diffuse )
  D4.z.struct = colorB( diffuse )
  D4.w.struct = colorA( diffuse )
''set diffuse of material
  calldll #gl , "glMaterialfv" _
  , GL.FRONT.AND.BACK as long _
  , GL.DIFFUSE as long _
  , D4 as struct _
  , ret as long
end sub
sub openglInit
''start opengl
    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
''set a gl.light0 as sun
  call glEnable GL.LIGHTING
  call glEnable GL.LIGHT0
''fill possition struct
  D4.x.struct = 0
  D4.y.struct = 1
  D4.z.struct = 0
  D4.w.struct = 1
''set position of gl.light0
  calldll #gl , "glLightfv" _
  , GL.LIGHT0 as long _
  , GL.POSITION as long _
  , D4 as struct _
  , ret as long
''gl.light0 has as defalt a white color
''so i dont have to change that
end sub
[quit]
''closing and reseting everything
    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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor Gordon » do okt 17, 2013 11:21 pm

Hallo Bluatigro,

Kun je aangegeven welke DLL's extra nodig zijn?
Waar kun je die downloaden?

Gordon
Avatar gebruiker
Gordon
Site Admin
 
Berichten: 684
Geregistreerd: zo mei 22, 2005 12:50 am

Re: opengl

Berichtdoor bluatigro » vr okt 18, 2013 1:12 pm

@ gordon :

i denk opengl32.dll
i heb geen idee waar die staat
die stond kennelijk al op mijn pc
want de example en de roterende driehoek werken bij mij
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » ma sep 15, 2014 10:42 am

update :
- verdere ombouw van voorbeeld in t begin

Code: Selecteer alles
gosub [initOpenGL]
dim pnt(255,2)
global red , blue , green , angle
red = rgb( 255 , 0 , 0 )
blue = rgb( 0 , 0 , 255 )
green = rgb( 0 , 255 , 0 )
timer 40 , [tmr]
wait
[tmr]
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glRotate angle , 0 , 0 , 1
    call point 0 , -1 , 0 , 0
    call point 1 , 0.86 , 0.5 , 0
    call point 2 , 0.86 , -0.5 , 0
    call tri3 0 , red , 1 , green , 2 , blue
  call swapbuffers
  angle = angle + 1
wait
sub point no , x , y , z
  if no < 0 or no > 255 then exit sub
  pnt(no,0) = x
  pnt(no,1) = y
  pnt(no,2) = z
end sub
sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
  call glBegin GL.TRIANGLES
    call setColor kl1
    call glVertex pnt(p1,0) , pnt(p1,1) , pnt(p1,2)
    call setColor kl2
    call glVertex pnt(p2,0) , pnt(p2,1) , pnt(p2,2)
    call setColor kl3
    call glVertex pnt(p3,0) , pnt(p3,1) , pnt(p3,2)
  call glEnd
end sub
function rgb( r , g , b )
 r = r and 255
 g = g and 255
 b = b and 255
 rgb = r + g * 256 + b * 256 ^ 2
end function
[initOpenGL]
  nomainwin
  struct rect _
  , x as long _
  , y as long _
  , x2 as long _
  , y2 as long
  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
  global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
  global GL.DEPH.BUFFER.BIT : GL.DEPH.BUFFER.BIT = 256
  global GL.TRIANGLES : GL.TRIANGLES = 4
  open "opengl32.dll" for dll as #gl
  open "glu32.dll" for dll as #glu
  WindowWidth=DisplayWidth
  WindowHeight=DisplayHeight
  UpperLeftX=1
  UpperLeftY=1
  open "Triangle" for window_nf as #main
  print #main,"trapclose [quit]"
  global MainH , MainDC , GLContext
  MainH=hwnd(#main)
  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
return
sub glLoadIdentity
  calldll #gl , "glLoadIdentity" , ret as long
end sub
sub glRotate angle , x , y , z
  calldll #gl,"glRotated" _
  , angle as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glBegin code
  calldll #gl,"glBegin", code as long, ret as long
end sub
sub setColor color
  r = color and 255
  g = int( color / 256 ) and 255
  b = int( color / 256 ^ 2 ) and 255
  calldll #gl,"glColor3d" _
  , r as double _
  , g as double _
  , b as double _
  , ret as long
end sub
sub glVertex x , y , z
  calldll #gl,"glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub
sub glEnd
  calldll #gl,"glEnd", ret as void
end sub
sub glClear code
  calldll #gl,"glClear", code as long,  ret as long
end sub
sub swapbuffers
  calldll #gdi32,"SwapBuffers", MainDC as ulong, ret as long
end sub
[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 #main
  close #glu
  close #gl
end
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » vr sep 19, 2014 12:03 pm

update :
- meer functies en subs en constanten
- veel REM

warning :
- ik ben niet zeker dat dit werkt

Code: Selecteer alles
gosub [initOpenGL]
dim pnt(255,2) ''swarm of points
global angle
timer 40 , [tmr]
wait

[tmr]
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glRotate angle , 0 , 0 , 1
    call point 0 , -1 , 0 , 0
    call point 1 , 0.86 , 0.5 , 0
    call point 2 , 0.86 , -0.5 , 0
    call tri3 0 , red , 1 , green , 2 , blue
  call swapbuffers
  angle = angle + 1
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 #main
  close #glu
  close #gl
end

''==========================================================
''    openGL 3d engine
''==========================================================

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 255 then exit sub
  pnt(no,0) = x
  pnt(no,1) = y
  pnt(no,2) = z
end sub

sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
''draw a triangle whit 3 colors from points in the swarm
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  call glBegin GL.TRIANGLES
    call setColor kl1
    call glVertex pnt(p1,0) , pnt(p1,1) , pnt(p1,2)
    call setColor kl2
    call glVertex pnt(p2,0) , pnt(p2,1) , pnt(p2,2)
    call setColor kl3
    call glVertex pnt(p3,0) , pnt(p3,1) , pnt(p3,2)
  call glEnd
end sub

sub quad4 p1 , kl1 , p2 , kl2 , p3 , kl3 , p4 , kl4
''draw a quadangle whit 4 colors from points in the swarm
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  if p4 < 0 or p4 > 255 then exit sub
  call glBegin GL.QUADS
    call setColor kl1
    call glVertex pnt(p1,0) , pnt(p1,1) , pnt(p1,2)
    call setColor kl2
    call glVertex pnt(p2,0) , pnt(p2,1) , pnt(p2,2)
    call setColor kl3
    call glVertex pnt(p3,0) , pnt(p3,1) , pnt(p3,2)
    call setColor kl4
    call glVertex pnt(p4,0) , pnt(p4,1) , pnt(p4,2)
  call glEnd
end sub

''===========================================================
''    color object math
''===========================================================
function rgba( r , g , b , a )
''create a color object
  r = r and 255
  g = g and 255
  b = b and 255
  a = a and 255
  rgb = r + g * 256 + b * 256 ^ 2 + a * 256 ^ 3
end function
function colorR( clr )
''get red channel of color object
  colorR = int( clr ) and 255
end function
function colorG( clr )
''get green channel of color object
  colorG = int( clr / 256 ) and 255
end function
function colorB( clr )
''get blue channel of color object
  colorB = int( clr / 256 ^ 2 ) and 255
end function
function colorA( clr )
''get alpha channel of color object
  colorA = int( clr / 256 ^ 3 ) and 255
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 )
''get x of crossproduct normalized
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function
function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''get y of crossproduct nomalized
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function
function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''get z of crossproduct nomalized
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function

''=============================================================
''     openGL
''=============================================================

[initOpenGL]
''set openGL and color consts
  nomainwin
  struct rect _
  , x as long _
  , y as long _
  , x2 as long _
  , y2 as long
  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
  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 )
  '===========================================
  ' opengl consts
  '===========================================
  global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
  global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
  '===========================================
  ' 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 = 1032
  '===========================================
  ' 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
  open "opengl32.dll" for dll as #gl
  open "glu32.dll" for dll as #glu
  WindowWidth = DisplayWidth
  WindowHeight = DisplayHeight
  open "Triangle" for window_nf as #main
  #main "trapclose [quit]"
  global MainH , MainDC , GLContext
  MainH = hwnd( #main )
  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
return

sub glLoadIdentity
''set the drawingmatrix at standerd
  calldll #gl , "glLoadIdentity" , ret as long
end sub

sub glRotate angle , x , y , z
''rotate the drawingmatrix
  calldll #gl , "glRotated" _
  , angle as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

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

sub glBegin code
''set primativ mode
  calldll #gl , "glBegin" , code as long , ret as long
end sub

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

sub glVertex x , y , z
''create a new point in primativ
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glEnd
''end primativ
  calldll #gl , "glEnd" , ret as void
end sub

sub glClear code
''clear openGL screen : at begin of scene
  calldll #gl , "glClear" , code as long ,  ret as long
end sub

sub swapbuffers
''at end of scene
  calldll #gdi32 , "SwapBuffers" , MainDC as ulong , ret as long
end sub

bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » vr sep 19, 2014 1:03 pm

update :
- poging tot roterende kubus

NIET GETEST !!

Code: Selecteer alles
gosub [initOpenGL]
dim pnt(255,2) ''swarm of points
dim box(5) ''bodingbox
global angle
timer 40 , [tmr]
wait

[tmr]
  call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
  call glLoadIdentity
  call glRotate angle , 1 , 0 , 1
  call setbox  0,0,0  ,  1,1,1
  call cube red , cyan , green , magenta , blue , yellow
  call swapbuffers
  angle = angle + 1
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 #main
  close #glu
  close #gl
end

''==========================================================
''    openGL 3d engine
''==========================================================

sub point no , x , y , z
''set a point in the swarm
  if no < 0 or no > 255 then exit sub
  pnt(no,0) = x
  pnt(no,1) = y
  pnt(no,2) = z
end sub

sub tri p1 , p2 , p3 , kl
''draw a triangle whit 1 color from points in the swarm
  call tri3 p1 , kl , p2 , kl , p3 , kl
end sub
sub tri3 p1 , kl1 , p2 , kl2 , p3 , kl3
''draw a triangle whit 3 colors from points in the swarm
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  call glBegin GL.TRIANGLES
    call setColor kl1
    call glVertex pnt(p1,0) , pnt(p1,1) , pnt(p1,2)
    call setColor kl2
    call glVertex pnt(p2,0) , pnt(p2,1) , pnt(p2,2)
    call setColor kl3
    call glVertex pnt(p3,0) , pnt(p3,1) , pnt(p3,2)
  call glEnd
end sub

sub quad p1 , p2 , p3 , p4 , kl
''draw a quadangle whit 1 color 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 whit 4 colors from points in the swarm
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  if p4 < 0 or p4 > 255 then exit sub
  call glBegin GL.QUADS
    call setColor kl1
    call glVertex pnt(p1,0) , pnt(p1,1) , pnt(p1,2)
    call setColor kl2
    call glVertex pnt(p2,0) , pnt(p2,1) , pnt(p2,2)
    call setColor kl3
    call glVertex pnt(p3,0) , pnt(p3,1) , pnt(p3,2)
    call setColor kl4
    call glVertex pnt(p4,0) , pnt(p4,1) , pnt(p4,2)
  call glEnd
end sub

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

sub cube left , right , front , back , down , up
''create a cube whit 6 colors and whit bodingbox coordinates
''first fil swarm whit points
  point 0 , -1 , -1 , -1
  point 1 , -1 , -1 ,  1
  point 2 , -1 ,  1 , -1
  point 3 , -1 ,  1 ,  1
  point 4 ,  1 , -1 , -1
  point 5 ,  1 , -1 ,  1
  point 6 ,  1 ,  1 , -1
  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



''===========================================================
''    color object math
''===========================================================

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

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

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

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

function colorA( clr )
''get alpha channel of color object
  colorA = int( clr / 256 ^ 3 ) and 255
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 )
''get x of crossproduct normalized
  x = y1 * z2 - y2 * z1
  nx = x / lenght(x1,y1,z1,x2,y2,z2)
end function

function ny( x1 , y1 , z1 , x2 , y2 , z2 )
''get y of crossproduct nomalized
  y = z1 * x2 - z2 * x1
  ny = y / lenght(x1,y1,z1,x2,y2,z2)
end function

function nz( x1 , y1 , z1 , x2 , y2 , z2 )
''get z of crossproduct nomalized
  z = x1 * y2 - x2 * y1
  nz = z / lenght(x1,y1,z1,x2,y2,z2)
end function



''=============================================================
''     openGL
''=============================================================

[initOpenGL]
''set openGL and color consts
  nomainwin
  struct rect _
  , x as long _
  , y as long _
  , x2 as long _
  , y2 as long
  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

  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 )
  '===========================================
  ' opengl consts
  '===========================================
  global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
  global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
  '===========================================
  ' 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 = 1032
  '===========================================
  ' 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

  open "opengl32.dll" for dll as #gl
  open "glu32.dll" for dll as #glu
  WindowWidth = DisplayWidth
  WindowHeight = DisplayHeight
  open "Triangle" for window_nf as #main
  #main "trapclose [quit]"
  global MainH , MainDC , GLContext
  MainH = hwnd( #main )
  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
return

sub glLoadIdentity
''set the drawingmatrix at standerd
  calldll #gl , "glLoadIdentity" , ret as long
end sub

sub glRotate angle , x , y , z
''rotate the drawingmatrix
  calldll #gl , "glRotated" _
  , angle as double _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

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

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

sub glBegin code
''set primativ mode
  calldll #gl , "glBegin" , code as long , ret as long
end sub

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

sub glVertex x , y , z
''create a new point in primativ
  calldll #gl , "glVertex3d" _
  , x as double _
  , y as double _
  , z as double _
  , ret as long
end sub

sub glEnd
''end primativ
  calldll #gl , "glEnd" , ret as void
end sub

sub glClear code
''clear openGL screen : at begin of scene
  calldll #gl , "glClear" , code as long ,  ret as long
end sub

sub swapbuffers
''at end of scene
  calldll #gdi32 , "SwapBuffers" , MainDC as ulong , ret as long
end sub

bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Re: opengl

Berichtdoor bluatigro » za sep 20, 2014 2:57 pm

update :
- ik heb nu n roterende kubus

error :
- er gaat iets mis met t sorteren van de polygonen

Code: Selecteer alles
nomainwin
dim pntx(256),pnty(256),pntz(256),box(5)
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 )

''                                                     opengl consts
global GL.COLOR.BUFFER.BIT : GL.COLOR.BUFFER.BIT = 16384
global GL.DEPTH.BUFFER.BIT : GL.DEPTH.BUFFER.BIT = 256
'                                                      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 = 1032
'                                                       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 MainH , MainDC ''wil be filled by code
struct D4 _
, x as double _
, y as double _
, z as double _
, w as double
global angle
open "opengl32.dll" for dll as #gl
open "Cube 1.0" for window as #m
  #m , "trapclose [quit]"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
    call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
    call glLoadIdentity
    call glRotate angle , 1 , 0 , 1
    call setbox  0,0,0  ,  0.5,0.5,0.5
    call cube red,cyan , green,magenta , blue,yellow

    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long
    angle = angle + 1
wait
''                                             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
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 left , right , front , back , down , up
''create a cube 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 triangle p1 , p2 , p3
''draw a triangle whit 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
  call tri pntx( p1 ) , pnty( p1 ) , pntz( p1 ) _
         , pntx( p2 ) , pnty( p2 ) , pntz( p2 ) _
         , pntx( p3 ) , pnty( p3 ) , pntz( p3 )
end sub
sub tri x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3
''draw a triangle
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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 point in the swarm
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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 setColor clr
''set color of point[s] of polygon
  r = colorR( clr )
  g = colorG( clr )
  b = colorB( clr )
  calldll #gl , "glColor3d" _
  , r as double _
  , g as double _
  , b 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
end sub
[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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

[solved] opengl : rot cube

Berichtdoor bluatigro » ma jul 06, 2015 9:38 am

update :
- sorteren gaat nu wel goed
- sorting of poly's goes now wel

Code: Selecteer alles
''bluatigro 5 jul 2015
''openGL : rotating cube

nomainwin
dim pntx(256),pnty(256),pntz(256),box(5)
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 , 127 )
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 )

''    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 MainH , MainDC ''wil be filled by code
global angle
open "opengl32.dll" for dll as #gl
open "Cube 1.0" for graphics as #m
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "setfocus"
  MainH = hwnd( #m )
  call openglInit
  timer 40 , [tmr]
wait
[tmr]
    call glClear GL.COLOR.BUFFER.BIT or GL.DEPTH.BUFFER.BIT
    call glLoadIdentity
    call glRotate angle , 1 , 0 , 1
    call setbox  0,0,0  ,  0.5,0.5,0.5
    call cube red,cyan , green,magenta , blue,yellow

    calldll #gdi32,"SwapBuffers" _
    , MainDC as ulong _
    , ret as long
    angle = angle + 1
wait

''    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

''    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 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 whit 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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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 point in the swarm
  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
    calldll #gl , "glNormal3f" _
    , x as double _
    , y as double _
    , z as double _
    , ret as long
    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 drawingmatrix
  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 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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

openGL : avatar robot

Berichtdoor bluatigro » ma jul 06, 2015 9:47 am

update :
- voorbeeld avatar

error ;
- als de z waarde van gltranlate in [tmr]
te groot of te klein word zie je de robot [ gedeeltelijk ] niet

Code: Selecteer alles
''bluatigro 5 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 , 127 )
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,lr
arm = 0
elbow = 1
wrist = 2
leg = 3
knee = 4
enkle = 5
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 MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
open "Avatar : robot 1.0" for graphics as #m
  #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 glTranslate 0,0,0
  call glScale 0.3,0.3,0.3
  call glRotate angle , 0,1,0
  f = angle * 5
  a = 30

''example animation
  call skelet arm , pend( f , a ) , 0 , 0
  call skelet elbow , 0 -abs( a ) , 0 , 0
  call skelet arm + lr , pend( f + 180, a ) , 0 , 0
  call skelet elbow + lr , 0-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

''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

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

''    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

''    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 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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

opengl : minion

Berichtdoor bluatigro » ma jul 13, 2015 2:07 pm

update :
- nog n avatar [ n minion ]

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 , 127 )
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 MainH , MainDC ''wil be filled by code

global angle
open "opengl32.dll" for dll as #gl
if instr( Platform$ , "LBB" ) then
  open "Avatar : robot 1.0" for window as #m
else
  open "Avatar : robot 1.0" 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 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 glLoadIdentity
  call glTranslate -.6,0,0
  call glScale 0.3,0.3,0.3
  call glRotate angle , 0,1,0
  call robot

  calldll #gdi32,"SwapBuffers" _
  , MainDC as ulong _
  , ret as long
  angle = angle + 5
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 , 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 child .3 , .7 , -.8 , eye , xyz
    call setbox 0,0,0 , .4,.4,.4
    call sphere 12 , 12 , white
    call setbox 0,0,-.4 , .2,.2,.2
    call sphere 12 , 12 , black
  call glPopMatrix
  call glPushMatrix
  call child -.3 , .7 , -.8 , eye+lr , xyz
    call setbox 0,0,0 , .4,.4,.4
    call sphere 12 , 12 , white
    call setbox 0,0,-.4 , .2,.2,.2
    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

''    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

''    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 h , r , 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
        call point 0,sin(ri)*cos(hi),sin(hi),cos(ri)*cos(hi)
        call point 1,sin(ri2)*cos(hi),sin(hi),cos(ri2)*cos(hi)
        call point 2,sin(ri2)*cos(hi2),sin(hi2),cos(ri2)*cos(hi2)
        call point 3,sin(ri)*cos(hi2),sin(hi2),cos(ri)*cos(hi2)
        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: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Volgende

Keer terug naar Liberty BASIC Code

Wie is er online

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

cron