Flip Mesh

Blitz3D Forums/Blitz3D Programming/Flip Mesh

Grovesy(Posted 2005) [#1]
I have mesh pieces that i use to create my levels, and can obviously rotate them, but is it possible to flip meshes?

I have tried scaling them with a negative value but parts of the model disappear!

Any ideas?


big10p(Posted 2005) [#2]
The FlipMesh command any help? :)


Grovesy(Posted 2005) [#3]
No and yes,

Flip mesh on its own does a similar thing as the scale where bits disappear, but if I then also scaleEntity (model, -1,1,1) it works.

Any idea why some of the textures (like the light coloured ones) are now darker?


jhocking(Posted 2005) [#4]
Do you mean "flip" or "mirror?" The former refers to making all the polygon normals reverse direction, whereas the latter means to make the object look like a mirror-image of itself.


Grovesy(Posted 2005) [#5]
Not sure which i mean lol. For example, i have a model that slops to the right. I want to flip or mirror it so that it slops to the left!

Cheers


jhocking(Posted 2005) [#6]
Ah, so you want to mirror the mesh.


Grovesy(Posted 2005) [#7]
Errr, yeah sure!! lol

So how do I do that?

Cheers ;-)


Scherererer(Posted 2005) [#8]
you just did. when you scale an entity/mesh with a negative value it mirrors it along the axis that you put a negative argument in.


Grovesy(Posted 2005) [#9]
ok. But any idea why the light textures of the model are now darker?


KuRiX(Posted 2005) [#10]
It could be a problem with the normals? Not sure, but try the updatenormals command...


Grovesy(Posted 2005) [#11]
Nope its still dark. I also tried using BotBuilder's Calculate_Normals, but still not luck.

Any other ideas?


Ross C(Posted 2005) [#12]
What the command is doing, is pointing the normals in the opposite direction. The normals of the vertices, are uses to calculate, how the lights affect the polygons. by flipping the mesh, or negative scaling the mesh, you are, flipping also the normals.

If you can, it wouldn't be hard to invert/mirror the mesh, using code. Find the centre point. IE. if you want to mirror it along the X Z plane, then uses the Y centre point. Then loop through all the verts, and alter their positions accordingly.


Ross C(Posted 2005) [#13]
Ok, i knocked this up. It should do what you want it to. I've not tried it on any complex meshes, but it works on primates. All you need to do, is copy the function into your code, and call it. You must pass across a mesh and a plane you wish to mirror along. 0 = x, 1 = y and 2 = z.

Also, if you have a surface you wish to mirror, you can pass that across. If not, omit it, and the function will mirror all surfaces.

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




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




Grovesy(Posted 2005) [#14]
Cheers for that Ross. I'll have a look at it tonight :)