Code archives/3D Graphics - Mesh/Mirror Mesh

This code has been declared by its author to be Public Domain code.

Download source code

Mirror Mesh by Ross C2005
This Code/Function will take a mesh, and mirror it along the specified axis, X, Y or Z. This code will not create a copy, and will modify the actual mesh, and it's normals.

The demo code included: press UP arrow to mirror along the Y axis/direction, LEFT arrow key to mirror along the X axis/direction and Down arrow key for the Z axis.

To use the function, just simply copy it into your copy, and call it, using the parameters.

** You don't need to pass across a surface to the function. If you don't, it will mirror all vertices on the mesh. **
Graphics3D 800,600
SetBuffer BackBuffer()


Global cam = CreateCamera()
PositionEntity cam,0,0,-10

Global light = CreateLight()

Global cube = CreateCube()
PositionMesh cube,2,1,2


While Not KeyHit(1)


	If KeyHit(203) Then mirror_mesh(cube,0)
	If KeyHit(200) Then mirror_mesh(cube,1)
	If KeyHit(208) Then mirror_mesh(cube,2)


	UpdateWorld
	RenderWorld
	Flip
Wend
End



;plane to mirror along - 0 = x, 1 = y and 2 = z
Function mirror_mesh(mesh,plane,surface = 0)


	If plane < 0 Or plane > 2 Then Return 0

	If surface = 0 Then
		s_count = CountSurfaces(mesh)
		l_count = 1
	Else
		For loop = 0 To CountSurfaces(mesh)
			temp = GetSurface(mesh,loop)
			If surface = temp Then
				s_count = temp
				l_count = temp
			End If
		Next
	End If
	
	average_x# = EntityX(mesh) - (MeshWidth(mesh)/2.0)
	average_y# = EntityY(mesh) - (MeshHeight(mesh)/2.0)
	average_z# = EntityZ(mesh) - (MeshDepth(mesh)/2.0)


	If plane = 0 Then
		For sloop = l_count To s_count
			surface = GetSurface(mesh,sloop)
			For loop = 0 To CountVertices(surface) - 1

				ny# = VertexNY (surface,loop)
				nz# = VertexNZ (surface,loop)
				nx# = VertexNX (surface,loop)
				VertexCoords surface,loop, average_x - ( average_x + VertexX(surface,loop)), VertexY(surface,loop), VertexZ(surface,loop)
				VertexNormal surface,loop,nx,ny,-nz
			Next
		Next
	ElseIf plane = 1 Then
		For sloop = l_count To s_count
			surface = GetSurface(mesh,sloop)
			For loop = 0 To CountVertices(surface) - 1

				ny# = VertexNY (surface,loop)
				nz# = VertexNZ (surface,loop)
				nx# = VertexNX (surface,loop)

				VertexCoords surface,loop, VertexX(surface,loop), average_y - ( average_y + VertexY(surface,loop)), VertexZ(surface,loop)
				VertexNormal surface,loop,-nx,ny,-nz
			Next
		Next
	ElseIf plane = 2 Then
		For sloop = l_count To s_count
			surface = GetSurface(mesh,sloop)
			For loop = 0 To CountVertices(surface) - 1

				ny# = VertexNY (surface,loop)
				nz# = VertexNZ (surface,loop)
				nx# = VertexNX (surface,loop)

				VertexCoords surface,loop, VertexX(surface,loop), VertexY(surface,loop),  average_z - ( average_z + VertexZ(surface,loop))
				VertexNormal surface,loop,-nx,ny,nz
			Next
		Next
	End If

	FlipMesh mesh
	
End Function

Comments

Dock2005
What does this do that 'ScaleEntity name,1,-1,1' (etc) doesn't do? Also, I tried using it on a boned mesh, and it doesn't seem to have an effect (even when not animating).


Ross C2006
Well, i think ScaleEntity would flip the mesh inside out. And if you used flip mesh, it would mess up the normals. I think it's for the normals more than anything. Sorry, but i don't know much about Boned meshes. It was just incase anyone needed the normals adjusted as well.


Code Archives Forum