opengl 3d engine

General FreeBASIC programming questions.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

opengl 3d engine

Post by bluatigro »

this is a try at a 3d-engine whit opengl

jet to build :
- fog
- textures [ need some help whit this ]
- ?

t_3d.bas

Code: Select all

''bluatigro 18 jun 2013
''OOP game lib : 3Dvector
type t3d
  x as single
  y as single
  z as single
  declare constructor()
  declare constructor ( x as single , y as single, z as single )
  declare sub fill( x as single , y as single , z as single )
  declare sub normalize
end type
constructor t3d()
  this.x = 0
  this.y = 0
  this.z = 0
end constructor 
constructor t3d( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z
end constructor 
operator +( a as t3d , b as t3d ) as t3d
  return type( a.x + b.x , a.y + b.y , a.z + b.z )
end operator
operator *( a as t3d , d as single ) as t3d
  return type( a.x * d , a.y * d , a.z * d )
end operator
operator \( a as t3d , b as t3d ) as t3d
  return type( a.y * b.z - a.z * b.y _
             , a.z * b.x - a.x * b.z _
             , a.x * b.y - a.y * b.x )
end operator
operator -( a as t3d , b as t3d ) as t3d
  return type( a.x - b.x , a.y - b.y , a.z - b.z )
end operator
operator /( a as t3d , d as single ) as t3d
  return type( a.x / d , a.y / d , a.z / d )
end operator
sub t3d.fill( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z
end sub
declare function dot( a as t3d , b as t3d ) as single
function dot( a as t3d , b as t3d ) as single
  return a.x * b.x + a.y * b.y + a.z * b.z
end function
declare function length( q as t3d ) as single
function length( q as t3d ) as single
   return sqr( q.x * q.x + q.y * q.y + q.z * q.z ) + 1e-7
end function  
declare function anlge( a as t3d , b as t3d ) as single
function angle( a as t3d , b as t3d ) as single
  return acos( dot( a , b ) _
  / ( length( a ) * length( b ) ) )
end function
sub t3d.normalize
  this /= length( this )
end sub
t_opengl.bas

Code: Select all

#ifndef OPENGL_H
#define OPENGL_H

#include once "GL/gl.bi"
#include once "GL/glu.bi"

#include "t_3d.bas"

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif

const as single PI = atn( 1 ) * 4
const as integer false = 0
const as integer true = not false

dim shared as integer mouse_x
dim shared as integer mouse_y
dim shared as integer mouse_button

dim shared pnt( 255 ) as t3d
dim shared sk( 63 ) as t3d
type Tbox
  m as t3d
  d as t3d
end type
dim shared box as Tbox

const as integer xyz = 0
const as integer xzy = 1
const as integer yxz = 2
const as integer yzx = 3
const as integer zxy = 4
const as integer zyx = 5

declare sub child( x as single , y as single , z as single , ax as integer , lim as integer )
declare function rad( deg as single ) as single
declare function pend( fase as single , amp as single ) as single
declare sub skelet( no as integer , x as single , y as single , z as single )
declare sub animate( anim as integer , f as single , a as single )
declare sub human()
declare sub insect()
declare sub pilko()

declare sub opengl_init()
declare sub material( a as single ptr _
, d as single ptr , s as single )

declare sub isoca( i as integer )
declare sub sphere( h as integer , r as integer _
, a as single , b as single )
declare sub torus( hsides as integer , rsides as integer )
declare sub cilinder( sides as integer ,dx as single , dy as single , top as integer , bot as integer ) 
declare sub cube( )
declare sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )

declare sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
declare sub setcolor( clr as integer )
declare sub setpoint( no as integer , x as single , y as single , z as single )
declare sub tri1( p1 as integer , p2 as integer , p3 as integer , kl as integer )
declare sub tri3( p1 as integer , kl1 as integer , p2 as integer , kl2 as integer , p3 as integer , kl3 as integer )
declare sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer , kl as integer )
declare sub quad2( p1 as integer , kl1 as integer _
, p2 as integer , kl2 as integer _
, p3 as integer , kl3 as integer _
, p4 as integer , kl4 as integer )
declare function hill( x as double ) as integer
declare function mix( kla as integer , f as double , klb as integer ) as integer
declare function rainbow( x as double ) as integer
dim shared as single black( 3 )   = { 0 , 0 , 0 , 1 }
dim shared as single red( 3 )     = { 1 , 0 , 0 , 1 }
dim shared as single green( 3 )   = { 0 , 1 , 0 , 1 }
dim shared as single dgreen( 3 )  = { 0 , .5 , 0 , 1 }
dim shared as single yellow( 3 )  = { 1 , 1 , 0 , 1 }
dim shared as single blue( 3 )    = { 0 , 0 , 1 , 1 }
dim shared as single magenta( 3 ) = { 1 , 0 , 1 , 1 }
dim shared as single cyan( 3 )    = { 0 , 1 , 1 , 1 }
dim shared as single white( 3 )   = { 1 , 1 , 1 , 1 }

dim shared as single orange( 3 )   = { 1 , .5 , 0 , 1 }
dim shared as single gray( 3 )     = { .5 , .5 , .5 , 1 }

const as integer body = 0 
const as integer arm = 1
const as integer elbow = 2 
const as integer wrist = 3
const as integer leg = 4
const as integer knee = 5 
const as integer enkle = 6 
const as integer neck = 7
const as integer eye = 8 
const as integer ileg = 4 
const as integer iknee = 9 
const as integer wing = 14
const as integer tail = 16
const as integer sensor = 17
const as integer thumb = 18
const as integer finger = 19
const as integer lr = 32

const as integer human_walk = 1
const as integer dog_walk = 2
const as integer I_FLY = 3
const as integer I_LEFT_LEGS = 4
const as integer I_LEFT_BOX = 5
const as integer I_RIGHT_LEGS = 6
const as integer I_RIGHT_BOX = 7
const as integer I_STING = 8
const as integer I_STAND = 9



function lenght( a as t3d ) as single
  return sqr( a.x ^ 2 + a.y ^ 2 + a.z ^ 2 ) + 0.00001
end function
sub child( x as single , y as single , z as single , lim as integer , ax as integer )
  glTranslatef x , y , z
  select case ax
    case xyz
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
    case xzy
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).y , 0 , 1 , 0
    case yxz
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
    case yzx
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).x , 1 , 0 , 0
    case zxy
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).y , 0 , 1 , 0
    case zyx
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).x , 1 , 0 , 0
    case else
  end select  
end sub
sub skelet( no as integer , x as single , y as single , z as single )
  sk( no and 63 ).fill x , y , z
end sub
sub animate( anim as integer , f as single , a as single )
  DIM I AS INTEGER
  select case anim
  case human_walk
    skelet arm , pend( f , a ) , 0 , 0
    skelet elbow , -abs( a ) , 0 , 0
    skelet arm + lr , pend( f + 180, a ) , 0 , 0
    skelet elbow + lr , -abs( a ) , 0 , 0
    skelet leg , pend( f + 180 , a ) , 0 , 0
    skelet knee , pend( f + 90 , a ) + a , 0 , 0
    skelet leg + lr , pend( f , a ) , 0 , 0
    skelet knee + lr , pend( f - 90 , a ) + a , 0 , 0
  case dog_walk
    skelet arm , pend( f + 180 , a ) , 0 , 0
    skelet elbow , pend( f + 90 , a ) + a , 0 , 0
    skelet arm + lr , pend( f , a ) , 0 , 0
    skelet elbow + lr , pend( f - 90 , a ) + a , 0 , 0
    skelet leg , pend( f + 180 , a ) , 0 , 0
    skelet knee , pend( f + 90 , a ) + a , 0 , 0
    skelet leg + lr , pend( f , a ) , 0 , 0
    skelet knee + lr , pend( f - 90 , a ) + a , 0 , 0
    skelet tail , -45 , pend( f * 2 , a ) , 0
    skelet neck , 0 , 0 , 0
    skelet neck + lr , 0 , 0 , 0
  Case I_FLY
    For i = 0 To 1
      skelet wing + i, 0 , 0 , Pend(f, a)
      skelet wing+lr + i, 0,0, Pend(f, -a)
    Next
  Case I_LEFT_BOX
    skelet arm, 0, Pend(f, -a) + 45 , 0
    skelet elbow, 0, Pend(f, a * 2) - 60 , 0
  Case I_LEFT_LEGS
    For i = 0 To 4
      skelet ileg + i, 0 , 0, Pend(f + i * 180, a)
      skelet iknee + i, Pend(f + i * 180 + 90, a) , 0 , 0
    Next
  Case I_RIGHT_BOX
    skelet arm+lr, 0, Pend(f, a) - 45,0
    skelet elbow+lr, 0, Pend(f, -a * 2) + 60, 0
  Case I_RIGHT_LEGS
    For i = 0 To 4
      skelet ileg+lr+ i, 0,0, Pend(f + i * 180, a)
      skelet iknee+lr + i, Pend(f + i * 180 + 90, a),0,0
    Next
  Case I_STAND
    skelet arm, 0, 45, 0
    skelet elbow, 0, -60 , 0
    skelet finger, 0, 0, 0
    skelet thumb, 0, 0, 0
    skelet arm+lr, 0, -45, 0
    skelet elbow+lr, 0, 60 , 0
    skelet finger+lr, 0, 0, 0
    skelet thumb+lr, 0, 0, 0
    skelet tail, 10, 0 , 0
    skelet tail+lr, 10, 0 , 0
  Case I_STING
    skelet tail, 10 + Pend(f, a), 0, 0
    skelet tail+lr, 10 - Pend(f, a), 0, 0
  case else
    dim i as integer
    for i = 0 to 63
      skelet i , 0,0,0
    next i
  end select
end sub

function rad( deg as single ) as single
  return deg * PI / 180
end function
function pend( fase as single , amp as single ) as single
  return sin( rad( fase ) ) * amp
end function
sub opengl_init()

''  DIM AS INTEGER winx, winy, bitdepth
''  SCREENINFO winx, winy, bitdepth
''  SCREENRES winx, winy, 32, , FB.GFX_FULLSCREEN

  screen 20 , 32 , , 2

	'' ReSizeGLScene
	glViewport 0, 0, 1024, 768                     '' Reset The Current Viewport
	glMatrixMode GL_PROJECTION                     '' Select The Projection Matrix
	glLoadIdentity                                 '' Reset The Projection Matrix
	gluPerspective 45.0, 640.0/480.0, 0.1, 100.0   '' Calculate The Aspect Ratio Of The Window
	glMatrixMode GL_MODELVIEW                      '' Select The Modelview Matrix
	glLoadIdentity                                 '' Reset The Modelview Matrix
	
	'' All Setup For OpenGL Goes Here
	glShadeModel GL_SMOOTH                         '' Enable Smooth Shading
	glClearColor 0.0, 0.0, 0.0, 1               '' Black Background
	glClearDepth 1.0                               '' Depth Buffer Setup
	glEnable GL_DEPTH_TEST                         '' Enables Depth Testing
	glDepthFunc GL_LEQUAL                          '' The Type Of Depth Testing To Do
	glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations

  glEnable( gl_lighting )
  dim as single lightpos( 3 ) = { 0 , 50 , 0 , 1 }
  dim as single diffuse( 3 ) = { 1 , 1 , 1 , 1 }
  glLightfv( gl_light0 , gl_position, @lightpos(0) )
  glLightfv( gl_light0 , gl_diffuse , @diffuse(0) )
  glEnable( gl_light0 )
  
end sub
sub pilko()
    setbox 0,0,0 , .3,.3,.3
    isoca 3
    setbox 0,0,-.5 , .3,.3,.3
    isoca 3
    glpushmatrix
      child .15 , -.3 , 0 , arm , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , elbow , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child .15 , -.3 , -.5 , leg , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , knee , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child -.15 , -.3 , 0 , arm + lr , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , elbow + lr , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child -.15 , -.3 , -.5 , leg + lr , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , knee + lr , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      dim i as single
      child 0,.3, -.7 , tail , xyz
      for i = 0 to 4
        setbox 0,i*.15,0 , .1 , .1  , .1
        isoca 2
      next i
    glpopmatrix
    glpushmatrix
      child 0,.15,.15 , neck , xyz
      glPushMatrix
        child 0,.15,.15,neck+lr, zyx
        setbox 0,0,0 , .2 , .2 , .2
        isoca 3
        setbox .2, .2 , 0 , .07 , .07 , .07
        isoca 2
        setbox -.2 , .2 , 0 , .07 , .07 , .07
        isoca 2
        setbox 0,0,.24 , .07 , .07 , .07
        isoca 2
        glPushMatrix
          child .1 , .14 , .14 , eye , xyz
          setbox 0,0,0 , .07 , .07 , .07 
          glmaterialfv gl_front , gl_ambient , @white( 0 )
          glmaterialfv gl_front , gl_diffuse , @white( 0 )
          isoca 2
          setbox 0,0,.07 , .05,.05,.05
          glmaterialfv gl_front , gl_ambient , @black( 0 )
          glmaterialfv gl_front , gl_diffuse , @black( 0 )
          isoca 2
        glPopMatrix
        glPushmatrix
          child -.1 , .14 , .14 , eye + lr , xyz
          setbox 0,0,0 , .07 , .07 , .07 
          glmaterialfv gl_front , gl_ambient , @white( 0 )
          glmaterialfv gl_front , gl_diffuse , @white( 0 )
          isoca 2
          setbox 0,0,.07 , .05,.05,.05
          glmaterialfv gl_front , gl_ambient , @black( 0 )
          glmaterialfv gl_front , gl_diffuse , @black( 0 )
          isoca 2
        glPopMatrix
      glPopMatrix
    glpopmatrix
end sub

sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri1 p1 , p2 , p3 , 0
  else
  dim p12 as integer , p13 as integer , p23 as integer
    p12 = 255 - no * 3
    p13 = 255 - no * 3 - 1
    p23 = 255 - no * 3 - 2
    pnt( p12 ) = ( pnt( p1 ) + pnt( p2 ) ) / 2
    pnt( p13 ) = ( pnt( p1 ) + pnt( p3 ) ) / 2
    pnt( p23 ) = ( pnt( p2 ) + pnt( p3 ) ) / 2
    pnt( p12 ) = pnt( p12 ) / lenght( pnt( p12 ) )
    pnt( p13 ) = pnt( p13 ) / lenght( pnt( p13 ) )
    pnt( p23 ) = pnt( p23 ) / lenght( pnt( p23 ) )
    geo no - 1 , p1 , p12 , p13
    geo no - 1 , p2 , p23 , p12
    geo no - 1 , p3 , p13 , p23
    geo no - 1 , p12 , p23 , p13
  end if
