Author Topic: Smoke  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Smoke
« on: May 02, 2013, 02:16:50 AM »
Triangles on fire:

The secret is to create a very very small texture

  Sub textureA(sys n)
  ===================
  static long v[16]
  v<= 0,  0,  0, 0, 0, -1, 0x88ffffff, 0,
  0, 0x88ffffff, -1, 0,  0,  0,  0, 0
  MakeTexture @v, 4, 4, n
  End Sub


Then deploy the texture in blended quad particles

Code: [Select]

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

  sys texn[16]


  type tParticle
  ===============
  float x,y,z
  float size,speed,angle
  float fade
  float r,g,b
  end type


  'RANDOMISER
  ===========
  '
  sys seed
  '
  Function Rnd() as float
  Static As float f, d=1/0x7fffffff
  mov eax,seed
  inc eax
  rol eax,7
  imul eax,eax,13
  mov seed,eax
  push eax
  fild dword [esp]
  add esp,4
  fmul dword d
  fstp dword f
  return f
  End Function


  'ABS RANDOMISER
  ===============
  '
  Function arnd() as float
  return Abs(Rnd)
  End Function


  Sub textureA(sys n)
  ===================
  static long v[16]
  v<= 0,  0,  0, 0, 0, -1, 0x88ffffff, 0,
  0, 0x88ffffff, -1, 0,  0,  0,  0, 0
  MakeTexture @v, 4, 4, n
  End Sub


  function QuadTex(float x,y,z)
  =============================
  glBegin GL_QUADS
  glTexCoord2f 0.0,0.0 : glVertex3f -x,-y, z
  glTexCoord2f 1.0,0.0 : glVertex3f  x,-y, z
  glTexCoord2f 1.0,1.0 : glVertex3f  x, y, z
  glTexCoord2f 0.0,1.0 : glVertex3f -x, y, z   
  glend
  end function


  Function DrawParticle (tparticle*p)
  ===================================
  glPushMatrix   
  glTranslatef p.x, p.y, p.z
  glColor4f 1,.5,.0,.7-p.y/2 ' The particle will fade out
  p.angle+=Rnd()
  glscalef 1, p.angle*.0004+1, 1 'itr
  glRotatef p.angle, 0, 0, 1
  QuadTex p.size,p.size,0
  glPopMatrix   
  End Function


  Function ReNewParticle(tparticle*p,sys i,e)
  ===========================================
  p(i).x    = Rnd()*.5
  p(i).y    = 0
  p(i).z    = -e*.1 / i 'z layers
  p(i).size = .025+aRnd()*0.05
  p(i).Speed= .5+aRnd()
  p(i).Angle= aRnd()*360
  End Function


  Function RenderParticles(tparticle*p,sys e)
  ===========================================
  sys i
  for i = 1 to e
  if p(i).y > 1 or p(i).speed=0
    ReNewParticle(p,i,e)
  else   
    p(i).size += 0.1*.0167
    p(i).x    += 0.1*.0167
    p(i).y     = p(i).y + p(i).Speed *.00167 'assume framerate 60
    drawParticle p(i)
  end if
  Next 
  End Function


  sub Triangle
  ============
  glBegin GL_TRIANGLES
  glColor4f   1.0, 0.0, 0.0, 0.99 : glVertex3f   0.0,  1.0, 0.0
  glColor4f   0.0, 1.0, 0.0, 0.99 : glVertex3f  -1.0, -1.0, 0.0
  glColor4f   0.0, 0.0, 1.0, 0.99 : glVertex3f   1.0, -1.0, 0.0
  glEnd
  end sub

  sub Quads
  =========
  glBegin GL_QUADS
  glColor4f   1.0, 0.0, 0.0, 0.99 : glVertex3f  -1.0, -1.0, 0.0
  glColor4f   0.0, 1.0, 0.0, 0.99 : glVertex3f   1.0, -1.0, 0.0
  glColor4f   0.0, 0.0, 1.0, 0.99 : glVertex3f   1.0,  1.0, 0.0
  glColor4f   0.5, 0.5, 0.5, 0.99 : glVertex3f  -1.0,  1.0, 0.0
  glEnd
  end sub

  sub initialize(sys hWnd)
  '=======================
  seed=0x12345678
  SetTimer hWnd,1,10,NULL
  glGenTextures 1, texn
  textureA texn[1]
  end sub

  tparticle p[50]
  sys count

  sub scene(sys hWnd)
  '==================
  '
  static single s1,s2,s3,s4,ang1,angi1=1,ra,ri
  '
  glLoadIdentity
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  glEnable GL_DEPTH_TEST
  glClearColor 0.5, 0.5, 0.7, 0
  'glClearColor 0.0, 0.0, 0.0, 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
  '
  glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
  glEnable GL_BLEND
  '
  glPushMatrix
  gltranslatef    6.0, 0.0, -16.0
  glrotatef    ang1,0,1,0
  Triangle
  glPopMatrix
  '
  glPushMatrix
  gltranslatef    2.0, 0.0, -8.0
  glrotatef    ang1,0,1,0
  Triangle
  glPopMatrix
  '
  glPushMatrix
  gltranslatef    0.0, 0.0, -4.0
  glrotatef    ang1,0,1,0
  Triangle
  glPopMatrix
  '
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[1]
  glPushMatrix
  gltranslatef 0.0, 0.0, -2.0
  RenderParticles p,40
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  '
  end sub



  sub Release(sys hwnd)
  '====================
  killTimer hwnd, 1
  glDeleteTextures 1, texn
  end sub

