Adding cubes to mesh with major slowdown

BlitzMax Forums/MiniB3D Module/Adding cubes to mesh with major slowdown

Hezkore(Posted 2013) [#1]
I've been trying to add cubes to a mesh by simply using an AddCube function that takes a TMesh parameter pointing to which mesh to add the cube to, sorta like how AddMesh would work but without having to have a floating dummy cube somewhere.

The problem however is that this causes MAJOR slowdown in the end after all the cubes has been added.
I'm down to an impressive 12 FPS, compared to the ~900 FPS I'd normally have using the AddMesh function... and as further shame, the function (AddCube) actually seems to be SLOWER than AddMesh per cube added!

So I was wondering if anyone could help me out with what I'm doing wrong.
I'm just trying to make a function for adding a cube to a mesh without using a dummy object, and the function should preferably also be able to specify which faces of the cube to actually generate and be textured depending on the value for that face.

This is what I made (Please forgive me, I'm a 3D newb):
Function AddCube(mesh:TMesh, x:Int, y:Int, z:Int, front:Int, back:Int, top:Int, bottom:Int, Right:Int, Left:Int)
	Local surf:TSurface = GetSurface(mesh, 0)
	If Not surf Then surf = Self.mesh.CreateSurface()
	
	surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)
	
	surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
		
	surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
	
	surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)

	surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
	
	surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
	surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
	surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)
	
	surf.VertexNormal(0,0.0,0.0,-1.0)
	surf.VertexNormal(1,0.0,0.0,-1.0)
	surf.VertexNormal(2,0.0,0.0,-1.0)
	surf.VertexNormal(3,0.0,0.0,-1.0)

	surf.VertexNormal(4,0.0,0.0,1.0)
	surf.VertexNormal(5,0.0,0.0,1.0)
	surf.VertexNormal(6,0.0,0.0,1.0)
	surf.VertexNormal(7,0.0,0.0,1.0)
	
	surf.VertexNormal(8,0.0,-1.0,0.0)
	surf.VertexNormal(9,0.0,1.0,0.0)
	surf.VertexNormal(10,0.0,1.0,0.0)
	surf.VertexNormal(11,0.0,-1.0,0.0)
			
	surf.VertexNormal(12,0.0,-1.0,0.0)
	surf.VertexNormal(13,0.0,1.0,0.0)
	surf.VertexNormal(14,0.0,1.0,0.0)
	surf.VertexNormal(15,0.0,-1.0,0.0)

	surf.VertexNormal(16,-1.0,0.0,0.0)
	surf.VertexNormal(17,-1.0,0.0,0.0)
	surf.VertexNormal(18,1.0,0.0,0.0)
	surf.VertexNormal(19,1.0,0.0,0.0)
			
	surf.VertexNormal(20,-1.0,0.0,0.0)
	surf.VertexNormal(21,-1.0,0.0,0.0)
	surf.VertexNormal(22,1.0,0.0,0.0)
	surf.VertexNormal(23,1.0,0.0,0.0)

	surf.VertexTexCoords(0,0.0,1.0)
	surf.VertexTexCoords(1,0.0,0.0)
	surf.VertexTexCoords(2,1.0,0.0)
	surf.VertexTexCoords(3,1.0,1.0)
	
	surf.VertexTexCoords(4,1.0,1.0)
	surf.VertexTexCoords(5,1.0,0.0)
	surf.VertexTexCoords(6,0.0,0.0)
	surf.VertexTexCoords(7,0.0,1.0)
	
	surf.VertexTexCoords(8,0.0,1.0)
	surf.VertexTexCoords(9,0.0,0.0)
	surf.VertexTexCoords(10,1.0,0.0)
	surf.VertexTexCoords(11,1.0,1.0)
		
	surf.VertexTexCoords(12,0.0,0.0)
	surf.VertexTexCoords(13,0.0,1.0)
	surf.VertexTexCoords(14,1.0,1.0)
	surf.VertexTexCoords(15,1.0,0.0)

	surf.VertexTexCoords(16,0.0,1.0)
	surf.VertexTexCoords(17,0.0,0.0)
	surf.VertexTexCoords(18,1.0,0.0)
	surf.VertexTexCoords(19,1.0,1.0)
			
	surf.VertexTexCoords(20,1.0,1.0)
	surf.VertexTexCoords(21,1.0,0.0)
	surf.VertexTexCoords(22,0.0,0.0)
	surf.VertexTexCoords(23,0.0,1.0)

	surf.VertexTexCoords(0,0.0,1.0,0.0,1)
	surf.VertexTexCoords(1,0.0,0.0,0.0,1)
	surf.VertexTexCoords(2,1.0,0.0,0.0,1)
	surf.VertexTexCoords(3,1.0,1.0,0.0,1)
	
	surf.VertexTexCoords(4,1.0,1.0,0.0,1)
	surf.VertexTexCoords(5,1.0,0.0,0.0,1)
	surf.VertexTexCoords(6,0.0,0.0,0.0,1)
	surf.VertexTexCoords(7,0.0,1.0,0.0,1)
	
	surf.VertexTexCoords(8,0.0,1.0,0.0,1)
	surf.VertexTexCoords(9,0.0,0.0,0.0,1)
	surf.VertexTexCoords(10,1.0,0.0,0.0,1)
	surf.VertexTexCoords(11,1.0,1.0,0.0,1)
		
	surf.VertexTexCoords(12,0.0,0.0,0.0,1)
	surf.VertexTexCoords(13,0.0,1.0,0.0,1)
	surf.VertexTexCoords(14,1.0,1.0,0.0,1)
	surf.VertexTexCoords(15,1.0,0.0,0.0,1)

	surf.VertexTexCoords(16,0.0,1.0,0.0,1)
	surf.VertexTexCoords(17,0.0,0.0,0.0,1)
	surf.VertexTexCoords(18,1.0,0.0,0.0,1)
	surf.VertexTexCoords(19,1.0,1.0,0.0,1)
			
	surf.VertexTexCoords(20,1.0,1.0,0.0,1)
	surf.VertexTexCoords(21,1.0,0.0,0.0,1)
	surf.VertexTexCoords(22,0.0,0.0,0.0,1)
	surf.VertexTexCoords(23,0.0,1.0,0.0,1)
	
	If front <> 0 Then
		surf.AddTriangle(0, 1, 2) ' front
		surf.AddTriangle(0, 2, 3)
	EndIf
	
	If back <> 0 Then
		surf.AddTriangle(6, 5, 4) ' back
		surf.AddTriangle(7, 6, 4)
	EndIf
	
	If top <> 0 Then
		surf.AddTriangle(6+8,5+8,1+8) ' top
		surf.AddTriangle(2 + 8, 6 + 8, 1 + 8)
	EndIf
	
	If bottom <> 0 Then
		surf.AddTriangle(0+8,4+8,7+8) ' bottom
		surf.AddTriangle(0 + 8, 7 + 8, 3 + 8)
	EndIf
	
	If Right <> 0 Then
		surf.AddTriangle(6+16,2+16,3+16) ' right
		surf.AddTriangle(7 + 16, 6 + 16, 3 + 16)
	EndIf
	
	If Left <> 0 Then
		surf.AddTriangle(0+16,1+16,5+16) ' left
		surf.AddTriangle(0 + 16, 5 + 16, 4 + 16)
	EndIf
End Function



Hezkore(Posted 2013) [#2]
Alright so apparently GetSurface() works with a -1 indexing which means that it should be GetSurface(mesh, 1) not 0.

And thanks to a friend I've figured out that I (of course) will need to change the TexCoords and Triangles to meet the higher vertex count.

The code below does work just fine, but it's still not quite as fast as I want. Any tips?
		Local surf:TSurface = GetSurface(mesh, 1)
		If Not surf Then surf = Self.mesh.CreateSurface()
		Local vCount:Int = surf.CountVertices()
		
		surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)
		
		surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
			
		surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
		
		surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)
	
		surf.AddVertex(-1.0 + x, -1.0 + y, 1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, 1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, 1.0 + z)
		
		surf.AddVertex(-1.0 + x, -1.0 + y, -1.0 + z)
		surf.AddVertex(-1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, 1.0 + y, -1.0 + z)
		surf.AddVertex(1.0 + x, -1.0 + y, -1.0 + z)
		
		surf.VertexNormal(vCount + 0, 0.0, 0.0, -1.0)
		surf.VertexNormal(vCount + 1, 0.0, 0.0, -1.0)
		surf.VertexNormal(vCount + 2, 0.0, 0.0, -1.0)
		surf.VertexNormal(vCount + 3, 0.0, 0.0, -1.0)
	
		surf.VertexNormal(vCount + 4, 0.0, 0.0, 1.0)
		surf.VertexNormal(vCount + 5, 0.0, 0.0, 1.0)
		surf.VertexNormal(vCount +6,0.0,0.0,1.0)
		surf.VertexNormal(vCount + 7, 0.0, 0.0, 1.0)
		
		surf.VertexNormal(vCount + 8, 0.0, -1.0, 0.0)
		surf.VertexNormal(vCount + 9, 0.0, 1.0, 0.0)
		surf.VertexNormal(vCount + 10, 0.0, 1.0, 0.0)
		surf.VertexNormal(vCount + 11, 0.0, -1.0, 0.0)
				
		surf.VertexNormal(vCount + 12, 0.0, -1.0, 0.0)
		surf.VertexNormal(vCount + 13, 0.0, 1.0, 0.0)
		surf.VertexNormal(vCount + 14, 0.0, 1.0, 0.0)
		surf.VertexNormal(vCount + 15, 0.0, -1.0, 0.0)
	
		surf.VertexNormal(vCount + 16, -1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 17, -1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 18, 1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 19, 1.0, 0.0, 0.0)
				
		surf.VertexNormal(vCount + 20, -1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 21, -1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 22, 1.0, 0.0, 0.0)
		surf.VertexNormal(vCount + 23, 1.0, 0.0, 0.0)
	
		surf.VertexTexCoords(vCount + 0, 0.0, 1.0)
		surf.VertexTexCoords(vCount + 1, 0.0, 0.0)
		surf.VertexTexCoords(vCount + 2, 1.0, 0.0)
		surf.VertexTexCoords(vCount + 3, 1.0, 1.0)
		
		surf.VertexTexCoords(vCount + 4, 1.0, 1.0)
		surf.VertexTexCoords(vCount + 5, 1.0, 0.0)
		surf.VertexTexCoords(vCount + 6, 0.0, 0.0)
		surf.VertexTexCoords(vCount + 7, 0.0, 1.0)
		
		surf.VertexTexCoords(vCount + 8, 0.0, 1.0)
		surf.VertexTexCoords(vCount +9,0.0,0.0)
		surf.VertexTexCoords(vCount + 10, 1.0, 0.0)
		surf.VertexTexCoords(vCount + 11, 1.0, 1.0)
			
		surf.VertexTexCoords(vCount + 12, 0.0, 0.0)
		surf.VertexTexCoords(vCount + 13, 0.0, 1.0)
		surf.VertexTexCoords(vCount + 14, 1.0, 1.0)
		surf.VertexTexCoords(vCount + 15, 1.0, 0.0)
	
		surf.VertexTexCoords(vCount + 16, 0.0, 1.0)
		surf.VertexTexCoords(vCount + 17, 0.0, 0.0)
		surf.VertexTexCoords(vCount + 18, 1.0, 0.0)
		surf.VertexTexCoords(vCount + 19, 1.0, 1.0)
				
		surf.VertexTexCoords(vCount + 20, 1.0, 1.0)
		surf.VertexTexCoords(vCount + 21, 1.0, 0.0)
		surf.VertexTexCoords(vCount + 22, 0.0, 0.0)
		surf.VertexTexCoords(vCount + 23, 0.0, 1.0)
	
		surf.VertexTexCoords(vCount + 0, 0.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 1, 0.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 2, 1.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 3, 1.0, 1.0, 0.0, 1)
		
		surf.VertexTexCoords(vCount + 4, 1.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 5, 1.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 6, 0.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 7, 0.0, 1.0, 0.0, 1)
		
		surf.VertexTexCoords(vCount + 8, 0.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 9, 0.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 10, 1.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 11, 1.0, 1.0, 0.0, 1)
			
		surf.VertexTexCoords(vCount + 12, 0.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 13, 0.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 14, 1.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 15, 1.0, 0.0, 0.0, 1)
	
		surf.VertexTexCoords(vCount + 16, 0.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount +17,0.0,0.0,0.0,1)
		surf.VertexTexCoords(vCount + 18, 1.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 19, 1.0, 1.0, 0.0, 1)
				
		surf.VertexTexCoords(vCount + 20, 1.0, 1.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 21, 1.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 22, 0.0, 0.0, 0.0, 1)
		surf.VertexTexCoords(vCount + 23, 0.0, 1.0, 0.0, 1)
		
		If front <> 0 Then
			surf.AddTriangle(vCount + 0, vCount + 1, vCount + 2) ' front
			surf.AddTriangle(vCount + 0, vCount + 2, vCount + 3)
		EndIf
		
		If back <> 0 Then
			surf.AddTriangle(vCount + 6, vCount + 5, vCount + 4) ' back
			surf.AddTriangle(vCount + 7, vCount + 6, vCount + 4)
		EndIf
		
		If top <> 0 Then
			surf.AddTriangle(vCount + 6 + 8, vCount + 5 + 8, vCount + 1 + 8) ' top
			surf.AddTriangle(vCount + 2 + 8, vCount + 6 + 8, vCount + 1 + 8)
		EndIf
		
		If bottom <> 0 Then
			surf.AddTriangle(vCount + 0+8,vCount + 4+8,vCount + 7+8) ' bottom
			surf.AddTriangle(vCount + 0 + 8, vCount + 7 + 8, vCount + 3 + 8)
		EndIf
		
		If Right <> 0 Then
			surf.AddTriangle(vCount + 6 + 16, vCount + 2 + 16, vCount + 3 + 16) ' right
			surf.AddTriangle(vCount + 7 + 16, vCount + 6 + 16, vCount + 3 + 16)
		EndIf
		
		If Left <> 0 Then
			surf.AddTriangle(vCount + 0 + 16, vCount + 1 + 16, vCount + 5 + 16) ' left
			surf.AddTriangle(vCount + 0 + 16, vCount + 5 + 16, vCount + 4 + 16)
		EndIf



