where is Mesh Triangle number 0 or 100 ??
Posted: Wed Jul 01, 2015 4:23 pm
it is confusing to know where is triangle number 0 or 100 or to get a List of all triangles vertices coordinates in the mesh. so look this example, it uses a custom plane from MeshManualFlag.pb (because it has many squares)
a sphere is positioned in the middle of triangle 105, and every space key press will move it to the next triangle by calling procedure triangle(num). if you press D then that triangle will go deep a little (like a hole) , W will toggle wire/solid frame.
haven't thought yet of using a mouse to choose specific triangle
but what about a known mesh such as robot or sphere or cylinder ? the solution is : first make a copy from it over a new created dynamic mesh, and then color triangle number 0 (the color will spread to a nearby triangle since the vertices are shared)
the amusing fact is if we use EntityLookAt, in the case of a cube the triangle 0 will be on the rear side of the cube looking at some point. but this is not a rule for every mesh, the triangle 0 in the robot is in his right hand (you need to move the camera away to see the robot) this is determined initially by the designer.
a sphere is positioned in the middle of triangle 105, and every space key press will move it to the next triangle by calling procedure triangle(num). if you press D then that triangle will go deep a little (like a hole) , W will toggle wire/solid frame.
haven't thought yet of using a mouse to choose specific triangle
Code: Select all
Enumeration
#mesh
#cube
#sphere
EndEnumeration
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
#CameraSpeed = 0.1
#NbX=10
#NbZ=10
Define.f KeyX, KeyY, MouseX, MouseY
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Declare CreatePlane2()
Declare triangle(num.l)
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "Space : to move the ball to the next triangle ...W : wire/solid frame,....D: toggle deform ... arrow keys+mouse for cam ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-10, 0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Parse3DScripts()
Global monitor=CreateSprite(#PB_Any,120,40)
;-Material
CreateMaterial(1, LoadTexture(1, "Geebee2.bmp"))
DisableMaterialLighting(1, 1)
MaterialCullingMode(1, 1)
SetMaterialColor(1, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))
CreateMaterial(0, LoadTexture(0, "White.jpg"))
MaterialShadingMode(0, #PB_Material_Wireframe)
MaterialCullingMode(0, #True)
DisableMaterialLighting(0, #True)
;CreateMaterial(6, LoadTexture(6, "MRAMOR6X6.jpg"))
CreateMaterial(6, LoadTexture(6, "MRAMOR6X6.jpg"))
MaterialShadingMode(6, #PB_Material_Wireframe)
MaterialCullingMode(6, #True)
DisableMaterialLighting(6, #True)
CreateSphere(#sphere, 0.1)
CreateEntity(#sphere, MeshID(#sphere), MaterialID(1) ,0,2,0)
;-Mesh ; draw the plane with many triangles
CreatePlane2()
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,0,10,10, #PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(0,0,0))
;-Light
CreateLight(0, RGB(255, 255, 255), 0, 2, 30)
AmbientColor(RGB(255, 255, 255))
GetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent , 0, MeshVertexCount(#Mesh)-1)
GetMeshData(#mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, 0)-1)
Global tri=105
triangle(tri)
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
UpdateMeshBoundingBox(#mesh)
Global deform = 0
Repeat
WindowEvent()
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_W)
If wireFrame
MaterialShadingMode(6, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(6, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
;00000000000000000000000000000000000000000000000000000000000000000000000000000
If KeyboardReleased(#PB_Key_D)
deform ! 1
EndIf
;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
If KeyboardReleased(#PB_Key_Space)
tri+1
If tri*3 > MeshIndexCount(#mesh)-1
tri=0
EndIf
triangle(tri)
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
EndIf
If ExamineMouse()
MouseX = -(MouseDeltaX()/10)
MouseY = -(MouseDeltaY()/10)
EndIf
MoveCamera (0, KeyX, 0, KeyY)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
RenderWorld()
StartDrawing(SpriteOutput(monitor))
DrawText(5,5,"the ball in Triangle " )
DrawText(5,20,"Triangle= " +" ")
DrawText(5,20,"Triangle= " +Str(tri))
StopDrawing()
DisplaySprite(monitor,0,0)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
End
Procedure CreatePlane2()
CreateMesh(#mesh, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
Protected.l a, b, c, Nb
Protected.w P1, P2, P3, P4
For b=0 To #Nbz
For a=0 To #NbX
MeshVertexPosition(a - #NbX/2, y, b - #Nbz/2)
MeshVertexNormal(0,1,0)
MeshVertexColor(RGB(255,255,255))
MeshVertexTextureCoordinate(a/#NbX, b/#Nbz)
Next a
Next b
Nb=#NbX+1
For b=0 To #NbZ-1
For a=0 To #NbX-1
P1=a+(b*Nb)
P2=P1+1
P3=a+(b+1)*Nb
P4=P3+1
MeshFace(P3, P2, P1)
MeshFace(P2, P3, P4)
Next
Next
FinishMesh(#True)
UpdateMeshBoundingBox(#mesh)
SetMeshMaterial(#mesh, MaterialID(6))
CreateEntity(#mesh, MeshID(#mesh), #PB_Material_None)
GetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh, 0)-1)
GetMeshData(#mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, 0)-1)
EndProcedure
Procedure triangle(num.l)
i = 3*num
x1.f = MeshData(MeshDataInd(i)\Index)\x
y1.f = MeshData(MeshDataInd(i)\Index)\y
z1.f = MeshData(MeshDataInd(i)\Index)\z
;MeshData(MeshDataInd(i)\Index)\Color = RGB(0,0,255)
x2.f = MeshData(MeshDataInd(i+1)\Index)\x
y2.f = MeshData(MeshDataInd(i+1)\Index)\y
z2.f = MeshData(MeshDataInd(i+1)\Index)\z
;MeshData(MeshDataInd(i+1)\Index)\Color = RGB(255,0,0)
x3.f = MeshData(MeshDataInd(i+2)\Index)\x
y3.f = MeshData(MeshDataInd(i+2)\Index)\y
z3.f = MeshData(MeshDataInd(i+2)\Index)\z
If deform ; to deform the triangle vertices or not
MeshData(MeshDataInd(i)\Index)\y - 0.3
MeshData(MeshDataInd(i+1)\Index)\y - 0.3
MeshData(MeshDataInd(i+2)\Index)\y - 0.3
EndIf
;MeshData(MeshDataInd(i+2)\Index)\Color = RGB(0,255,0)
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
; position a sphere at the center of the triangle
MoveEntity(#sphere, (x1+x2+x3)/3,(y1+y2+y3)/3,(z1+z2+z3)/3, #PB_Absolute)
EndProcedure
the amusing fact is if we use EntityLookAt, in the case of a cube the triangle 0 will be on the rear side of the cube looking at some point. but this is not a rule for every mesh, the triangle 0 in the robot is in his right hand (you need to move the camera away to see the robot) this is determined initially by the designer.
Code: Select all
Enumeration
#mesh
#cube
#cubeTemp
#sphere
EndEnumeration
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
#CameraSpeed = 0.1
Define.f KeyX, KeyY, MouseX, MouseY
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Global v.l
If InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "press W wire/Solid frame, .... arrow keys+mouse for the camera ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0)-10, 0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Models", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Parse3DScripts()
;-Material
CreateMaterial(1, LoadTexture(1, "Wood.jpg"))
DisableMaterialLighting(1, 1)
MaterialCullingMode(1, 1)
MaterialShadingMode(1, #PB_Material_Wireframe)
SetMaterialColor(1, #PB_Material_SelfIlluminationColor, RGB(255, 255, 255))
CreateMaterial(0, LoadTexture(0, "White.jpg"))
;MaterialShadingMode(0, #PB_Material_Wireframe)
MaterialCullingMode(0, #True)
DisableMaterialLighting(0, #True)
CreateMaterial(6, LoadTexture(6, "ground_diffuse.png"))
;-Camera
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0,-6,4,4, #PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(0,0,0))
;-Light
CreateLight(0, RGB(255, 255, 255), 5, 15, 5)
AmbientColor(RGB(255, 255, 255))
CreateCube(#cubeTemp, 1) ; the wanted mesh , you can also use any of the following
;LoadMesh(#cubeTemp, "robot.mesh") ; note this mesh is very big
;CreateSphere(#cubeTemp, 1)
;CreateCylinder(#cubeTemp, 0.4, 3)
;TransformMesh(#cubeTemp, 0,0,0, 0.5 ,0.5, 0.5, 90,0,0) ; rotate the mesh 90 degree for the cylinder to appear vertical on the surface
;UpdateMeshBoundingBox(#cubeTemp)
CreateMesh(#cube, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic); this is what we want to copy the mesh to
GetMeshData(#cubeTemp,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#cubeTemp, 0)-1)
GetMeshData(#cubeTemp,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#cubeTemp, 0)-1)
ArrSize = ArraySize(MeshData())
;Debug ArrSize
; drawing the vertices one by one using the info from the MeshData
For c=0 To ArrSize
x.f = MeshData(c)\x
y.f = MeshData(c)\y
z.f = MeshData(c)\z
MeshVertexPosition(x,y,z)
MeshVertexColor(RGB(255,255,255))
MeshVertexTextureCoordinate(MeshData(c)\u, MeshData(c)\v)
Next
ArrSizeInd = ArraySize(MeshDataInd())
For i=0 To ArrSizeInd Step 3
;constructing the triangles like the original mesh
MeshFace(MeshDataInd(i)\Index, MeshDataInd(i+1)\Index,MeshDataInd(i+2)\Index)
Next
FinishMesh(#True)
CreateEntity(#cube, MeshID(#cube), MaterialID(0), 0 ,1 ,0 )
GetMeshData(#cube,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#cube, 0)-1)
GetMeshData(#cube,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#cube, 0)-1)
;Debug MeshIndexCount(#cube); = 36
;change the color of the first triangle in the mesh
ind=0
MeshData(MeshDataInd(ind)\Index)\Color = RGB(255,0,0)
MeshData(MeshDataInd(ind+1)\Index)\Color = RGB(0,255,0)
MeshData(MeshDataInd(ind+2)\Index)\Color = RGB(0,0,255)
SetMeshData(#cube,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_Tangent, 0, MeshVertexCount(#cube)-1)
; extra amusements
EntityFixedYawAxis(#cube, #True, 0,1,0)
EntityLookAt(#cube, 0,0,0)
wireFrame = 1
Repeat
WindowEvent()
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_W)
If wireFrame
MaterialShadingMode(0, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(0, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
;;oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
If KeyboardReleased(#PB_Key_V)
MeshData(v)\y + 0.3:v+1
If v=MeshVertexCount(#mesh, 0):v=0:EndIf
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Normal, 0, MeshVertexCount(#mesh, 0)-1)
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
EndIf
If ExamineMouse()
MouseX = -(MouseDeltaX()/10)
MouseY = -(MouseDeltaY()/10)
EndIf
MoveCamera (0, KeyX, 0, KeyY)
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
;RotateEntity(#cube, 0,1,0, #PB_Relative)
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
Else
MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf
End