a simple maze

Game development specific discussions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

a simple maze

Post by bluatigro »

this is my first game in fb

future :
-windows fulscreen & no border
-joystick control

how big can de maze be max ?
i took a save bet at 30 but i think it can be more

Code: Select all


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

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

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

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


type T3d
  x as single
  y as single
  z as single
  declare constructor()
  declare constructor( x as single , y as single , y as single )
  declare sub fill( x as single , y as single , z as single )
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
sub T3d.fill( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z 
end sub
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.x - b.x , a.y - b.y , a.z - b.z )
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 , 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
declare function lenght( a as T3d ) as single
dim shared pnt( 255 ) as T3d
dim shared sk( 64 ) as T3d
type Tbox
  m as T3d
  d as T3d
end type
dim shared box as Tbox

declare sub opengl_init()

declare sub isoca( i 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 setpoint( no as integer , x as single , y as single , z as single )
declare sub tri( p1 as integer , p2 as integer , p3 as integer )
declare sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
declare function rad( deg as single ) as single

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 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 dgreen( 3 )   = { 0 , .5 , 0 , 1 }

type wall
  as integer wallx, wally, roomx, roomy
end type
print
print
print
print " bluatigro presents :"
print " the maze ."
print 
print " instructions :"
print "   use mouse to move forwart and back and to turn left and right ."
print "   use cursorkeys to move up , down , left and right ."
print "   use esc key to quit game at any time ."
print
print "   you cant move while your up ."
print
dim as string large
dim as integer size
input " how large must the maze be ? [ 10 - 30 ] = " ; large
size = val( large )
if size < 10 then size = 10
if size > 30 then size = 30  
if size mod 2 then size += 1 ''no odd sizes
dim shared as integer maxx 
dim shared as integer maxy 
maxx = size
maxy = size
dim shared as integer maze( maxx , maxy )
dim shared as wall walls( maxx * maxy )

dim as integer x , y , maxwall

for x = 0 to maxx
  for y = 0 to maxy
    maze( x , y ) = not 0
  next y
next x

randomize

walls( 0 ).roomx = 1
walls( 0 ).roomy = 1
walls( 0 ).wallx = 1
walls( 0 ).wally = 1
do
  x = int(rnd * (1 + maxwall))
  if walls( x ).roomx >= 0 and walls( x ).roomx <= maxx and walls( x ).roomy >= 0 and walls( x ).roomy <= maxy then
    if maze( walls( x ).roomx , walls( x ).roomy ) then
      maze( walls( x ).roomx , walls( x ).roomy ) = 0
      maze( walls( x ).wallx , walls( x ).wally ) = 0
      
      walls( maxwall + 1 ).wallx = walls( x ).roomx - 1
      walls( maxwall + 1 ).wally = walls( x ).roomy
      walls( maxwall + 1 ).roomx = walls( x ).roomx - 2
      walls( maxwall + 1 ).roomy = walls( x ).roomy
      
      walls( maxwall + 2 ).wallx = walls( x ).roomx
      walls( maxwall + 2 ).wally = walls( x ).roomy + 1
      walls( maxwall + 2 ).roomx = walls( x ).roomx
      walls( maxwall + 2 ).roomy = walls( x ).roomy + 2
      
      walls( maxwall + 3 ).wallx = walls( x ).roomx
      walls( maxwall + 3 ).wally = walls( x ).roomy - 1
      walls( maxwall + 3 ).roomx = walls( x ).roomx
      walls( maxwall + 3 ).roomy = walls( x ).roomy - 2
      
      walls( maxwall + 4 ).wallx = walls( x ).roomx + 1
      walls( maxwall + 4 ).wally = walls( x ).roomy
      walls( maxwall + 4 ).roomx = walls( x ).roomx + 2
      walls( maxwall + 4 ).roomy = walls( x ).roomy
      
      maxwall += 4
    end if
  end if
  
  walls( x ).wallx = walls( maxwall ).wallx
  walls( x ).wally = walls( maxwall ).wally
  walls( x ).roomx = walls( maxwall ).roomx
  walls( x ).roomy = walls( maxwall ).roomy

  maxwall -= 1
loop until maxwall = -1

maze( maxx - 1 , maxy - 1 ) = 2

type entity
  dim m as T3d
  dim a as single
  dim dm as T3d
  dim da as single
end type

dim cam as entity
cam.m.fill 1.5 , 0 , 1.5

const as single speed = .03

dim shared as single rainbow( 3 )

dim shared frame as single , state as integer , angle as single

dim i as integer , j as integer , k as integer



opengl_init
do
      
  glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT      '' Clear Screen And Depth Buffer
  
  glLoadIdentity
  glrotatef -cam.a , 0 , 1 , 0
  gltranslatef -cam.m.x , -cam.m.y , -cam.m.z
  
  rainbow( 0 ) = sin( rad( frame * 4 ) ) / 2 + .5
  rainbow( 1 ) = sin( rad( frame * 4 + 120 ) ) / 2 + .5
  rainbow( 2 ) = sin( rad( frame * 4 - 120 ) ) / 2 + .5
  rainbow( 3 ) = 1

  glmaterialfv gl_front , gl_diffuse , @green( 0 )
  glmaterialfv gl_front , gl_ambient , @green( 0 )

  for i = 0 to maxx
    for j = 0 to maxy
      if ( i + j ) and 1 then
        glmaterialfv gl_front , gl_diffuse , @green( 0 )
        glmaterialfv gl_front , gl_ambient , @green( 0 )
      else
        glmaterialfv gl_front , gl_diffuse , @magenta( 0 )
        glmaterialfv gl_front , gl_ambient , @magenta( 0 )
      end if
      select case maze( i , j ) 
        case true
          setbox i , 0 , j , .4 , .4 , .4
          cube
        case 2
          glmaterialfv gl_front , gl_diffuse , @rainbow( 0 )
          glmaterialfv gl_front , gl_ambient , @rainbow( 0 )
          setbox i , 0 , j , .3 , .5 , .3
          isoca 1
        case else
      end select
    next j
  next i

  
  glflush
  
    
  cam.dm.fill 0 , 0 , 0
  if multikey( sc_up ) and cam.m.y < 10 then cam.m.y += speed
  if multikey( sc_down ) and cam.m.y > 0 then cam.m.y -= speed
  if multikey( sc_right ) then 
    cam.dm.x =   cos( rad( cam.a ) ) * speed
    cam.dm.z = - sin( rad( cam.a ) ) * speed
  end if
  if multikey( sc_left ) then 
    cam.dm.x = - cos( rad( cam.a ) ) * speed
    cam.dm.z =   sin( rad( cam.a ) ) * speed
  end if
  if not getmouse( mouse_x , mouse_y ) then
    if mouse_x < 1024 / 3 then cam.a += 1
    if mouse_x > 1024 * 2 / 3 then cam.a -= 1
    if mouse_y < 768 / 3 then 
      cam.dm.x = - sin( rad( cam.a ) ) * speed
      cam.dm.z = - cos( rad( cam.a ) ) * speed
    end if
    if mouse_y > 786 * 2 / 3 then 
      cam.dm.x =   sin( rad( cam.a ) ) * speed
      cam.dm.z =   cos( rad( cam.a ) ) * speed
    end if
  end if
  if cam.m.x > 0 and cam.m.x < 21 and cam.m.z > 0 and cam.m.z < 21 then
    if not maze( cam.m.x + cam.dm.x , cam.m.z + cam.dm.z ) and cam.m.y < 1 then
      cam.m = cam.m + cam.dm
    end if
  else
    cam.m = cam.m + cam.dm
  end if


  if frame = 0 then state = ( state + 1 ) mod 5
  frame = ( frame + 1 ) mod 360
  sleep 40
  flip
loop until asc( inkey ) = 27
end




function lenght( a as T3d ) as single
  return sqr( a.x ^ 2 + a.y ^ 2 + a.z ^ 2 ) + 0.00001
end function
function rad( deg as single ) as single
  return deg * PI / 180
end function
sub opengl_init()
	screen 20, 16, , 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 , 100 , 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 geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri p1 , p2 , p3 
  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 setpoint( no as integer , x as single , y as single , z as single )
  pnt( no and 255 ).fill x , y , z
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  ''right
  quad 7 , 6 , 4 , 5  ''left
  quad 0 , 4 , 5 , 1  ''up
  quad 7 , 3 , 2 , 6  ''down
  quad 0 , 4 , 6 , 2  ''back
  quad 7 , 5 , 1 , 3  ''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 tri( p1 as integer , p2 as integer , p3 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 
    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
  glend
end sub
sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
  dim q as T3d
  q = ( pnt( p2 ) - pnt( p1 ) ) * ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_QUADS
    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
  

SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: a simple maze

Post by SARG »

Extract from the freebasic manual : If the mouse runs off the window, all values are set to -1.
So just add the marked test to avoid an undesired effect.

Code: Select all

  if not getmouse( mouse_x , mouse_y ) Then
  	If mouse_x<>-1 Then <--------------------------------------------------------------
    if mouse_x < 1024 / 3 then cam.a += 1
    if mouse_x > 1024 * 2 / 3 then cam.a -= 1
    if mouse_y < 768 / 3 then 
      cam.dm.x = - sin( rad( cam.a ) ) * speed
      cam.dm.z = - cos( rad( cam.a ) ) * speed
    end if
    if mouse_y > 786 * 2 / 3 then 
      cam.dm.x =   sin( rad( cam.a ) ) * speed
      cam.dm.z =   cos( rad( cam.a ) ) * speed
    end If 
  	EndIf <--------------------------------------------------------------
  end if
I think that I'll use some parts of your code for one of my games.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: a simple maze

Post by bluatigro »

you mean :
if mouse_x <> -1 and mouse_y <> -1 then

Code: Select all


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

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

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

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


type T3d
  x as single
  y as single
  z as single
  declare constructor()
  declare constructor( x as single , y as single , y as single )
  declare sub fill( x as single , y as single , z as single )
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
sub T3d.fill( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z 
end sub
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.x - b.x , a.y - b.y , a.z - b.z )
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 , 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
declare function lenght( a as T3d ) as single
dim shared pnt( 255 ) as T3d
dim shared sk( 64 ) as T3d
type Tbox
  m as T3d
  d as T3d
end type
dim shared box as Tbox

declare sub opengl_init()

declare sub isoca( i 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 setpoint( no as integer , x as single , y as single , z as single )
declare sub tri( p1 as integer , p2 as integer , p3 as integer )
declare sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
declare function rad( deg as single ) as single

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 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 dgreen( 3 )   = { 0 , .5 , 0 , 1 }

type wall
  as integer wallx, wally, roomx, roomy
end type
print
print
print
print " bluatigro presents :"
print " the maze ."
print 
print " instructions :"
print "   use mouse to move forwart and back and to turn left and right ."
print "   use cursorkeys to move up , down , left and right ."
print "   use esc key to quit game at any time ."
print
print "   you cant move while your up ."
print
dim as string large
dim as integer size
input " how large must the maze be ? [ 10 - 30 ] = " ; large
size = val( large )
if size < 10 then size = 10
if size > 30 then size = 30  
if size mod 2 then size += 1 ''no odd sizes
dim shared as integer maxx 
dim shared as integer maxy 
maxx = size
maxy = size
dim shared as integer maze( maxx , maxy )
dim shared as wall walls( maxx * maxy )

dim as integer x , y , maxwall

for x = 0 to maxx
  for y = 0 to maxy
    maze( x , y ) = not 0
  next y
next x

randomize

walls( 0 ).roomx = 1
walls( 0 ).roomy = 1
walls( 0 ).wallx = 1
walls( 0 ).wally = 1
do
  x = int(rnd * (1 + maxwall))
  if walls( x ).roomx >= 0 and walls( x ).roomx <= maxx and walls( x ).roomy >= 0 and walls( x ).roomy <= maxy then
    if maze( walls( x ).roomx , walls( x ).roomy ) then
      maze( walls( x ).roomx , walls( x ).roomy ) = 0
      maze( walls( x ).wallx , walls( x ).wally ) = 0
      
      walls( maxwall + 1 ).wallx = walls( x ).roomx - 1
      walls( maxwall + 1 ).wally = walls( x ).roomy
      walls( maxwall + 1 ).roomx = walls( x ).roomx - 2
      walls( maxwall + 1 ).roomy = walls( x ).roomy
      
      walls( maxwall + 2 ).wallx = walls( x ).roomx
      walls( maxwall + 2 ).wally = walls( x ).roomy + 1
      walls( maxwall + 2 ).roomx = walls( x ).roomx
      walls( maxwall + 2 ).roomy = walls( x ).roomy + 2
      
      walls( maxwall + 3 ).wallx = walls( x ).roomx
      walls( maxwall + 3 ).wally = walls( x ).roomy - 1
      walls( maxwall + 3 ).roomx = walls( x ).roomx
      walls( maxwall + 3 ).roomy = walls( x ).roomy - 2
      
      walls( maxwall + 4 ).wallx = walls( x ).roomx + 1
      walls( maxwall + 4 ).wally = walls( x ).roomy
      walls( maxwall + 4 ).roomx = walls( x ).roomx + 2
      walls( maxwall + 4 ).roomy = walls( x ).roomy
      
      maxwall += 4
    end if
  end if
  
  walls( x ).wallx = walls( maxwall ).wallx
  walls( x ).wally = walls( maxwall ).wally
  walls( x ).roomx = walls( maxwall ).roomx
  walls( x ).roomy = walls( maxwall ).roomy

  maxwall -= 1
loop until maxwall = -1

maze( maxx - 1 , maxy - 1 ) = 2

type entity
  dim m as T3d
  dim a as single
  dim dm as T3d
  dim da as single
end type

dim cam as entity
cam.m.fill 1.5 , 0 , 1.5

const as single speed = .03

dim shared as single rainbow( 3 )

dim shared frame as single , state as integer , angle as single

dim i as integer , j as integer , k as integer



opengl_init
do
      
  glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT      '' Clear Screen And Depth Buffer
  
  glLoadIdentity
  glrotatef -cam.a , 0 , 1 , 0
  gltranslatef -cam.m.x , -cam.m.y , -cam.m.z
  
  rainbow( 0 ) = sin( rad( frame * 4 ) ) / 2 + .5
  rainbow( 1 ) = sin( rad( frame * 4 + 120 ) ) / 2 + .5
  rainbow( 2 ) = sin( rad( frame * 4 - 120 ) ) / 2 + .5
  rainbow( 3 ) = 1

  glmaterialfv gl_front , gl_diffuse , @green( 0 )
  glmaterialfv gl_front , gl_ambient , @green( 0 )

  for i = 0 to maxx
    for j = 0 to maxy
      if ( i + j ) and 1 then
        glmaterialfv gl_front , gl_diffuse , @green( 0 )
        glmaterialfv gl_front , gl_ambient , @green( 0 )
      else
        glmaterialfv gl_front , gl_diffuse , @magenta( 0 )
        glmaterialfv gl_front , gl_ambient , @magenta( 0 )
      end if
      select case maze( i , j ) 
        case true
          setbox i , 0 , j , .4 , .4 , .4
          cube
        case 2
          glmaterialfv gl_front , gl_diffuse , @rainbow( 0 )
          glmaterialfv gl_front , gl_ambient , @rainbow( 0 )
          setbox i , 0 , j , .3 , .5 , .3
          isoca 1
        case else
      end select
    next j
  next i

  
  glflush
  
    
  cam.dm.fill 0 , 0 , 0
  if multikey( sc_up ) and cam.m.y < 10 then cam.m.y += speed
  if multikey( sc_down ) and cam.m.y > 0 then cam.m.y -= speed
  if multikey( sc_right ) then 
    cam.dm.x =   cos( rad( cam.a ) ) * speed
    cam.dm.z = - sin( rad( cam.a ) ) * speed
  end if
  if multikey( sc_left ) then 
    cam.dm.x = - cos( rad( cam.a ) ) * speed
    cam.dm.z =   sin( rad( cam.a ) ) * speed
  end if
  if not getmouse( mouse_x , mouse_y ) then
    if mouse_x <> -1 and mouse_y <> -1 then
      if mouse_x < 1024 / 3 then cam.a += 1
      if mouse_x > 1024 * 2 / 3 then cam.a -= 1
      if mouse_y < 768 / 3 then 
        cam.dm.x = - sin( rad( cam.a ) ) * speed
        cam.dm.z = - cos( rad( cam.a ) ) * speed
      end if
      if mouse_y > 786 * 2 / 3 then 
        cam.dm.x =   sin( rad( cam.a ) ) * speed
        cam.dm.z =   cos( rad( cam.a ) ) * speed
      end if
    end if
  end if
  if cam.m.x > 0 and cam.m.x < 21 and cam.m.z > 0 and cam.m.z < 21 then
    if not maze( cam.m.x + cam.dm.x , cam.m.z + cam.dm.z ) and cam.m.y < 1 then
      cam.m = cam.m + cam.dm
    end if
  else
    cam.m = cam.m + cam.dm
  end if


  if frame = 0 then state = ( state + 1 ) mod 5
  frame = ( frame + 1 ) mod 360
  sleep 40
  flip
loop until asc( inkey ) = 27
end




function lenght( a as T3d ) as single
  return sqr( a.x ^ 2 + a.y ^ 2 + a.z ^ 2 ) + 0.00001
end function
function rad( deg as single ) as single
  return deg * PI / 180
end function
sub opengl_init()
	screen 20, 16, , 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 , 100 , 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 geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri p1 , p2 , p3 
  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 setpoint( no as integer , x as single , y as single , z as single )
  pnt( no and 255 ).fill x , y , z
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  ''right
  quad 7 , 6 , 4 , 5  ''left
  quad 0 , 4 , 5 , 1  ''up
  quad 7 , 3 , 2 , 6  ''down
  quad 0 , 4 , 6 , 2  ''back
  quad 7 , 5 , 1 , 3  ''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 tri( p1 as integer , p2 as integer , p3 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 
    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
  glend
end sub
sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
  dim q as T3d
  q = ( pnt( p2 ) - pnt( p1 ) ) * ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_QUADS
    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
  

SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: a simple maze

Post by SARG »

As "all values are set to -1" mouse_x <> -1 should be enough.

By the way, using the "andalso" instead "and" avoids the execution of the next parts of code.
Don't forget that as these parts are not executed something could be missing.

Code: Select all

if a=1 andalso func(a)=12 then 
The func(a) is not executed.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: a simple maze

Post by bluatigro »

a try at a windows maze

so i have fulscreen

error :
-cubes not visible
-the scene keeps rotating
-so does not react to mouse
-opengl window not ful screen [ right is missing ]

code reacts wel to keyboard

Code: Select all



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

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

dim shared as integer mouse_x
dim shared as integer mouse_y
dim shared as integer key( 255 )

const as double PI = atn( 1 ) * 4


type T3d
  x as single
  y as single
  z as single
  declare constructor()
  declare constructor( x as single , y as single , y as single )
  declare sub fill( x as single , y as single , z as single )
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
sub T3d.fill( x as single , y as single , z as single )
  this.x = x
  this.y = y
  this.z = z 
end sub
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.x - b.x , a.y - b.y , a.z - b.z )
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 , 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
declare function lenght( a as T3d ) as single
dim shared pnt( 255 ) as T3d
dim shared sk( 64 ) as T3d
type Tbox
  m as T3d
  d as T3d
end type
dim shared box as Tbox

declare sub opengl_init()

declare sub isoca( i 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 setpoint( no as integer , x as single , y as single , z as single )
declare sub tri( p1 as integer , p2 as integer , p3 as integer )
declare sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
declare function rad( deg as single ) as single

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 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 dgreen( 3 )   = { 0 , .5 , 0 , 1 }

type wall
  as integer wallx, wally, roomx, roomy
end type

dim shared as integer maxx 
dim shared as integer maxy 
maxx = 20
maxy = 20
dim shared as integer maze( maxx , maxy )
dim shared as wall walls( maxx * maxy )

dim as integer x , y , maxwall

for x = 0 to maxx
  for y = 0 to maxy
    maze( x , y ) = not 0
  next y
next x

randomize

walls( 0 ).roomx = 1
walls( 0 ).roomy = 1
walls( 0 ).wallx = 1
walls( 0 ).wally = 1
do
  x = int(rnd * (1 + maxwall))
  if walls( x ).roomx >= 0 and walls( x ).roomx <= maxx and walls( x ).roomy >= 0 and walls( x ).roomy <= maxy then
    if maze( walls( x ).roomx , walls( x ).roomy ) then
      maze( walls( x ).roomx , walls( x ).roomy ) = 0
      maze( walls( x ).wallx , walls( x ).wally ) = 0
      
      walls( maxwall + 1 ).wallx = walls( x ).roomx - 1
      walls( maxwall + 1 ).wally = walls( x ).roomy
      walls( maxwall + 1 ).roomx = walls( x ).roomx - 2
      walls( maxwall + 1 ).roomy = walls( x ).roomy
      
      walls( maxwall + 2 ).wallx = walls( x ).roomx
      walls( maxwall + 2 ).wally = walls( x ).roomy + 1
      walls( maxwall + 2 ).roomx = walls( x ).roomx
      walls( maxwall + 2 ).roomy = walls( x ).roomy + 2
      
      walls( maxwall + 3 ).wallx = walls( x ).roomx
      walls( maxwall + 3 ).wally = walls( x ).roomy - 1
      walls( maxwall + 3 ).roomx = walls( x ).roomx
      walls( maxwall + 3 ).roomy = walls( x ).roomy - 2
      
      walls( maxwall + 4 ).wallx = walls( x ).roomx + 1
      walls( maxwall + 4 ).wally = walls( x ).roomy
      walls( maxwall + 4 ).roomx = walls( x ).roomx + 2
      walls( maxwall + 4 ).roomy = walls( x ).roomy
      
      maxwall += 4
    end if
  end if
  
  walls( x ).wallx = walls( maxwall ).wallx
  walls( x ).wally = walls( maxwall ).wally
  walls( x ).roomx = walls( maxwall ).roomx
  walls( x ).roomy = walls( maxwall ).roomy

  maxwall -= 1
loop until maxwall = -1

maze( maxx - 1 , maxy - 1 ) = 2

type entity
  dim m as T3d
  dim a as single
  dim dm as T3d
  dim da as single
end type

dim shared cam as entity
cam.m.fill 1.5 , 0 , 1.5

const as single speed = .03

dim shared as single rainbow( 3 )

dim shared frame as single , state as integer , angle as single

declare function        WinMain     ( byval hInstance as HINSTANCE, _
                                      byval hPrevInstance as HINSTANCE, _
                                      byval szCmdLine as string, _
                                      byval iCmdShow as integer ) as integer
                                  
                                  
end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )

dim shared as integer rot

'':::::
function WndProc ( byval hWnd as HWND, _
                   byval wMsg as UINT, _
                   byval wParam as WPARAM, _
                   byval lParam as LPARAM ) as LRESULT
    
  function = 0
  dim rct as RECT
  dim pnt as PAINTSTRUCT
  dim hDC as HDC

  select case( wMsg )
    case WM_CREATE    
        'setup TIMER     
      Dim As Integer idTimer1 = 1 , nTimerDelay = 10
      SetTimer(hWnd, idTimer1, nTimerDelay, NULL)
      dim As HDC hDc
      Dim As HGLRC hRc
      Dim As PIXELFORMATDESCRIPTOR pfd
      Dim As Integer iFormat
     
      hDc = GetDC(hwnd)
     
      ZeroMemory(@pfd, sizeof(pfd))
      pfd.nSize = sizeof(pfd)
      pfd.nVersion = 1
      pfd.dwFlags = PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER or PFD_SUPPORT_OPENGL
      pfd.iPixelType = PFD_TYPE_RGBA
      pfd.cColorBits = 24
      pfd.cDepthBits = 16
      pfd.iLayerType = PFD_MAIN_PLANE
        
      iFormat = ChoosePixelFormat(hDc, @pfd)
      SetPixelFormat(hDc, iFormat, @pfd)
       
      hRc = wglCreateContext(hDc)
      wglMakeCurrent(hDc, hRc)
      
      getclientrect( hWnd , @rct )
      
	'' ReSizeGLScene
	glViewport 0, 0, rct.right, rct.bottom                    '' 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 , 100 , 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 )

      Exit Function
    Case WM_TIMER   
        Dim rct as RECT
        GetClientRect( hWnd, @rct )           
        InvalidateRect( hwnd, @rct, FALSE) 'PAINT N.E.B.K
        Exit function  
    case WM_PAINT
            
      Dim pnt as PAINTSTRUCT
      Dim hDC as HDC
      hDC = BeginPaint( hWnd, @pnt )
      dim as integer i , j

            
  glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT      '' Clear Screen And Depth Buffer
  
  glLoadIdentity
  glrotatef -cam.a , 0 , 1 , 0
  gltranslatef -cam.m.x , -cam.m.y , -cam.m.z
  
  rainbow( 0 ) = sin( rad( frame * 4 ) ) / 2 + .5
  rainbow( 1 ) = sin( rad( frame * 4 + 120 ) ) / 2 + .5
  rainbow( 2 ) = sin( rad( frame * 4 - 120 ) ) / 2 + .5
  rainbow( 3 ) = 1

  glmaterialfv gl_front , gl_diffuse , @green( 0 )
  glmaterialfv gl_front , gl_ambient , @green( 0 )

  for i = 0 to maxx
    for j = 0 to maxy
      if ( i + j ) and 1 then
        glmaterialfv gl_front , gl_diffuse , @green( 0 )
        glmaterialfv gl_front , gl_ambient , @green( 0 )
      else
        glmaterialfv gl_front , gl_diffuse , @magenta( 0 )
        glmaterialfv gl_front , gl_ambient , @magenta( 0 )
      end if
      select case maze( i , j ) 
        case true
          setbox i , 0 , j , .4 , .4 , .4
          isoca 0
        case 2
          glmaterialfv gl_front , gl_diffuse , @rainbow( 0 )
          glmaterialfv gl_front , gl_ambient , @rainbow( 0 )
          setbox i , 0 , j , .3 , .5 , .3
          isoca 1
        case else
      end select
    next j
  next i

  
  glflush
  
    
  cam.dm.fill 0 , 0 , 0
  if key( vk_up ) and cam.m.y < 10 then cam.m.y += speed
  if key( vk_down ) and cam.m.y > 0 then cam.m.y -= speed
  if key( vk_right ) then 
    cam.dm.x =   cos( rad( cam.a ) ) * speed
    cam.dm.z = - sin( rad( cam.a ) ) * speed
  end if
  if key( vk_left ) then 
    cam.dm.x = - cos( rad( cam.a ) ) * speed
    cam.dm.z =   sin( rad( cam.a ) ) * speed
  end if
  if not getmouse( mouse_x , mouse_y ) then
''    if mouse_x <> -1 and mouse_y <> -1 then
      if mouse_x < 1024 / 3 then cam.a += 1
      if mouse_x > 1024 * 2 / 3 then cam.a -= 1
      if mouse_y < 768 / 3 then 
        cam.dm.x = - sin( rad( cam.a ) ) * speed
        cam.dm.z = - cos( rad( cam.a ) ) * speed
      end if
      if mouse_y > 786 * 2 / 3 then 
        cam.dm.x =   sin( rad( cam.a ) ) * speed
        cam.dm.z =   cos( rad( cam.a ) ) * speed
      end if
''    end if
  end if
  if cam.m.x > 0 and cam.m.x < 21 and cam.m.z > 0 and cam.m.z < 21 then
    if not maze( cam.m.x + cam.dm.x , cam.m.z + cam.dm.z ) and cam.m.y < 1 then
      cam.m = cam.m + cam.dm
    end if
  else
    cam.m = cam.m + cam.dm
  end if

      SwapBuffers(hDc)

      EndPaint( hWnd, @pnt )
    case WM_KEYDOWN
			if( lobyte( wParam ) = 27 ) then
				PostMessage( hWnd, WM_CLOSE, 0, 0 )
			end if
      key( loword( wParam ) ) = true
    case WM_KEYUP
      key( loword( wParam ) ) = false
    case WM_MOUSEMOVE
      mouse_x = loword( lParam )
      mouse_y = hiword( lParam )
    case WM_DESTROY
      PostQuitMessage( 0 )
      exit function
  end select
  function = DefWindowProc( hWnd, wMsg, wParam, lParam )    
end function

'':::::
function WinMain ( byval hInstance as HINSTANCE, _
                   byval hPrevInstance as HINSTANCE, _
                   byval szCmdLine as string, _
                   byval iCmdShow as integer ) as integer    
     
    dim wMsg as MSG
    dim wcls as WNDCLASS     
    dim hWnd as HWND
     
    function = 0
     
    with wcls
    	.style         = CS_HREDRAW or CS_VREDRAW
    	.lpfnWndProc   = @WndProc
    	.cbClsExtra    = 0
    	.cbWndExtra    = 0
    	.hInstance     = hInstance
    	.hIcon         = LoadIcon( NULL, IDI_APPLICATION )
    	.hCursor       = LoadCursor( NULL, IDC_ARROW )
    	.hbrBackground = GetStockObject( BLACK_BRUSH )
    	.lpszMenuName  = NULL
    	.lpszClassName = @"HelloWin"
    end with
          
    if( RegisterClass( @wcls ) = FALSE ) then
       MessageBox( null, "Failed to register wcls", "Error", MB_ICONERROR )
       exit function
    end if
    
    hWnd = CreateWindowEx( 0, _
    			 		   @"HelloWin", _
                           "", _
                           WS_OVERLAPPEDWINDOW or WS_MAXIMIZE , _
                           0 , _
                           0 , _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           NULL, _
                           NULL, _
                           hInstance, _
                           NULL )
                          

    ShowWindow( hWnd, SW_SHOWMAXIMIZED )''iCmdShow )
    UpdateWindow( hWnd )
     
    while( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )    
        TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    wend
    
    function = wMsg.wParam

end function




function lenght( a as T3d ) as single
  return sqr( a.x ^ 2 + a.y ^ 2 + a.z ^ 2 ) + 0.00001
end function
function rad( deg as single ) as single
  return deg * PI / 180
end function
sub opengl_init()
	screen 20, 16, , 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 , 100 , 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 geo( no as integer , p1 as integer _
, p2 as integer , p3 as integer )
  if no < 1 then 
    tri p1 , p2 , p3 
  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 setpoint( no as integer , x as single , y as single , z as single )
  pnt( no and 255 ).fill x , y , z
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  ''right
  quad 7 , 6 , 4 , 5  ''left
  quad 0 , 4 , 5 , 1  ''up
  quad 7 , 3 , 2 , 6  ''down
  quad 0 , 4 , 6 , 2  ''back
  quad 7 , 5 , 1 , 3  ''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 tri( p1 as integer , p2 as integer , p3 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 
    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
  glend
end sub
sub quad( p1 as integer , p2 as integer , p3 as integer , p4 as integer )
  dim q as T3d
  q = ( pnt( p2 ) - pnt( p1 ) ) * ( pnt( p3 ) - pnt( p1 ) )
  q = q / lenght( q )
  glBegin GL_QUADS
    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
  


Post Reply