Anyone solved this problem. Normal lightning

Blitz3D Forums/Blitz3D Programming/Anyone solved this problem. Normal lightning

Wings(Posted 2007) [#1]
I encounter a normal error with lightning while building a world in voidrpg. iam trying to activate lightning.




some code here



;VoidRPG World Editor

Dim HMAP#(1024,1024)
;World mesh globals.
Global wx=1024,wz=1024


Dim verts(64,64,8) ; 8 layers per square




Global camera



Graphics3D 1280,800,32,1

SetBuffer BackBuffer()

camera=CreateCamera()
CameraViewport camera,0,0,1280,800
CameraRange camera,0.01,1000
PositionEntity camera,32,256,32



light=CreateLight()
sphere=CreateSphere(8,light)
ScaleEntity sphere,100,100,100
rot=CreateCube()
EntityParent light,rot
PositionEntity light,0,1000,0


map=LoadImage("hmap.bmp")

;Load in the height data into world matrix
width=128
height=128
For z=1 To height
For x=1 To width
h#=0
For x1=-2 To 2
For z1=-2 To 2

H#=h#+(ReadPixel(x+z1,z+z1,ImageBuffer(map)) And 255)
Next
Next
h#=h#/24.0
HMAP(x,z)=h#

Next
Print "Completed line "+z+" of "+height

Next





;CreateWorld
For z=1 To 128 Step 63
For x=1 To 128 Step 63
CreatePlane(x,z)
Next
Next



While Not KeyHit(1)

TurnEntity rot,1,1,1
UpdateWorld()
RenderWorld()

camera_handling()

Flip

Wend




















Function camera_handling()

If KeyDown(17);W
MoveEntity camera,0,0,0.1
End If
If KeyDown(31)
MoveEntity camera,0,0,-0.1
End If


If KeyDown(30)
MoveEntity camera,-0.1,0,0
End If

If KeyDown(32)
MoveEntity camera,0.1,0,0
End If





smx#=MouseXSpeed()/15
smy#=MouseYSpeed()/15
MoveMouse 320,200
TurnEntity camera,smy#,-smx#,0
RotateEntity camera,EntityPitch(camera),EntityYaw(camera),0
TranslateEntity camera,0,-0.1,0

y#=BilinearInterpValue# ( Float(EntityX(camera)) ,Float(EntityZ(camera)) )

If y#+1.5>EntityY#(camera)

PositionEntity camera,EntityX(camera),y#+1.5,EntityZ(camera)
jumps=0
yspeed#=0

End If








End Function










Function CreatePlane(xoffs,zoffs)

;Creates a matrix
mesh=CreateMesh()
surface=CreateSurface(mesh)

;firstvert=AddVertex (surface,0,0,0) ;A dummi verts sens i begin reading mem from 1 and above.

For z=1 To 64
For x=1 To 64

vert=AddVertex(surface,x+xoffs,HMAP#(x+xoffs,z+zoffs),z+zoffs,x+xoffs,z+zoffs) ; Ads a vertex
verts(x,z,1)=vert ;Put the vertex on place.

Next
Next


;Addtriangles to surface.
For z=1 To 63
For x=1 To 63

AddTriangle surface,verts(x,z,1),verts(x,z+1,1),verts(x+1,z+1,1)
AddTriangle surface,verts(x,z,1),verts(x+1,z+1,1),verts(x+1,z,1)

Next
Next


;Update the normals
MESHnormals( mesh )

End Function































;Functions not created by me


Function BilinearInterpValue# ( x1# , y1# )

X_low = Int ( Floor(x1) )
X_hi = Int ( Ceil (x1) )
Y_low = Int ( Floor(y1) )
Y_hi = Int ( Ceil (y1) )

;Kepp things in bound :)
If X_low < 0 Or X_low > WX Then x_low=0
If X_hi < 0 Or X_hi > WX Then x_hi=0
If Y_low < 0 Or Y_low > WZ Then Y_low=0
If Y_hi < 0 Or Y_hi > WZ Then Y_hi=0


x_per# = x1 - x_low
y_per# = y1 - y_low

p00# = HMAP# ( x_low , y_low )
p10# = HMAP# ( x_hi , y_low )
p01# = HMAP# ( x_low , y_hi )
p11# = HMAP# ( x_hi , y_hi )

i1# = LinearInterpolate#(p00#,p10#,x_per#)
i2# = LinearInterpolate#(p01#,p11#,x_per#)

Return LinearInterpolate#(i1#,i2#,y_per#)

End Function

;thanks too gabrielknight74. philings2@...
Function LinearInterpolate#(a#,b#,x#)
Return a#+(b#-a#)*x#
End Function






Function MESHnormals( tmesh )

For l = 1 To CountSurfaces(tmesh )
s = GetSurface( tmesh , l )
For t = 0 To CountTriangles( s )-1
v0 = TriangleVertex( s, t, 0 )
v1 = TriangleVertex( s, t, 1 )
v2 = TriangleVertex( s, t, 2 )
ax# = VertexX( s, v1 ) - VertexX( s, v0 )
ay# = VertexY( s, v1 ) - VertexY( s, v0 )
az# = VertexZ( s, v1 ) - VertexZ( s, v0 )
bx# = VertexX( s, v2 ) - VertexX( s, v1 )
by# = VertexY( s, v2 ) - VertexY( s, v1 )
bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )
Nx# = ( ay * bz ) - ( az * by )
Ny# = ( az * bx ) - ( ax * bz )
Nz# = ( ax * by ) - ( ay * bx )
Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz )
Nx = Nx / Ns
Ny = Ny / Ns
Nz = Nz / Ns
For v = v0 To v2
VertexNormal s, v, Nx, Ny, Nz
Next
Next
Next

