Ooops
January 26, 2021, 06:36:45 AM

Author Topic: [bb] Specular reflections by fredborg [ 1+ years ago ]  (Read 652 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Specular reflections by fredborg [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Specular reflections
Author : fredborg
Posted : 1+ years ago

Description : You will need some textures, which is really the important bit. <a href="http://www.frecle.net/misc/roadtex.zip" target="_blank">So first download this .zip[/url] it contains the source as well.

The trick is that the "specular" texture should be completely white and have an alpha channel that masks it. Then the specular material should be competely black and additive, but have a high shininess value. And that's it really.

Have fun!


Code :
Code: BlitzBasic
  1. ;
  2. ; How to make nice specular reflections with DirectX7 tech...
  3. ; Created by Mikkel Fredborg
  4. ; Use as you please!
  5. ;
  6.  
  7. Graphics3D 640,480,0,2
  8. SetBuffer BackBuffer()
  9.  
  10. ;
  11. ; Textures
  12. ClearTextureFilters
  13. Local tex_dif   = LoadTexture("Road_Diffuse.png",1+8)
  14. Local tex_spc   = LoadTexture("Road_Specular.png",1+2+8)
  15.  
  16. ;
  17. ; Diffuse material
  18. Local brush_dif = CreateBrush()
  19. BrushTexture(brush_dif,tex_dif)
  20. BrushShininess(brush_dif,0.02)
  21.  
  22. ;
  23. ; Specular material
  24. Local brush_spc = CreateBrush()
  25. BrushTexture(brush_spc,tex_spc)
  26. BrushBlend(brush_spc,3)
  27. BrushColor(brush_spc,0,0,0)
  28. BrushShininess(brush_spc,0.5)
  29.  
  30. ;
  31. ; Roads
  32. Local road_dif = CreateRoad(5,20,8,32)
  33. PaintEntity road_dif,brush_dif
  34. Local road_spc = CreateRoad(5,20,8,32)
  35. PaintEntity road_spc,brush_spc
  36. PositionEntity road_spc,0,0.01,0
  37.  
  38. ;
  39. ; Light
  40. Local light = CreateLight(2)
  41. PositionEntity light,1,40,200
  42. LightColor light,200,200,160
  43.  
  44. AmbientLight 60,70,90
  45.  
  46. ;
  47. ; Camera
  48. Local cam = CreateCamera()
  49. PositionEntity cam,0,2,0
  50. RotateEntity cam,20,0,0
  51.  
  52. ;
  53. ; Action!
  54. While Not KeyHit(1)
  55.  
  56.         TranslateEntity cam,(KeyDown(205)-KeyDown(203))*0.1,0.0,(KeyDown(200)-KeyDown(208))*0.1
  57.  
  58.         RenderWorld
  59.         Flip
  60.  
  61. Wend
  62.  
  63. End
  64.  
  65. Function CreateRoad(w#,d#,xseg,zseg)
  66.  
  67.         Local mesh = CreateMesh()
  68.         Local surf = CreateSurface(mesh)
  69.        
  70.         Local hw# = w/2.0
  71.         Local hd# = d/2.0
  72.        
  73.         Local dx# = 0.0
  74.         Local dz# = 0.0
  75.        
  76.         Local vscale# = zseg / Float(xseg)
  77.        
  78.         For x = 0 To xseg
  79.                 dx = x / Float(xseg)
  80.                 For z = 0 To zseg
  81.                         dz = z / Float(zseg)
  82.                         v = AddVertex(surf,dx*w - hw, 0.0, dz*d - hd, dx, dz*vscale)
  83.                         VertexNormal(surf,v,0,1,0)
  84.                 Next
  85.         Next
  86.  
  87.         For x = 0 To xseg-1
  88.                 For z = 0 To zseg-1
  89.                         v0 = x*(zseg+1) + z                    
  90.                         v1 = x*(zseg+1) + z + 1        
  91.                         v2 = (x+1)*(zseg+1) + z + 1    
  92.                         v3 = (x+1)*(zseg+1) + z          
  93.                         AddTriangle(surf,v0,v1,v2)
  94.                         AddTriangle(surf,v0,v2,v3)
  95.                 Next
  96.         Next   
  97.  
  98.         Return mesh
  99.  
  100. End Function


Comments :


OJay(Posted 1+ years ago)

 thats cool! you can even change the brushs shininess at runtime to create nice transistion e.g. for changes between wet and dry surfaces!short example (use keys 1 and 2 to change the streets shininess):
Code: [Select]
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
WireFrame False
shininess# = 0.02

;
; Textures
Local tex_dif = LoadTexture("Road_Diffuse.png",1+2)
Local tex_spc = LoadTexture("Road_Specular.png",1+2+8)

;
; Diffuse material
Local brush_dif = CreateBrush()
BrushTexture(brush_dif,tex_dif)
BrushShininess(brush_dif,shininess)

;
; Specular material
Local brush_spc = CreateBrush()
BrushTexture(brush_spc,tex_spc)
BrushBlend(brush_spc,3)
BrushColor(brush_spc,0,0,0)
BrushShininess(brush_spc,0.5)

;
; Roads
Local road_dif = CreateRoad(5,20,8,32)
PaintEntity road_dif,brush_dif
Local road_spc = CreateRoad(5,20,8,32)
PaintEntity road_spc,brush_spc
PositionEntity road_spc,0,0.01,0

;
; Light
Local light = CreateLight(2)
PositionEntity light,1,40,200
LightColor light,200,200,160

AmbientLight 60,70,90

;
; Camera
Local cam = CreateCamera()
PositionEntity cam,0,2,0
RotateEntity cam,10,0,0

;
; Action!
While Not KeyHit(1)

shininess = shininess - (KeyDown(2) Or -KeyDown(3))*0.001
If shininess < 0 Then shininess = 0
If shininess > 1 Then shininess = 1
BrushShininess(brush_dif,shininess)
PaintEntity road_dif,brush_dif

TranslateEntity cam,(KeyDown(205)-KeyDown(203))*0.1,0.0,(KeyDown(200)-KeyDown(208))*0.1

RenderWorld

Text 10,10,shininess

Flip

Wend

End

Function CreateRoad(w#,d#,xseg,zseg)

Local mesh = CreateMesh()
Local surf = CreateSurface(mesh)

Local hw# = w/2.0
Local hd# = d/2.0

Local dx# = 0.0
Local dz# = 0.0

Local vscale# = zseg / Float(xseg)

For x = 0 To xseg
dx = x / Float(xseg)
For z = 0 To zseg
dz = z / Float(zseg)
v = AddVertex(surf,dx*w - hw, 0.0, dz*d - hd, dx, dz*vscale)
VertexNormal(surf,v,0,1,0)
Next
Next

For x = 0 To xseg-1
For z = 0 To zseg-1
v0 = x*(zseg+1) + z
v1 = x*(zseg+1) + z + 1
v2 = (x+1)*(zseg+1) + z + 1
v3 = (x+1)*(zseg+1) + z
AddTriangle(surf,v0,v1,v2)
AddTriangle(surf,v0,v2,v3)
Next
Next

Return mesh

End Function
thanks again to fredborg!


puki(Posted 1+ years ago)

 It's about time "fredborg" handed over his hard-drive.I quite like 'LightColor light,100,100,0' - has a kind of warmth to it.<div class="quote"> you can even change the brushs shininess at runtime to create nice transistion e.g. for changes between wet and dry surfaces! </div>Also to simulate the sun going in and out behind clouds, etc.Most exciting.


Pinete(Posted 1+ years ago)

 impressive!:)regards!


degac(Posted 1+ years ago)

 WOW!


fredborg(Posted 1+ years ago)

 Hi,I added another version which shows how to achieve the specular light with a reflection map instead of a real light. <a href="http://www.frecle.net/misc/roadtex.zip" target="_blank">It's all in the .zip file![/url]To use this properly you need to align the highlight in the reflection texture with where your sun/light is positioned.


Filax(Posted 1+ years ago)

 Nice effect !


Naughty Alien(Posted 1+ years ago)

 ..nice..its seems that on geometry whats afected by this nice stuff, will be double number of polys...is there any way to achieve ssame effect without making copy of the 'difuse map' geometry?


IceVAN(Posted 1+ years ago)

 Nice effect!!This run perfectly in blitzmax - minib3d mod


Kippykip(Posted 1+ years ago)

 Link is dead [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal