war ship sim [ opengl ]

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

war ship sim [ opengl ]

Post by bluatigro »

this is a try at a war ship sim
using opengl

rem :
this not ready jet
ai must be build
mouse control must be build
i have a idea jet

error :
my sceen is black
my sea is not visable

war ship

Code: Select all

'' bluatigro 21 jan 2020
'' war ship
'' battle sim

#include "_shapes.bas"

dim as t_camara emy

emy.spot = t3d( 10 , 0 , 50 )
emy.angle = t3d( 0 , 90 , 0 )

camara.spot = t3d( 0 , 0 , -50 )
camara.angle = t3d( 0 , 0 , 0 )

dim as double newdist , olddist , dist , angle 
dim as double camspeed , emyspeed
dim as integer camfly , emyfly , i , j , mx , my , mbtn
dim as t3d lred , lredd , lgreen , lgreend 
dim as sng4d q , ldiffuse , lambient
ldiffuse = sng4d( 0 , 0 , 1 , 1 )
lambient = sng4d( 0 , 0 , .5 , 1 )
glLightfv( gl_light0 , gl_position, @lightpos(0) )
glLightfv( gl_light0 , gl_diffuse , @ldiffuse.x )
gllightfv( gl_light0 , gl_ambient , @lambient.x )
glEnable( gl_light0 )
ldiffuse = sng4d( 0 , 1 , 0 , 1 )
lambient = sng4d( 0 , .5 , 0 , 1 )
glLightfv( gl_light1 , gl_position, @lightpos(0) )
glLightfv( gl_light1 , gl_diffuse , @ldiffuse.x )
gllightfv( gl_light1 , gl_ambient , @lambient.x )
glEnable( gl_light1 )
ldiffuse = sng4d( 1 , 0 , 0 , 1 )
lambient = sng4d( 1 , 0 , 0 , 1 )
glLightfv( gl_light2 , gl_position, @lightpos(0) )
glLightfv( gl_light2 , gl_diffuse , @ldiffuse.x )
gllightfv( gl_light2 , gl_ambient , @lambient.x )
glEnable( gl_light2)

do
  glclear gl_depth_buffer_bit or gl_color_buffer_bit
  camara.use
  '' fire lights
  if camfly then 
    lgreen += lgreend
    lgreend += t3d( 0 , -0.01 , 0 )
    if lgreen.y < 0 then camfly = 0
  else
    lgreen = camara.spot + t3d( 0 , 2 , 0 )
    lgreend = t3d()
  end if
  if emyfly then
    lred += lredd
    lredd += t3d( 0 , -0.01 , 0 )
    if lred.y < 0 then emyfly = 0
  else
    lred = emy.spot + t3d( 0 , 2 , 0 )
    lred = t3d()
  end if
  ''ai
  newdist = length( emy.spot - camara.spot )
  angle = atan2( camara.spot.x - emy.spot.x _
               , camara.spot.z - emy.spot.z )
  emy.angle.y += sgn( angle / pi * 180 - emy.angle.y )
  
  if abs( angle / pi * 180 - emy.angle.y ) < 3 then
    if emyfly = 0 then
      emyfly = 1
      lredd.x = sin( rad( emy.angle.y ) ) / 100
      lredd.y = 1
      lredd.z = cos( rad( emy.angle.z ) ) / 100
    end if
  end if
