Author Topic: openGL primitives and keyboard feature  (Read 1882 times)

0 Members and 1 Guest are viewing this topic.

Frankolinox

  • Guest
openGL primitives and keyboard feature
« on: May 29, 2013, 09:56:23 AM »
http://www.oxygenbasic.org/forum/index.php?topic=760.0

here's my first example how to change features (movements, zooming, something else..) of keyboard keys in openGL. this is one possible way to move or change properties for primitives by keys (arrows). here you can find a simple way how to start rotation and zooming effects.

copy the example into "openGL" folder. the "crate.jpg" is still in "images" folder so the example should run.

Code: [Select]
 '
  'test for rotation cubes and zooming by keys [left,right,pageup,pagedown, space bar] 'space bar for original status quo of scene
  'by frankolinox, 29.mai.2013, test 1 :-)
  '
  includepath "$\inc\"
  $ FileName "t.exe"
  'include "RTL32.inc"
  'include "RTL64.inc"
  title="Crates+Keyboards (left,right,pageup,pagedown,spacebar)"
  % ExplicitMain
  include "OpenglSceneFrame.inc"

  includepath ""
  include "particles.inc"

  'SHARED
  '======

  indexbase 1
  float ang1
  float li[16]
  float ma[13]
  sys cube pent
  cloud cloud1
  smoke smoke1
  

  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  '
  'PREPARE TEXTURES
  '
  glGenTextures 2, texn
  '
  static sys res=256
  string imgs[3]
  MinTexture texn[1],0xffffffff,0x80ffffff,0x00ffffff
  MinTexture texn[2],0xffffffff,0x80ffffff,0x00000000
  GetTexImage "../images/crate.jpg",res,imgs[3]
  MakeTexture *imgs[3],res,res,texn[3]
  '
  cube=CompileList : CubeForm    : glEndList
  pent=CompileList : Sides 5,1,1 : glEndList
  '
  'standard lighting
  ------------------
  li<=
  0.0, 8.0, 8.0, 1.0,  'position
  1.0, 1.0, 1.0, 1.0,  'ambient
  1.0, 1.0, 1.0, 1.0,  'diffuse
  1.0, 1.0, 1.0, 1.0   'specular

  'standard material
  ------------------
  ma<=
  .15, .15, 0.2, 1.0, 'ambient
  0.7, 0.7, 0.6, 1.0, 'diffuse
  0.0, 0.0, 0.1, 1.0, 'specular  
  0                   'shininess
  Material ma
  Lighting li
  '
  SetTimer hWnd,1,10,null
  end sub

  sub Release(sys hwnd)
  '====================
  glDeleteLists    cube,1
  glDeleteLists    pent,1
  glDeleteTextures 3, texn
  Gdiplus          0
  killTimer hwnd, 1
  end sub

  sub RenderCrate(float x,y,z,sc,sys tex)
  =======================================
  'glColor4f .99,.99,.99,.99
  sys xrot,yrot,zoom,zoom1, filter,xspeed,yspeed
  static single ra,ri,ang1,angi1=1,z1=-1.0,zoomi
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z-zoomi
  glrotatef    ang1,0,1,0
  ''glrotatef    45.0,0,1,0
  glscalef sc,sc,sc 'zoom
  glCallList Cube

'------------------------------- // keys start
if lastkey = 0x25 '= 36.99999
    '0x20 spacebar, 0x26=up,0x28=down,0x25=left,0x27=right,0x21=pageup,0x22=pagedown
    ang1+=angi1
  end if

if lastkey = 0x27 'right
   ang1-=angi1
end if

if lastkey = 0x21 'pageup  
   zoomi=zoomi-0.02
end if

if lastkey = 0x22 'pagedown  
   zoomi=zoomi+0.02
end if

if lastkey = 0x20 'spacebar
   ang1=0
   zoomi=0.0
