The example in the first link is missing the vertex normals. NormalizeMesh() seems to only recalculate the normals to the correct normal vector (based on the shape of the mesh), but it won't generate the normals for you.
All you need to do is make sure that every vertex has a normal.
I'm not sure if this is how the command was intended to function, but it's the only way I can get it to work right.
The BuildMeshTangents() can also be excluded from the example unless you need the tangents for something else.
Code: Select all
EnableExplicit
Enumeration
#Mainform
EndEnumeration
Global Rx.f=0, Rz.f = 10
Global Event
InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
Parse3DScripts()
;Add mesh to a mesh (Sub Mesh)
Procedure AddMesh(Mesh, NewX.f=0 , NewY.f=0, NewZ.f=0, ScaleX.f=1, ScaleY.f=1, ScaleZ.f=1, RotateX.f=0, RotateY.f=0, RotateZ.f=0)
Protected Dim MeshData.PB_MeshVertex(0)
Protected Dim MeshDataInd.PB_MeshFace(0)
Protected ArrSize, ArrSizeInd, c, i, x.f, y.f, z.f
TransformMesh(Mesh, NewX,NewY,NewZ, ScaleX,ScaleY,ScaleZ, RotateX,RotateY,RotateZ)
GetMeshData(Mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_UVCoordinate, 0, MeshVertexCount(Mesh, 0)-1)
GetMeshData(Mesh,0, MeshDataInd(), #PB_Mesh_Face| #PB_Mesh_Color, 0, MeshIndexCount(Mesh, 0)-1)
ArrSize = ArraySize(MeshData())
For c=0 To ArrSize
x = MeshData(c)\x
y = MeshData(c)\y
z = MeshData(c)\z
MeshVertexPosition(x,y,z)
;####
; We add a normal to every vertex (the direction doesn't matter in this case), and then
; later we will use NormalizeMesh() to recalculate them to their proper directions.
;####
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
Next
ArrSizeInd = ArraySize(MeshDataInd())
For i=0 To ArrSizeInd Step 3
MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
Next
AddSubMesh(#PB_Mesh_TriangleList)
EndProcedure
OpenWindow(#Mainform,0,0, 1024, 768, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform),0,0,1024,768,0, 0, 0)
;Light & Shadow
AmbientColor(RGB(127, 127, 127))
CreateLight(#PB_Any,RGB(255, 0, 0), 5, 10, 0)
WorldShadows(#PB_Shadow_Additive)
;Camera
CreateCamera(0, 0, 0,100,100)
CameraBackColor(0, RGB(145, 182, 201))
MoveCamera(0, 0, 5, 15, #PB_Absolute)
CameraLookAt(0, 0,0,0)
;Textures & material
CreateMaterial(0, TextureID(LoadTexture(#PB_Any, "Dirt.jpg")))
CreateMaterial(1, TextureID(LoadTexture(#PB_Any, "MRAMOR6X6.jpg")))
;Create an empty mesh
CreateMesh(0, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
;Add Sub Mesh
AddMesh(CreateCube(#PB_Any, 1), 0,0,0, 10,0.1,10) ;Sub-Mesh 0
AddMesh(CreateCube(#PB_Any, 1), -5,0.5,0, 0.1,1,10) ;Sub-Mesh 1
AddMesh(CreateCube(#PB_Any, 1), 5,0.5,0, 0.1,1,10) ;Sub-Mesh 2
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,5, 0.1,1,10, 0,90,0) ;Sub-Mesh 3
AddMesh(CreateCube(#PB_Any, 1), 0,0.5,-5, 0.1,1,10, 0,90,0) ;Sub-Mesh 4
;End Create Mesh
NormalizeMesh(0)
FinishMesh(#True)
;Save your mesh if you want
;SaveMesh(0, "test.mesh")
;For each mesh, adding a material
SetMeshMaterial(0, MaterialID(0), 0)
SetMeshMaterial(0, MaterialID(1), 1)
SetMeshMaterial(0, MaterialID(1), 2)
SetMeshMaterial(0, MaterialID(1), 3)
SetMeshMaterial(0, MaterialID(1), 4)
;The sandbox is ready
CreateEntity(0, MeshID(0), #PB_Material_None)
EntityPhysicBody(0, #PB_Entity_StaticBody, 1, 1, 1)
RotateEntity(0, Rx, 0, Rz)
;Ball
CreateEntity(1, MeshID(CreateSphere(#PB_Any, 0.7)), MaterialID(1), 0, 3, 0)
EntityPhysicBody(1, #PB_Entity_SphereBody, 0.5, 0.5, 0.2)
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event = 0
If ExamineKeyboard()
If KeyboardPushed (#PB_Key_Escape)
Break
EndIf
If KeyboardPushed (#PB_Key_Right)
Rz - 0.2
EndIf
If KeyboardPushed (#PB_Key_Left)
Rz + 0.2
EndIf
If KeyboardPushed (#PB_Key_Up)
Rx - 0.2
EndIf
If KeyboardPushed (#PB_Key_Down)
Rx + 0.2
EndIf
EndIf
;BugWare
ApplyEntityImpulse(1, 0, 0, 0 )
RotateEntity(0, Rx, 0, Rz)
RenderWorld(40)
FlipBuffers()
ForEver