End Function


puki(Posted 2007) [#2]
Does the problem amplify (ie become more pronounced) when you run it in 16 bit?


Wings(Posted 2007) [#3]
nope same ressult with 16 bit

i think its the normals byt dont know how to fix it.


Stevie G(Posted 2007) [#4]
Your landscape mesh is not unwelded so vertices are shared.

Try this function before MESHnormals. e.g.

mesh = MESHunweld( mesh )
MESHnormals( mesh )

EDIT: You may also want to set entityfx mesh, 4

Function MESHunweld( Mesh )

	Copy = CreateMesh()
	ns = CreateSurface( Copy )
	For su = 1 To CountSurfaces( Mesh )
		s = GetSurface( Mesh , su )
		For t = 0 To CountTriangles( s ) - 1
			v0 = TriangleVertex( s, t, 0 )
			v1 = TriangleVertex( s, t, 1 )
			v2 = TriangleVertex( s, t, 2 )
			Nv0 = AddVertex( ns , VertexX( s , v0 ) , VertexY( s, v0 ) , VertexZ( s, v0 ) )
			Nv1 = AddVertex( ns , VertexX( s , v1 ) , VertexY( s, v1 ) , VertexZ( s, v1 ) )
			Nv2 = AddVertex( ns , VertexX( s , v2 ) , VertexY( s, v2 ) , VertexZ( s, v2 ) )
			VertexColor ns, Nv0 , VertexRed( s, v0 ) , VertexGreen( s, v0 ) , VertexBlue( s, v0 )
			VertexColor ns, Nv1 , VertexRed( s, v1 ) , VertexGreen( s, v1 ) , VertexBlue( s, v1 )
			VertexColor ns, Nv2 , VertexRed( s, v2 ) , VertexGreen( s, v2 ) , VertexBlue( s, v2 )
			AddTriangle ns , Nv0 , Nv1 , Nv2
		Next
	Next
	FreeEntity mesh
	Return Copy

End Function	



Wings(Posted 2007) [#5]
that works. but al smothnes is gone with wind.


b32(Posted 2007) [#6]
I think the problem is calculating the vertex normals. The command UpdateNormals does the sort of calculations you need for this. However, because the terrain is built up from separate parts, for the edged of each square, it misses the reference vertices that are lying around it (on the squares that are lying beside it). Therefore, the normals are not calculated correctly for the edges of each square.

The solution would be, I think, calculating the vertex normals yourself. However, I don't know the formula for it. But I'm sure it can be found somewhere online. I suppose such a formula would need the coordinates of the surrounding vertices to calculate the vertex normal.

To test if this theory is correct, I tried this:

It is a workaround: it first creates a square that is one line of vertices bigger than the square should be, calculates the normals using UpdateNormals, and stores their values. Then it clears the surface and creates a smaller square (the size that is needed), with these (pre)calculated normals.


Wings(Posted 2007) [#7]
Superb 100% thanks b32 solved issue

I run the code and the disorder has "get lost" at last.

I can now continue the edtor :=)


Stevie G(Posted 2007) [#8]

that works. but al smothnes is gone with wind.



Well, I assumed you wanted per triangle shading since you were calculating per triangle normals.


Wings(Posted 2007) [#9]
Yes Stevie G. This Triangle shade will be usefull for making clifs.


Wings(Posted 2007) [#10]
I Final Fullu understand this now. Thanks again all :)