end sub
sub isoca( i as integer )
  if i < 0 then i = 0
  if i > 5 then i = 5
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z 
  glScalef box.d.x , box.d.y , box.d.z
    
  setpoint  1 ,  0       ,  0 , 1.118034
  setpoint  2 ,  1       ,  0         ,  .5 
  setpoint  3 ,  .309017 ,  .95105654 ,  .5 
  setpoint  4 , -.809017 ,  .58778524 ,  .5 
  setpoint  5 , -.809017 , -.58778524 ,  .5 
  setpoint  6 ,  .309017 , -.95105654 ,  .5 
  setpoint  7 ,  .809017 ,  .58778524 , -.5 
  setpoint  8 , -.309017 ,  .95105654 , -.5 
  setpoint  9 , -1       ,  0         , -.5 
  setpoint 10 , -.309017 , -.95105654 , -.5
  setpoint 11 ,  .809017 , -.58778524 , -.5 
  setpoint 12 ,  0       ,  0         , -1.118034
  dim t as integer
  for t = 1 to 12
    pnt( t ) = pnt( t ) / lenght( pnt( t ) )
  next t
  geo i , 1 ,  2 , 3
  geo i , 1 ,  3 ,  4 
  geo i , 1 ,  4 ,  5 
  geo i , 1 ,  5 ,  6 
  geo i , 1 ,  6 ,  2 
  geo i , 2 ,  7 ,  3
  geo i , 3 ,  7 ,  8 
  geo i , 3 ,  8 ,  4
  geo i , 4 ,  8 ,  9 
  geo i , 4 ,  9 ,  5 
  geo i , 5 ,  9 , 10 
  geo i , 5 , 10 ,  6 
  geo i , 6 , 10 , 11 
  geo i , 6 , 11 ,  2
  geo i , 2 , 11 ,  7 
  geo i , 12 ,  8 ,  7
  geo i , 12 ,  9 ,  8
  geo i , 12 , 10 ,  9 
  geo i , 12 , 11 , 10 
  geo i , 12 ,  7 , 11 
  glPopMatrix
end sub
sub sphere( a as integer , b as integer _
, da as single , db as single )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z
  glScalef box.d.x , box.d.y , box.d.z
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 step PI / b * 2 
      j2 = j + PI / b * 2 

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

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
  glPopMatrix
end sub
sub torus( a as integer , b as integer )
  dim i as single , j as single , i2 as single , j2 as single
  if a < 3 then a = 3 
  if a > 64 then a = 643
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as single , my as single , mz as single , dx as single , dy as single , dz as single 
  mx = box.m.x 
  my = box.m.y 
  mz = box.m.z 
  dx = box.d.x 
  dy = box.d.y 
  dz = box.d.z 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
end sub
sub cilinder( sides as integer , dx as single , dy as single , top as integer , bot as integer )
  dim f as single
  if sides < 3 then sides = 3
  if sides > 64 then sides = 64
  for f = 0 to sides + 2
    setpoint f , box.m.x + sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z
    setpoint f + sides + 1 , box.m.x + sin( f * pi * 2 / sides ) * dx _
                           , box.m.y + box.d.y _
                           , box.m.z + cos( f * pi * 2 / sides ) * dy
  next f
  for f = 0 to sides + 1
    quad f , f + 1 , f + 2 + sides , f + 1 + sides , 0
  next f
  if top then
    setpoint 255 , 0 , box.m.y + box.d.y , 0
    for f = 0 to sides
        setpoint f , box.m.x + sin( f * pi * 2 / sides ) * dx _
               , box.m.y + box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * dy  
    next f
    for f = 0 to sides
      tri1 255 , f , f + 1 , 0
    next f
  end if
  if bot then
    setpoint 255 , 0 , box.m.y - box.d.y , 0
    for f = 0 to sides + 2
        setpoint f , box.m.x - sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z  
    next f
    for f = 0 to sides + 2
      tri1 255 , f , f + 1 , 0
    next f
  end if
end sub
sub cube()
  setpoint 0 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 1 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 2 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 3 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  setpoint 4 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 5 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 6 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 7 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  quad 0 , 2 , 3 , 1 , 0 ''right
  quad 7 , 6 , 4 , 5 , 0 ''left
  quad 0 , 4 , 5 , 1 , 0 ''up
  quad 7 , 3 , 2 , 6 , 0 ''down
  quad 0 , 4 , 6 , 2 , 0 ''back
  quad 7 , 5 , 1 , 3 , 0 ''front
end sub
sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )
  box.m.x = mx
  box.m.y = my
  box.m.z = mz
  box.d.x = dx
  box.d.y = dy
  box.d.z = dz
end sub
sub setcolor( clr as integer )
  dim r as single , g as single ,b as single
  r = ( clr and 255 ) / 256
  g = ( ( clr / 256 ) and 255 ) / 256
  b = ( ( clr / 256 ^ 2 ) and 255 ) / 256
  glcolor3f( r , g , b )
end sub
sub tri1( p1 as integer , p2 as integer , p3 as integer , kl as integer )
  tri3 p1 , kl , p2 , kl , p3 , kl
end sub
sub tri3( p1 as integer , kl1 as integer , p2 as integer , kl2 as integer , p3 as integer , kl3 as integer )
  dim q as t3d
  q = ( pnt( p2 ) - pnt( p1 ) ) \ ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_TRIANGLES  
    glnormal3f q.x , q.y , q.z 
    setcolor kl1
    glVertex3f pnt( p1 ).x , pnt( p1 ).y , pnt( p1 ).z
    setcolor kl2
    glVertex3f pnt( p2 ).x , pnt( p2 ).y , pnt( p2 ).z
    setcolor kl3
    glVertex3f pnt( p3 ).x , pnt( p3 ).y , pnt( p3 ).z
  glend
end sub
sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer , kl as integer )
  dim q as t3d
  q = ( pnt( p2 ) - pnt( p1 ) ) \ ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_QUADS
    setcolor kl
    glnormal3f q.x , q.y , q.z 
    glVertex3f pnt( p1 ).x , pnt( p1 ).y , pnt( p1 ).z
    glVertex3f pnt( p2 ).x , pnt( p2 ).y , pnt( p2 ).z
    glVertex3f pnt( p3 ).x , pnt( p3 ).y , pnt( p3 ).z
    glVertex3f pnt( p4 ).x , pnt( p4 ).y , pnt( p4 ).z
  glEnd
end sub
sub quad2( p1 as integer , kl1 as integer _
, p2 as integer , kl2 as integer _
, p3 as integer , kl3 as integer _
, p4 as integer , kl4 as integer )
  dim q as t3d
  q = ( pnt( p2 ) - pnt( p1 ) ) \ ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_QUADS
    setcolor kl1
    glnormal3f q.x , q.y , q.z 
    glVertex3f pnt( p1 ).x , pnt( p1 ).y , pnt( p1 ).z
    setcolor kl2
    glVertex3f pnt( p2 ).x , pnt( p2 ).y , pnt( p2 ).z
    setcolor kl3
    glVertex3f pnt( p3 ).x , pnt( p3 ).y , pnt( p3 ).z
    setcolor kl4
    glVertex3f pnt( p4 ).x , pnt( p4 ).y , pnt( p4 ).z
  glEnd
end sub
sub setpoint( no as integer , x as single , y as single , z as single )
  pnt( no and 255 ).x = x
  pnt( no and 255 ).y = y
  pnt( no and 255 ).z = z
end sub

sub insect()
  Dim i as integer
glPushmatrix
  glScalef .01 , .01 , .01
  setbox 0, 0, 0, 30, 10.0, 60.0
  Cube
  For i = 0 To 4
    glPushMatrix
      child 35.0, 0.0, i * 25 - 50 , ileg + i, xyz
      setbox 30.0, 0.0, 0.0, 30.0, 5.0, 5.0
      Cube
      glPushMatrix
        child 65.0, -5.0, 0.0 , iknee + i, xyz
        setbox 0.0, -30.0, 0.0, 5.0, 30.0, 5.0
        Cube
      glPopMatrix
    glPopMatrix
    glpushMatrix
      child -35.0, 0.0, i * 25 - 50, ileg + lr + i, xyz
      setbox -30.0, 0.0, 0.0, 30.0, 5.0, 5.0
      Cube 
      glPushMatrix
        child -65.0, -5.0, 0.0 , iknee + lr + 1, xyz
        setbox 0.0, -30.0, 0.0, 5.0, 30.0, 5.0
        Cube
      glPopmatrix
    glPopMatrix
  Next
  glPushMatrix
    child 0 , 0 , -50 , tail , xyz
    For i = 0 To 9
      glPushMatrix
        child 0.0, 0.0, -30.0 , tail, xyz
        setbox 0.0, 0.0, -15.0, 10.0, 10.0, 10.0
        Cube
    Next
    for i = 0 to 8
        glPushMatrix
          child 0 , 0 , -30 , tail+lr , xyz
          cube
    next i
    for i = 0 to 8
        glPopMatrix
      glPopMatrix
    next i
  glPopMatrix
  glPushMatrix
    child 30.0, 0.0, 65.0, arm, xyz
    setbox 0.0, 0.0, 30.0, 5.0, 5.0, 30.0
    Cube
    glPushMatrix
      child 0.0, 0.0, 65.0, elbow, xyz
      Cube
      glPushmatrix
        child 0.0, 0.0, 65.0 , wrist, xyz
        glPushmatrix
          child -10.0, 0.0, 5.0 , thumb, xyz
          Cube
        glPopMatrix
        glPushMatrix
          child 5.0, 0.0, 5.0, finger, xyz
          setbox 0.0, 0.0, 30.0, 5.0, 10.0, 30.0
          Cube
        glPopMatrix
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -30.0, 0.0, 65.0, arm + lr, xyz
    setbox 0.0, 0.0, 30.0, 5.0, 5.0, 30.0
    Cube
    glPushMatrix
      child 0.0, 0.0, 65.0, elbow +lr, xyz
      Cube
      glPushMatrix
        child 0.0, 0.0, 65.0, wrist+lr, xyz
        glPushMatrix
          child 10.0, 0.0, 5.0, thumb+lr, xyz
          Cube
        glPopMatrix
        glPushMatrix
          child -5.0, 0.0, 5.0, finger+lr, xyz
          setbox 0.0, 0.0, 30.0, 5.0, 10.0, 30.0
          Cube
        glPopMatrix
      glPopMatrix
    glPopMatrix
   glPopMatrix
   For i = 0 To 1
     glPushMatrix
       child 20.0, 20.0, 40.0 - 50.0 * i, wing + i, xyz
       setbox 60.0, 0.0, 8.0, 60.0, 2.0, 16.0
       Cube
     glPopMatrix
     glPushMatrix
       child -20.0, 20.0, 40.0 - 50.0 * i , wing+lr + i,  xyz
       setbox -60.0, 0.0, 8.0, 60.0, 2.0, 16.0
       Cube
     glPopmatrix
   Next
glPopMatrix
end sub
sub human()
  setbox  0 , 0 , 0  ,  .5 , .1 , .1
  cube 
  setbox 0 , .75 , 0 , .1 , .5 , .1
  cube 
  setbox 0 , 1.8 , 0 , .2 , .2 , .2
  cube 
  setbox 0 , 1.4 , 0 , .7 , .1 , .1
  cube 
  glPushMatrix
    child .45 , 0 , 0 , leg , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle , xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.45 , 0 , 0 , leg + lr , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee + lr , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle + lr , xyz 
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child .65 , 1.3 , 0 , arm , xyz
    setbox 0 , -.5 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow , xyz
      cube 
      glPushMatrix
        child 0 , -1 , 0 , wrist , zyx
        setbox 0 , -.3 , 0 , .05 , .2 , .15
        cube
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.65 , 1.3 , 0 , arm + lr , xyz
    setbox 0 , -.5 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow + lr , xyz
      cube 
      glPushMatrix
        child 0 , -1 , 0 , wrist + lr , zyx
        setbox 0 , -.3 , 0 , .05 , .2 , .15
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
end sub
  
#endif
main

Code: Select all

#include "t_opengl.bas"
opengl_init
dim as single a
dim as integer state = 0
do
      
  glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT      '' Clear Screen And Depth Buffer
  
  glLoadIdentity
  gltranslatef 0 , 0 , -5
  glrotatef a , 0 , 1 , 0
  select case state
    case 0 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 
      glrotatef a , 1 , 0 , 0
    case else
  end select
  
  glmaterialfv GL_FRONT_AND_BACK , GL_DIFFUSE , @red( 0 )
  glmaterialfv GL_FRONT_AND_BACK , GL_AMBIENT , @red( 0 )
  
  setbox 0,0,0 , 1,1,1
  select case state
    case 0 
      sphere 64 , 64 _
      , cos( rad( a * 3 ) ) + 1.1 _
      , sin( rad( a * 3 ) ) + 1.1
    case 1
      sphere 12 , 12 , 1 , 1
    case 2
      animate DOG_WALK , a * 3 , 30
      pilko
    case 3
      animate HUMAN_WALK , a * 3 , 30
      human
    case 4
      cube
    case 5
      torus 12 , 12
    case 6
      torus 64 , 64
    case 7
      isoca 0
    case 8
      isoca 1
    case 9
      isoca 2
    case 10
      isoca 3
    case 11
      isoca 4
    case else
      animate I_STAND , 0 , 0
      insect
  end select
  a += 1
  a = a mod 360
  if a = 359 then state += 1
  state = state mod 13
  sleep 40
  flip
loop while inkey <> chr( 27 )
  

dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: opengl 3d engine

Post by dodicat »

Runs well bluatigro.

For textures, you can transfer a freebasic image to Quads.
Either the image could be from a bitmap or just something you make up yourself.

Simple example, a made up image - planted onto quad faces, seven in all, one full background quad and then the six faces of a cube:
I've made it kinda blua to suit this thread.
I've set full screen, but maybe you won't get full screen, depend on your desktop settings I suppose!

Code: Select all


#include once "fbgfx.bi"
#Include Once "GL/glu.bi"
#include "GL/glext.bi"

Enum
    opengl      = 2
    fullscreen  = 1
    #define plus Or
End Enum

Dim Shared As Integer xres,yres
Screen 20,32,,opengl plus fullscreen
Screeninfo xres,yres

'Simple structure just to hold corners of one quad
Type pair
    As single x,y
End Type
Operator *(x As Double,n As pair) As pair
Return Type<pair>(x*n.x,x*n.y)
End Operator


'Create a FreeBasic image
Sub CreateFBimageBackground(Byref im2 As Any Ptr)
    Dim As Single minx,maxx,miny,maxy,lasty,grad
    #define dist(x1,y1,x2,y2) Sqr((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
    #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
    #macro paintsketch(_function,r,g,b)
    For x As Double=minx To maxx Step (maxx-minx)/5000
        Dim As Double x1=(xres)*(x-minx)/(maxx-minx)
        Dim As Double y1=(yres)*(_function-maxy)/(miny-maxy)
        grad=y1-lasty
        lasty=y1
        grad=grad*250
        Line im2,(x1,0)-(x1,yres-y1),Rgb(r+grad,g+grad,b)
    Next x
    #endmacro
    #macro _window(topleftX,topleftY,bottomrightX,bottomrightY)
    minx=topleftX
    maxx=bottomrightX
    miny=bottomrightY
    maxy=topleftY
    #endmacro
    For x As Integer=0 To xres
        For y As Integer=0 To yres
            Var d=dist(x,y,(.8*xres),(.9*yres))
            Var c=map(0,800,d,255,50)
            Pset im2,(x,y),Rgb(0,0,c)
        Next y
    Next x
    _window(-5,3,25,-1.2)
    paintsketch(.05*Sin(x)+.05*Sin(2*x),100,100,50)
    _window(5,2,30,-.8) 
    paintsketch(.1*Sin(x),100,100,0)
    
    _window(1,2,12,-.6) 
    paintsketch(.1*Sin(x),100,100,0)  
    _window(0,2,8,-.5)
    paintsketch(.2*Sin(x),100,100,0)