X

Frankolinox

  • Guest
Re: Smoke and loading image as texture?
« Reply #1 on: May 12, 2013, 03:33:14 AM »
first thanks for that interesting example charles!

second: my question belongs to load an image as texture (here: "cloud.bmp"). Can you help to look upon my code example how to load an image as texture for the particle example?

Code: [Select]
 includepath "$\inc\"
  $ FileName "t.exe"
  'include "RTL32.inc"
  'include "RTL64.inc"
  title="Smoke with Cloud"
  include "OpenglSceneFrame.inc"

  sys texn[16]


  type tParticle
  ===============
  float x,y,z
  float size,speed,angle
  float fade
  float r,g,b
  end type
  

  Sub textureA(sys n)
  ===================
  static long v[16]
  v<= 0,  0,  0, 0, 0, -1, 0x88ffffff, 0,
  0, 0x88ffffff, -1, 0,  0,  0,  0, 0
  'MakeTexture @v, 4, 4, n
  MakeTexture @v, 4, 4, n
  End Sub
  
  '------------------------------------ idea ------- //
  '------------------------------------ idea ------- //
  sub anotherTexture(sys nn)
  ============================
  static long v[16]  
  static sys res=256
  string imgs[1]=""  
  sys   texn[3]
  sys   GdiplusToken
  sys texn
  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
  
  glGenTextures 1, texn          
  v<=GetTexImage "../images/cloud.bmp",res,imgs[1]
  'GetTexImage "cloud.bmp",res,imgs[1]
  'MakeTexture *imgs[1],res,res,texn[1]
  MakeTexture @v, 4, 4, texn
  End sub
  '------------------------------------ idea end ------- //
  '------------------------------------ idea end ------- //
  
  Function DrawParticle (tparticle*p)
  ===================================
  glPushMatrix  
  glTranslatef p.x, p.y, p.z
  glColor4f 1,.5,.0,.7-p.y/2 ' The particle will fade out
  p.angle+=Rnd()
  glscalef 1, p.angle*.0004+1, 1 'itr
  glRotatef p.angle, 0, 0, 1
  QuadTex p.size,p.size,0
  glPopMatrix  
  End Function


  Function ReNewParticle(tparticle*p,sys i,e)
  ===========================================
  p(i).x    = Rnd()*.5
  p(i).y    = 0
  p(i).z    = -e*.1 / i 'z layers
  p(i).size = .025+aRnd()*0.05
  p(i).Speed= .5+aRnd()
  p(i).Angle= aRnd()*360
  End Function


  Function RenderParticles(tparticle*p,sys e)
  ===========================================
  sys i
  for i = 1 to e
  if p(i).y > 1 or p(i).speed=0
    ReNewParticle(p,i,e)
  else    
    p(i).size += 0.1*.0167
    p(i).x    += 0.1*.0167
    p(i).y     = p(i).y + p(i).Speed *.00167 'assume framerate 60
    drawParticle p(i)
  end if
  Next  
  End Function


  sub ColorTriangle
  =================
  glBegin GL_TRIANGLES
  glColor4f   1.0, 0.0, 0.0, 0.99 : glVertex3f   0.0,  1.0, 0.0
  glColor4f   0.0, 1.0, 0.0, 0.99 : glVertex3f  -1.0, -1.0, 0.0
  glColor4f   0.0, 0.0, 1.0, 0.99 : glVertex3f   1.0, -1.0, 0.0
  glEnd
  end sub

  sub ColorQuad
  =============
  glBegin GL_QUADS
  glColor4f   1.0, 0.0, 0.0, 0.99 : glVertex3f  -1.0, -1.0, 0.0
  glColor4f   0.0, 1.0, 0.0, 0.99 : glVertex3f   1.0, -1.0, 0.0
  glColor4f   0.0, 0.0, 1.0, 0.99 : glVertex3f   1.0,  1.0, 0.0
  glColor4f   0.5, 0.5, 0.5, 0.99 : glVertex3f  -1.0,  1.0, 0.0
  glEnd
  end sub

  sub initialize(sys hWnd)
  '=======================
  seed=0x12345678
  SetTimer hWnd,1,10,NULL
  glGenTextures 1, texn
  ''textureA texn[1] 'ok
  anotherTexture texn[1]
  'MinTexture texn[1]
  end sub

  tparticle p[50]
  sys count

  sub scene(sys hWnd)
  '==================
  '
  static single s1,s2,s3,s4,ang1,angi1=1,ra,ri
  '
  glLoadIdentity
  glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
  glEnable GL_DEPTH_TEST
  glClearColor 0.5, 0.5, 0.7, 0
  'glClearColor 0.0, 0.0, 0.0, 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
  '
  glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
  glEnable GL_BLEND
  '
  glPushMatrix
  gltranslatef    6.0, 0.0, -16.0
  glrotatef    ang1,0,1,0
  ColorTriangle
  glPopMatrix
  '
  glPushMatrix
  gltranslatef    2.0, 0.0, -8.0
  glrotatef    ang1,0,1,0
  ColorTriangle
  glPopMatrix
  '
  glPushMatrix
  gltranslatef    0.0, 0.0, -4.0
  glrotatef    ang1,0,1,0
  ColorTriangle
  glPopMatrix
  '
  'SMOKE
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[1]
  glPushMatrix
  gltranslatef 0.0, 0.0, -2.0
  RenderParticles p,20
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  sleep 10
  '
  end sub



  sub Release(sys hwnd)
  '====================
  killTimer hwnd, 1
  glDeleteTextures 1, texn
  end sub


  