q = sng4d( lgreen.x , lgreen.y , lgreen.z , 1 )
glLightfv( gl_light1 , gl_position, @q.x )
q = sng4d( lred.x , lred.y , lred.z , 1 )
glLightfv( gl_light2 , gl_position, @q.x )
  ''sea
  for i = -50 to 50
    for j = -50 to 50
      setpoint 0 , i , -3 , j
      setpoint 1 , i + 1 , -3 , j
      setpoint 2 , i + 1 , -3 , j + 1
      setpoint 3 , i , -3 , j + 1
      if ( i + j ) and 1 then
        material.diffuse = white
        material.ambient = white
      else
        material.diffuse = gray
        material.ambient = gray
      end if
      setmaterial gl_front_and_back , material
      quad 0 , 1 , 2 , 3
    next j
  next i
  ''draw emy
  glpushmatrix
    gltranslated emy.spot.x , emy.spot.y , emy.spot.z
    glrotated emy.angle.y , 0,1,0
    material.diffuse = magenta
    material.ambient = magenta
    setmaterial gl_front , material
    setbox 0,0,0 , 2,.5,.5
    cube
  glpopmatrix
  
  flip
  sleep 40
loop while inkey = ""


_blua_gl

Code: Select all

'' BLUATIGRO 16 JAN 2020
'' blua gl

'' openGL stuf

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB 
#endif

const as double pi = atn( 1 ) * 4
const as double golden_ratio = ( sqr( 5 ) - 1 ) / 2

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

DIM shared AS INTEGER winx , winy
screen 18 , 32 , , 2
''SCREENRES 1024 , 768 , 32 , 2 , FB.GFX_FULLSCREEN 
screeninfo winx , winy

'' ReSizeGLScene
glViewport 0, 0, winx , winy                      '' Reset The Current Viewport
glMatrixMode GL_PROJECTION                     '' Select The Projection Matrix
glLoadIdentity                                 '' Reset The Projection Matrix
gluPerspective 45.0, csng(winx/winy), 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.0                '' Blue 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


''SUN LIGHT

glEnable( gl_lighting )
dim as single lightpos( 3 ) = { -50 , 50 , 50 , 1 }
dim as single diffuse( 3 ) = { 1 , 1 , 1 , 1 }
dim as single ambient( 3 ) = { .5 , .5 , .5 , 1 }
glLightfv( gl_light0 , gl_position, @lightpos(0) )
glLightfv( gl_light0 , gl_diffuse , @diffuse(0) )
gllightfv( gl_light0 , gl_ambient , @ambient(0) )
glEnable( gl_light0 )

''COLORS

type sng4d
  dim as single x , y , z , w
  declare constructor ()
  declare constructor ( nx as single , ny as single , nz as single , nw as single )
  declare sub fill( nx as single , ny as single , nz as single , nw as single )
end type
constructor sng4d()
end constructor
constructor sng4d( nx as single , ny as single , nz as single , nw as single )
  x = nx
  y = ny
  z = nz
  w = nw
end constructor
operator + ( a as sng4d , b as sng4d ) as sng4d 
  return sng4d(a.x+b.x,a.y+b.y,a.z+b.z,1)
end operator
operator - ( a as sng4d , b as sng4d ) as sng4d 
  return sng4d(a.x-b.x,a.y-b.y,a.z-b.z,1)
end operator
operator / ( a as sng4d , b as single ) as sng4d 
  return sng4d(a.x/b,a.y/b,a.z/b,1)
end operator
operator * ( a as sng4d , b as single ) as sng4d 
  return sng4d(a.x*b,a.y*b,a.z*b,1)
end operator
function rad( deg as single ) as single
  return deg * pi / 180
end function
function rainbow( deg as double ) as sng4d
  return sng4d( sin( rad( deg ) ) / 2 + .5 _
              , sin( rad( deg - 120 ) ) / 2 + .5 _
              , sin( rad( deg + 120 ) ) / 2 + .5 , 1 ) 
end function
sub sng4d.fill( nx as single , ny as single , nz as single , nw as single )
  x = nx 
  y = ny 
  z = nz
  w = nw
end sub

dim shared as sng4d black , red , green , yellow _
, blue , magenta , cyan , white _
, orange , gray , pink 
black.fill   0,0,0,1
red.fill     1,0,0,1
green.fill   0,1,0,1
yellow.fill  1,1,0,1
blue.fill    0,0,1,1
magenta.fill 1,0,1,1
cyan.fill    0,1,1,1
white.fill   1,1,1,1

