No problem with the way the dot3 texture goes on the cube, and I can understand a dot3 texture may not look quite right on models other than that for which it was made, but it looks vaguely like a face when on a flat surface. The problem with your demo (it's an excellent demo btw- taught me a thing or 2), is that it doesn't account for the rotation of vertices about their normal. Say I have a flat poly with a normal map for a face on it (designed for use on a flat poly). Now say I rotate the poly 180 degrees about its normal- the normal, hence the vertex colours have not changed, but a rotation of the values in the vertex colour are required to make the shadows on the normal map face the right way.Perhaps this isn't an issue if wrapping onto a proper 3D model, but it may be. Try this bodged version. Lose the decals and hit 3 a couple of times to see what I'm on about..........
; Dot3 Lighting example
;
; By Mikkel Fredborg
; Model and texture from <a href="http://www.soclab.bth.se/practices/orb.html" target="_blank">http://www.soclab.bth.se/practices/orb.html</a>
;RUINED BY LEE
Type Dot3Light
Field ent
Field mul#
Field typ
End Type
Graphics3D 640,480,0,2
decl=LoadTexture( "derhead_decal.jpg",1+8)
bump=LoadTexture( "derhead_local.tga",1+8)
cube=CreateQuad();LoadMesh("head.x")
PositionEntity cube,0,0,-1
EntityFX cube,1+2
TextureBlend bump,4
EntityTexture cube,bump,0,0
EntityTexture cube,decl,0,1
UpdateNormals cube
light = Dot3_CreateLight(2)
lighticon = CreateSphere(8,light)
EntityFX lighticon,1
ScaleEntity lighticon,0.1,0.1,0.1
Dot3_LightRange light,8
camera=CreateCamera()
CameraClsColor camera,100,120,130
PositionEntity camera,0,0,-1.6
CameraRange camera,0.1,100
a# = 0.
AmbientLight 0,0,0
declon = True
bumpon = True
rotatemode=0
While Not KeyHit(1)
a = a + 0.5
If a = 360.0 Then a = 0
If rotatemode>0
PositionEntity light,Cos(0)*5,Sin(0)*5,-9
RotateEntity cube,0,0,a
Else
PositionEntity light,Cos(a)*5,Sin(a)*5,-9
RotateEntity cube,0,0,0
EndIf
If KeyHit(2)
If declon
TextureBlend decl,0
Else
TextureBlend decl,2
End If
declon = Not declon
End If
If KeyHit(3)
If bumpon
TextureBlend bump,0
EntityFX cube,0
Else
TextureBlend bump,4
EntityFX cube,1+2
End If
bumpon = Not bumpon
End If
If KeyHit(4) rotatemode=rotatemode+1
If rotatemode=4 Then rotatemode=0
If KeyDown(30) TranslateEntity camera,0,0,0.01
If KeyDown(44) TranslateEntity camera,0,0,-0.01
If bumpon
If rotatemode=2
UpdateBumpNormals(cube,light,a)
Else
updatebumpnormals(cube,light,0)
EndIf
End If
UpdateWorld
RenderWorld
Text GraphicsWidth()/2,0,"1 - Toggle Texture | 2 - Toggle Bumpmap | 3 - Toggle rotmode",True,False
Flip
Wend
Function UpdateBumpNormals(mesh,light,ang#, lighttype=0)
n_surf = CountSurfaces(mesh)
For s = 1 To n_surf
surf = GetSurface(mesh,s)
n_vert = CountVertices(surf)-1
For v = 0 To n_vert
red2# = 0.0
grn2# = 0.0
blu2# = 0.0
For d3l.Dot3Light = Each Dot3Light
If d3l\typ = 1 ; Directional light
TFormVector 0,0,1,d3l\ent,0
nx# = TFormedX()
ny# = TFormedY()
nz# = TFormedZ()
TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,0
red# = TFormedX()
grn# = TFormedY()
blu# = TFormedZ()
ElseIf d3l\typ = 2 ; Point light
TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,0
nx# = -TFormedX()
ny# = -TFormedY()
nz# = -TFormedZ()
TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),mesh,0
red# = TFormedX()-EntityX(d3l\ent,True)
grn# = TFormedY()-EntityY(d3l\ent,True)
blu# = TFormedZ()-EntityZ(d3l\ent,True)
d# = Sqr(red*red + grn*grn + blu*blu)
red = red/d
grn = grn/d
blu = blu/d
Else
RuntimeError "Do it yourself, will ya?"
End If
;UGLY HAK
red=red*Cos(ang)-grn*Sin(ang)
grn=grn*Cos(ang)+red*Sin(ang)
;UGLY HAK
dot# = (red*nx + grn*ny + blu*nz)
If dot<0.0 Then dot = 0.0
red# = ((1.0+(red*dot))*127.5)*d3l\mul
grn# = ((1.0+(grn*dot))*127.5)*d3l\mul
blu# = ((1.0+(blu*dot))*127.5)*d3l\mul
If red<0 Then red = 0
If grn<0 Then grn = 0
If blu<0 Then blu = 0
red2# = red2+red
grn2# = grn2+grn
blu2# = blu2+blu
Next
VertexColor surf,v,red2,grn2,blu2
Next
Next
End Function
Function Dot3_CreateLight(typ=1,parent=0,real=True)
d3l.Dot3Light = New Dot3Light
If real
d3l\ent = CreateLight(typ,parent)
Else
d3l\ent = CreatePivot(parent)
End If
d3l\typ = typ
d3l\mul = 1.0
Return d3l\ent
End Function
Function Dot3_LightRange(ent,range#)
For d3l.dot3light = Each dot3light
If d3l\ent = ent
If Lower$(EntityClass(d3l\ent))="light"
LightRange d3l\ent,range
Return
End If
End If
Next
End Function
Function Dot3_LightIntensity(ent,intens#)
For d3l.dot3light = Each dot3light
If d3l\ent = ent
d3l\mul = intens
If Lower$(EntityClass(d3l\ent))="light"
LightColor d3l\ent,d3l\mul*255.0,d3l\mul*255.0,d3l\mul*255.0
End If
Return
End If
Next
End Function
Function createquad(parent=0)
quad=CreateMesh(parent)
surf=CreateSurface(quad)
AddVertex surf,-1,1,0,0,0
AddVertex surf,1,1,0,1,0
AddVertex surf,1,-1,0,1,1
AddVertex surf,-1,-1,0,0,1
AddTriangle surf,0,1,2
AddTriangle surf,0,2,3
Return quad
End Function