Drawing triangles quickly

Blitz3D Forums/Blitz3D Programming/Drawing triangles quickly

amitjf(Posted 2008) [#1]
I need to draw many triangle shapes on a texture quickly(either red or blue). can someone suggest the quickest way to do that?


Ross C(Posted 2008) [#2]
All the same size? Or different sizes? Are they supposed to be solid, or alphaed (see through)?


amitjf(Posted 2008) [#3]
different size and alphaed.
if you know how to color a selected triangle on a textured mesh/surface, in an alphaed color it would be better.
EDIT:
it not must be alphaed, i can use multiple layers.


Ross C(Posted 2008) [#4]
Well, you can use VertexColor() to alter each individual vertex.

I'm afraid i can't think of any quick way of doing what you need though...


amitjf(Posted 2008) [#5]
and slow way?
quick= not more then ~15 sec for a 2048*2048 bitmap


Ross C(Posted 2008) [#6]
Slow would be writepixelfast and readpixel fast.

What i'd do is create a triangle mesh. Entitycolor it the colour i need, and scale it the scale i need. Then, renderworld, readpixel on the triangle and copy the whole thing onto the texture. Obviously missing any black pixels, and manually writeing the alpha info at the same time.

I'd say about 0.3 secs per triangle tops. Depends on the size of course. Very large triangles will take alot more time.


amitjf(Posted 2008) [#7]
Well, I think I will need here an external .net program using gdi+. does blitz supports gdi+?


Ross C(Posted 2008) [#8]
I don't think so. Can you explain more about what your trying to do?


amitjf(Posted 2008) [#9]
I'm having a b3d textured(2048*2048) model loaded to blitz(done). Now i have the camera movin there using keys and mouse(done). now i also want that when user press a specified key all the visible polygons to the camera on the model(done) would be coloured blue and the rest would be red(not done).

The loaded model may vary.


Stevie G(Posted 2008) [#10]
In which case use vertexcolor .... you can also use the optional alpha parameter if you set entityfx to 2+32.

First unweld the mesh if you haven't already done so to ensure you can colour each individual triangle it's own color. I have a function which does this if you need it. Next use a function which calculates the triangle normal rather than the averaged shared vertex normal ( as per updatenormals ) function ..

Function MESHnormals( mesh )

	For su = 1 To CountSurfaces(mesh )
		s = GetSurface( mesh , su )

		For t = 0 To CountTriangles( s )-1
			v0 = TriangleVertex( s, t, 0 )
			v1 = TriangleVertex( s, t, 1 )
			v2 = TriangleVertex( s, t, 2 )
			ax# = VertexX( s, v1 ) - VertexX( s, v0 )
			ay# = VertexY( s, v1 ) - VertexY( s, v0 )	
			az# = VertexZ( s, v1 ) - VertexZ( s, v0 )	
			bx# = VertexX( s, v2 ) - VertexX( s, v1 )
			by# = VertexY( s, v2 ) - VertexY( s, v1 )	
			bz# = VertexZ( s, v2 ) - VertexZ( s, v1 )	
			Nx# = ( ay * bz ) - ( az * by )
			Ny# = ( az * bx ) - ( ax * bz )
			Nz# = ( ax * by ) - ( ay * bx )
			Ns# = Sqr( Nx * Nx + Ny*Ny + Nz*Nz ) 
			Nx = Nx / Ns
			Ny = Ny / Ns
			Nz = Nz / Ns
			
			For v = v0 To v2
				VertexNormal s, v, Nx, Ny, Nz 
			Next
		Next
	Next

End Function


Then use a function like so ... by passing both the mesh and the camera entities.

Function TRIScolor( Mesh , Camera )

	;set mesh to vertexcolor
	EntityFX Mesh, 2

	;get cameras direction normal
	TFormNormal 0,0,1,Camera, 0
	CamNx# = TFormedX()
	CamNy# = TFormedY()
	CamNz# = TFormedZ()

	For su = 1 To CountSurfaces( Mesh )
		s = GetSurface( Mesh , su )
		For t = 0 To CountTriangles( s ) - 1
		
			;get first vertex in triangle - normals should be same on each
			v0 = TriangleVertex( s, t, 0 )
			TriNX# = VertexNX( s, v0 )
			TriNy# = VertexNY( s, v0 )
			TriNz# = VertexNZ( s, v0 )			 
			
			;compute dot product of camera normal and tri normal
			CdotN# = CamNx*TriNx + CamNY * TriNy + CamNz * TriNz
			
			If CdotN < 0 Then
			
				;triangle facing camera - color red
				For v = v0 To v0 + 2
					VertexColor s, v, 255,0,0
				Next
			
			Else
			
				;triangle not facing camera - color white
				For v = v0 To v0 + 2
					VertexColor s, v, 255,255,255
				Next
			
			EndIf

		Next
	Next
	
End Function


Stevie


amitjf(Posted 2008) [#11]
thank you very much. somewhy the algorithm for finding the facing triangles is not fine, because... it just wrong, it colours me some wrong triangles and some wont be coloured. When using the function I refer to the camera pivot and not the camera itselfs as its parent is the pivot.
EDIT:
Wait checking again.... don't respond

EDIT:
Checked and yes it not workin right
In every place im placing the camera it shows the same. just rotating the camera change the colors


Stevie G(Posted 2008) [#12]
Oops, sorry just typed that in without proper testing .. forgot to transform the vertex normal into worldspace coords.

This works ..

Function TRIScolor( Mesh , Camera )

	;set mesh to vertexcolor
	EntityFX Mesh, 2+4

	;get cameras direction normal
	TFormNormal 0,0,1,Camera, 0
	CamNx# = TFormedX()
	CamNy# = TFormedY()
	CamNz# = TFormedZ()

	For su = 1 To CountSurfaces( Mesh )
		s = GetSurface( Mesh , su )
		For t = 0 To CountTriangles( s ) - 1
		
			;get first vertex in triangle - normals should be same on each
			v0 = TriangleVertex( s, t, 0 )
			TFormVector VertexNX( s, v0 ), VertexNY( s, v0 ) , VertexNZ( s, v0 ) , Mesh , 0
			TriNx# = TFormedX() 
			TriNy# = TFormedY() 
			TriNz# = TFormedZ() 			 
			
			;compute dot product of camera normal and tri normal
			CdotN# = CamNx*TriNx + CamNY * TriNy + CamNz * TriNz
			
			If CdotN <= .001 Then
			
				;triangle facing camera - color red
				For v = v0 To v0 + 2
					VertexColor s, v, 255,0,0
				Next
			
			Else
			
				;triangle not facing camera - color white
				For v = v0 To v0 + 2
					VertexColor s, v, 255,255,255
				Next
			
			EndIf

		Next
	Next
	
End Function



amitjf(Posted 2008) [#13]
Not working yet. only the rotation of the camera change the colors. I need that when i am on a specified place it would color different the model than on another position.
EDIT:
I thought about it and I may didn't explained myself well. i need to detect if i pass a line from the triangle to the camera, would be there any other triangles in the way.


Stevie G(Posted 2008) [#14]
If there are other triangles in the way then the triangle is probably hidden from view so you wouldn't be able to see the colour?

Sorry but you're still not making perfect sense? Tell us what the purpose of this function would be in game.


amitjf(Posted 2008) [#15]
Ok sorry. I have 2 cameras. one above the model(top view) and one is the character. when running this function, a line would be passed to every single triangle and would check whether there are triangles in the way or not. if there aren't, the camera can see the triangle and it would be colored red. if there are some in the way, the camera cant see it, and the triangle would colored blue or white and some of them might be visible for the camera of the top view.

EDIT:
It just like EntityVisible, but for Entity-Triangle instead of Entity-Entity mode.


Stevie G(Posted 2008) [#16]
Well that's a fair bit different from what you originally posted!!

Ok, I can't think of a faster solution atm. This will likely be slow due to the number of linepicks required.

......

* Make the level geometry pickable : entitypickmode 2.
* Set each mesh to vertexcolor : EntityFX Mesh, 2
* In the function below, the 'Viewer' is whatever entity you want the triangles to be coloured in relation to. It can be a camera, entity, pivot etc..



If it still doesn't work for you then you will need to either send or post me some code.

Stevie


amitjf(Posted 2008) [#17]
"now i also want that when user press a specified key all the visible polygons to the camera on the model..." but nvm. Thank you very much for your help, most of the triangles are colored correct, some aren't maybe if ill change the model it would be better.


Stevie G(Posted 2008) [#18]
Your welcome. Try playing about with the .000001 threshold for determining whether a poly is facing the camera.

Alternatively, post your code.


Axel Wheeler(Posted 2008) [#19]
This is probably several days late and dollars short, but:

Why not put a bright blue spotlight at the camera position? Depending on your needs this might suit and would be fast and easy, although:

- It would also light up other objects (but not the player's ship itself if you do it right)

- It would not be a pure blue color, but would alter the base texture color according to distance and angle.

If you can tolerate those factors, it might simplify things.