Vertex Light ( source code )
Blitz3D Forums/Blitz3D Programming/Vertex Light ( source code )
| ||
This is just a little code that I used to get the fundimentals of vertex lighting going for my self before I would work it into my main project. I built this up from part of a code that Halo posted a couple of weeks ago. I was about to delete this little code and then thought it might just give sombody some help. This code still needs work on moving the mesh or entity and the Range setting will need to be redone. Like I said its just the Fundimentals of vertex lighting. Good Luck. ;______________________________________________________________________________________; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; STICKMAN VERTEX LIGHT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;______________________________________________________________________________________; Graphics3D 800,600,16,2 SetBuffer BackBuffer() Global Light,Entity,bout,but LgtNum=1 Eindex=1 CP1=CreatePivot() CP2=CreatePivot(CP1) camera=CreateCamera(CP2) PositionEntity Camera,0,15,-4 RotateEntity CP1,-22,22,0 ZAxis=CreatePivot() PositionEntity ZAxis,0,0,2 XAxis=CreatePivot() PositionEntity XAxis,2,0,0 YAxis=CreatePivot() PositionEntity YAxis,0,2,0 Entity=CreateCube() EntityFX Entity,3 UpdateNormals Entity Light=CreateCube() ScaleEntity Light,.125,.125,.125 PositionEntity Light,0,4,0 Dim LightData(1,1,7) Lightdata(1,0,0)=30 Lightdata(1,0,1)=30 Lightdata(1,0,2)=30 ;Light Lightdata(1,1,0)=255 ;Lr Lightdata(1,1,1)=100 ;Lg Lightdata(1,1,2)=255 ;Lb Lightdata(1,1,3)=5 ;Lrng Lightdata(1,1,4)=0 ;lx Lightdata(1,1,5)=0 ;ly Lightdata(1,1,6)=0 ;lz Lightdata(1,1,7)=1 ;ltyp RecDat=3 Dim RecData$(RecDat);For Recording on screen text info. LightMesh_(Eindex,LgtNum);Light Mesh for First time. DrawButtons() ; Start Program _____________? While Not KeyHit(1) Cls RenderWorld Color 240,90,90 DrawImage but,20,100 Text 24,102,"+" DrawImage but,40,100 Text 44,102,"-" ;On Screen Text Color 0,255,255 For a=0 To RecDat L=20+L Text 10,L,RecData(a) Next L=0 Color 255,255,0 Text 400,20, "LeftMouseDown...[ Position View ]" Text 400,40, "F1..............[ Toggle My Light\Blitz Light ]" Text 400,60, "Arrow keys......[ Move Light X\Z ]" Text 400,80, "S-X Keys........[ Move Light Y ]" Text 70,100, "Set Range.." If RectsOverlap(MouseX(),MouseY(),1,1,20,100,18,18) If MouseHit(1) DrawImage bout,20,100 Lightdata(1,1,3)=Lightdata(1,1,3)+1 UpDateLights=1 End If End If If RectsOverlap(MouseX(),MouseY(),1,1,40,100,18,18) If MouseHit(1) DrawImage bout,40,100 Lightdata(1,1,3)=Lightdata(1,1,3)-1 UpDateLights=1 End If End If If KeyHit(61) Lightdata(1,1,3)=Lightdata(1,1,3)+1 UpDateLights=1 If RectsOverlap(MouseX(),MouseY(),1,1,120,120,300,300) If MouseDown(1) TurnEntity CP1,0,MouseXSpeed(),0 TurnEntity CP2,MouseYSpeed(),0,0 MoveMouse 400,300 End If End If If KeyDown(203) MoveEntity Light,-.125,0,0 UpDateLights=1 If KeyDown(205) MoveEntity Light,+.125,0,0 UpDateLights=1 If KeyDown(200) MoveEntity Light,0,0,+.125 UpDateLights=1 If KeyDown(208) MoveEntity Light,0,0,-.125 UpDateLights=1 If KeyDown(31) MoveEntity Light,0,+.125,0 UpDateLights=1 If KeyDown(45) MoveEntity Light,0,-.125,0 UpDateLights=1 If KeyHit(59) Lightdata(1,1,7)=1-Lightdata(1,1,7) UpDateLights=1 ;KeyF1 LightX#=EntityX(Light,False) LightY#=EntityY(Light,False) LightZ#=EntityZ(Light,False) Lightdata(1,1,4)=LightX Lightdata(1,1,5)=LightY Lightdata(1,1,6)=LightZ If UpDateLights=1 LightMesh_(Eindex,LgtNum) UpDateLights=0 End If PointEntity Camera,CP1 DrawAxis(camera,Entity,Zaxis,Xaxis,Yaxis) FlushMouse Flip Wend End Function LightMesh_(Eidx,Lgt) ;Ambient Ar =Lightdata(Eidx,0,0) Ag =Lightdata(Eidx,0,1) Ab =Lightdata(Eidx,0,2) SetAmbient(Ar,Ab,Ag,Eidx) ;Light Lr =Lightdata(Eidx,Lgt,0) Lg =Lightdata(Eidx,Lgt,1) Lb =Lightdata(Eidx,Lgt,2) Rg =Lightdata(Eidx,Lgt,3) Lx#=Lightdata(Eidx,Lgt,4) Ly#=Lightdata(Eidx,Lgt,5) Lz#=Lightdata(Eidx,Lgt,6) Tp =Lightdata(Eidx,Lgt,7) Select Tp;Type of Light Case 1 ;Directional Light recdata(0 )=" My Light " For Sn=1 To CountSurfaces(Entity) S=GetSurface(Entity,Sn) Verts=CountVertices(S)-1 For v=0 To Verts VX#=VertexX(S,v) VY#=VertexY(S,v) VZ#=VertexZ(S,v) NX#=VertexNX(S,v) NY#=VertexNY(S,v) NZ#=VertexNZ(S,v) VR=VertexRed(S,v) VG=VertexGreen(S,v) VB=VertexBlue(S,v) ;POINT......................... ;V-Point----\ TFormPoint( VX,VY,VZ,Light,0);;; VPointX#=TFormedX() VPointY#=TFormedY() VPointZ#=TFormedZ() ;L-Point----\ TFormPoint( LX,LY,LZ,Light,0) LPointX#=TFormedX() LPointY#=TFormedY() LPointZ#=TFormedZ() ;VECTOR........................ ;V-Vector----\ TFormVector(VpointX,VpointY,VpointZ,Light,0);;; VVectorX#=TFormedX() VVectorY#=TFormedY() VVectorZ#=TFormedZ() ;L-Vector----\ TFormVector(LpointX,LpointY,LpointZ,Light,0) LVectorX#=TFormedX() LVectorY#=TFormedY() LVectorZ#=TFormedZ() ;NORMAL ;V-Normal----\ TFormNormal(VVectorX,VVectorY,VVectorZ,Light,0);;;; VNormalX#=TFormedX() VNormalY#=TFormedY() VNormalZ#=TFormedZ() ;L-Normal----\ TFormNormal(LVectorX,LVectorY,LVectorZ,Light,0) LNormalX#=TFormedX() LNormalY#=TFormedY() LNormalZ#=TFormedZ() Xval#=(Abs(VPointX)-Abs(LPointX)) Yval#=(Abs(VPointY)-Abs(LPointY)) Zval#=(Abs(VPointZ)-Abs(LPointZ)) ;Intensity#=(LNormalX*VNormalX)+(LNormalY*VNormalY)+(LNormalZ*VNormalZ) Intensity#=(LNormalX*NX)+(LNormalY*NY)+(LNormalZ*NZ) Distance#=(Xval+Yval+Zval) If Intensity>0.0 vr=vr+lr*intensity+Distance*255/rg vg=vg+lg*intensity+Distance*255/rg vb=vb+lb*intensity+Distance*255/rg End If If vr<Ar vr=Ar If vg<Ag vg=Ag If vb<Ab vb=Ab If vr>255 vr=255 If vg>255 vg=255 If vb>255 vb=255 VertexColor S,v,vr,vg,vb If V=0 recdata(1 )=" Intensity :"+Intensity recdata(2 )=" Distance :"+Distance recdata(3 )=" Range :"+Lightdata(1,1,3) End If Next Next ;Blitz Light Mesh.... Default recdata(0 )=" Blitz Light " For Sn=1 To CountSurfaces(Entity) S=GetSurface(Entity,Sn) Verts=CountVertices(S)-1 For v=0 To Verts VertexColor S,v,ar,ag,ab Next Next LightMesh Entity,lr,lg,lb,rg,LX,LY,LZ End Select End Function ;Draw On screen Axis Function DrawAxis(C,EN,ZA,XA,YA) CameraProject C,EntityX(en),EntityY(en),EntityZ(en) EX=ProjectedX() EY=ProjectedY() CameraProject C,EntityX(za),EntityY(za),EntityZ(za) ZX=ProjectedX() ZY=ProjectedY() CameraProject C,EntityX(xa),EntityY(xa),EntityZ(xa) XX=ProjectedX() XY=ProjectedY() CameraProject C,EntityX(ya),EntityY(ya),EntityZ(ya) YX=ProjectedX() YY=ProjectedY() ;Zaxis Color 0,0,255 Line ex,ey,zx,zy Text zx+5,zy-5,"Z" ;Xaxis Color 255,0,0 Line ex,ey,xx,xy Text xx+5,xy-5,"X" ;Yaxis Color 0,255,0 Line ex,ey,yx,yy Text yx-5,yy-15,"Y" End Function ;Set Ambient Light Color Function SetAmbient(AR,AG,AB,Eidx) For Sn=1 To CountSurfaces(Entity) S=GetSurface(Entity,Sn) Verts=CountVertices(S)-1 For v=0 To Verts VertexColor S,v,ar,ag,ab Next Next End Function Function DrawButtons() But=CreateImage(18,18) SetBuffer ImageBuffer(But) Color 200,200,200 Rect 2,2,16,16,1 Color 120,120,120 Rect 1,1,16,16,0 Color 190,190,190 Rect 2,2,16,16,0 Bout=CreateImage(18,18) SetBuffer ImageBuffer(Bout) Color 120,80,120 Rect 2,2,16,16,1 Color 90,90,90 Rect 2,2,16,16,0 Color 190,190,190 Rect 1,1,16,16,0 SetBuffer BackBuffer() End Function |