Hezkore(Posted 2013) [#3]
This now works a lot better, but the normals don't seem to be correct and it'd be neat if the vertices I have previously created would be re-used when adding new boxes, instead of creating them above each other which results in the edge sparkles... Any help?
	Function AddCube(surf:TSurface, x:Int, y:Int, z:Int, Front:Int, Left:Int, Right:Int, Back:Int, Top:Int, Bottom:Int, TexSeg:Float = 16, TexSize:Float = 256)
		Local u:Float
		Local v:Float
		Local s:Float = TexSeg / TexSize
		Local v0:Int;Local v1:Int;Local v2:Int;Local v3:Int
		
		If Front <> 0
			Front:-1
			u = (Front Mod (TexSize / TexSeg)) * s
			v = ((Front / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x - 1, y + 1, z - 1, u + 0, v + 0)
			v1 = AddVertex (surf, x + 1, y + 1, z - 1, u + s, v + 0)
			v2 = AddVertex (surf, x + 1, y - 1, z - 1, u + s, v + s)
			v3 = AddVertex (surf, x - 1, y - 1, z - 1, u + 0, v + s)
			
			VertexNormal(surf, v0, 0, 0, -1)
			VertexNormal(surf, v1, 0, 0, -1)
			VertexNormal(surf, v2, 0, 0, -1)
			VertexNormal(surf, v3, 0, 0, -1)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
		
		If Back <> 0
			Back:-1
			u = (Back Mod (TexSize / TexSeg)) * s
			v = ((Back / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x - 1, y - 1, z + 1, u + 0, v + 0)
			v1 = AddVertex (surf, x + 1, y - 1, z + 1, u + s, v + 0)
			v2 = AddVertex (surf, x + 1, y + 1, z + 1, u + s, v + s)
			v3 = AddVertex (surf, x - 1, y + 1, z + 1, u + 0, v + s)
			
			VertexNormal(surf, v0, 0, 0, 1)
			VertexNormal(surf, v1, 0, 0, 1)
			VertexNormal(surf, v2, 0, 0, 1)
			VertexNormal(surf, v3, 0, 0, 1)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
		
		If Top <> 0
			Top:-1
			u = (Top Mod (TexSize / TexSeg)) * s
			v = ((Top / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x + 1, y + 1, z - 1, u + 0, v + 0)
			v1 = AddVertex (surf, x - 1, y + 1, z - 1, u + s, v + 0)
			v2 = AddVertex (surf, x - 1, y + 1, z + 1, u + s, v + s)
			v3 = AddVertex (surf, x + 1, y + 1, z + 1, u + 0, v + s)
			
			VertexNormal(surf, v0, 0, -1, 0)
			VertexNormal(surf, v1, 0, 1, 0)
			VertexNormal(surf, v2, 0, 1, 0)
			VertexNormal(surf, v3, 0, -1, 0)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
		
		If Bottom <> 0
			Bottom:-1
			u = (Bottom Mod (TexSize / TexSeg)) * s
			v = ((Bottom / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x + 1, y - 1, z + 1, u + 0, v + 0)
			v1 = AddVertex (surf, x - 1, y - 1, z + 1, u + s, v + 0)
			v2 = AddVertex (surf, x - 1, y - 1, z - 1, u + s, v + s)
			v3 = AddVertex (surf, x + 1, y - 1, z - 1, u + 0, v + s)
			
			VertexNormal(surf, v0, 0, -1, 0)
			VertexNormal(surf, v1, 0, 1, 0)
			VertexNormal(surf, v2, 0, 1, 0)
			VertexNormal(surf, v3, 0, -1, 0)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
		
		If Right <> 0
			Right:-1
			u = (Right Mod (TexSize / TexSeg)) * s
			v = ((Right / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x + 1, y + 1, z - 1, u + 0, v + 0)
			v1 = AddVertex (surf, x + 1, y + 1, z + 1, u + s, v + 0)
			v2 = AddVertex (surf, x + 1, y - 1, z + 1, u + s, v + s)
			v3 = AddVertex (surf, x + 1, y - 1, z - 1, u + 0, v + s)
			
			VertexNormal(surf, v0, -1, 0, 0)
			VertexNormal(surf, v1, -1, 0, 0)
			VertexNormal(surf, v2, 1, 0, 0)
			VertexNormal(surf, v3, 1, 0, 0)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
		
		If Left <> 0
			Left:-1
			u = (Left Mod (TexSize / TexSeg)) * s
			v = ((Left / Int(TexSize / TexSeg) * Int(TexSize / TexSeg)) / Int(TexSize / TexSeg)) * s
			
			v0 = AddVertex (surf, x - 1, y + 1, z + 1, u + 0, v + 0)
			v1 = AddVertex (surf, x - 1, y + 1, z - 1, u + s, v + 0)
			v2 = AddVertex (surf, x - 1, y - 1, z - 1, u + s, v + s)
			v3 = AddVertex (surf, x - 1, y - 1, z + 1, u + 0, v + s)
			
			VertexNormal(surf, v0, -1, 0, 0)
			VertexNormal(surf, v1, -1, 0, 0)
			VertexNormal(surf, v2, 1, 0, 0)
			VertexNormal(surf, v3, 1, 0, 0)
			
			AddTriangle surf, v0, v1, v2
			AddTriangle surf, v0, v2, v3
		End If
	End Function



ima747(Posted 2013) [#4]
Regarding the normals not being right, you might want to just call UpdateNormals rather than manually sorting them out... might not be as fast but it's definitely not slow so just how speed critical does it need to be?

http://www.blitzbasic.com/b3ddocs/command.php?name=UpdateNormals&ref=3d_cat

Regarding the normal stacking and the resulting edge sparkle:
When I make a cube I usually recycle my verts. I will create all the verts for the cube, and then just create the tri's using those. It can be a head scratcher sometimes getting things facing the right way some times but it results in fewer verts, which makes for a faster mesh in most instances. And since you can't remove verts from a mesh with minib3d the fewer you have the better, especially if you have vertex lighting on etc. The down side is that you can't always assign things the way you may want with re-used verts, so you need to take your specific usage into account. See this thread for some more details on possible speed benefits/drawbacks of multiple verts in the render pass

http://www.blitzbasic.com/Community/posts.php?topic=99872#1175198


Hezkore(Posted 2013) [#5]
I'm measuring milliseconds and 2-4ms is too big of a slowdown.
Sadly UpdateNormals adds several ms which makes it unusable for my situation.
It looks like only the top normal is wrong though, I'll experiment a bit with it and see if I can't solve it.

I'm a real beginner when it comes to coding 3D, so how to handle creating triangles from old verts almost seems too advanced for me even though that is what I'd really want.


ima747(Posted 2013) [#6]
It's not too hard, just getting the point order correct to get it facing the right way is the tricky bit, which is a non issue if you disable backface culling, but that obviously changes some things. trial and error will solve ordering. The trick to to keep in mind where each point is. For a cube, you only need 8 points, vs. making each face it's own quad you need 64, which is obviously a lot more verts to make, render and keep track of when you make changes.

I like to think of the cube as a box around a point. So if that point were a camera, the "front" face is made up of 4 points. the "back" face is made up of 4 more, and the sides, top and bottom are half front and half back points.

Points that make up the front face
-1, 1, 1
1, 1, 1
1,-1,1
-1,-1,1

Points that make up the back face
-1, 1, -1
1, 1, -1
1,-1,-1
-1,-1,-1

that's 8 points, 0 through 7
The front face is made up of 2 tri's made up of the following points
0,1,2
1,2,3

The tricky bit is when it comes to the sides and top
The right side is also made up of 2 tris with the following points
1, 5, 6
1, 6, 2

The first tri is the upper right corner of the front face, to the upper right corner of the back face, to the lower right corner of the back face.

I think I may have gotten the orders wrong on the tri points... but that's where you can trial and error your way through sorting out how the points need to connect to get the face right.

I find that turning off backface culling helps a lot, you can make sure you're connecting the right points, then turn backface culling back on to make sure they're facing the right way...

I've never had a creation routine that was so time sensitive I couldn't use update normals personally so I haven't really played with vertex normals in a long long time so can't really give any recommendations there.

Hope that points you in the right direction.