Code archives/3D Graphics - Mesh/Mesh builder

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

Download source code

Mesh builder by puki2004
Just something I knocked up while playing with the vertex stuff in Blitz3D.

Not sure how efficient it is - but it's small and cute.

Edit:
Actually, I think the code is duff - seems to me like I am creating far too many vertices. So, I'll have to look at it later and rewrite it.
; mesh builder by "puki" - 24/10/04

Graphics3D 640,480,16,2 
SetBuffer BackBuffer() 

cam = CreateCamera() 
MoveEntity cam, 0,0,-5

WireFrame 1

mesh = CreateMesh()
surface=CreateSurface(mesh)

; here we set the size of the mesh (in squares)
; both values must each be at least 1 - they do not have to be matching values
mwidth=8
mheight=6

; there will only be 4 vertices per square - 0 to 3
; we re-use the array cells repeatedly to save memory
Dim vert_no(3)

; the following code draws each section of the mesh from left to right
; then it starts a new row below the first and again works along left to right
For height=1 To mheight
	For width=1 To mwidth
			vert_no(0)=AddVertex(surface,curx,cury-1,curz)
			vert_no(1)=AddVertex(surface,curx,cury,curz)
			vert_no(2)=AddVertex(surface,curx+1,cury,curz)
			vert_no(3)=AddVertex(surface,curx+1,cury-1,curz)
		
			tri1=AddTriangle(surface,vert_no(0),vert_no(1),vert_no(2))
			tri2=AddTriangle(surface,vert_no(0),vert_no(2),vert_no(3))
			curx=curx+1
	Next
	curx=0
	cury=cury-1
Next

UpdateNormals mesh
PositionMesh mesh,-4,3,0

While Not KeyHit(1)
RenderWorld
Flip 
Wend
End

Comments

puki2004
EDIT - well, almost there - the vertices are under control - but they are still produced 4x each square - need to modify this. Other than that, the sucker is okayish.

EDIT:

Just to save my botty over creating this code archive.

Here's one I have found from 2 years ago!:

Mesh Plane by "Neo Genesis10"
http://www.blitzbasic.com/codearcs/codearcs.php?code=382

This nullifies mine.

I was going to redo mine to be more efficient, but it will probably end up like "Neo Genesis10's". I think mine is cuter - I like the fact that mine wasn't using an array in the same way.

I was never going to use vertex coords though - I was just going to modify progressive squares to use the right-hand vertices of the preceding square. Which would bring my vertex count down to optimal.


aab2004
Good Luck


puki2004
Mine' will never work without making the code bigger. I optimised it (although the above code has not been amended) and this works 100% for the rows running left to right. However, when adding a new row, the top left and top right vertices are always repeated - there is no way around it other than using an array or, perhaps, types. It can be modified to work, it is just the code will be twice the size.

Another solution is to do the mesh as a 'spread' from vertex 0,0 by processing the rows and columns together (at the same time). However, the only gain of this would be keeping the initial array small.

I'm just glad I found 'Mesh Plane' by "Neo Genesis10"
www.blitzbasic.com/codearcs/codearcs.php?code=382


Ross C2004
I don't know if this will help you or not, but you don't need to store the vertexs in an array or anything. Each vertex is given an index number anyway, and if you build the mesh using loops, you can easily find which vertex your looking for :o)


Ziltch2004
I wrote a CreateSquare function that uses less Vertices. It is in the Codes Archives. If you are interested !
It does use more code.


puki2004
I found it "Ziltch"
http://www.blitzbasic.com/codearcs/codearcs.php?code=463

; Top bit added by puki (for demonstration purposes) - 1/11/04
Graphics3D 640,480,16,2

cam=CreateCamera()
PositionEntity cam,0,0,2

; set the size of Ziltch's square, now use it as a normal entity
; so, as Ziltch said, it is scaleable.
ground=createsquare(25)

PointEntity cam,ground

While KeyHit(1)=0

TurnEntity ground,1,0,0

RenderWorld
Flip

Wend
End

;===================================================================

Function createsquare(segs#=2,parent=0)
; by ADAmor Ziltch. Oct 2002
; v2 Feb 2003
;v3  may 2003 fixed rounding bug

    mesh=CreateMesh( parent )
    surf=CreateSurface( mesh )

    l# =-.5
    b# = -.5
    tvc= 0

    ;create all the vertices first
    Repeat
      u# = l + .5
      v# = b + .5
      AddVertex surf,l,0,b,u,1-v
      tvc=tvc + 1
      l = l + 1/segs
      If l > .501 Then
        l = -.5
        b = b + 1/segs
      End If
    Until b > .5

    ;create polys
    vc# =0
    Repeat

      AddTriangle (surf,vc,vc+segs+1,vc+segs+2)
      AddTriangle (surf,vc,vc+segs+2,vc+1)

      vc = vc + 1
      tst# =  ((vc+1) /(segs+1)) -Floor ((vc+1) /(segs+1))

      If (vc > 0) And (tst=0) Then
        vc = vc + 1
      End If

    Until vc=>tvc-segs-2
    UpdateNormals mesh
    Return mesh


End Function



Code Archives Forum