orange.fill   1,.5, 0,1
gray.fill    .5,.5,.5,1
pink.fill     1,.5,.5,1

function mix( a as sng4d , f as single , b as sng4d ) as sng4d 
  dim uit as sng4d
  uit = a + ( b - a ) * f
  uit.w = 1
  return uit
end function

function glass( kl as sng4d , f as single ) as sng4d 
  return sng4d( kl.x , kl.y , kl.z , f )
end function

''MATERIAL

type t_material
  dim as sng4d ambient , diffuse , specular , emision
  dim as single shininess
end type
dim shared as t_material material
sub setMaterial( a as long , m as t_material )
  glMaterialfv a , GL_AMBIENT , @m.ambient.x 
  glMaterialfv a , GL_DIFFUSE , @m.diffuse.x 
  glMaterialfv a , GL_SPECULAR , @m.specular.x 
  glMaterialfv a , GL_EMISSION , @m.emision.x
  glMaterialf a , GL_SHININESS , m.shininess
end sub 

'' t3d

type t3d
  x as double
  y as double
  z as double
  declare constructor()
  declare constructor ( x as double , y as double , z as double )
  declare sub fill( x as double , y as double , z as double )
  declare sub normalize
end type
constructor t3d()
  this.x = 0
  this.y = 0
  this.z = 0
end constructor 
constructor t3d( x as double , y as double , z as double )
  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 double ) 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 double ) as t3d
  return type( a.x / d , a.y / d , a.z / d )
end operator
sub t3d.fill( x as double , y as double , z as double )
  this.x = x
  this.y = y
  this.z = z
end sub
function dot( a as t3d , b as t3d ) as double
  return a.x * b.x + a.y * b.y + a.z * b.z
end function
function length( q as t3d ) as double
   return sqr( q.x * q.x + q.y * q.y + q.z * q.z ) + 1e-7
end function  
function getangle( a as t3d , b as t3d ) as double
  return acos( dot( a , b ) _
  / ( length( a ) * length( b ) ) )
end function
sub t3d.normalize
  this /= length( this )
end sub 

'' 3d engine


dim shared as t3d sk( 64 )

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

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

function pend( fase as double , amp as double ) as double
  return sin( fase * PI / 180 ) * amp
end function

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

function degrees( x as double ) as double
  return x * 180 / pi
end function

function range( l as double , h as double ) as double
  return rnd * ( h - l ) + l
end function

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

''CAMARA

type t_camara
public :
  dim as t3d spot , angle
  declare sub move( dx as double _
  , dy as double , dz as double , dpan as double )
  declare sub use()
end type
sub t_camara.move( dx as double _
  , dy as double , dz as double , dpan as double )
  rotate dx , dz , -angle.y
  spot.x += dx
  spot.y += dy
  spot.z += dz
  angle.y += dpan
end sub
sub t_camara.use
  glLoadIdentity
  glrotated -angle.y , 0 , 1 , 0
  glTranslatef -spot.x , -spot.y , -spot.z
end sub

dim as t_camara camara




_shapes

Code: Select all

'' bluatigro 15 jan 2020
'' shapes + primativs

#include "_BLUA_GL.bas"

dim shared pnt( 255 ) as t3d
sub setpoint( no as integer , x as double , y as double , z as double )
  if no < 0 or no > 255 then exit sub
  pnt( no ) = t3d( x , y , z )
end sub