end if
'------------------------------- // keys end
  glPopMatrix
  glDisable GL_TEXTURE_2D
  end sub

  sub scene(sys hWnd)
  '==================
  '
  Snapshots hwnd 'Ctrl-P to take snapshot

  static single ra,ri,ang1,angi1=1 '.5
  static long vKeyCode,bKeyDown
  sys xrot,yrot,zoom,filter,xspeed,yspeed,x,y,z
  
  '
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  glEnable GL_DEPTH_TEST
  glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
  glEnable GL_BLEND
  glLoadIdentity
  '
  glClearColor 0.5, 0.5, 0.7, 0
  '
  'FOG
  float fcolor[4]={.5, .5, .7, .99}
  glfogi GL_FOG_MODE,GL_EXP
  glfogf GL_FOG_DENSITY,0.02
  glfogfv GL_FOG_COLOR,  fcolor
  glfogf GL_FOG_INDEX,  0
  glfogf GL_FOG_START, -1.0
  glfogf GL_FOG_END,   -100.0
  glEnable GL_FOG
  '
  'CRATES
  glEnable GL_LIGHTING
  glEnable GL_NORMALIZE
  glColor4f .99,.99,.99,.99
  
  sys n=texn[3]
  RenderCrate  11.5,-0.9,-24.0,0.5,n
  RenderCrate   6.5,-0.9,-16.0,0.5,n
  RenderCrate   2.5,-0.9,-8.0, 0.5,n
  RenderCrate   0.5,-0.9,-4.0, 0.5,n
  RenderCrate  -0.5,-0.9,-2.0, 0.5,n
  
  glDisable GL_LIGHTING
  '
  'CLOUD
  glEnable GL_TEXTURE_2D
  glPushMatrix
  gltranslatef -10.0, -10.0, -22.0
  glscalef      20.0,12.0,1.0
  cloud1.render 40 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  'SMOKE
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[2]
  glPushMatrix
  gltranslatef  0.0, -0.6, -3.5
  glScalef      1.5,2.0,1.0
  smoke1.r<=    .55,.40,.70
  smoke1.render 20 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  'ang1+=angi1
  'if ang1>360 then ang1-=360
  '
  sleep 10
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW

best regards, frank

X
« Last Edit: May 29, 2013, 10:08:08 AM by Frankolinox »

Charles Pegge

  • Guest
Re: openGL primitives and keyboard feature
« Reply #1 on: May 29, 2013, 10:19:51 AM »
Hi Frank, thanks!

I'm working on a pick-example with keyboard as well as mouse control. It will be ready soon. Here is how the keyboard controller looks. (controlling x,y,z position and rotation)

 
  'CONTROL WITH KEYBOARD KEYS
  a=lastkey-48
  select a
  case 1 to 6 : picked=a : picks=picked
  end select
  if picked
    if not key[82] 'R'
      if key[37] then c[picked].x-=.01
      if key[39] then c[picked].x+=.01
      if key[38] then c[picked].y+=.01
      if key[40] then c[picked].y-=.01
      if key[33] then c[picked].z-=.01
      if key[34] then c[picked].z+=.01
    else
      if key[37] then c[picked].angy-=1
      if key[39] then c[picked].angy+=1
      if key[38] then c[picked].angx-=1
      if key[40] then c[picked].angx+=1
      if key[33] then c[picked].angz+=1
      if key[34] then c[picked].angz-=1
      if key[36]
        c[picked].angx=0. : c[picked].angy=0. : c[picked].angz=0. 'HOME'
      end if
    end if
  end if


Charles
« Last Edit: May 29, 2013, 10:59:56 AM by Charles Pegge »

Frankolinox

  • Guest
Re: Lights, texture + transparence effect / openGL keyboard keys
« Reply #2 on: May 30, 2013, 02:22:39 AM »
a) topic: openGL, lights + texture + arrows by pushing keys ("L","T", "left","right","pageup","pagedown") here's an interesting test for a new cube that looks like having kinf of "transparence". with pushing key "L" you can light up the transparence cube if there's no texture, push any other key to activate the light modus. same things with key "T" to cover the cube with texture or not (for desactivating push another key).

my "cubeMe" function for a new cube has an interesting effect as you can test. push "t" for texture or "L" for light and you can see this nice effect ;)