End Sub

'Set a Quad to hold image
Sub setbackgroundquad(e() As pair)
    Dim As Single r1=xres/yres,r2=1 'same ratio as screen
    Dim As Single n=1 'left open for a fiddle around
    e(1)=n*Type(-r1,r2)
    e(2)=n*Type(r1,r2)
    e(3)=n*Type(r1,-r2)
    e(4)=n*Type(-r1,-r2)
End Sub

Sub DrawBackGroundQuad(e() As pair)
    Dim As Single n=1
    glLoadIdentity() 
    glTranslatef(0,0,-2) 'adjust the z translate for a good fit
    glbegin gl_quads
    glTexCoord2f( 0,n )
    glvertex3f(e(1).x,e(1).y,0)
    glTexCoord2f( n,n )
    glvertex3f(e(2).x,e(2).y,0)
    glTexCoord2f( n,0 )
    glvertex3f(e(3).x,e(3).y,0)
    glTexCoord2f( 0,0 )
    glvertex3f(e(4).x,e(4).y,0)
    glend
End Sub

Sub glsetup 
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0) 
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    
End Sub

'Transfer FB image to OpenGL
Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


'Rotate and draw the cube with texturing on each face
Sub DrawGlCube(Byref rotangle As Single)
    glLoadIdentity() 
    glTranslatef(0,0,-5)
    glRotatef(rotangle,1,.5,.25)           ' Rotate 
    glBegin(GL_QUADS)
    
    glTexCoord2f( 1,1 )
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (top)
    glTexCoord2f( 0,1 )
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (top)
    glTexCoord2f( 0,0 )
    glVertex3f(-1.0, 1.0, 1.0)            ' Bottom left of the quad (top)
    glTexCoord2f( 1,0 )
    glVertex3f( 1.0, 1.0, 1.0)            ' Bottom right of the quad (top)
    
    glTexCoord2f( 1,1 )       
    glVertex3f( 1.0,-1.0, 1.0)            ' Top right of the quad (bottom)
    glTexCoord2f( 0,1 )
    glVertex3f(-1.0,-1.0, 1.0)            ' Top left of the quad (bottom)
    glTexCoord2f( 0,0 )
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (bottom)
    glTexCoord2f( 1,0 )
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom right of the quad (bottom)
    
    glTexCoord2f( 1,1 )
    glVertex3f( 1.0, 1.0, 1.0)            ' Top right of the quad (front)
    glTexCoord2f( 0,1 )
    glVertex3f(-1.0, 1.0, 1.0)            ' Top left of the quad (front)
    glTexCoord2f( 0,0 )
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom left of the quad (front)
    glTexCoord2f( 1,0 )
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom right of the quad (front)
    
    glTexCoord2f( 1,1 )
    glVertex3f( 1.0,-1.0,-1.0)            ' Bottom left of the quad (back)
    glTexCoord2f( 0,1 )
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom right of the quad (back)
    glTexCoord2f( 0,0 )
    glVertex3f(-1.0, 1.0,-1.0)            ' Top right of the quad (back)
    glTexCoord2f( 1,0 )
    glVertex3f( 1.0, 1.0,-1.0)            ' Top left of the quad (back)
    
    glTexCoord2f( 1,1 )
    glVertex3f(-1.0, 1.0, 1.0)            ' Top right of the quad (left)
    glTexCoord2f( 0,1 )
    glVertex3f(-1.0, 1.0,-1.0)            ' Top left of the quad (left)
    glTexCoord2f( 0,0 )
    glVertex3f(-1.0,-1.0,-1.0)            ' Bottom left of the quad (left)
    glTexCoord2f( 1,0 )
    glVertex3f(-1.0,-1.0, 1.0)            ' Bottom right of the quad (left)
    
    glTexCoord2f( 1,1 )
    glVertex3f( 1.0, 1.0,-1.0)            ' Top right of the quad (right)
    glTexCoord2f( 0,1 )
    glVertex3f( 1.0, 1.0, 1.0)            ' Top left of the quad (right)
    glTexCoord2f( 0,0 )
    glVertex3f( 1.0,-1.0, 1.0)            ' Bottom left of the quad (right)
    glTexCoord2f( 1,0 )
    glVertex3f( 1.0,-1.0,-1.0)
    glend
End Sub

'Some variables
Dim As gluint tex1
Dim As Any Ptr background=Imagecreate(xres,yres)
Dim  As pair BackgroundCorners(1 To 4)


CreateFBimageBackground(background)
setbackgroundquad(BackgroundCorners())

'NOW START OPENGL
glsetup
'transfer freebasic image to openGL
settexture(tex1,background)
'enable texturing
glEnable( GL_TEXTURE_2D )
Dim As Single angle

Do
    angle=angle+1
    glClear(GL_COLOR_BUFFER_BIT)
    
    'freebasic image is planted onto the background quad
    DrawBackGroundQuad(BackgroundCorners())
    
    'standard rotate cube
    'with image planted to each face
    glEnable (GL_CULL_FACE)
    DrawGlcube(angle)
    gldisable(GL_CULL_FACE)
    glend
    Flip
    Sleep 1,1
Loop Until Inkey=Chr(27)
Imagedestroy background
 
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: opengl 3d engine

Post by bluatigro »

tanks dodicat for help

it is a bit slow om my pc

i want create a setMaterial sub
so i can automate a view lines

Code: Select all

sub setMaterial( glenum as long , ambient as ? , diffuse as ? , etc... )
  glmaterialfv glenum , GL_AMBENT , @ambient( 0 )
  glmaterialfv glenum , GL_DIFFUSSE , @diffuse( 0 )

  etc...

end sub
need some help whit etc... to

on the moment i got only 1 light [ the sun ]
? ;
- how to create a spotlight
- how to create a pointlight
- more kinds of light possible
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: opengl 3d engine

Post by integer »

dodicat wrote:...
Simple example, a made up image - planted onto quad faces, seven in all, one full background quad and then the six faces of a cube:
I've made it kinda blua to suit this thread.
I've set full screen, but maybe you won't get full screen, depend on your desktop settings I suppose!

Code: Select all

#include once "fbgfx.bi"
#Include Once "GL/glu.bi"
#include "GL/glext.bi"
...
Screen 20,32,,opengl plus fullscreen
 
bluatigro wrote:...
it is a bit slow om my pc
The code compiles, no errors.
After approximately 1 minute it is an all white screen with no image.
About how long should it take to generate the image?
using fb 0.90.1 xp sp3 running at 2.4 GHZ

I varied the statement:
Screen 19,8,,opengl plus fullscreen
Screen 19,16,,opengl plus fullscreen
Screen 19,32,,opengl plus fullscreen
Screen 20,8,,opengl plus fullscreen
Screen 20,16,,opengl plus fullscreen
Screen 20,32,,opengl plus fullscreen
Screen 21,8,,opengl plus fullscreen
Screen 21,16,,opengl plus fullscreen
Screen 21,32,,opengl plus fullscreen

All had same result:
Fullscreen, no color (all white), no image

Any suggestions as to what could be a problem?
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: opengl 3d engine

Post by bluatigro »

@ integer : it works on my pc
i m a littlebit new at FB + openGL

i want to solve my last question like this :

Code: Select all

type t_4d
  dim as single x , y , z , w
  declare sub fill( x as single , y as single , z as single , w as single )
end type
sub t_4d.fill( x as single , y as single , z as single , w as single )
  this.x = x
  this.y = y
  this.z = z
  this.w = w
end sub
type t_light
  dim as t_4d possition , ambient , diffuse , specular
  dim as single spot_constant , spot_exponent , spot_cutoff ,  constand_a , linear_a , quaddratic_a
  declare constructor
end type
constructor t_light
  this.constant_a = 1
end constructor
''glenum = GL_LIGHT0 ... GL_LIGHT7
sub setLight( glenum as long , light as t_light )
  glLightfv( glenum , GL_POSSITION , @light.possition.x )

  etc...

end sub
type t_material
  dim as t_4d ambient , diffuse , specular , emission
  dim as single shinines
end type
''glenum = GL_FRONT_AND_BACK , GL_FRONT or GL_BACK
sub setMaterial( glenum as long , material as t_material )
  glMaterialfv( glenum , GL_AMBIENT , @material.amient.x )
  glMaterialfv( glenum , GL_DIFFUSE , @material.diffuse.x )
  glMaterialfv( glenum , GL_SPECULAR , @material.specular.x )
  glMaterialfv( glenum , GL_EMISSION , @material.emission.x )
  glMaterialf( glenum , GL_SHININES , @material.shinnes )
end sub
of corse i wil need to change the colors from single[ 4 ] type to t_4d type

i did not test this jet

wil this work ?
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: opengl 3d engine

Post by bluatigro »

and i m thinking about fog to

Code: Select all

type t_fog
  dim as t_4d fog_color
  dim as single fog_end , fog_start , fog_density
  declare sub activate( mode as long )
end type
''mode = GL_LINEAR , GL_EXP or GL_EXP2
sub t_fog.activate( mode as long )
  glFogi GL_FOG_MODE , mode 
  glFogf GL_FOG_END , this.fog_end 
  glFogf GL_FOG_END , this.fog_start 
  glFogfv GL_FOG_COLOR , @this.color.x 
  glFogf GL_FOG_DENSITY , this.fog_density 
  glEnable GL_FOG
end sub
dim shared as t_fog fog
it may need some tinkering
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: opengl 3d engine

Post by dodicat »

Integer.
WinXP sp3 here.
fb.90.1
3GHZ
The image load should be just about immediate.
Perhaps the GL version at fault.
Here's mine:
Renderer: Quadro NVS 285/PCIe/SSE2
Vendor: NVIDIA Corporation
Version: 2.1.2

Extensions:

1 GL_ARB_color_buffer_float
2 GL_ARB_compressed_texture_pixel_storage
3 GL_ARB_conservative_depth
4 GL_ARB_copy_buffer
5 GL_ARB_depth_clamp
6 GL_ARB_depth_texture

blah
blah

A known bug with fb.90.1 is you cannot move a windowed screen
Perhaps you could skip full screen anyway.
Just
Screen 20,32,,2

What is your OpenGL version?

Code: Select all



#Include "GL/gl.bi"
Type d2
    As Integer x,y,w,h,index
    As Double col(1 To 8)
    As String caption
End Type
Dim Shared As Double capcol(1 To 4)
capcol(4)=1 'black captions here

Dim As Integer fileflag
#define _d as double 'save space
#macro SetValues(num,_index,_x,_y,_w,_h,_caption)
num.x=_x:num.y=_y:num.w=_w:num.h=_h:num.index=_index:num.caption=_caption
#endmacro

Function inbox(p1() As d2,p As d2,i As Integer) As Integer
    Return (p.x>p1(i).x)*(p.x<(p1(i).x+p1(i).w))*(p.y>p1(i).y)*(p.y<(p1(i).y+p1(i).h))
End Function


Sub setcolour(a() As d2,i As Integer,r1 _d,g1 _d,b1 _d,a1 _d,r2 _d,g2 _d,b2 _d,a2 _d)
    a(i).col(1)=r1
    a(i).col(2)=g1
    a(i).col(3)=b1
    a(i).col(4)=a1
    a(i).col(5)=r2
    a(i).col(6)=g2
    a(i).col(7)=b2
    a(i).col(8)=a2
End Sub

Dim As d2 slider(1 to 7)
'get indexes (not really required here)
For z As Integer=1 To Ubound(slider):slider(z).index=z:Next
    'the four boxes for vertical scroller
    setcolour(slider(),1,.8,.8,.8,1,.8,.8,.8,1)
    setcolour(slider(),2,0,0,1,.5,0,0,1,.5)
    setcolour(slider(),3,0,1,0,1,0,1,0,1)
    setcolour(slider(),4,0,1,0,1,0,1,0,1)
    'not for scroller
    setcolour(slider(),5,1,0,0,1,1,0,0,1)
    setcolour(slider(),6,1,1,1,1,1,1,1,1)
    setcolour(slider(),7,0,1,1,1,0,1,1,1)
    
    'the four boxes for vertical scroller
    SetValues(slider(1),1,785,15,15,570,"")'long vert
    SetValues(slider(2),2,785,15,15,30,chr(176)) 'short moving bit
    SetValues(slider(3),3,785,0,15,15,Chr(24))'top box
    SetValues(slider(4),4,785,585,15,15,Chr(25))'base box
    'not for scroller
    SetValues(slider(5),5,700,0,50,20,"File")
    SetValues(slider(6),6,700,20,50,20,"Save")
    SetValues(slider(7),7,700,40,50,20,"Quit")
    
    Sub thickline(x1 As Double,_
        y1 As Double,_
        x2 As Double,_
        y2 As Double,_
        thickness As Double,_
        col() As Double)
        glend
        glLineWidth(1)
        glBegin (GL_LINES)
        Dim As Integer xres,yres
        Screeninfo xres,yres
        y1=yres-y1
        y2=yres-y2
        Dim As Double yp,s,h,c
        h=Sqr((x2-x1)^2+(y2-y1)^2)
        If h=0 Then h=1e-6
        s=(y1-y2)/h
        c=(x2-x1)/h
        For yp=-thickness/2 To thickness/2 
            glColor4f (col(1),col(2),col(3),col(4) )
            glVertex2f (x1+(s*yp),y1+(c*yp) )
            glColor4f (col(5),col(6),col(7),col(8))
            glVertex2f (x2+(s*yp),y2+(c*yp))
        Next yp
    End Sub
    
    Sub GL2dsetup
        Dim As Integer xres,yres
        Screeninfo xres,yres
        'glMatrixMode (GL_PROJECTION)
        'glLoadIdentity ()
        glOrtho (0,xres, 0,yres, -1, 1)
        glMatrixMode (GL_MODELVIEW)
        glDisable (GL_DEPTH_TEST)
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
        glEnable (GL_BLEND)
        glEnable (GL_LINE_SMOOTH)
        glLineWidth(4)
    End Sub
    'the font maker
    Sub drawstring(xpos As Integer,ypos As Integer,text As String,colour() As Double,size As Single,textangle As Single=0,charangle As Single=0,im As Any Pointer=0)
    Dim As Integer wx,wy
    Screeninfo wx,wy
    glend
    glLineWidth(1.1*size)
    glBegin (GL_LINES) 
    Type point2d
        As Single x,y
    End Type
    Dim As Integer flag,codenum=256 
    If Instr(text,"|") Then flag=1
    Static As Integer runflag
    Static As point2d infoarray()
    Redim Preserve As point2d infoarray(64,codenum) '64 = 8 x 8 pixel size
    If runflag=0 Then   '                  'scan codenum of codepage once
        Dim As Uinteger background=0
        Screenres 10,10  '8 x 8 pixels on this screen
        Dim count As Integer
        For ch As Integer=1 To codenum
            Cls
            Draw String(1,1),Chr(ch)
            For x As Integer=1 To 8  'scan for characters
                For y As Integer=1 To 8
                    If Point(x,y)<>background Then
                        count=count+1
                        infoarray(count,ch)=Type<point2d>(x,y)'save pixel position
                    End If 
                Next y
            Next x
            count=0
        Next ch
        runflag=1 
    End If
    If size=0 Then Exit Sub
    Dim As point2d temp(1 To 64,codenum),np
    Dim As Single cr= 0.01745329 'degs to radians
    #macro rotate(p1,p2,a,d)
    np.x=d*(Cos(a*cr)*(p2.x-p1.x)-Sin(a*cr)*(p2.y-p1.y)) +p1.x
    np.y=d*(Sin(a*cr)*(p2.x-p1.x)+Cos(a*cr)*(p2.y-p1.y)) +p1.y
    #endmacro
    
    Dim As point2d cpt(1 To 64),c=Type<point2d>(xpos,ypos),c2
    Dim As Integer dx=xpos,dy=ypos
    For z6 As Integer=1 To Len(text)
        var asci=text[z6-1]
        If asci=124 Then 
            If charangle<>0 Then xpos=xpos+12*Sin(charangle*cr)
            dx=xpos:dy=dy+12:Goto skip 'pipe | for new line
        End If
        For _x1 As Integer=1 To 64
            temp(_x1,asci).x=infoarray(_x1,asci).x+dx
            temp(_x1,asci).y=infoarray(_x1,asci).y+dy
            rotate(c,temp(_x1,asci),textangle,size)
            cpt(_x1)=np
            var copyy=np.y
            If charangle<>0 Then
                dim as integer p
                If flag Then  p=1 Else  p=(z6-1)
                c2=Type<point2d>(xpos+(size*8)*p*(Cos(textangle*cr)),ypos+(size*8)*p*(Sin(textangle*cr))) 
                rotate(c2,cpt(_x1),charangle,1)
                If flag Then np.y=copyy
                cpt(_x1)=np
            End If
            If infoarray(_x1,asci).x<>0 Then 'paint only relevant points
                If Abs(size)>0 Then
                    glColor4f (colour(1),colour(2),colour(3),colour(4))
                    glVertex3f (cpt(_x1).x-size/2,wy-(cpt(_x1).y-size/2),0)
                    glColor4f (colour(1),colour(2),colour(3),colour(4))
                    glVertex3f (cpt(_x1).x+size/2,wy-(cpt(_x1).y-size/2),0)
                End If
            End If
        Next _x1
        dx=dx+8+4*(Sin(charangle*cr))*flag
        skip:
    Next z6 
    glend