sub tri( p1 as integer , p2 as integer , p3 as integer )
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  dim as t3d n = ( pnt( p2 ) - pnt( p1 ) ) _
               \ ( pnt( p3 ) - pnt( p1 ) )
  n.normalize()
  glbegin gl_quads
    glnormal3d n.x , n.y , n.z
    glvertex3d pnt( p1 ).x , pnt( p1 ).y , pnt( p1 ).z
    glvertex3d pnt( p2 ).x , pnt( p2 ).y , pnt( p2 ).z
    glvertex3d 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 )
  if p1 < 0 or p1 > 255 then exit sub
  if p2 < 0 or p2 > 255 then exit sub
  if p3 < 0 or p3 > 255 then exit sub
  if p4 < 0 or p4 > 255 then exit sub
  dim as t3d n = ( pnt( p2 ) - pnt( p1 ) ) _
               \ ( pnt( p3 ) - pnt( p1 ) )
  n.normalize()
  glbegin gl_quads
    glnormal3d n.x , n.y , n.z
    glvertex3d pnt( p1 ).x , pnt( p1 ).y , pnt( p1 ).z
    glvertex3d pnt( p2 ).x , pnt( p2 ).y , pnt( p2 ).z
    glvertex3d pnt( p3 ).x , pnt( p3 ).y , pnt( p3 ).z
    glvertex3d pnt( p4 ).x , pnt( p4 ).y , pnt( p4 ).z
  glend 
end sub

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

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 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 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
  
  tri 7 , 6 , 3
  tri 7 , 5 , 6 
  tri 7 , 3 , 5 
  
  quad 6 , 5 , 10 , 11 
  quad 5 , 3 , 8 , 9 
  quad 3 , 6 , 12 , 0 
  
  tri 6 , 12 , 11 
  tri 3 , 8 , 0 
  tri 5 , 9 , 10 
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 ).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
  glTranslated box.m.x , box.m.y , box.m.z 
  glScaled 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 double , db as double )
  dim as double i , j , i2 , j2 
  dim as double 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
  glTranslated box.m.x , box.m.y , box.m.z
  glScaled 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 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      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 
    next j
  next i
  glPopMatrix
end sub

sub hsphere( a as integer , b as integer _
, t as integer , da as double , db as double )
  dim as double i , j , i2 , j2 
  dim as double 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
  glTranslated box.m.x , box.m.y , box.m.z
  glScaled 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 t * pi / b / 2 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      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 
    next j
  next i
  glPopMatrix
end sub

sub torus( a as integer , b as integer )
  dim i as double , j as double , i2 as double , j2 as double
  if a < 3 then a = 3 
  if a > 64 then a = 64
  if b < 3 then b = 3 
  if b > 64 then b = 64 
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  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 
    next j
  next i
end sub

sub banana( a as integer , b as integer )
  dim i as double , j as double , i2 as double , j2 as double
  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 double , my as double , mz as double , dx as double , dy as double , dz as double 
  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/1.99 to PI/1.99 - pi/b*2 step PI / b * 1.99
      j2 = j + PI / b * 1.99
      setpoint 0 _ 
      , mx + ( dx + dy * cos( i ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i ) * dz * cos( j )
      setpoint 1 _
      , mx + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i ) * dz * cos( j2 )
      setpoint 2 _
      , mx + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * cos( j2 ) _
      , my + ( dx + dy * cos( i2 ) * cos( j2 ) ) _
      * sin( j2 ) _
      , mz + sin( i2 ) * dz * cos( j2 )
      setpoint 3 _ 
      , mx + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * cos( j ) _
      , my + ( dx + dy * cos( i2 ) * cos( j ) ) _
      * sin( j ) _
      , mz + sin( i2 ) * dz * cos( j )
      quad 0 , 1 , 2 , 3 
    next j
  next i
end sub

sub cilinder( sides as integer , dx as double , dy as double , top as integer , bot as integer )
  dim f as double
  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 
  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
      tri 255 , f , f + 1 
    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
      tri 255 , f , f + 1 
    next f
  end if
end sub

