Author Topic: Texture, Fog and Directional Light  (Read 2161 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Texture, Fog and Directional Light
« on: May 02, 2013, 02:39:56 PM »
Using Frank's Nehe Crate:

Directional light adds solidity to objects.

Also added Ctrl-P for taking jpeg snapshots of the scene.

Code: [Select]

  includepath "$\inc\"
  $ FileName "t.exe"
  'include "RTL32.inc"
  'include "RTL64.inc"
  title="Crates in Fog"
  include "OpenglSceneFrame.inc"

  indexbase 1
  sys   texn[3]
  sys   GdiplusToken
  float ang1
  float li[16]
  float ma[13]
  

  sub Lighting(float*li)
  ======================
  glPolygonMode GL_FRONT_AND_BACK, GL_FILL
  glLightfv GL_LIGHT1, GL_POSITION, li[1]
  glLightfv GL_LIGHT1, GL_AMBIENT,  li[5]
  glLightfv GL_LIGHT1, GL_DIFFUSE,  li[9]
  glLightfv GL_LIGHT1, GL_SPECULAR, li[13]
  glEnable GL_LIGHT1
  end sub

  sub Material(single* ma)
  '=======================
  glMaterialfv GL_FRONT, GL_AMBIENT,   ma[1]
  glMaterialfv GL_FRONT, GL_DIFFUSE,   ma[5]
  glMaterialfv GL_FRONT, GL_SPECULAR,  ma[9]
  glMaterialf  GL_FRONT, GL_SHININESS, ma[13]
  end sub


  sub initialize(sys hWnd)
  ========================
  '
  'GDIPLUS
  '
  sys hr
  GdiplusStartupInput StartupInput

  StartupInput.GdiplusVersion = 1
  hr=GdiplusStartup GdiplusToken, StartupInput, null
  '
  if hr then
    print "Error initializing GDIplus: " hex hr
    exit function
  end if
  '
  'PREPARE TEXTURES
  '
  glGenTextures 2, texn
  '
  static sys res=256
  string imgs[1]=""
  GetTexImage "../images/crate.jpg",res,imgs[1]
  MakeTexture *imgs[1],res,res,texn[1]
  '
  '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 Cube(optional sys mode)
  ===========================
  static shape
  if mode<0 then GlDeleteLists shape,1 : exit sub
  if shape then glCallList shape       : exit sub
  shape = glGenLists 1
  glNewList shape, GL_COMPILE_AND_EXECUTE
  '
  '
  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
  glEndList
  end sub


  sub RenderCrate(float x,y,z,sc,sys tex)
  =======================================
  glColor4f .99,.99,.99,.99
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z
  glrotatef    ang1,0,1,0
  glscalef sc,sc,sc
  Cube
  glPopMatrix
  glDisable GL_TEXTURE_2D
  end sub


  sub scene(sys hWnd)
  '==================
  '
  'Ctrl-P take snapshot
  '
  static sys photo
  if key[80] and key[17] and photo=0
    TakeOpenglSnapShot hwnd,"tim50.jpg","image/jpeg",50
    photo=1
  elseif key[80]=0
    photo=0
  end if

  static single ra,ri,angi1=.5
  '
  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
  '
  float fcolor[4]={.5, .5, .7, .99}
  glfogi GL_FOG_MODE,GL_EXP
  glfogf GL_FOG_DENSITY,0.1
  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
  '
  glEnable GL_LIGHTING
  glEnable GL_NORMALIZE
  glColor4f .99,.99,.99,.99
  sys n=texn[1]
  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
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  sleep 10
  end sub


  sub Release(sys hwnd)
  '====================
  Cube -1
  glDeleteTextures 3, texn
  GdiplusShutdown GdiplusToken
  killTimer hwnd, 1
  end sub


X