best regards from raining, sunny, raining, sunny centre of germany, frank

X
« Last Edit: May 12, 2013, 03:40:52 AM by Frankolinox »

Charles Pegge

  • Guest
Re: Smoke
« Reply #2 on: May 14, 2013, 12:57:06 AM »
Hi Frank,

Here is another example which I hope answers your question. You can bind any image BMP/JPEG etc to a texture. In this case textures 1 and 2 are synthesised internally for the clouds and smoke, and texture 3 is the crate surface derived from ..\images\crate.jpg. If you wanted to make smoke made from crate.jpg, then you could for instance, switch textures 1 and 3:

a=textn(1) : texn(1)=text(3) : texn(3)=a

Get the latest Oxygen.dll, and and put the files into examples/Opengl, except for those in the for_inc folder which contains replacements for files in inc/

Charles

X

Frankolinox

  • Guest
Re: Smoke
« Reply #3 on: May 14, 2013, 02:44:01 AM »
many thanks for your example, I will test this evening! :-)

btw: it's possible to make a texture without the "quad" effect for smoking or cloud animation (your first examples) that there are no edges in rendering part? so the aim could be that the smoke or clouds animation will be more smooth? ;)

nice day, frank

Charles Pegge

  • Guest
Re: Smoke
« Reply #4 on: May 14, 2013, 03:28:23 AM »
Alpha has to fall to zero towards the edges of the texture. It's easier to do this with small synthesised images, PNG images have an alpha channel. But with BMP or JPEG, the image pixels have to be processed - moving blue or green to alpha, for example.