sub smooth_sphere( xx as double , yy as double , zz as double , d as double , kl as sng4d )
  setbox xx,yy,zz , d,d,d
  material.diffuse = kl
  material.ambient = kl
  material.specular = white
  material.shininess = 100
  setmaterial gl_front , material

  dim as double i , j , i2 , j2 , da = 1 , db = 1
  dim as double x , y , z , a = 16 , b = 16
  glPushMatrix
  glTranslated box.m.x , box.m.y , box.m.z
  glScaled 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 - pi / b * 2 step PI / b * 2 
      j2 = j + PI / b * 2 

      x = sin( i ) * cos( j )
      y = sin( j )
      z = cos( i ) * cos( j )
      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 )
      pnt( 4 ) = pnt( 0 )
      pnt( 5 ) = pnt( 1 )
      pnt( 6 ) = pnt( 2 )
      pnt( 7 ) = pnt( 3 )
      pnt( 4 ).normalize
      pnt( 5 ).normalize
      pnt( 6 ).normalize
      pnt( 7 ).normalize
      glbegin gl_quads
        glnormal3d pnt(4).x , pnt(4).y , pnt(4).z 
        glvertex3d pnt(0).x , pnt(0).y , pnt(0).z
        glnormal3d pnt(5).x , pnt(5).y , pnt(5).z
        glvertex3d pnt(1).x , pnt(1).y , pnt(1).z
        glnormal3d pnt(6).x , pnt(6).y , pnt(6).z
        glvertex3d pnt(2).x , pnt(2).y , pnt(2).z
        glnormal3d pnt(7).x , pnt(7).y , pnt(7).z
        glvertex3d pnt(3).x , pnt(3).y , pnt(3).z
      glend
    next j
  next i
  glPopMatrix
end sub 

sub smoot_torus( x as double , y as double , z as double _
  , gr as double , kr as double , kl as sng4d )
  dim i as double , j as double , i2 as double , j2 as double
  dim as integer a = 16 , b = 16
  dim mx as double , my as double , mz as double , dx as double , dy as double , dz as double 
  setbox x , y , z , gr , kr , kr
  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 
setpoint 4 , cos( i ) * cos( j ) , cos( i ) * sin( j ) , sin( i ) 
setpoint 5 , cos( i ) * cos( j2 ) , cos( i ) * sin( j2 ) , sin( i ) 
setpoint 6 , cos( i2 ) * cos( j2 ) , cos( i2 ) * sin( j2 ) , sin( i2 ) 
setpoint 7 , cos( i2 ) * cos( j ) , cos( i2 ) * sin( j ) , sin( i2 ) 
      glbegin gl_quads
        glnormal3d pnt(4).x , pnt(4).y , pnt(4).z 
        glvertex3d pnt(0).x , pnt(0).y , pnt(0).z
        glnormal3d pnt(5).x , pnt(5).y , pnt(5).z
        glvertex3d pnt(1).x , pnt(1).y , pnt(1).z
        glnormal3d pnt(6).x , pnt(6).y , pnt(6).z
        glvertex3d pnt(2).x , pnt(2).y , pnt(2).z
        glnormal3d pnt(7).x , pnt(7).y , pnt(7).z
        glvertex3d pnt(3).x , pnt(3).y , pnt(3).z
      glend
       
    next j
  next i
end sub

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

Re: war ship sim [ opengl ]

Post by dodicat »

If I comment out:
'camara.use
in the loop I see a pinkish chequered floor, but no ships.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: war ship sim [ opengl ]

Post by bluatigro »

update :
red ship added
eny ai added [ not ready jet ]
sea is now blue

error :
my fire system does not work good
mouse button shoot be firing green cube
emy shoot be firing constantly

rem :
game plays in the sail time
wind is Always in the sail

Code: Select all

'' bluatigro 21 jan 2020
'' war ship
'' battle sim

#include "_shapes.bas"

dim as t_camara emy

emy.spot = t3d( 0 , -1 , -50 )
emy.angle = t3d( 0 , 90 , 0 )

camara.spot = t3d( 0 , 0 , 0 )
camara.angle = t3d( 0 , 0 , 0 )

