Mesh library

Could some please post an example or explain how the mesh rendering works? I obviously tried looking at the wiki but there’s almost nothing there and my attempts at piecing together the cryptic info currently on there lead to me breaking rendering completely.

well first things first you probably have to call mesh.Begin first and mesh.End last. in a format such as this.



mesh.Begin(MATERIAL_TRIANGLES,5)

local verts = mesh.VertexCount()
while verts > 0 do
(the stuff you want to do)
mesh.AdvanceVertex()
verts = verts - 1
end

mesh.End()


but basicly you must call


mesh.AdvanceVertex()

after you finish editing each vertex and after you finish editing the mesh you must call


mesh.End()

but i would like to see an appropriate guide to this library since my rough outline is probably not good enough.

The really important part is ordering your vertices so that they are facing outward and visible, rather than inward.



local temp = Vector( )

local function Vert( p, x, y, z, c )
	temp.x = p.x + x
	temp.y = p.y + y
	temp.z = p.z + z
		
	mesh.Position( temp )
	mesh.Color( 255, 255, 255, 255 )
	mesh.AdvanceVertex( )
end
	
function effect:BoxFromPoints( x, y, width )
	Vert( x, -width, width, 0 )
	Vert( x, -width, -width, 0 )
	Vert( y, -width, -width, 0 )
	Vert( y, -width, width, 0 )
	
	Vert( x, -width, -width, 0 )
	Vert( x, width, -width, 0 )
	Vert( y, width, -width, 0 )
	Vert( y, -width, -width, 0 )
		
	Vert( x, width, width, 0 )
	Vert( x, -width, width, 0 )
	Vert( y, -width, width, 0 )
	Vert( y, width, width, 0 )
		
	Vert( x, width, -width, 0 )
	Vert( x, width, width, 0 )
	Vert( y, width, width, 0 )
	Vert( y, width, -width, 0 )
end

function effect:Render( )
	local k, v
		
	render.SetMaterial( self.Mat )
		
	mesh.Begin( MATERIAL_QUADS, #self.Tree * 4 )
		for k, v in ipairs( self.Tree ) do
			self:BoxFromPoints( v.A, v.B, self.Thickness * .5 )
		end
	mesh.End( )
end

http://puu.sh/1NkWD

http://puu.sh/1NlyR

http://puu.sh/1Nmd3

You only need to get involved with Normal if you are going to be shading and TexCoord if you are going to be drawing parts of the texture instead of the entire thing ( like how they do textures for complex models ), but it is mostly straight forward otherwise.

If you are going to be drawing this inside an entity rather than an effect, you have a little more work ahead of you since you will need to use cam.PushModelMatrix and may need to override depth handling if your mesh will have translucent parts.

The best thing to do in this case is to just jump in and start playing with it - the documentation isn’t that great but it is easy to get quick results.