3d Wire Grid (Help Improvements)

Blitz3D Forums/Blitz3D Programming/3d Wire Grid (Help Improvements)

Inner(Posted 2004) [#1]
Function createline(x1#,y1#,z1#, x2#,y2#,z2#, mesh=0)
	
	If mesh = 0 Then 
		mesh=CreateMesh()
		EntityFX(mesh,16)
		surf=CreateSurface(mesh)	
		verts = 0	
	
		AddVertex surf,x1#,y1#,z1#,0,0
	Else
		surf = GetSurface(mesh,1)
		verts = CountVertices(surf)-1
	End If
	
	AddVertex surf,(x1#+x2#)/2,(y1#+y2#)/2,(z1#+z2#)/2,0,0 
	; you could skip creating the above vertex and change the line below to
	;AddTriangle surf,verts,verts+1,verts+0
	; so your line mesh would use less vertices, the drawback is that some videocards (like the matrox g400)
	; aren't able to create a triangle with 2 vertices. so, it's your call :)
	AddVertex surf,x2#,y2#,z2#,1,0
	
	AddTriangle surf,verts,verts+2,verts+1
	
	Return mesh
End Function

; --- set graphics
Graphics3D 640,480,32,0
SetBuffer(BackBuffer())

AntiAlias False

; --- create scene setup
camPiv = CreatePivot()
camera = CreateCamera(camPiv)
CameraClsColor camera,128,128,128
PositionEntity(camera, 0,0,-10)

light=CreateLight(2) 
PositionEntity(light,4,10,0) 
LightRange(light,10)

; --- create lines

lines = createLine(-1+x#,1+x#,1,    1+x#,1+x#,1)
lines = createLine(1+x#,1+x#,1,    1+x#,-1+x#,1, lines)
lines = createLine(1+x#,-1+x#,1,    -1+x#,-1+x#,1, lines)
lines = createLine(-1+x#,-1+x#,1,    -1+x#,1+x#,1, lines)
EntityColor(lines, 0,0,0)
HideEntity lines
gsize=24


x=-gsize
y=-gsize

Repeat 
	l=CopyEntity(lines)
	PositionEntity l,x,0,y
	RotateEntity l,90,0,0
	x=x+2
	If x=>gsize Then x=-gsize : y=y+2
Until y=>gsize

CreateSphere()

;TurnEntity(campiv, 35,35,35)

While Not KeyHit(1)

	; --- camera controls
	scrollwheel = MouseZSpeed()
	If MouseDown(1) Then 
		TurnEntity(camPiv, MouseYSpeed(),-MouseXSpeed(),0)
	Else If scrollwheel <> 0 Then 
		MoveEntity(camera, 0,0,scrollwheel*3)
	Else
		dummy = MouseYSpeed():dummy = MouseXSpeed():dummy = MouseZSpeed() ; prevent mousespeed blips.
	End If

	; --- rendering
	CameraClsMode(camera, 1, 1)
	WireFrame(0)
	RenderWorld()
	
	CameraClsMode(camera, 0, 0)
	WireFrame(1)
	RenderWorld()

	Flip()
Wend

End


Can anyone improve on this?, it seams a little over kill.

While this works, I'd much rather the grid be infinit in x,y & z rather than just an area, anyone have any idea how ?


Rob(Posted 2004) [#2]
There's a much better way done by kokejbaby or someone - can't remember his nick.

It was the fastest/best way to do wireframes though!

Beaks and Fredborg will also probably know which one I'm talking about. It uses mesh tricks.


Inner(Posted 2004) [#3]
Cool thanks, I shall await there reply. :D


big10p(Posted 2004) [#4]
Here's the 3D line function I use, based on the code by that guy BloodLocust mentions. :) Did someone come up with an even better method, then?

;
; Adds a 3D line to the specified mesh.
; Note: 3D lines are only properly visible when rendered in wireframe mode!
; 
; Params:
; mesh     - Mesh to add 3D line to. If 0, a new mesh is created.
; x0,y0,z0 - Start point of line.
; x1,y2,z1 - End point of line.
; r,g,b    - Line colour.
;
; Returns:
; Handle of mesh the 3D line was added to.
;
Function create_3D_line(mesh,x0#,y0#,z0#,x1#,y1#,z1#,r%=255,g%=255,b%=255) 

	If mesh = 0 
		mesh = CreateMesh() 
		surf = CreateSurface(mesh) 
		EntityFX mesh,1+2+16
	Else 
		last_surf = CountSurfaces(mesh)
		surf = GetSurface(mesh,last_surf)
		If CountVertices(surf) > 30000 Then surf = CreateSurface(mesh)
	End If 

	v0 = AddVertex(surf,x0,y0,z0) 
	v1 = AddVertex(surf,x1,y1,z1)  
	v2 = AddVertex(surf,x0,y0,z0)  
	AddTriangle surf,v0,v1,v2
	
	VertexColor surf,v0,r,g,b
	VertexColor surf,v1,r,g,b
	VertexColor surf,v2,r,g,b

	Return mesh 

End Function


It's similar to yours, Inner, but instead of putting a vertex in the middle of the line, it simply puts 2 verts at one end and 1 vert at the other. The reason for this is so lines can have individual colours (using vertex colouring) and, more importantly, the lines are better quality (putting a vert in the middle tends to make some lines look too thick in places).

As for infinite lines, just make them massively long.


Inner(Posted 2004) [#5]
Thanks that works a lot better, however making them massively long like 3000 for example makes the lines go screwy.


koekjesbaby(Posted 2004) [#6]
the vertex in the middle is for old video card compatibility; some old card won't render the 'triangle' if one of its three corners is made from the same vertex.


big10p(Posted 2004) [#7]
I'm using 3 separate verts for each 'line', just that 2 have the same coords. Will this still cause probs on older cards, then?


koekjesbaby(Posted 2004) [#8]
i can't remember, i personally don't have an old card here. when i use my lines i actually never use the 3 vertex variant.

by the way, this is my grid function:
Function createGrid(w#,d#, dw#,dd#, r=212,g=212,b=212, centerR=227, centerG=227, centerB=227)
	grid = CreatePivot()
	PositionEntity(grid, w#/2, 0,d#/2)
	
	hx = (w#/dw#)/2 + 1
	For x# = 0 To w#/dw#
		x_ = x_ + 1
		l = createLine(x*dw#,0,0, x*dw#,0,d)
		EntityFX(l, 16+1+8)
		If x_ = hx
			EntityColor(l, centerR,centerG,centerB)
		Else
			EntityColor(l, r,g,b)
		EndIf
		EntityParent(l, grid)
	Next
	
	hz = (d#/dd#)/2 + 1
	For z# = 0 To d#/dd#
		z_ = z_ + 1
		l = createLine(0,0,z*dd#, w,0,z*dd#)
		EntityFX(l, 16+1+8)
		If z_ = hz
			EntityColor(l, centerR,centerG,centerB)
		Else
			EntityColor(l, r,g,b)
		EndIf
		
		EntityParent(l, grid)
	Next

	PositionEntity(grid, 0,0,0)
	
	Return grid
End Function

not brilliant, but it works :)


Beaker(Posted 2004) [#9]
On some (older cards - I had one) the card culls tris that have verts in the same place.


big10p(Posted 2004) [#10]
Works OK on my GeForce 256. How much older than this are you talking?


koekjesbaby(Posted 2004) [#11]
hehehehe, we're talking stoneage here. i beleive the card in question was a matrox g400, but i could be wrong.