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 fredborg2007
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

OJay2007
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):



thanks again to fredborg!


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


Pinete2007
impressive!
:)

regards!


degac2007
WOW!


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


Filax2007
Nice effect !


Naughty Alien2007
..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?


IceVAN2007
Nice effect!!

This run perfectly in blitzmax - minib3d mod


Kippykip2015
Link is dead


Code Archives Forum