End Sub
Sub init Constructor 'automatic loader
    Dim As Double col(1 To 4)
    drawstring(0,0,"",col(),0)
    Screen 0
End Sub
Function framecounter() As Double
    Static As Double frame,fps
    frame=frame+1
    Static As Double t1,t2
    If frame>=fps Then
        t1 = Timer
        fps = frame/(t1-t2)
        t2=Timer
        frame=0
    End If
    Function=fps
End Function
    Sub drawbox(p() As d2,i As Integer,col() As Double,th As Single=1,cap As String="")
        thickline(p(i).x+.5*p(i).w,p(i).y,p(i).x+.5*p(i).w,p(i).y+p(i).h,th,col())
        If cap<>"" Then
            Dim As Single l=Len(cap)
            Dim As Single startx=p(i).x+.5*p(i).w-l*1.5*8/2
            Dim As Single starty=p(i).y+.5*p(i).h-4*1.5
            drawstring(startx,starty,cap,capcol(),1.5)
        End If
        
    End Sub
    Sub string_split(s_in As String,char As String,result() As String)
        Dim As String s=s_in,var1,var2
        Dim As Long n,pst
        #macro split(stri,char,var1,var2)
        pst=Instr(stri,char)
        var1="":var2=""
        If pst<>0 Then
            var1=Mid(stri,1,pst-1)
            var2=Mid(stri,pst+1)
        Else
            var1=stri
            Endif
            Redim Preserve result(1 To 1+n-((Len(var1)>0)+(Len(var2)>0)))
            result(n+1)=var1
            #endmacro
            
            Do
                split(s,char,var1,var2):n=n+1:s=var2
            Loop Until var2=""
            Redim Preserve result(1 To Ubound(result)-1)
        End Sub
        
        Sub savefile(r As String,v As String,ver As String,t() As String)
            Open "mygl.txt" For Output As #7
            Print #7,"Renderer:  ";r
            Print #7,"Vendor:    ";v
            Print #7,"Version:   ";ver
            Print #7,""
            Print #7,"Extensions:"
            Print #7," "
            For z As Integer=1 To Ubound(t)
                Print #7,z,t(z)
            Next z
            Close #7
            Shell "notepad mygl.txt"
        End Sub
        
        
        
        
        Dim As Double col(1 To 4) 'font colour
        Dim As String info,renderer,vendor,version
        Redim As String temp()
        Dim As Integer ypos=170,mx,my,mb
        col(1)=1:col(4)=1
        Screen 19,32,,2
        
        gl2dsetup 'set gl parameters
        
        renderer=*glGetString(GL_RENDERER)
        vendor=*glGetString(GL_VENDOR)
        version=*glGetString(GL_VERSION)
        info= *glGetString(GL_EXTENSIONS)
        string_split(info," ",temp())
        'extensions are now in array temp
        Dim As Integer maxy=1.5*12*Ubound(temp) 'base of outputs
        Dim As d2 mouse
        dim as double framecount
        Do
            framecount=framecounter
            Getmouse(mx,my,,mb)
            mouse.x=mx:mouse.y=my
            'mouse events__________
            If inbox(slider(),mouse,5) And mb=1 Then
                fileflag=1
            End If
            If fileflag=1 Then
                If inbox(slider(),mouse,6) And mb=1 Then
                    savefile(renderer,vendor,version,temp())
                    fileflag=0
                End If
                If inbox(slider(),mouse,7) And mb=1 Then
                    Flip
                    End
                End If
            End If
            If fileflag=1 Then
                If  (inbox(slider(),mouse,6)=0 And mb=1) Or  (inbox(slider(),mouse,7)=0 And mb=1) Then
                    If inbox(slider(),mouse,5)=0 Then
                        fileflag=0
                    End If
                End If
            End If
            'the moving scroller
            If inbox(slider(),mouse,2) And mb=1 Then
                If slider(2).y>0 And slider(2).y<575 Then
                    slider(2).y=my-15
                    ypos=170-maxy*my/600
                End If
                
            End If
            If inbox(slider(),mouse,1) And mb=1 Then
                If slider(2).y>0 And slider(2).y<575 Then
                    slider(2).y=my-15
                    ypos=170-maxy*my/600
                End If
                
            End If
            If inbox(slider(),mouse,3) And mb=1 Then
                If slider(2).y>0 And slider(2).y<575 Then
                    ypos=ypos+5*maxy/600
                    slider(2).y=slider(2).y-5
                End If
            End If
            If inbox(slider(),mouse,4) And mb=1 Then
                If slider(2).y>0 And slider(2).y<575 Then
                    ypos=ypos-5*maxy/600
                    slider(2).y=slider(2).y+5
                End If
            End If
            'end mouse events
            
            'tame slider(keep on screen)
            If slider(2).y<=0 Then slider(2).y=1:ypos=170-maxy*slider(2).y/600
            If slider(2).y>=575 Then slider(2).y=574:ypos=170-maxy*slider(2).y/600
            
            col(2)=0 'redden the version bit
            glClear (GL_COLOR_BUFFER_BIT)
            'draw scroll and file box
            For z As Integer=1 To Ubound(slider)-2
                drawbox(slider(),z,slider(z).col(),slider(z).w,slider(z).caption)
            Next z
            If fileflag=1 Then
                'draw save and quit box
                drawbox(slider(),6,slider(6).col(),slider(6).w,slider(6).caption) 
                drawbox(slider(),7,slider(7).col(),slider(7).w,slider(7).caption)
            End If
            'write the information on screen
            
            drawstring(20,ypos-160,"Graphics card information/Opengl version:",col(),1.5)
            drawstring(20,ypos-160+40,"RENDERER: " & renderer,col(),1.5)
            drawstring(20,ypos-160+80,"VENDOR  : " & vendor,col(),1.5)
            drawstring(20,ypos-160+120,"VERSION : " & version,col(),1.5)
            
            drawstring(20,ypos-160+120+20,"(Frames per second =  "& int(framecount) & ")",col(),1)
            drawstring(20,ypos-160+120+20+10,"Free BASIC Version " & __FB_version__,col(),1)
            col(2)=1 'extensions a different colour
            drawstring(20,ypos-160+160,"Extensions ("& Ubound(temp) & ") in all:",col(),1.5)
            'extensions
            For z As Integer=1 To Ubound(temp)
                drawstring(20,ypos+20+1.5*12*z,Str(z)+String(Len(Str(Ubound(temp)))-Len(Str(z))+2," ")+temp(z),col(),1.5)
            Next z
            glend
            Flip
        Loop Until Inkey = Chr(27) 'don't need esc, but just incase
        
          
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: opengl 3d engine

Post by dodicat »

I don't yet see a way to return a vector co-ordinate during a rotation.
glrotatef(~,~.~,~.~) seems to do only a half rotation, the other half is somehow swapped.

Perhaps glrotatef(angle,x,y,z) where x,y,z are in [0,1] is wrong syntax.
I cannot see that anything here should be commutative.

I've managed a shading with the light on the right, but only if I rotate manually with my own rotator.
VIZ:

Code: Select all

 



'FREEBASIC ROTATOR
'========================================================================
Type V3 Field =1
    As Single x,y,z
    As Uinteger col
    #define vct Type<V3>
    '---------------------------------------------------------------------
    #macro rotate(c,p,angle,scale,result)
    sx=Sin(angle.x):sy=Sin(angle.y):sz=Sin(angle.z)
    cx=Cos(angle.x):cy=Cos(angle.y):cz=Cos(angle.z)
    dx=P.x-c.x:dy=P.y-c.y:dz=P.z-c.z
    result= Type<V3>((scale.x)*((cy*cz)*dx+(-cx*sz+sx*sy*cz)*dy+(sx*sz+cx*sy*cz)*dz)+c.x,_
    (scale.y)*((cy*sz)*dx+(cx*cz+sx*sy*sz)*dy+(-sx*cz+cx*sy*sz)*dz)+c.y,_
    (scale.z)*((-sy)*dx+(sx*cy)*dy+(cx*cy)*dz)+c.z,P.col)
    #endmacro
    
    '------------------- unused here  ------------------------------------
    #macro apply_perspective(P,eyepoint,result)
    w=1+(P.z/eyepoint.z)
    result= Type<V3>((P.x-eyepoint.x)/w+eyepoint.x,_
    (P.y-eyepoint.y)/w+eyepoint.y,(P.z-eyepoint.z)/w+eyepoint.z,P.col)
    #endmacro
    '----------------------------------------------------------------------
    #macro setvariables()
    Dim Shared As Single cx,cy,cz,sx,sy,sz,dx,dy,dz,w
    #endmacro
    '-----------------------------------------------------------------------
End Type
setvariables()
'only some of the operators in use here
Operator *(n As Single,v1 As V3) As V3
Return Type<V3>(n*v1.x,n*v1.y,n*v1.z)
End Operator
Operator +(v1 As v3,v2 As v3) As v3
Return Type<V3>(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z)
End Operator
Operator -(v1 As v3,v2 As v3) As v3
Return v1+(-1*v2)
End Operator
Operator Abs (P As V3) As Single
Return Sqr((P.x)*(P.x)+(P.y)*(P.y)+(P.z)*(P.z))
End Operator
'============= END FREEBASIC ROTATOR =========================
'The normals to cube faces (for shading)
Dim Shared As V3 normS(1 To 6)={vct(0,1,0),_  'top
                                vct(0,-1,0),_ 'base
                                vct(0,0,1),_  'front
                                vct(0,0,-1),_ 'back
                                vct(-1,0,0),_ 'left
                                vct(1,0,0)}   'right
Dim Shared As Double cols(1 To 6) 'face colouration

#include once "fbgfx.bi"
#Include Once "GL/glu.bi"
#include "GL/glext.bi"

Enum
    opengl      = 2
    fullscreen  = 1
    #define plus Or
End Enum

Dim Shared As Integer xres,yres
Screen 20,32,,opengl 'plus fullscreen
Screeninfo xres,yres

'Simple structure just to hold corners of one quad
Type pair
    As Single x,y
End Type
Operator *(x As Double,n As pair) As pair
Return Type<pair>(x*n.x,x*n.y)
End Operator


'Create a FreeBasic image
Sub CreateFBimageBackground(Byref im2 As Any Ptr)
    Dim As Single minx,maxx,miny,maxy,lasty,grad
    #define dist(x1,y1,x2,y2) Sqr((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
    #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
    #macro paintsketch(_function,r,g,b)
    For x As Double=minx To maxx Step (maxx-minx)/5000
        Dim As Double x1=(xres)*(x-minx)/(maxx-minx)
        Dim As Double y1=(yres)*(_function-maxy)/(miny-maxy)
        grad=y1-lasty
        lasty=y1
        grad=grad*250
        Line im2,(x1,0)-(x1,yres-y1),Rgb(r+grad,g+grad,b)
    Next x
    #endmacro
    #macro _window(topleftX,topleftY,bottomrightX,bottomrightY)
    minx=topleftX
    maxx=bottomrightX
    miny=bottomrightY
    maxy=topleftY
    #endmacro
    For x As Integer=0 To xres
        For y As Integer=0 To yres
            Var d=dist(x,y,(.8*xres),(.9*yres))
            Var c=map(0,800,d,255,50)
            Pset im2,(x,y),Rgb(0,0,c)
        Next y
    Next x
    _window(-5,3,25,-1.2)
    paintsketch(.05*Sin(x)+.05*Sin(2*x),100,100,50)
    _window(5,2,30,-.8) 
    paintsketch(.1*Sin(x),100,100,0)
    
    _window(1,2,12,-.6) 
    paintsketch(.1*Sin(x),100,100,0)  
    _window(0,2,8,-.5)
    paintsketch(.2*Sin(x),100,100,0)
End Sub

'Set a Quad to hold image
Sub setbackgroundquad(e() As pair)
    Dim As Single r1=xres/yres,r2=1 'same ratio as screen
    Dim As Single n=1 'left open for a fiddle around
    e(1)=n*Type(-r1,r2)
    e(2)=n*Type(r1,r2)
    e(3)=n*Type(r1,-r2)
    e(4)=n*Type(-r1,-r2)
End Sub

Sub DrawBackGroundQuad(e() As pair)
    Dim As Single n=1
    glLoadIdentity() 
    glTranslatef(0,0,-2) 'adjust the z translate for a good fit
    glbegin gl_quads
    glTexCoord2f( 0,n )
    glvertex3f(e(1).x,e(1).y,0)
    glTexCoord2f( n,n )
    glvertex3f(e(2).x,e(2).y,0)
    glTexCoord2f( n,0 )
    glvertex3f(e(3).x,e(3).y,0)
    glTexCoord2f( 0,0 )
    glvertex3f(e(4).x,e(4).y,0)
    glend
End Sub

Sub glsetup 
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0) 
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    
End Sub

'Transfer FB image to OpenGL
Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