test example:
Code: [Select]
 '
  'experimental test for effects LIGHT on/off, texture on/off,
  'rotation cubes and zooming by keys [left,right,pageup,pagedown]
  'by frankolinox, 30.mai.2013, test 2 :-)
  '
  includepath "$\inc\"
  $ FileName "t.exe"
  'include "RTL32.inc"
  'include "RTL64.inc"
  title="Crate Effect Light+Texture+Keyboards (l,t,left,right,pageup,pagedown,spacebar)"
  % ExplicitMain
  include "OpenglSceneFrame.inc"

  includepath ""
  include "particles.inc"

  'SHARED
  '======

  indexbase 1
  float ang1
  float li[16]
  float ma[13]
  sys cube pent
  cloud cloud1
  smoke smoke1
  

  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  '
  'PREPARE TEXTURES
  '
  glGenTextures 2, texn
  '
  static sys res=256
  string imgs[3]
  MinTexture texn[1],0xffffffff,0x80ffffff,0x00ffffff
  MinTexture texn[2],0xffffffff,0x80ffffff,0x00000000
  GetTexImage "../images/crate.jpg",res,imgs[3]
  MakeTexture *imgs[3],res,res,texn[3]
  '
  cube=CompileList : CubeForm    : glEndList
  pent=CompileList : Sides 5,1,1 : glEndList
  '
  'standard lighting
  ------------------
  li<=
  0.0, 8.0, 8.0, 1.0,  'position
  1.0, 1.0, 1.0, 1.0,  'ambient
  1.0, 1.0, 1.0, 1.0,  'diffuse
  1.0, 1.0, 1.0, 1.0   'specular

  'standard material
  ------------------
  ma<=
  .15, .15, 0.2, 1.0, 'ambient
  0.7, 0.7, 0.6, 1.0, 'diffuse
  0.0, 0.0, 0.1, 1.0, 'specular  
  0                   'shininess
  Material ma
  Lighting li
  '
  SetTimer hWnd,1,10,null
  end sub

  sub Release(sys hwnd)
  '====================
  glDeleteLists    cube,1
  glDeleteLists    pent,1
  glDeleteTextures 3, texn
  Gdiplus          0
  killTimer hwnd, 1
  end sub
  
  '----------------------------------- // transparence cube + effect // -----------
  sub cubeMe(float x,y,z,sc, sys tex)
  ===================================
    sys xrot,yrot,zoom,filter,xspeed,yspeed,light,lp
    static single ra,ri,ang1,angi1=1,z1=-1.0,zoomi

   ' Clear the screen buffer
   glClear %GL_COLOR_BUFFER_BIT or %GL_DEPTH_BUFFER_BIT
   'glEnable GL_TEXTURE_2D
   'glBindTexture GL_TEXTURE_2D,tex
    
   glPushMatrix
   glTranslatef 0.0,0.0,z-zoomi
   glrotatef  ang1,0,1,0

   glBegin GL_QUADS
      ' Front Face
      glNormal3f   0.0 , 0.0 , 1.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f -1.0 , -1.0 ,  1.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f  1.0 , -1.0 ,  1.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f  1.0 ,  1.0 ,  1.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f -1.0 ,  1.0 ,  1.0
      ' Back Face
      glNormal3f   0.0 , 0.0 , -1.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f -1.0 , -1.0 , -1.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f -1.0 ,  1.0 , -1.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f  1.0 ,  1.0 , -1.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f  1.0 , -1.0 , -1.0
      ' Top Face
      glNormal3f   0.0 , 1.0 , 0.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f -1.0 ,  1.0 , -1.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f -1.0 ,  1.0 ,  1.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f  1.0 ,  1.0 ,  1.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f  1.0 ,  1.0 , -1.0
      ' Bottom Face
      glNormal3f   0.0 ,-1.0 , 0.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f -1.0 , -1.0 , -1.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f  1.0 , -1.0 , -1.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f  1.0 , -1.0 ,  1.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f -1.0 , -1.0 ,  1.0
      ' Right face
      glNormal3f   1.0 , 0.0 , 0.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f  1.0 , -1.0 , -1.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f  1.0 ,  1.0 , -1.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f  1.0 ,  1.0 ,  1.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f  1.0 , -1.0 ,  1.0
      ' Left Face
      glNormal3f  -1.0 ,  0.0 , 0.0
      glTexCoord2f 0.0 , 0.0  : glVertex3f -1.0 , -1.0 , -1.0
      glTexCoord2f 1.0 , 0.0  : glVertex3f -1.0 , -1.0 ,  1.0
      glTexCoord2f 1.0 , 1.0  : glVertex3f -1.0 ,  1.0 ,  1.0
      glTexCoord2f 0.0 , 1.0  : glVertex3f -1.0 ,  1.0 , -1.0
   glEnd
   glPopMatrix
   'glDisable GL_TEXTURE_2D
 
'------------------------------- // keys start
if lastkey = 0x25 '"LEFT" = 36.99999
    '0x20 spacebar, 0x26=up,0x28=down,0x25=left,0x27=right,0x21=pageup,0x22=pagedown
    ang1+=angi1
  end if
if lastkey = 0x27 'right
   ang1-=angi1
end if
if lastkey = 0x54 ' "T" texture on/off
    glEnable GL_TEXTURE_2D
    glBindTexture GL_TEXTURE_2D,tex
end if
if lastkey = 0x4C ' "L", Light on/off
    glDisable GL_LIGHTING
    light = not light
    else
    glEnable GL_LIGHTING    
  end if
  if lastkey = 0x20 'spacebar
   ang1=0
   zoomi=0.0
end if
if lastkey = 0x21 'pageup  
   zoomi=zoomi-0.02
end if

if lastkey = 0x22 'pagedown  
   zoomi=zoomi+0.02
end if