dim as double newdist , olddist = 50 , dist , angle 
dim as double emyspeed = 0.03
dim as integer camfly = 0 , emyfly = 0 , i , j , mx , my , mbtn
dim as t3d lred , lredd , lgreen , lgreend 

do
  glclear gl_depth_buffer_bit or gl_color_buffer_bit
  camara.use

  '' fire lights
  if camfly then 
    lgreen += lgreend
    lgreend += t3d( 0 , -0.01 , 0 )
  glpushmatrix
    gltranslated lgreen.x , lgreen.y , lgreen.z
    material.diffuse = green
    material.ambient = green
    setmaterial gl_front , material
    setbox 0,0,0 , .1,.1,.1
    cube
  glpopmatrix
    if lgreen.y < 0 then camfly = 0
  else
    lgreen = camara.spot + t3d( 0 , 4 , 0 )
    lgreend = t3d()
  end if
  if emyfly then
    lred += lredd
    lredd += t3d( 0 , -0.01 , 0 )
  glpushmatrix
    gltranslated lred.x , lred.y , lred.z
    material.diffuse = red
    material.ambient = red
    setmaterial gl_front , material
    setbox 0,0,0 , .1,.1,.1
    cube
  glpopmatrix
    if lred.y < 0 then emyfly = 0
  else
    lred = emy.spot + t3d( 0 , 4 , 0 )
    lred = t3d()
  end if
  ''ai
  newdist = length( emy.spot - camara.spot )
  angle = atan2( camara.spot.x - emy.spot.x _
               , camara.spot.z - emy.spot.z )
  emy.angle.y += sgn( angle / pi * 180 - emy.angle.y )
  
  if abs( angle / pi * 180 - emy.angle.y ) < 10 then
    if emyfly = 0 then
      emyfly = 1
      lredd.x = sin( rad( emy.angle.y ) ) / 10
      lredd.y = .1
      lredd.z = cos( rad( emy.angle.z ) ) / 10
    end if
  end if
  emy.move 0 , 0 , emyspeed , 0
  ''sea
  for i = -50 to 50
    for j = -50 to 50
      setpoint 0 , i , -1 , j
      setpoint 1 , i + 1 , -1 , j
      setpoint 2 , i + 1 , -1 , j + 1
      setpoint 3 , i , -1 , j + 1
      if ( i + j ) and 1 then
        material.diffuse = blue
        material.ambient = blue
      else
        material.diffuse = cyan
        material.ambient = cyan
      end if
      setmaterial gl_front_and_back , material
      quad 0 , 1 , 2 , 3
    next j
  next i
  ''draw emy
  glpushmatrix
    gltranslated emy.spot.x , emy.spot.y , emy.spot.z
    glrotated emy.angle.y , 0,1,0
    material.diffuse = red
    material.ambient = red
    setmaterial gl_front , material
    setbox 0,0,0 , .5,.5,2
    cube
    setbox 0,1.7,0 , 1,1,.01
    cube
  glpopmatrix
  if not getmouse( mx , my , mbtn ) then
    if mx <> -1 and my <> -1 then
      if mx < winx * 1 / 3 then camara.move 0 , 0 , 0 , 1
      if mx > winx * 2 / 3 then camara.move 0 , 0 , 0 , -1
      if my < winy * 1 / 3 then camara.move 0 , 0 , -.03 , 0
      if my > winy * 2 / 3 then camara.move 0 , 0 , .03 , 0
    end if
    if ( mbtn and 1 ) and not camfly then
      camfly = 1
      lgreend.x = sin( rad( camara.angle.y ) ) / 100
      lgreend.y = 0.1
      lgreend.z = cos( rad( camara.angle.y ) ) / 100
    end if
  end if
  flip
  sleep 40
loop while inkey = ""


Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Re: war ship sim [ opengl ]

Post by Dr_D »

Hey, I made this demo some years ago. It was going to be an updated version of that oldschool submarine battle game. I don't know if you're interested, but maybe it will be of use. ;)
Post Reply