AddMesh source_mesh,dest_mesh
Parameters
|
source_mesh - handle of the mesh to copy triangles from dest_mesh - handle of the mesh the triangles are copied into |
Description
|
Copies all of a mesh's geometry into another mesh. This is how you weld a pile of small pieces into one entity. Build a wall out of twenty crates, a level out of corridor sections, a spaceship out of hull parts - then AddMesh them all into a single mesh and the renderer has one entity to draw, one bounding box to cull and one handle for your code to move around. It is the cheapest optimisation in the whole 3D toolbox, and the reason a hand-built level made of primitives can still run fast. The copy works in mesh-local coordinates, which trips people up the first time. AddMesh does not care where the source entity has been moved to with PositionEntity - it copies the raw vertex positions. To place a piece before welding it, use the mesh commands that move the vertices themselves: PositionMesh, RotateMesh and ScaleMesh. Only then does the piece land where you expect it inside the destination. Triangles are sorted into the destination's surfaces by brush. Pieces painted with the same brush all end up sharing one surface, which is exactly what you want, so keep the number of distinct paints down - two brushes means two surfaces, and every surface is another draw. The engine will start a fresh surface anyway if one would grow past 65536 vertices, because triangle indices are 16-bit. The source mesh is left completely untouched. Nothing is moved, nothing is freed, and you can add the same piece over and over to stamp out a repeating structure. When you are finished with a piece, free it yourself with FreeEntity - it is easy to leak a hundred spare cubes. Adding a mesh to itself is a runtime error. After the copy the destination's bounding box, vertex normals and collision geometry are all out of date, and the engine rebuilds them the next time they are needed - so meshes stay pickable and collidable without you doing anything. If the destination has no explicit cull box of its own but the source has one, the source's cull box comes across with the geometry. See also: CreateMesh, CopyMesh, PositionMesh, RotateMesh, PaintMesh, CountSurfaces. |
Example
; AddMesh Example ; --------------- Graphics3D 640,480,0,2 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,0,3,-16 light=CreateLight() RotateEntity light,45,45,0 ; Two paints, so we can watch how AddMesh sorts pieces into surfaces grey=CreateBrush(170,170,180) red=CreateBrush(220,70,55) ; One empty mesh will collect the whole barricade barricade=CreateMesh() ; Weld five grey crates into it For i=0 To 4 part=CreateCube() PaintMesh part,grey ; Mesh commands move the vertices themselves. AddMesh copies raw local ; coordinates, so a part's entity position would be ignored PositionMesh part,i*2.2-4.4,0,0 AddMesh part,barricade ; The source mesh is left alone - free it once it has been copied FreeEntity part Next reds=0 While Not KeyDown(1) ; Space welds one more crate on top, this one painted red If KeyHit(57) And reds<10 part=CreateCube() PaintMesh part,red PositionMesh part,(reds Mod 5)*2.2-4.4,2.2+(reds/5)*2.2,0 AddMesh part,barricade FreeEntity part reds=reds+1 EndIf ; The whole barricade is one entity now, so one turn spins every crate TurnEntity barricade,0,0.4,0 ; Arrow keys move the camera If KeyDown(200) Then MoveEntity camera,0,0,0.1 If KeyDown(208) Then MoveEntity camera,0,0,-0.1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 RenderWorld Text 0,0,"Space: weld another crate into the barricade Arrows: camera Esc: exit" Text 0,20,"AddMesh part,barricade crates welded: "+(5+reds) Text 0,40,"CountSurfaces(barricade) = "+CountSurfaces(barricade) Text 0,60,"Crates sharing a brush merge into one surface - the red ones need their own" Flip Wend End
Index