'Rotate and draw the cube with shading
Sub DrawGlCube(Byref rotangle As Single)
    Dim As V3 rot,piv=vct(0,0,0),scale=vct(1,1,1)
      #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
      'rotation ratios
Dim As Single xr=1,yr=.5,zr=.25
Dim As Single d= .01745329 'degrees to radians
Dim As V3 v 'rotated vector

'macro to rotate corners
#macro turn(p1,p2,p3,value)
rotate(piv,vct(p1,p2,p3),ang,scale,value)
#endmacro

    glLoadIdentity()
    
    glTranslatef(0,0,-5)
   'convert degrees to radians and apply ratios
    Dim As V3 ang=vct(xr*rotangle*d,yr*rotangle*d ,zr*rotangle*d)
    
    glBegin(GL_QUADS)
    
     rotate(piv,normS(1),ang,scale,rot)'rotate normal(1)
    cols(1)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(1),cols(1),cols(1))
  
    turn(1,1,-1,v)            ' Top right of the quad (top)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,1,-1,v)           ' Top left of the quad (top)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,1,1,v)            ' Bottom left of the quad (top)
    glvertex3f(v.x,v.y,v.z)
    turn(1,1,1,v)             ' Bottom right of the quad (top)
    glvertex3f(v.x,v.y,v.z)
    
    
    rotate(piv,normS(2),ang,scale,rot)'rotate normal(2)
    cols(2)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(2),cols(2),cols(2))
    
   turn(1,-1,1,v)             ' Top right of the quad (bottom)
    glvertex3f(v.x,v.y,v.z)
   turn(-1,-1,1,v)            ' Top left of the quad (bottom)
    glvertex3f(v.x,v.y,v.z)

    turn(-1,-1,-1,v)          ' Bottom left of the quad (bottom)
    glvertex3f(v.x,v.y,v.z)
    turn(1,-1,-1,v)           ' Bottom right of the quad (bottom)
    glvertex3f(v.x,v.y,v.z)
    
    rotate(piv,normS(3),ang,scale,rot)'rotate normal(3)
    cols(3)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(3),cols(3),cols(3))  
           
   turn(1,1,1,v)                ' Top right of the quad (front)
    glvertex3f(v.x,v.y,v.z)
   turn(-1,1,1,v)               ' Top left of the quad (front)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,-1,1,v)             ' Bottom left of the quad (front)
    glvertex3f(v.x,v.y,v.z)
    turn(1,-1,1,v)              ' Bottom right of the quad (front)
    glvertex3f(v.x,v.y,v.z)
    
    rotate(piv,normS(4),ang,scale,rot)'rotate normal(4)
    cols(4)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(4),cols(4),cols(4))
    
   turn(1,-1,-1,v)               ' Bottom left of the quad (back)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,-1,-1,v)             ' Bottom right of the quad (back)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,1,-1,v)              ' Top right of the quad (back)
    glvertex3f(v.x,v.y,v.z)
    turn(1,1,-1,v)               ' Top left of the quad (back)
    glvertex3f(v.x,v.y,v.z)
    
    rotate(piv,normS(5),ang,scale,rot)'rotate normal(5)
    cols(5)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(5),cols(5),cols(5))
    
    turn(-1,1,1,v)                ' Top right of the quad (left)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,1,-1,v)               ' Top left of the quad (left)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,-1,-1,v)              ' Bottom left of the quad (left)
    glvertex3f(v.x,v.y,v.z)
    turn(-1,-1,1,v)               ' Bottom right of the quad (left)
    glvertex3f(v.x,v.y,v.z)

    rotate(piv,normS(6),ang,scale,rot)'rotate normal(6)
    cols(6)= map(-1,1,rot.x,.1,1)
    glColor3f(cols(6),cols(6),cols(6))
    
   turn(1,1,-1,v)                 ' Top right of the quad (right)
    glvertex3f(v.x,v.y,v.z)
    turn(1,1,1,v)                 ' Top left of the quad (right)
    glvertex3f(v.x,v.y,v.z)
    turn(1,-1,1,v)                ' Bottom left of the quad (right)
    glvertex3f(v.x,v.y,v.z)
    turn(1,-1,-1,v)                ' Bottom right of the quad (right)
    glvertex3f(v.x,v.y,v.z)
    glend
End Sub

'Some variables
Dim As gluint tex1
Dim As Any Ptr background=Imagecreate(xres,yres)
Dim  As pair BackgroundCorners(1 To 4)


CreateFBimageBackground(background)
setbackgroundquad(BackgroundCorners())

'NOW START OPENGL
glsetup
'transfer freebasic image to openGL
settexture(tex1,background)

Dim As Single angle

Do
    angle=angle+1
    glClear(GL_COLOR_BUFFER_BIT)
    
    'freebasic image is planted onto the background quad
    'enable and disable textures
    glEnable( GL_TEXTURE_2D )
    DrawBackGroundQuad(BackgroundCorners())
    glDisable( GL_TEXTURE_2D )
    
    'Freebasic rotate cube
    'with light at right
    glEnable (GL_CULL_FACE)
    DrawGlcube(angle)
    gldisable(GL_CULL_FACE)
    glend
    Flip
    Sleep 1,1
Loop Until Inkey=Chr(27)
Imagedestroy background
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: opengl 3d engine

Post by Gonzo »

dodicat wrote:I don't yet see a way to return a vector co-ordinate during a rotation.
glrotatef(~,~.~,~.~) seems to do only a half rotation, the other half is somehow swapped.

Perhaps glrotatef(angle,x,y,z) where x,y,z are in [0,1] is wrong syntax.
I cannot see that anything here should be commutative.
http://en.wikipedia.org/wiki/Gimbal_lock
http://www.youtube.com/watch?v=zc8b2Jo7mno

The only way to have total control over rotation is to use quaternions, or set up a complex rotation matrix
if you only need to rotate around 2 axes, you shouldn't be having this problem if you align one axis to face front
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: opengl 3d engine

Post by dodicat »

You could do the Gimbals with Rodrigues rotator.

Code: Select all


#Include Once "GL/glu.bi"
Screen 19,32,,2
Sub glsetup 
    dim as integer xres,yres
    screeninfo xres,yres
    glShadeModel(GL_SMOOTH)                 ' Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable GL_ALPHA
    glEnable GL_BLEND
    glViewport(0, 0, xres, yres)       ' Set the viewport
    glMatrixMode(GL_PROJECTION)        ' Change Matrix Mode to Projection
    glLoadIdentity                     ' Reset View
    gluPerspective(45.0, xres/yres, 1.0, 100.0) 
    glMatrixMode(GL_MODELVIEW)         ' Return to the modelview matrix
    glLoadIdentity                     '  Reset View
    glclearcolor .5,.5,.5,1
End Sub
Type v3
    As Single x,y,z
    As Uinteger colour
    Declare Property length As Single
    Declare Property unit As v3
    Declare Function AxialRotate(As v3,As Single,As v3) As v3
    Declare Function CentreRotate(As v3,As v3=Type<v3>(1,1,1)) As v3
    Declare Function perspective(eyepoint As v3) As v3
End Type

#define vct Type<v3>
#define dot *
#define cross ^
Dim Shared As V3  X_axis,Y_axis,Z_axis
'Some vector operators:
Operator + (v1 As v3,v2 As v3) As v3
Return Type<v3>(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z)
End Operator

Operator -(v1 As v3,v2 As v3) As v3
Return Type<v3>(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z)
End Operator

Operator * (f As Single,v1 As v3) As v3 'pre mult by scalar
Return vct(f*v1.x,f*v1.y,f*v1.z)
End Operator

Operator *(v1 As v3,f As Single) As v3  'post mult by scalar
Return f*v1
End Operator

Operator * (v1 As v3,v2 As v3) As Single 'dot product
Return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z
End Operator

Operator ^ (v1 As v3,v2 As v3) As v3     'cross product
Return Type<v3>(v1.y*v2.z-v2.y*v1.z,-(v1.x*v2.z-v2.x*v1.z),v1.x*v2.y-v2.x*v1.y)
End Operator

Property v3.length As Single
Return Sqr(this.x*this.x+this.y*this.y+this.z*this.z)
End Property

' ~ normalize
Property v3.unit As v3
Dim n As Single=this.length
If n=0 Then n=1e-20
Return vct(this.x/n,this.y/n,this.z/n)
End Property

'UNUSED HERE
Function v3.perspective(eyepoint As v3) As v3
    Dim As Single   w=1+(this.z/eyepoint.z)
    If w=0 Then w=1e-20
    Var result=(This-eyepoint)*(1/w)+eyepoint
    result.colour=this.colour
    Return result
End Function

'Rotate about a given axis direction (Rodrigue method)
'norm in the function is passed as a unit vector (the axis)
Function v3.AxialRotate(centre As v3,Angle As Single,norm As v3) As v3
    Dim As v3 V=This-centre,result
    result=(V*Cos(Angle)+(Norm cross V)*Sin(Angle)+Norm*(Norm dot V)*(1-Cos(Angle)))+centre
    result.colour=this.colour
    Return result
End Function

'General rotate about  any three axis
'using the Axial rotate about the chosen axis
Function V3.CentreRotate(ctr As v3,Ang As v3) As V3
    Var t1=this.AxialRotate(ctr,Ang.x,x_axis)
    Var t2=t1.AxialRotate(ctr,Ang.y,y_axis)
    Return t2.AxialRotate(ctr,Ang.z,z_axis)
End Function


'================  End of Rotation stuff ========================

'Quicksort
Sub Qsort(array() As V3,begin As Integer,Finish As Uinteger)
    Dim As Integer i=begin,j=finish 
    Dim As V3 x =array(((I+J)\2))
    While  I <= J
        While array(I).z > X.z
            I+=1
        Wend
        While array(J).z < X.z
            J-=1
        Wend
        If I<=J Then
            Swap array(I),array(J)
            I+=1
            J-=1
        End If
    Wend
    If J > begin Then Qsort(array(),begin,J)
    If I < Finish Then Qsort(array(),I,Finish)
End Sub

'speed regulators
Function framecounter() As Integer
    Var t1=Timer,t2=t1
    Static As Double t3,frames,answer
    frames=frames+1
    If (t2-t3)>=1 Then
        t3=t2
        answer=frames
        frames=0
    End If
    Return answer
End Function

Function regulate(MyFps As Integer,Byref fps As Integer) As Integer
    fps=framecounter
    Static As Double timervalue
    Static As Double delta,lastsleeptime,sleeptime
    Var k=1/myfps
    If Abs(fps-myfps)>1 Then
        If fps<Myfps Then delta=delta-k Else delta=delta+k
    End If
    sleeptime=lastsleeptime+((1/myfps)-(Timer-timervalue))*(2000)+delta
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function
'draw a filled polygon
Sub glpolygonfill(x As Single,y As Single,rx As Single,ry As Single,numsides As Integer) Export
    Var pi2 = 8*Atn(1),st=pi2/(numsides)
    glend
    glBegin GL_TRIANGLE_FAN
    For a As Single=0 To pi2  Step st
        glVertex2f (x)+Cos(a)*(rx),(y)+Sin(a)*(ry)
    Next
    glEnd
End Sub

'============ LOCAL SUBS TO EXAMPLE ===================
Sub setrings(a() As V3,flag As Integer)
    Dim As Single pi=4*Atn(1),count
    For n As Single=0 To 2*pi Step 2*pi/60
        count=count+1
        Select Case As Const flag
        Case 1: a(count)=Type<V3>(0,Cos(n),Sin(n)):a(count).colour=1
        Case 2:a(count)=.9*Type<V3>(Cos(n),0,Sin(n)):a(count).colour=2
        Case 3:a(count)=.8*Type<V3>(Cos(n),Sin(n),0):a(count).colour=3
        End Select
    Next n
End Sub


Sub BuildAllRings(a() As V3,axis As V3,angle As Single,t() As V3)
    Static As Integer count
    For n As Integer=Lbound(a) To Ubound(a)-1
        count=count+1
        Var temp1= a(n).axialrotate(vct(0,0,0),angle,axis)
        t(count)=vct(temp1.x,temp1.y,temp1.z):t(count).colour=temp1.colour
    Next n
    If count>120 Then count=0
End Sub
'===================================================



X_axis=vct(1,0,0)
Y_axis=vct(0,1,0)
Z_axis=vct(0,0,1)
'normalize (actually already normal vectors)
X_axis=X_Axis.unit
Y_axis=Y_Axis.unit
Z_axis=Z_axis.unit

'Some variables
Dim As V3 Xring(1 To 61)
Dim As V3 Yring(1 To 61)
Dim As V3 Zring(1 To 61)
Dim As V3 AllRings(1 To 3*60)
setrings(Xring(),1)
setrings(Yring(),2)
setrings(Zring(),3)
Dim As Single angle
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
dim as integer fps
'============  START ============================
glsetup
Do
    windowtitle "Frames per second " & fps
    var sleeptime=regulate(60,fps)
    angle=angle+.01
    'rotate the X,Y,Z axis varying amounts
    Var tempX=X_axis.centrerotate(vct(0,0,0),vct(angle,angle/2,angle/3))
    Var tempY=Y_axis.centrerotate(vct(0,0,0),vct(angle/2,angle/3,angle))
    Var tempZ=Z_axis.centrerotate(vct(0,0,0),vct(angle/3,angle,angle/2))
    'build each ring into the working array (AllRings())
    BuildAllRings(Xring(),tempy,angle,AllRings())
    BuildAllRings(Yring(),tempz,angle,AllRings())
    BuildAllRings(Zring(),tempx,angle,AllRings())
    'Sort by .z
    Qsort(AllRings(),1,Ubound(AllRings))
    
    'Draw OpenGL
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0,0,-3)
    For n As Integer=1 To Ubound(AllRings)
        Var c=map(-1,1,AllRings(n).z,1,.2)
        Select Case As Const AllRings(n).colour
        Case 1:glcolor4f(c,0,0,1)
        Case 2:glcolor4f(0,c,0,1)
        Case 3:glcolor4f(0,0,c,1)
        End Select 
        Var w=(map(1,-1,AllRings(n).z,10,15))/200
        glpolygonfill(AllRings(n).x,AllRings(n).y,w,w,20)
    Next n
    
    Flip
    Sleep sleeptime,1
Loop Until Inkey=Chr(27)

 
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: opengl 3d engine

Post by dafhi »

euler method can be used for non-locking rotation .. just have to rotate relative
to object's last orientation.

though i prefer to know quat or this Rodrigues' technique
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: opengl 3d engine

Post by dodicat »

Pretty straight forward really once you have some vector operators set up.
http://en.wikipedia.org/wiki/Rodrigues' ... on_formula

I'll try and use quads to make ribbons instead of balls, maybe tomorrow.
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: opengl 3d engine

Post by dafhi »

that's quat, short for quaternion

Code: Select all

/'
  quat-like Euler rotation

  some_angle = pi / 4

  1. pre-rotate the object (-some_angle) on the z
  2. x rotate
  3. rotate back (some_angle) on the z
'/

const TwoPi = 8 * Atn(1)
const Pi = 4 * Atn(1)


Type vector3d
  As double         x,y,z
End Type

Type Axis3D
  As vector3d              AxisX=(1,0,0), AxisY=(0,-1,0), AxisZ=(0,0,1)
  As double                x,y,z
End Type


