Help with subdivide function...

Blitz3D Forums/Blitz3D Programming/Help with subdivide function...

JA2(Posted 2007) [#1]
I'm working on a subdivide function that comes along with one of the examples with b3d.

Does anybody know how to fix the function so that both sides of my cube look the same (like the left side)?

Press space to divide. Any help much appreciated :)

Graphics3D 650,480,15,2
SetBuffer BackBuffer()

cam=CreateCamera()
light=CreateLight()
cube=CreateCube()
MoveEntity cam,0,0,-5
PointEntity cam,cube

WireFrame True
While Not KeyDown (1)
Cls

If KeyHit (57) Then cube = Divide (cube)

UpdateWorld
RenderWorld

Flip
Wend
End

Function Divide (Mesh)

Local NewMesh,Surface,NewSurface,Count,CountB,A,X1#,Y1#,Z1#,X2#,Y2#,Z2#,X3#,Y3#,Z3#,X4#,Y4#,Z4#,X5#,Y5#,Z5#,X6#,Y6#,Z6#

NewMesh		= CreateMesh()
Surface		= CreateSurface (NewMesh)
NewSurface	= GetSurface (Mesh,CountSurfaces(Mesh))
Count		= CountTriangles (NewSurface)

For A = 0 To Count-1

	X1# = VertexX (NewSurface,TriangleVertex(NewSurface,A,0))
	Y1# = VertexY (NewSurface,TriangleVertex(NewSurface,A,0))
	Z1# = VertexZ (NewSurface,TriangleVertex(NewSurface,A,0))
	X2# = VertexX (NewSurface,TriangleVertex(NewSurface,A,1))
	Y2# = VertexY (NewSurface,TriangleVertex(NewSurface,A,1))
	Z2# = VertexZ (NewSurface,TriangleVertex(NewSurface,A,1))
	X3# = VertexX (NewSurface,TriangleVertex(NewSurface,A,2))
	Y3# = VertexY (NewSurface,TriangleVertex(NewSurface,A,2))
	Z3# = VertexZ (NewSurface,TriangleVertex(NewSurface,A,2))

	X4# = (X1+X2)/2
	Y4# = (Y1+Y2)/2
	Z4# = (Z1+Z2)/2
	X5# = (X2+X3)/2
	Y5# = (Y2+Y3)/2
	Z5# = (Z2+Z3)/2
	X6# = (X3+X1)/2
	Y6# = (Y3+Y1)/2
	Z6# = (Z3+Z1)/2

	AddVertex (Surface,X1,Y1,Z1)
	AddVertex (Surface,X2,Y2,Z2)
	AddVertex (Surface,X3,Y3,Z3)
	AddVertex (Surface,X4,Y4,Z4)
	AddVertex (Surface,X5,Y5,Z5)
	AddVertex (Surface,X6,Y6,Z6)

	AddTriangle (Surface,CountB+0,CountB+3,CountB+5)
	AddTriangle (Surface,CountB+1,CountB+4,CountB+3)
	AddTriangle (Surface,CountB+2,CountB+5,CountB+3)
	AddTriangle (Surface,CountB+3,CountB+4,CountB+2)

	CountB = CountB + 6

Next

UpdateNormals NewMesh
FreeEntity Mesh
Return NewMesh

End Function



jfk EO-11110(Posted 2007) [#2]
As far as I see: The easiest way would be to use a loaded cube mesh with parallel tirangle edges, instead of the (correctly) flipped ones of CreateCube.


DJWoodgate(Posted 2007) [#3]
I think you are using the longest edge as the basis for the subdivision? This finds the longest edge and then creates the verts and triangles accordingly. Not tested much but it seems to work here.

Might be an idea to add a check on the number of triangles and verts. It exceeds surface limits after a few subdivisions. Also still need to make it generate UVs.



Edit. Fixed a bug which was not obvious with the default cube.


DJWoodgate(Posted 2007) [#4]
Having done that I have just remembered that big10p did something very similar to this for his exploding mesh demo so that is definately worth a look.

http://www.blitzbasic.com/codearcs/codearcs.php?code=680

To create a segmented cube from scratch have a look at Birdies Function. It also creates different surfaces for each face.

http://www.blitzbasic.com/codearcs/codearcs.php?code=116


JA2(Posted 2007) [#5]
That's perfect. Thanks very much :)