'------------------------------- // keys end
end sub

  '--------------------------------------
  sub RenderCrate(float x,y,z,sc,sys tex)
  =======================================
  'glColor4f .99,.99,.99,.99
  sys xrot,yrot,zoom,zoom1, filter,xspeed,yspeed
  static single ra,ri,ang1,angi1=1,z1=-1.0,zoomi
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z-zoomi
  glrotatef    ang1,0,1,0
  ''glrotatef    45.0,0,1,0
  glscalef sc,sc,sc 'zoom
  glCallList Cube
'static single ra,ri,ang1,angi1=1,z1=-1.0,zoomi
'------------------------------- // keys start
if lastkey = 0x25 '= 36.99999
    '0x20 spacebar, 0x26=up,0x28=down,0x25=left,0x27=right,0x21=pageup,0x22=pagedown
    ang1+=angi1
  end if

if lastkey = 0x27 'right
   ang1-=angi1
end if

if lastkey = 0x21 'pageup  
   zoomi=zoomi-0.02
end if

if lastkey = 0x22 'pagedown  
   zoomi=zoomi+0.02
end if

if lastkey = 0x20 'spacebar
   ang1=0
   zoomi=0.0
end if
'------------------------------- // keys end
  glPopMatrix
  glDisable GL_TEXTURE_2D
  end sub

  '---------------------- OPENGL SCENE ---------------- //
  sub scene(sys hWnd)
  '==================
  '
  Snapshots hwnd 'Ctrl-P to take snapshot

  static single ra,ri,ang1,angi1=1 '.5
  static long vKeyCode,bKeyDown
  sys xrot,yrot,zoom,filter,xspeed,yspeed,x,y,z
  
  '
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  glEnable GL_DEPTH_TEST
  glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
  glEnable GL_BLEND
  glLoadIdentity
  '
  glClearColor 0.5, 0.5, 0.7, 0
  '
  'FOG
  float fcolor[4]={.5, .5, .7, .99}
  glfogi GL_FOG_MODE,GL_EXP
  glfogf GL_FOG_DENSITY,0.02
  glfogfv GL_FOG_COLOR,  fcolor
  glfogf GL_FOG_INDEX,  0
  glfogf GL_FOG_START, -1.0
  glfogf GL_FOG_END,   -100.0
  glEnable GL_FOG
  '
  'CRATES
  glEnable GL_LIGHTING
  glEnable GL_NORMALIZE
  glColor4f .99,.99,.99,.99
  
  sys n=texn[3]
  sys nt=texn[4]
  
  gltranslatef -0.5,0.5,-5.0
  'RenderCrate  11.5,-0.9,-24.0,0.5,n
  'RenderCrate   6.5,-0.9,-16.0,0.5,n
  'RenderCrate   2.5,-0.9,-8.0, 0.5,n
  'RenderCrate   0.5,-0.9,-4.0, 0.5,n
  'RenderCrate  -0.5,-0.9,-2.0, 0.5,n '1 'no color + effect ;)
  'glDisable GL_LIGHTING
  
  '
  'CUBE TRANSPARENCE WITH EFFECT ---------------- //
  glEnable GL_TEXTURE_2D
  glPushMatrix
  gltranslatef -1.5,-0.5,-4.0
  cubeMe  11.5,-0.9,-24.0,0.5,nt
  cubeMe   6.5,-0.9,-16.0,0.5,nt
  cubeMe   2.5,-0.9,-8.0, 0.5,nt
  cubeMe   0.5,-0.9,-4.0, 0.5,nt
  cubeMe  -0.5,-0.9,-2.0, 0.5,nt
  glPopMatrix
  glDisable GL_TEXTURE_2D
  'CUBE TRANSPARENCE WITH EFFECT ---------------- //
  
  '
  'CLOUD
  glEnable GL_TEXTURE_2D
  glPushMatrix
  gltranslatef -10.0, -10.0, -22.0
  glscalef      20.0,12.0,1.0
  cloud1.render 40 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  
  'SMOKE
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[2]
  glPushMatrix
  gltranslatef  0.0, -0.6, -3.5
  glScalef      1.5,2.0,1.0
  smoke1.r<=    .55,.40,.70
  smoke1.render 20 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  'ang1+=angi1
  'if ang1>360 then ang1-=360
  '
  sleep 10
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW

  'glLoadIdentity
  'cubeMe  11.5,-0.9,-24.0,0.5,1'nt
  'cubeMe   6.5,-0.9,-16.0,0.5,1'nt
  'cubeMe   2.5,-0.9,-8.0, 0.5,1'nt
  'cubeMe   0.5,-0.9,-4.0, 0.5,1'nt
  'cubeMe  -0.5,-0.9,-2.0, 0.5,1'nt
  

push the example into "openGL" folder you can start and test this example.

b) thanks charles for your keycode code snippets. similar technique I have tested here yesterday, but I have had some problems with the "select case" modus I have done it wrong as I can see ;)

best regards, frank

X