Lost normals ...

Blitz3D Forums/Blitz3D Programming/Lost normals ...

necky(Posted 2003) [#1]
Hi,

I have a problem with my code below, for some reason once I've rebuilt my mesh I can't seem to get the vertex normals for the new model? It's also refusing to accept any light. I've looked over my code and it seems to be okay and I'm now at a loss at what could be causing the problem.

If anyone could help I'd be very grateful :)

thanx,
Mike

;-----------Code starts here -----------

;********************
;* *
;* Shoot to Deflate *
;* *
;* by Mike Oakley *
;* *
;********************

Graphics3D 640,480,32,2

SetBuffer BackBuffer()

;AmbientLight 0,0,0

Global sphere=CreateSphere()
EntityPickMode sphere,2

Global camera=CreateCamera()
MoveEntity camera,0,0,-2

;Global light=CreateLight()
;MoveEntity light,0,0,3

Global xa#,xb#,xc#,ya#,yb#,yc#,za#,zb#,zc#
Global vxa,vya,vxb,vyb,vxc,vyc,nx,ny

Global verta,vertb,vertc
Global newpointx#,newpointy#,newpointz#
Global tria,trib,tric
Global tx#,ty#,tz#
Global pp
Global gbb

Dim vertsmain#(CountVertices(GetSurface(sphere,1))*2,2)
Dim verts#(CountVertices(GetSurface(sphere,1))*2,2)
Dim tris#(CountTriangles(GetSurface(sphere,1))*2,2)
Dim newfaces(CountTriangles(GetSurface(sphere,1))*2,2)
Dim wobblepos#(CountVertices(GetSurface(sphere,1))*2)
Dim tVertexN#(CountVertices(GetSurface(sphere,1))*2,2)
Dim Sine#(360)

While gg<360
Sine#(gg)=Sin(gg)
gg=gg+1
Wend
gg=0

gp=0

While gp<CountVertices(GetSurface(sphere,1))*2

wobblepos#(gp)=Rnd(0,360)
gp=gp+1

Wend

While gk<CountVertices(GetSurface(sphere,1))

vertsmain#(gk,0)=VertexX#(GetSurface(sphere,1),gk)
vertsmain#(gk,1)=VertexY#(GetSurface(sphere,1),gk)
vertsmain#(gk,2)=VertexZ#(GetSurface(sphere,1),gk)
gk=gk+1

Wend

gk=0

WireFrame 1

;********
;* MAIN *
;********

While Not KeyDown(1)=1

Cls

r#=r#+0.5

;If MouseHit(1)=True Then CameraPick(camera,MouseX(),MouseY()):gettridata():getmeshdata():rebuildmesh()

wobblemesh():UpdateWorld():getmeshdata():UpdateWorld():rebuildmesh()

RotateMesh sphere,0,r,r

UpdateWorld()
RenderWorld()

printtext()

Flip

Wend

;*****************
;* Get Mesh Data *
;*****************

Function getmeshdata()

vv=0
ff=0

Local vertices=CountVertices(GetSurface(sphere,1))
Local faces=CountTriangles(GetSurface(sphere,1))

While vv<vertices

verts#(vv,0)=vertsmain#(vv,0)
verts#(vv,1)=vertsmain#(vv,1)
verts#(vv,2)=vertsmain#(vv,2)

vv=vv+1
Wend
vv=0

While ff<faces

tris#(ff,0)=TriangleVertex(GetSurface(sphere,1),ff,0)
tris#(ff,1)=TriangleVertex(GetSurface(sphere,1),ff,1)
tris#(ff,2)=TriangleVertex(GetSurface(sphere,1),ff,2)
ff=ff+1

Wend
ff=0

vv=0
While vv<vertices

tVertexN#(vv,0)=VertexNX#(GetSurface(sphere,1),vv)
tVertexN#(vv,1)=VertexNY#(GetSurface(sphere,1),vv)
tVertexN#(vv,2)=VertexNZ#(GetSurface(sphere,1),vv)

If gbb<2 Then DebugLog vv+" = "+VertexNX#(GetSurface(sphere,1),vv)

vv=vv+1
Wend
vv=0

If gbb=0 Then DebugLog "================================"
gbb=gbb+1

End Function

;****************
;* Rebuild Mesh *
;****************

Function rebuildmesh()

vv=0
ff=0

Local vertices=CountVertices(GetSurface(sphere,1))
Local faces=CountTriangles(GetSurface(sphere,1))

FreeEntity sphere

sphere=CreateMesh()
CreateSurface (sphere)
EntityPickMode sphere,2
EntityColor sphere,255,255,255
EntityFX sphere,4

While vv<vertices

tx#=Sine#(wobblepos#(vv))/40
ty#=Sine#(wobblepos#(vv))/40
tz#=Sine#(wobblepos#(vv))/40

;tx#=tVertexN#(vv,0) * Sine#(wobblepos#(vv))
;ty#=tVertexN#(vv,1) * Sine#(wobblepos#(vv))
;tz#=tVertexN#(vv,2) * Sine#(wobblepos#(vv))

AddVertex(GetSurface(sphere,1),verts#(vv,0)+tx#,verts#(vv,1)+ty#,verts#(vv,2)+tz#)

vv=vv+1

Wend

vv=0

While ff<faces

AddTriangle(GetSurface(sphere,1),tris#(ff,0),tris#(ff,1),tris#(ff,2))
ff=ff+1

Wend
ff=0

End Function


;****************
;* Get Tri Data *
;****************

Function gettridata()

If PickedTriangle()<65535 Then xa#=VertexX(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),0))
If PickedTriangle()<65535 Then ya#=VertexY(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),0))
If PickedTriangle()<65535 Then za#=VertexZ(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),0))

If PickedTriangle()<65535 Then xb#=VertexX(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),1))
If PickedTriangle()<65535 Then yb#=VertexY(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),1))
If PickedTriangle()<65535 Then zb#=VertexZ(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),1))

If PickedTriangle()<65535 Then xc#=VertexX(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),2))
If PickedTriangle()<65535 Then yc#=VertexY(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),2))
If PickedTriangle()<65535 Then zc#=VertexZ(GetSurface(sphere,1),TriangleVertex(GetSurface(sphere,1),PickedTriangle(),2))

If PickedTriangle()<65535 Then CameraProject(camera,xa#,ya#,za#):vxa=ProjectedX():vya=ProjectedY()
If PickedTriangle()<65535 Then CameraProject(camera,xb#,yb#,zb#):vxb=ProjectedX():vyb=ProjectedY()
If PickedTriangle()<65535 Then CameraProject(camera,xc#,yc#,zc#):vxc=ProjectedX():vyc=ProjectedY()

If PickedTriangle()<65535 Then tria=TriangleVertex(GetSurface(sphere,1),PickedTriangle(),0)
If PickedTriangle()<65535 Then trib=TriangleVertex(GetSurface(sphere,1),PickedTriangle(),1)
If PickedTriangle()<65535 Then tric=TriangleVertex(GetSurface(sphere,1),PickedTriangle(),2)

newpointx#=(xa#+xb#+xc#)/3
newpointy#=(ya#+yb#+yc#)/3
newpointz#=(za#+zb#+zc#)/3

If PickedTriangle()<65535 Then CameraProject(camera,newpointx#,newpointy#,newpointz#):nx=ProjectedX():ny=ProjectedY()

End Function


;**************
;* Print Text *
;**************

Function printtext()

Text 10,10,"Triangles = "+CountTriangles(GetSurface(sphere,1)),0,0
Text 10,25,"Vertices = "+CountVertices(GetSurface(sphere,1)),0,0
Text 10,40,"Surfaces = "+CountSurfaces(sphere),0,0

Text 10,80,"tri picked= "+PickedTriangle()
Text 10,100," tria = "+tria,0,0
Text 10,120," trib = "+trib,0,0
Text 10,140," tric = "+tric,0,0

Text 10,180,"wobblepos#(0)="+(wobblepos#(0)),0,0
Text 10,200,"wobblepos#(1)="+(wobblepos#(1)),0,0
Text 10,220,"wobblepos#(2)="+(wobblepos#(2)),0,0

Text 10,260,"tx#= "+tVertexN#(10,0),0,0

Color 255,255,0

Rect MouseX()-1,MouseY()-1,2,2

;While gg<CountVertices(GetSurface(sphere,1))
; Plot gg,100+(100*Sine#(wobblepos#(gg)))
; gg=gg+1
;Wend
;gg=0

End Function

;***************
;* Wobble Mesh *
;***************

Function wobblemesh()

gp=0

While gp<CountVertices(GetSurface(sphere,1))

wobblepos#(gp)=wobblepos#(gp)+8
If wobblepos#(gp)>360 Then wobblepos#(gp)=0
gp=gp+1

Wend

End Function


Koriolis(Posted 2003) [#2]
You have to use the VertexNormal commands to set the normal of the vertices.
Or you could just use UpdateNormals that will set all the normals of a mesh according to his geometry (note that in this case you won't have the control on how the normals are exactly set but in most cases it does exactly what you want anyway).


-=Darkheart=-(Posted 2003) [#3]
I could be totally wrong here but I think you have to use UpdateNormals after you use VertexNormal otherwise it doesn't update them.

Darkheart


big10p(Posted 2003) [#4]
I don't think so. I never use UpdateNormals after setting normals manually. Just make sure the normals you set are unified (have a length on 1).


necky(Posted 2003) [#5]
thanx guys, it was the UpdateNormals that I forgot :)