#Macro RotC_(dsta,dstb,srca,srcb)
    temp = cosa_*srca - sina_*srcb
    dstb = cosa_*srcb + sina_*srca
    dsta = temp
#EndMacro
#Macro RotC(a_,dst,src,dota,dotb)
  Scope
    Dim As single a = (a_)
    Dim As double cosa_ = Cos(a), sina_ = Sin(a), temp
    RotC_( (dst.AxisX)dota, (dst.AxisX)dotb, (src.AxisX)dota, (src.AxisX)dotb )
    RotC_( (dst.AxisY)dota, (dst.AxisY)dotb, (src.AxisY)dota, (src.AxisY)dotb )
    RotC_( (dst.AxisZ)dota, (dst.AxisZ)dotb, (src.AxisZ)dota, (src.AxisZ)dotb )
  End Scope
#EndMacro

#Macro xRot(dst,a_)
  RotC(a_,dst,dst,.y,.z)
#EndMacro

#Macro yRot(dst,a_)
  RotC(a_,dst,dst,.z,.x)
#EndMacro

#Macro zRot(dst,a_)
  RotC(a_,dst,dst,.x,.y)
#EndMacro


Sub Main

  dim as integer  w = 640, wm = w-1
  dim as integer  h = 480, hm = h-1
  
  ScreenRes w,h,32

  dim as single   halfw = wm/2
  dim as single   halfh = hm/2
  
  dim as integer  UB = 9999
  dim as vector3d Points(UB)
  dim as Axis3D   Axis
  
  dim as single   zoom = 0.5 * sqr(w*w+h*h)
  Axis.z = 2.5
  
  #Define         rand  rnd - 0.5
  
  For p as Vector3D ptr = @Points(0) to @Points(UB)
    p->x = rand
    p->y = rand
    p->z = rand
  Next
  
  do
  
    dim as string keyp = inkey
    if keyp <> "" then exit do
    
    dim as single rspeed = TwoPi * -1
    
    dim as single z_rot_off = pi / 4
    zRot(axis, -z_rot_off)
    xRot(axis, rspeed/120)
    zRot(axis, z_rot_off)
    
    screenlock
      cls
      For p as Vector3D ptr = @Points(0) to @Points(UB)
      
        dim as single sx = p->x
        dim as single sy = p->y
        
        dim as single rz_=(axis.z + axis.AxisZ.z*p->z + axis.AxisX.z*sx + axis.AxisY.z*sy)
        If rz_ > 0.1 Then
          Dim As Single rz = zoom/rz_
          Dim as single y = halfh - rz*(axis.y + axis.AxisY.y*sy + axis.AxisZ.y*p->z + axis.AxisX.y*sx)
          if y >= 0 then
          if y < h then
            dim as single x = halfw + rz*(axis.x + axis.AxisX.x*sx + axis.AxisY.x*sy + axis.AxisZ.x*p->z)
            if x >= 0 then
            if x < w then
              pset (x, y)
            EndIf
            EndIf
          EndIf
          EndIf
        end if
        
      Next
    screenunlock
    
    sleep 10
    
  Loop
  
End Sub

Main
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: opengl 3d engine

Post by bluatigro »

update :
- i splited the code
- so maintenence is easyer

_math.bas

Code: Select all

''bluatigro 6-nov-2013
''game lib : math helper functions + connstands

#ifndef MATH_H
#define MATH_H

const as single pi = 3.1415926535897932384626433832795
const as single golden_divide = 0.61803398874989484820458683436564

const as integer false = 0
const as integer true = not false

declare function rad( x as single ) as single

function rad( x as single ) as single
''help function degrees to radians 
  return x * pi / 180
end function

#endif
t_3d.bas

Code: Select all

''bluatigro 6-nov-2013
''OOP game lib : 3D vector

#ifndef T3D_H
#define T3D_H

type t3d
  x as single
  y as single
  z as single
  declare constructor()
  declare constructor ( x as single , y as single, z as single )
  declare sub fill( x as single , y as single , z as single )
  declare sub normalize
end type
constructor t3d()
  this.x = 0
  this.y = 0
  this.z = 0
end constructor 
constructor t3d( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z
end constructor 
operator +( a as t3d , b as t3d ) as t3d
  return type( a.x + b.x , a.y + b.y , a.z + b.z )
end operator
operator *( a as t3d , d as single ) as t3d
  return type( a.x * d , a.y * d , a.z * d )
end operator
operator \( a as t3d , b as t3d ) as t3d
  return type( a.y * b.z - a.z * b.y _
             , a.z * b.x - a.x * b.z _
             , a.x * b.y - a.y * b.x )
end operator
operator -( a as t3d , b as t3d ) as t3d
  return type( a.x - b.x , a.y - b.y , a.z - b.z )
end operator
operator /( a as t3d , d as single ) as t3d
  return type( a.x / d , a.y / d , a.z / d )
end operator
sub t3d.fill( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z
end sub
declare function dot( a as t3d , b as t3d ) as single
function dot( a as t3d , b as t3d ) as single
  return a.x * b.x + a.y * b.y + a.z * b.z
end function
declare function length( q as t3d ) as single
function length( q as t3d ) as single
   return sqr( q.x * q.x + q.y * q.y + q.z * q.z ) + 1e-7
end function  
declare function anlge( a as t3d , b as t3d ) as single
function angle( a as t3d , b as t3d ) as single
  return acos( dot( a , b ) _
  / ( length( a ) * length( b ) ) )
end function
sub t3d.normalize
  this /= length( this )
end sub

#endif ''end of t3d
t_gl_engine.bas

Code: Select all

''bluatigro 6-nov-2013
''opengl game lib : 3d engine 

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif

#ifndef T_GL_ENGINE_H
#define T_GL_ENGINE_H

#include once "GL/gl.bi"
#include once "GL/glu.bi"

#include "t_3d.bas"
#include "_math.bas"

dim shared as single black( 3 )   = {  0 ,  0 ,  0 , 1 }
dim shared as single red( 3 )     = {  1 ,  0 ,  0 , 1 }
dim shared as single green( 3 )   = {  0 ,  1 ,  0 , 1 }
dim shared as single dgreen( 3 )  = {  0 , .5 ,  0 , 1 }
dim shared as single yellow( 3 )  = {  1 ,  1 ,  0 , 1 }
dim shared as single blue( 3 )    = {  0 ,  0 ,  1 , 1 }
dim shared as single magenta( 3 ) = {  1 ,  0 ,  1 , 1 }
dim shared as single cyan( 3 )    = {  0 ,  1 ,  1 , 1 }
dim shared as single white( 3 )   = {  1 ,  1 ,  1 , 1 }

dim shared as single orange( 3 )  = {  1 , .5 ,  0 , 1 }
dim shared as single gray( 3 )    = { .5 , .5 , .5 , 1 }
dim shared as single purple( 3 )  = { .5 ,  0  ,.5 , 1 }
dim shared as single pink( 3 )    = {  1 , .5 , .5 , 1 }

const as integer xyz = 0
const as integer xzy = 1
const as integer yxz = 2
const as integer yzx = 3
const as integer zxy = 4
const as integer zyx = 5

declare sub child( x as single , y as single , z as single , ax as integer , lim as integer )
declare function pend( fase as single , amp as single ) as single
declare sub skelet( no as integer , x as single , y as single , z as single )
dim shared sk( 63 ) as t3d
declare sub opengl_init()

sub child( x as single , y as single , z as single , lim as integer , ax as integer )
  glTranslatef x , y , z
  select case ax
    case xyz
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
    case xzy
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).y , 0 , 1 , 0
    case yxz
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
    case yzx
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).x , 1 , 0 , 0
    case zxy
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).x , 1 , 0 , 0
      glRotatef sk( lim ).y , 0 , 1 , 0
    case zyx
      glRotatef sk( lim ).z , 0 , 0 , 1
      glRotatef sk( lim ).y , 0 , 1 , 0
      glRotatef sk( lim ).x , 1 , 0 , 0
    case else
  end select  
end sub

function pend( fase as single , amp as single ) as single
  return sin( rad( fase ) ) * amp
end function

sub skelet( no as integer , x as single , y as single , z as single )
  sk( no and 63 ).fill x , y , z
end sub

sub opengl_init()

''  DIM AS INTEGER winx, winy, bitdepth
''  SCREENINFO winx, winy, bitdepth
''  SCREENRES winx, winy, 32, , FB.GFX_FULLSCREEN

  screen 20 , 32 , , 3

	'' ReSizeGLScene
	glViewport 0, 0, 1024, 768                     '' Reset The Current Viewport
	glMatrixMode GL_PROJECTION                     '' Select The Projection Matrix
	glLoadIdentity                                 '' Reset The Projection Matrix
	gluPerspective 45.0, 640.0/480.0, 0.1, 100.0   '' Calculate The Aspect Ratio Of The Window
	glMatrixMode GL_MODELVIEW                      '' Select The Modelview Matrix
	glLoadIdentity                                 '' Reset The Modelview Matrix
	
	'' All Setup For OpenGL Goes Here
	glShadeModel GL_SMOOTH                         '' Enable Smooth Shading
	glClearColor 0.0, 0.0, 0.0, 1               '' Black Background
	glClearDepth 1.0                               '' Depth Buffer Setup
	glEnable GL_DEPTH_TEST                         '' Enables Depth Testing
	glDepthFunc GL_LEQUAL                          '' The Type Of Depth Testing To Do
	glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations

  glEnable( gl_lighting )
  dim as single lightpos( 3 ) = { 0 , 50 , 0 , 1 }
  dim as single diffuse( 3 ) = { 1 , 1 , 1 , 1 }
  glLightfv( gl_light0 , gl_position, @lightpos(0) )
  glLightfv( gl_light0 , gl_diffuse , @diffuse(0) )
  glEnable( gl_light0 )
  
end sub

#endif
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: opengl 3d engine

Post by bluatigro »

here is the rest :
t_gl_primativs.bas

Code: Select all

''bluatigro 6-nov-2013
''opengl game lib : some 3d shapes

#ifndef T_GL_SHAPES_H
#define T_GL_SHAPES_H

#include "t_gl_primativs.bas"


type Tbox
  m as t3d
  d as t3d
end type
dim shared box as Tbox

declare sub isoca( i as integer )
declare sub sphere( h as integer , r as integer _
, a as single , b as single )
declare sub torus( hsides as integer , rsides as integer )
declare sub cilinder( sides as integer ,dx as single , dy as single , top as integer , bot as integer ) 
declare sub cube( )
declare sub hcube( )
declare sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )
declare sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )

sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri1 p1 , p2 , p3 , 0
  else
  dim p12 as integer , p13 as integer , p23 as integer
    p12 = 255 - no * 3
    p13 = 255 - no * 3 - 1
    p23 = 255 - no * 3 - 2
    pnt( p12 ) = ( pnt( p1 ) + pnt( p2 ) ) / 2
    pnt( p13 ) = ( pnt( p1 ) + pnt( p3 ) ) / 2
    pnt( p23 ) = ( pnt( p2 ) + pnt( p3 ) ) / 2
    pnt( p12 ).normalize
    pnt( p13 ).normalize
    pnt( p23 ).normalize
    geo no - 1 , p1 , p12 , p13
    geo no - 1 , p2 , p23 , p12
    geo no - 1 , p3 , p13 , p23
    geo no - 1 , p12 , p23 , p13
  end if
end sub

sub isoca( i as integer )
  if i < 0 then i = 0
  if i > 5 then i = 5
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z 
  glScalef box.d.x , box.d.y , box.d.z
    
  setpoint  1 ,  0       ,  0 , 1.118034
  setpoint  2 ,  1       ,  0         ,  .5 
  setpoint  3 ,  .309017 ,  .95105654 ,  .5 
  setpoint  4 , -.809017 ,  .58778524 ,  .5 
  setpoint  5 , -.809017 , -.58778524 ,  .5 
  setpoint  6 ,  .309017 , -.95105654 ,  .5 
  setpoint  7 ,  .809017 ,  .58778524 , -.5 
  setpoint  8 , -.309017 ,  .95105654 , -.5 
  setpoint  9 , -1       ,  0         , -.5 
  setpoint 10 , -.309017 , -.95105654 , -.5
  setpoint 11 ,  .809017 , -.58778524 , -.5 
  setpoint 12 ,  0       ,  0         , -1.118034
  dim t as integer
  for t = 1 to 12
    pnt( t ).normalize
  next t
  geo i , 1 ,  2 , 3
  geo i , 1 ,  3 ,  4 
  geo i , 1 ,  4 ,  5 
  geo i , 1 ,  5 ,  6 
  geo i , 1 ,  6 ,  2 
  geo i , 2 ,  7 ,  3
  geo i , 3 ,  7 ,  8 
  geo i , 3 ,  8 ,  4
  geo i , 4 ,  8 ,  9 
  geo i , 4 ,  9 ,  5 
  geo i , 5 ,  9 , 10 
  geo i , 5 , 10 ,  6 
  geo i , 6 , 10 , 11 
  geo i , 6 , 11 ,  2
  geo i , 2 , 11 ,  7 
  geo i , 12 ,  8 ,  7
  geo i , 12 ,  9 ,  8
  geo i , 12 , 10 ,  9 
  geo i , 12 , 11 , 10 
  geo i , 12 ,  7 , 11 
  glPopMatrix
end sub

sub sphere( a as integer , b as integer _
, da as single , db as single )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z
  glScalef box.d.x , box.d.y , box.d.z
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 step PI / b * 2 
      j2 = j + PI / b * 2 

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

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
  glPopMatrix
end sub

sub torus( a as integer , b as integer )
  dim i as single , j as single , i2 as single , j2 as single
  if a < 3 then a = 3 
  if a > 64 then a = 643
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as single , my as single , mz as single , dx as single , dy as single , dz as single 
  mx = box.m.x 
  my = box.m.y 
  mz = box.m.z 
  dx = box.d.x 
  dy = box.d.y 
  dz = box.d.z 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
end sub

sub cilinder( sides as integer , dx as single , dy as single , top as integer , bot as integer )
  dim f as single
  if sides < 3 then sides = 3
  if sides > 64 then sides = 64
  for f = 0 to sides + 2
    setpoint f , box.m.x + sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z
    setpoint f + sides + 1 , box.m.x + sin( f * pi * 2 / sides ) * dx _
                           , box.m.y + box.d.y _
                           , box.m.z + cos( f * pi * 2 / sides ) * dy
  next f
  for f = 0 to sides + 1
    quad f , f + 1 , f + 2 + sides , f + 1 + sides , 0
  next f
  if top then
    setpoint 255 , 0 , box.m.y + box.d.y , 0
    for f = 0 to sides
        setpoint f , box.m.x + sin( f * pi * 2 / sides ) * dx _
               , box.m.y + box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * dy  
    next f
    for f = 0 to sides
      tri1 255 , f , f + 1 , 0
    next f
  end if
  if bot then
    setpoint 255 , 0 , box.m.y - box.d.y , 0
    for f = 0 to sides + 2
        setpoint f , box.m.x - sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z  
    next f
    for f = 0 to sides + 2
      tri1 255 , f , f + 1 , 0
    next f
  end if
end sub

