Code archives/3D Graphics - Effects/Specular reflections

This code has been declared by its author to be Public Domain code.

Download source code

Specular reflections by fredborg
(Posted 20 years ago)
You will need some textures, which is really the important bit. So first download this .zip 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!
;
; How to make nice specular reflections with DirectX7 tech...
; Created by Mikkel Fredborg
; Use as you please!
;

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

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

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

;
; 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,20,0,0

;
; Action!
While Not KeyHit(1)

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

	RenderWorld
	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

Comments

OJay
(Posted 20 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):

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 20 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.


you can even change the brushs shininess at runtime to create nice transistion e.g. for changes between wet and dry surfaces!

Also to simulate the sun going in and out behind clouds, etc.

Most exciting.

Pinete
(Posted 20 years ago)
impressive!
:)

regards!

degac
(Posted 20 years ago)
WOW!

fredborg
(Posted 20 years ago)
Hi,

I added another version which shows how to achieve the specular light with a reflection map instead of a real light. It's all in the .zip file!

To use this properly you need to align the highlight in the reflection texture with where your sun/light is positioned.

Filax
(Posted 20 years ago)
Nice effect !

Naughty Alien
(Posted 20 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 20 years ago)
Nice effect!!

This run perfectly in blitzmax - minib3d mod

Code Archives Forum