sub cube()
  setpoint 0 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 1 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 2 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 3 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  setpoint 4 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 5 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 6 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 7 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  quad 0 , 2 , 3 , 1 , 0 ''right
  quad 7 , 6 , 4 , 5 , 0 ''left
  quad 0 , 4 , 5 , 1 , 0 ''up
  quad 7 , 3 , 2 , 6 , 0 ''down
  quad 0 , 4 , 6 , 2 , 0 ''back
  quad 7 , 5 , 1 , 3 , 0 ''front
end sub

sub hcube()
  setpoint 1 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 2 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 3 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  setpoint 4 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 5 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 6 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 7 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  
  setpoint 0 , box.m.x + box.d.x , box.m.y - box.d.y , 0
  setpoint 8 , box.m.x + box.d.x , 0 , box.m.z - box.d.z
  setpoint 9 , 0 , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 10 , box.m.x - box.d.x , box.m.x + box.d.y , 0
  setpoint 11 , box.m.x - box.d.x , 0 , box.m.z + box.d.z
  setpoint 12, 0 , box.m.y - box.d.y , box.m.z + box.d.z
  
  tri1 7 , 6 , 3 , 0
  tri1 7 , 5 , 6 , 0
  tri1 7 , 3 , 5 , 0
  
  quad 6 , 5 , 10 , 11 , 0
  quad 5 , 3 , 8 , 9 , 0
  quad 3 , 6 , 12 , 0 , 0
  
  tri1 6 , 12 , 11 , 0
  tri1 3 , 8 , 0 , 0
  tri1 5 , 9 , 10 , 0
end sub

sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )
  box.m.x = mx
  box.m.y = my
  box.m.z = mz
  box.d.x = dx
  box.d.y = dy
  box.d.z = dz
end sub

#endif
t_gl_shapes.bas

Code: Select all

''bluatigro 6-nov-2013
''opengl game lib : some 3d shapes

#ifndef T_GL_SHAPES_H
#define T_GL_SHAPES_H

#include "t_gl_primativs.bas"


type Tbox
  m as t3d
  d as t3d
end type
dim shared box as Tbox

declare sub isoca( i as integer )
declare sub sphere( h as integer , r as integer _
, a as single , b as single )
declare sub torus( hsides as integer , rsides as integer )
declare sub cilinder( sides as integer ,dx as single , dy as single , top as integer , bot as integer ) 
declare sub cube( )
declare sub hcube( )
declare sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )
declare sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )

sub geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri1 p1 , p2 , p3 , 0
  else
  dim p12 as integer , p13 as integer , p23 as integer
    p12 = 255 - no * 3
    p13 = 255 - no * 3 - 1
    p23 = 255 - no * 3 - 2
    pnt( p12 ) = ( pnt( p1 ) + pnt( p2 ) ) / 2
    pnt( p13 ) = ( pnt( p1 ) + pnt( p3 ) ) / 2
    pnt( p23 ) = ( pnt( p2 ) + pnt( p3 ) ) / 2
    pnt( p12 ).normalize
    pnt( p13 ).normalize
    pnt( p23 ).normalize
    geo no - 1 , p1 , p12 , p13
    geo no - 1 , p2 , p23 , p12
    geo no - 1 , p3 , p13 , p23
    geo no - 1 , p12 , p23 , p13
  end if
end sub

sub isoca( i as integer )
  if i < 0 then i = 0
  if i > 5 then i = 5
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z 
  glScalef box.d.x , box.d.y , box.d.z
    
  setpoint  1 ,  0       ,  0 , 1.118034
  setpoint  2 ,  1       ,  0         ,  .5 
  setpoint  3 ,  .309017 ,  .95105654 ,  .5 
  setpoint  4 , -.809017 ,  .58778524 ,  .5 
  setpoint  5 , -.809017 , -.58778524 ,  .5 
  setpoint  6 ,  .309017 , -.95105654 ,  .5 
  setpoint  7 ,  .809017 ,  .58778524 , -.5 
  setpoint  8 , -.309017 ,  .95105654 , -.5 
  setpoint  9 , -1       ,  0         , -.5 
  setpoint 10 , -.309017 , -.95105654 , -.5
  setpoint 11 ,  .809017 , -.58778524 , -.5 
  setpoint 12 ,  0       ,  0         , -1.118034
  dim t as integer
  for t = 1 to 12
    pnt( t ).normalize
  next t
  geo i , 1 ,  2 , 3
  geo i , 1 ,  3 ,  4 
  geo i , 1 ,  4 ,  5 
  geo i , 1 ,  5 ,  6 
  geo i , 1 ,  6 ,  2 
  geo i , 2 ,  7 ,  3
  geo i , 3 ,  7 ,  8 
  geo i , 3 ,  8 ,  4
  geo i , 4 ,  8 ,  9 
  geo i , 4 ,  9 ,  5 
  geo i , 5 ,  9 , 10 
  geo i , 5 , 10 ,  6 
  geo i , 6 , 10 , 11 
  geo i , 6 , 11 ,  2
  geo i , 2 , 11 ,  7 
  geo i , 12 ,  8 ,  7
  geo i , 12 ,  9 ,  8
  geo i , 12 , 10 ,  9 
  geo i , 12 , 11 , 10 
  geo i , 12 ,  7 , 11 
  glPopMatrix
end sub

sub sphere( a as integer , b as integer _
, da as single , db as single )
  dim as single i , j , i2 , j2 
  dim as single x , y , z
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64
  glPushMatrix
  glTranslatef box.m.x , box.m.y , box.m.z
  glScalef box.d.x , box.d.y , box.d.z
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI / 2 to PI / 2 step PI / b * 2 
      j2 = j + PI / b * 2 

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

      x = sin( i2 ) * cos( j )
      y = sin( j )
      z = cos( i2 ) * cos( j )
      setpoint 1 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i2 ) * cos( j2 )
      y = sin( j2 )
      z = cos( i2 ) * cos( j2 )
      setpoint 2 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      x = sin( i ) * cos( j2 )
      y = sin( j2 )
      z = cos( i ) * cos( j2 )
      setpoint 3 _
      , abs( x ) ^ da * sgn( x ) _
      , abs( y ) ^ db * sgn( y ) _
      , abs( z ) ^ da * sgn( z )
      
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
  glPopMatrix
end sub

sub torus( a as integer , b as integer )
  dim i as single , j as single , i2 as single , j2 as single
  if a < 3 then a = 3 
  if a > 64 then a = 643
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as single , my as single , mz as single , dx as single , dy as single , dz as single 
  mx = box.m.x 
  my = box.m.y 
  mz = box.m.z 
  dx = box.d.x 
  dy = box.d.y 
  dz = box.d.z 
  for i = -PI to PI  step PI / a * 2 
    i2 = i + PI / a * 2 
    for j = -PI to PI step PI / b * 2 
      j2 = j + PI / b * 2 
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) ) * cos( j ) _
      , my + ( dx + dy * cos( i ) ) * sin( j ) _
      , mz + sin( i ) * dz  
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i ) ) * sin( j2 ) _
      , mz + sin( i ) * dz 
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) ) * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j2 ) _
      , mz + sin( i2 ) * dz 
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) ) * cos( j ) _
      , my + ( dx + dy * cos( i2 ) ) * sin( j ) _
      , mz + sin( i2 ) * dz 
      quad 0 , 1 , 2 , 3 , 0
    next j
  next i
end sub

sub cilinder( sides as integer , dx as single , dy as single , top as integer , bot as integer )
  dim f as single
  if sides < 3 then sides = 3
  if sides > 64 then sides = 64
  for f = 0 to sides + 2
    setpoint f , box.m.x + sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z
    setpoint f + sides + 1 , box.m.x + sin( f * pi * 2 / sides ) * dx _
                           , box.m.y + box.d.y _
                           , box.m.z + cos( f * pi * 2 / sides ) * dy
  next f
  for f = 0 to sides + 1
    quad f , f + 1 , f + 2 + sides , f + 1 + sides , 0
  next f
  if top then
    setpoint 255 , 0 , box.m.y + box.d.y , 0
    for f = 0 to sides
        setpoint f , box.m.x + sin( f * pi * 2 / sides ) * dx _
               , box.m.y + box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * dy  
    next f
    for f = 0 to sides
      tri1 255 , f , f + 1 , 0
    next f
  end if
  if bot then
    setpoint 255 , 0 , box.m.y - box.d.y , 0
    for f = 0 to sides + 2
        setpoint f , box.m.x - sin( f * pi * 2 / sides ) * box.d.x _
               , box.m.y - box.d.y _
               , box.m.z + cos( f * pi * 2 / sides ) * box.d.z  
    next f
    for f = 0 to sides + 2
      tri1 255 , f , f + 1 , 0
    next f
  end if
end sub

sub cube()
  setpoint 0 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 1 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 2 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 3 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  setpoint 4 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 5 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 6 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 7 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  quad 0 , 2 , 3 , 1 , 0 ''right
  quad 7 , 6 , 4 , 5 , 0 ''left
  quad 0 , 4 , 5 , 1 , 0 ''up
  quad 7 , 3 , 2 , 6 , 0 ''down
  quad 0 , 4 , 6 , 2 , 0 ''back
  quad 7 , 5 , 1 , 3 , 0 ''front
end sub

sub hcube()
  setpoint 1 , box.m.x + box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 2 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 3 , box.m.x + box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  setpoint 4 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z + box.d.z
  setpoint 5 , box.m.x - box.d.x , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 6 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z + box.d.z
  setpoint 7 , box.m.x - box.d.x , box.m.y - box.d.y , box.m.z - box.d.z
  
  setpoint 0 , box.m.x + box.d.x , box.m.y - box.d.y , 0
  setpoint 8 , box.m.x + box.d.x , 0 , box.m.z - box.d.z
  setpoint 9 , 0 , box.m.y + box.d.y , box.m.z - box.d.z
  setpoint 10 , box.m.x - box.d.x , box.m.x + box.d.y , 0
  setpoint 11 , box.m.x - box.d.x , 0 , box.m.z + box.d.z
  setpoint 12, 0 , box.m.y - box.d.y , box.m.z + box.d.z
  
  tri1 7 , 6 , 3 , 0
  tri1 7 , 5 , 6 , 0
  tri1 7 , 3 , 5 , 0
  
  quad 6 , 5 , 10 , 11 , 0
  quad 5 , 3 , 8 , 9 , 0
  quad 3 , 6 , 12 , 0 , 0
  
  tri1 6 , 12 , 11 , 0
  tri1 3 , 8 , 0 , 0
  tri1 5 , 9 , 10 , 0
end sub

sub setbox( mx as single , my as single , mz as single , dx as single , dy as single , dz as single )
  box.m.x = mx
  box.m.y = my
  box.m.z = mz
  box.d.x = dx
  box.d.y = dy
  box.d.z = dz
end sub

#endif
t_gl_avatars.bas

Code: Select all

''bluatigro 6-nov-2013
''opengl game lib : some avatars

#ifndef T_GL_AVARTARS_H
#define T_GL_AVARTARS_H

#include "t_gl_shapes.bas"

const as integer body = 0 
const as integer arm = 1
const as integer elbow = 2 
const as integer wrist = 3
const as integer leg = 4
const as integer knee = 5 
const as integer enkle = 6 
const as integer neck = 7
const as integer eye = 8 
const as integer ileg = 4 
const as integer iknee = 9 
const as integer wing = 14
const as integer tail = 16
const as integer sensor = 17
const as integer thumb = 18
const as integer finger = 19
const as integer lr = 32

const as integer human_walk = 1
const as integer dog_walk = 2
const as integer I_FLY = 3
const as integer I_LEFT_LEGS = 4
const as integer I_LEFT_BOX = 5
const as integer I_RIGHT_LEGS = 6
const as integer I_RIGHT_BOX = 7
const as integer I_STING = 8
const as integer I_STAND = 9

declare sub animate( anim as integer , f as single , a as single )

declare sub human()
declare sub dog()
declare sub insect()

declare sub pilko()
declare sub man()

sub animate( anim as integer , f as single , a as single )
  DIM I AS INTEGER
  select case anim
  case human_walk
    skelet arm , pend( f , a ) , 0 , 0
    skelet elbow , -abs( a ) , 0 , 0
    skelet arm + lr , pend( f + 180, a ) , 0 , 0
    skelet elbow + lr , -abs( a ) , 0 , 0
    skelet leg , pend( f + 180 , a ) , 0 , 0
    skelet knee , pend( f + 90 , a ) + a , 0 , 0
    skelet leg + lr , pend( f , a ) , 0 , 0
    skelet knee + lr , pend( f - 90 , a ) + a , 0 , 0
  case dog_walk
    skelet arm , pend( f + 180 , a ) , 0 , 0
    skelet elbow , pend( f + 90 , a ) + a , 0 , 0
    skelet arm + lr , pend( f , a ) , 0 , 0
    skelet elbow + lr , pend( f - 90 , a ) + a , 0 , 0
    skelet leg , pend( f + 180 , a ) , 0 , 0
    skelet knee , pend( f + 90 , a ) + a , 0 , 0
    skelet leg + lr , pend( f , a ) , 0 , 0
    skelet knee + lr , pend( f - 90 , a ) + a , 0 , 0
    skelet tail , -45 , pend( f * 2 , a ) , 0
    skelet neck , 0 , 0 , 0
    skelet neck + lr , 0 , 0 , 0
  Case I_FLY
    For i = 0 To 1
      skelet wing + i, 0 , 0 , Pend(f, a)
      skelet wing+lr + i, 0,0, Pend(f, -a)
    Next
  Case I_LEFT_BOX
    skelet arm, 0, Pend(f, -a) + 45 , 0
    skelet elbow, 0, Pend(f, a * 2) - 60 , 0
  Case I_LEFT_LEGS
    For i = 0 To 4
      skelet ileg + i, 0 , 0, Pend(f + i * 180, a)
      skelet iknee + i, Pend(f + i * 180 + 90, a) , 0 , 0
    Next
  Case I_RIGHT_BOX
    skelet arm+lr, 0, Pend(f, a) - 45,0
    skelet elbow+lr, 0, Pend(f, -a * 2) + 60, 0
  Case I_RIGHT_LEGS
    For i = 0 To 4
      skelet ileg+lr+ i, 0,0, Pend(f + i * 180, a)
      skelet iknee+lr + i, Pend(f + i * 180 + 90, a),0,0
    Next
  Case I_STAND
    skelet arm, 0, 45, 0
    skelet elbow, 0, -60 , 0
    skelet finger, 0, 0, 0
    skelet thumb, 0, 0, 0
    skelet arm+lr, 0, -45, 0
    skelet elbow+lr, 0, 60 , 0
    skelet finger+lr, 0, 0, 0
    skelet thumb+lr, 0, 0, 0
    skelet tail, 10, 0 , 0
    skelet tail+lr, 10, 0 , 0
  Case I_STING
    skelet tail, 10 + Pend(f, a), 0, 0
    skelet tail+lr, 10 - Pend(f, a), 0, 0
  case else
    dim i as integer
    for i = 0 to 63
      skelet i , 0,0,0
    next i
  end select
end sub

sub insect()
  Dim i as integer
glPushmatrix
  glScalef .01 , .01 , .01
  setbox 0, 0, 0, 30, 10.0, 60.0
  Cube
  For i = 0 To 4
    glPushMatrix
      child 35.0, 0.0, i * 25 - 50 , ileg + i, xyz
      setbox 30.0, 0.0, 0.0, 30.0, 5.0, 5.0
      Cube
      glPushMatrix
        child 65.0, -5.0, 0.0 , iknee + i, xyz
        setbox 0.0, -30.0, 0.0, 5.0, 30.0, 5.0
        Cube
      glPopMatrix
    glPopMatrix
    glpushMatrix
      child -35.0, 0.0, i * 25 - 50, ileg + lr + i, xyz
      setbox -30.0, 0.0, 0.0, 30.0, 5.0, 5.0
      Cube 
      glPushMatrix
        child -65.0, -5.0, 0.0 , iknee + lr + 1, xyz
        setbox 0.0, -30.0, 0.0, 5.0, 30.0, 5.0
        Cube
      glPopmatrix
    glPopMatrix
  Next
  glPushMatrix
    child 0 , 0 , -50 , tail , xyz
    For i = 0 To 9
      glPushMatrix
        child 0.0, 0.0, -30.0 , tail, xyz
        setbox 0.0, 0.0, -15.0, 10.0, 10.0, 10.0
        Cube
    Next
    for i = 0 to 8
        glPushMatrix
          child 0 , 0 , -30 , tail+lr , xyz
          cube
    next i
    for i = 0 to 8
        glPopMatrix
      glPopMatrix
    next i
  glPopMatrix
  glPushMatrix
    child 30.0, 0.0, 65.0, arm, xyz
    setbox 0.0, 0.0, 30.0, 5.0, 5.0, 30.0
    Cube
    glPushMatrix
      child 0.0, 0.0, 65.0, elbow, xyz
      Cube
      glPushmatrix
        child 0.0, 0.0, 65.0 , wrist, xyz
        glPushmatrix
          child -10.0, 0.0, 5.0 , thumb, xyz
          Cube
        glPopMatrix
        glPushMatrix
          child 5.0, 0.0, 5.0, finger, xyz
          setbox 0.0, 0.0, 30.0, 5.0, 10.0, 30.0
          Cube
        glPopMatrix
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -30.0, 0.0, 65.0, arm + lr, xyz
    setbox 0.0, 0.0, 30.0, 5.0, 5.0, 30.0
    Cube
    glPushMatrix
      child 0.0, 0.0, 65.0, elbow +lr, xyz
      Cube
      glPushMatrix
        child 0.0, 0.0, 65.0, wrist+lr, xyz
        glPushMatrix
          child 10.0, 0.0, 5.0, thumb+lr, xyz
          Cube
        glPopMatrix
        glPushMatrix
          child -5.0, 0.0, 5.0, finger+lr, xyz
          setbox 0.0, 0.0, 30.0, 5.0, 10.0, 30.0
          Cube
        glPopMatrix
      glPopMatrix
    glPopMatrix
   glPopMatrix
   For i = 0 To 1
     glPushMatrix
       child 20.0, 20.0, 40.0 - 50.0 * i, wing + i, xyz
       setbox 60.0, 0.0, 8.0, 60.0, 2.0, 16.0
       Cube
     glPopMatrix
     glPushMatrix
       child -20.0, 20.0, 40.0 - 50.0 * i , wing+lr + i,  xyz
       setbox -60.0, 0.0, 8.0, 60.0, 2.0, 16.0
       Cube
     glPopmatrix
   Next
glPopMatrix
end sub

sub human()
  setbox  0 , 0 , 0  ,  .5 , .1 , .1
  cube 
  setbox 0 , .75 , 0 , .1 , .5 , .1
  cube 
  setbox 0 , 1.8 , 0 , .2 , .2 , .2
  cube 
  setbox 0 , 1.4 , 0 , .7 , .1 , .1
  cube 
  glPushMatrix
    child .45 , 0 , 0 , leg , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle , xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.45 , 0 , 0 , leg + lr , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee + lr , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle + lr , xyz 
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child .65 , 1.3 , 0 , arm , xyz
    setbox 0 , -.5 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow , xyz
      cube 
      glPushMatrix
        child 0 , -1 , 0 , wrist , zyx
        setbox 0 , -.3 , 0 , .05 , .2 , .15
        cube
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.65 , 1.3 , 0 , arm + lr , xyz
    setbox 0 , -.5 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow + lr , xyz
      cube 
      glPushMatrix
        child 0 , -1 , 0 , wrist + lr , zyx
        setbox 0 , -.3 , 0 , .05 , .2 , .15
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
end sub

sub dog()
  setbox 0,.2,.5 , .3,.3,.7
  cube
  glpushmatrix
    child 0 , .6 , 1.5 , neck , xyz
    glpushmatrix
      child 0 , 0 , 0 , neck + lr , zyx
      setbox 0,0,0 , .3 , .3 , .3
      cube
      setbox 0,-.2,.3 , .2,.2,.2
      cube
      setbox 0,0,.5 , .1,.1,.1
      cube
      setbox .3,-.15,0 , .05,.3,.2
      cube
      setbox -.3,-.15,0 , .05,.3,.2
      cube
    glpopmatrix
  glpopmatrix
  glpushmatrix
    child 0 , .4 , -.5 , tail , yzx
    setbox 0,.3,0 , .1 , .3 , .1
    cube
  glpopmatrix
  glPushMatrix
    child .3 , 0 , 1 , leg , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle , xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.3 , 0 , 1 , leg + lr, zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , knee + lr, xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , enkle + lr, xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child .3 , 0 , 0 , arm , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , wrist , xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
  glPushMatrix
    child -.3 , 0 , 0 , arm + lr , zyx
    setbox 0 , -.6 , 0 , .1 , .4 , .1
    cube 
    glPushMatrix
      child 0 , -1 , 0 , elbow + lr , xyz
      cube 
      glPushMatrix
        child 0 , -1.2 , 0 , wrist + lr , xyz
        setbox 0 , 0 , .2 , .1 , .1 , .3
        cube 
      glPopMatrix
    glPopMatrix
  glPopMatrix
end sub

sub pilko()
    setbox 0,0,0 , .3,.3,.3
    isoca 3
    setbox 0,0,-.5 , .3,.3,.3
    isoca 3
    glpushmatrix
      child .15 , -.3 , 0 , arm , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , elbow , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child .15 , -.3 , -.5 , leg , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , knee , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child -.15 , -.3 , 0 , arm + lr , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , elbow + lr , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      child -.15 , -.3 , -.5 , leg + lr , yzx
      setbox 0,0,0 , .1 , .1 , .1
      isoca 2
      setbox 0,-.2,0 , .1 , .1 , .1
      isoca 2
      glpushmatrix
        child 0 , -.4 , 0 , knee + lr , xyz
        setbox 0,0,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.2,0 , .1 , .1 , .1
        isoca 2
        setbox 0,-.4,0 , .07 , .07 , .07
        isoca 2
        setbox 0,-.4,.1 , .07 , .07 , .07
        isoca 2
      glpopmatrix
    glpopmatrix
    glpushmatrix
      dim i as single
      child 0,.3, -.7 , tail , yxz
      for i = 0 to 4
        setbox 0,i*.15,0 , .1 , .1  , .1
        isoca 2
      next i
    glpopmatrix
    glpushmatrix
      child 0,.15,.15 , neck , xyz
      glPushMatrix
        child 0,.15,.15,neck+lr, zyx
        setbox 0,0,0 , .2 , .2 , .2
        isoca 3
        setbox .2, .2 , 0 , .07 , .07 , .07
        isoca 2
        setbox -.2 , .2 , 0 , .07 , .07 , .07
        isoca 2
        setbox 0,0,.24 , .07 , .07 , .07
        isoca 2
        glPushMatrix
          child .1 , .14 , .14 , eye , xyz
          setbox 0,0,0 , .07 , .07 , .07 
          glmaterialfv gl_front , gl_ambient , @white( 0 )
          glmaterialfv gl_front , gl_diffuse , @white( 0 )
          isoca 2
          setbox 0,0,.07 , .05,.05,.05
          glmaterialfv gl_front , gl_ambient , @black( 0 )
          glmaterialfv gl_front , gl_diffuse , @black( 0 )
          isoca 2
        glPopMatrix
        glPushmatrix
          child -.1 , .14 , .14 , eye + lr , xyz
          setbox 0,0,0 , .07 , .07 , .07 
          glmaterialfv gl_front , gl_ambient , @white( 0 )
          glmaterialfv gl_front , gl_diffuse , @white( 0 )
          isoca 2
          setbox 0,0,.07 , .05,.05,.05
          glmaterialfv gl_front , gl_ambient , @black( 0 )
          glmaterialfv gl_front , gl_diffuse , @black( 0 )
          isoca 2
        glPopMatrix
      glPopMatrix
    glpopmatrix
end sub

dim i as integer
dim shared as single trui( 3 )
dim shared as single broek( 3 )
dim shared as single huid( 3 ) 
for i = 0 to 3
  trui( i ) = yellow( i )
  broek( i ) = blue( i )
  huid( i ) = orange( i )
next i

sub man()
    glPushMatrix
      glScalef .01 , .01 , .01
      child 0, 0, 0, body + lr , xyz 
      glpushmatrix
        child 20, -10, 0, leg, yzx
        glmaterialfv gl_front , gl_ambient , @broek( 0 )
        glmaterialfv gl_front , gl_diffuse , @broek( 0 )
        setbox 0, 0, 0, 16, 16, 16
        isoca 3
        setbox 0, -20, 0, 16, 16, 16
        isoca 3
        glpushmatrix
          child 0, -40, 0, knee, xyz
          setbox 0, 0, 0, 16, 16, 16''broek
          isoca 3
          setbox 0, -20, 0, 16, 16, 16 ''broek
          isoca 3
          glpushmatrix
            child 0, -40, 0, enkle, xzy
            glmaterialfv gl_front , gl_ambient , @gray( 0 )
            glmaterialfv gl_front , gl_diffuse , @gray( 0 )
            setbox 0, 0, 0, 12, 12, 12''gray
            isoca 3
            setbox 0, 0, 20, 12, 12, 12 ''gray
            isoca 3
          glpopmatrix
        glpopmatrix
      glpopmatrix
      glpushmatrix
        child -20, -10, 0, leg + lr , yzx
        glmaterialfv gl_front , gl_ambient , @broek( 0 )
        glmaterialfv gl_front , gl_diffuse , @broek( 0 )
        setbox 0, 0, 0, 16, 16, 16''broek
        isoca 3
        setbox 0, -20, 0, 16, 16, 16'' broek
        isoca 3
        glpushmatrix
          child 0, -40, 0, knee + lr, xyz
          setbox 0, 0, 0, 16, 16, 16''broek
          isoca 3
          setbox 0, -20, 0, 16, 16, 16''broek
          isoca 3
          glpushmatrix
            child 0, -40, 0, enkle + lr, xzy
            glmaterialfv gl_front , gl_ambient , @gray( 0 )
            glmaterialfv gl_front , gl_diffuse , @gray( 0 )
            setbox 0, 0, 0, 12, 12, 12''gray
            isoca 3
            setbox 0, 0, 20, 12, 12, 12 ''gray
            isoca 3
          glpopmatrix
        glpopmatrix
      glpopmatrix
      glPushMatrix
        child 0, 0, 0, body , xyz 
        glmaterialfv gl_front , gl_ambient , @trui( 0 )
        glmaterialfv gl_front , gl_diffuse , @trui( 0 )
        setbox 0, 50, 0, 30, 30, 30 ''trui
        isoca 4
        glmaterialfv gl_front , gl_ambient , @broek( 0 )
        glmaterialfv gl_front , gl_diffuse , @broek( 0 )
        setbox 0, 25, 0, 23, 23, 23''broek
        isoca 4
        setbox 0, 0, 0, 15 , 15 , 15 '' broek
        isoca 3
        glpushmatrix
          child 0,80,0 , neck , xyz
          glPushMatrix
            child 0,15,0, neck + lr, zyx
            glmaterialfv gl_front , gl_ambient , @huid( 0 )
            glmaterialfv gl_front , gl_diffuse , @huid( 0 )
            setbox 0,0,0 , 20 , 20 , 20
            isoca 3
            setbox 20, 0 , 0 , 7 , 7 , 7
            isoca 2
            setbox -20 , 0 , 0 , 7 , 7 , 7
            isoca 2
            setbox 0,0,24 , 7 , 7 , 7
            isoca 2
            glPushMatrix
              child 10 , 14 , 14 , eye , xyz
              setbox 0,0,0 , 7 , 7 , 7 
              glmaterialfv gl_front , gl_ambient , @white( 0 )
              glmaterialfv gl_front , gl_diffuse , @white( 0 )
              isoca 2
              setbox 0,0,7 , 5,5,5
              glmaterialfv gl_front , gl_ambient , @black( 0 )
              glmaterialfv gl_front , gl_diffuse , @black( 0 )
              isoca 2
            glPopMatrix
            glPushmatrix
              child -10 , 14 , 14 , eye + lr , xyz
              setbox 0,0,0 , 7 , 7 , 7 
              glmaterialfv gl_front , gl_ambient , @white( 0 )
              glmaterialfv gl_front , gl_diffuse , @white( 0 )
              isoca 2
              setbox 0,0,7 , 5,5,5
              glmaterialfv gl_front , gl_ambient , @black( 0 )
              glmaterialfv gl_front , gl_diffuse , @black( 0 )
              isoca 2
            glPopMatrix
          glPopMatrix
        glpopmatrix
          glpushmatrix
            child 40, 60, 0, arm, xzy
            glmaterialfv gl_front , gl_ambient , @trui( 0 )
            glmaterialfv gl_front , gl_diffuse , @trui( 0 )
            setbox 0, 0, 0, 16, 16, 16''trui
            isoca 3
            setbox 6, -20, 0, 12, 12, 12''trui
            isoca 3
            glpushmatrix
              child 6, -40, 0, elbow, xyz
              setbox 0, 0, 0, 12, 12, 12''trui
              isoca 3
              setbox 0, -20, 0, 12, 12, 12''trui
              isoca 3
              glmaterialfv gl_front , gl_ambient , @huid( 0 )
              glmaterialfv gl_front , gl_diffuse , @huid( 0 )
               setbox 0, -42, 0, 8, 8, 8''pink
              isoca 3
            glpopmatrix
          glpopmatrix
          glpushmatrix
            child -40, 60, 0, arm + lr, xzy
            glmaterialfv gl_front , gl_ambient , @trui( 0 )
            glmaterialfv gl_front , gl_diffuse , @trui( 0 )
            setbox 0, 0, 0, 16, 16, 16
            isoca 3
            setbox -6, -20, 0, 12, 12, 12
            isoca 3
            glpushmatrix
              child -6, -40, 0, elbow + lr, xyz
              setbox 0, 0, 0, 12, 12, 12
              isoca 3
              setbox 0, -20, 0, 12, 12, 12
              isoca 3
              glmaterialfv gl_front , gl_ambient , @huid( 0 )
              glmaterialfv gl_front , gl_diffuse , @huid( 0 )
              setbox 0, -42, 0, 8, 8, 8
              isoca 3
            glpopmatrix
          glpopmatrix
      glpopmatrix
    glpopmatrix
end sub

#endif
Post Reply