Would this be overkill?

Blitz3D Forums/Blitz3D Programming/Would this be overkill?

Odds On(Posted 2004) [#1]
Would it be overkill to create types for every single entity, triangle and vertex in my level? I'd really like to be able to do it, but I'm not sure if it will take up too much memory... and before you ask, I do have valid reasons for wanting to do this (it will save a lot of needless loops)


fredborg(Posted 2004) [#2]
It's probably better to store triangles and vertices in banks.


sswift(Posted 2004) [#3]
That depends on how many entities, vertices, and triangles you have in your level.


Techlord(Posted 2004) [#4]
Chris,

Types are a nice way to package objects and build hierarcy for objects. I would only store this info if you need it readily available or to change it in game as it already stored in memory by blitz as an entity.


Odds On(Posted 2004) [#5]
I want to do it because I've created my own backface culling system using the dot product method and I need to be able to set the 'culled' Field of each vertex Type instance when looping through the triangles of the entity.

For each surface
	For each triangle
		;Cull or not?
		triangle\v0\culled = true/false
		triangle\v1\culled = true/false
		triangle\v2\culled = true/false
	Next
Next
Thats roughly what I want to do... I thought I had found a solution after starting this topic but it doesn't work because of a possible bug in Blitz3D.

I tried setting the W coordinate of the vertex to be 1.0 or 0.0 by using VertexTexCoords, but VertexW appears to return 1.0 no matter what I set it to. I tried the same thing using the V coordinate and it worked, but obviously I need that for texture coordinates.


Techlord(Posted 2004) [#6]
I want to do it because I've created my own backface culling system using the dot product method
Wow, Chris!

I'm not sure why you want to do this as Blitz3D has builtin backface culling and its most likely faster. Good Luck.


fredborg(Posted 2004) [#7]
If you aren't using vertex alpha, you could use that to store the flag.


Odds On(Posted 2004) [#8]
I'm using all the vertex properties apart from the W texture coordinate. I need to create my own backface culling method because I'm creating a custom effect on a per-vertex basis and I don't want it to affect back facing vertices.


Afrohorse(Posted 2004) [#9]
I had problems creating alot of types when I tried to adding extra information into a 256x256 map.

Basically I defined a type that had the information I needed per pixel of the map the ran though it new-ing 256x256(65536) of these types.

It seemed fine during the game, but when it came to freeing them off at the end it took ages - 10 minutes+! Not sure why, parhaps it runs through a list everytime you delete a type and re-organises the memory - but anyway, I changed it to create one type with arrays of data elements and it was fine.

So, creating too many types seems to cause a huge slow down when freeing them.


sswift(Posted 2004) [#10]
So, basically for each vertex you want to store whether is has been culled or not?

That's not the right way to do hidden face removal. You should determine the normal of the face and cull it based on that.

Consider a cube, where the normals of the vertcies have been smoothed between all faces. The normals now point out towards the corners. You are facing the front of the cube, and can only see the front face. Yet the vertices that the side faces are visible. So with the method you're using those side faces would not be culled.

To calculate the normal of a face, you take the positions of the three vertcies of that face, perform a cross product using two sides of the triangle, and then normalize the resulting vector. That's your face normal.

I'd SHOW you how to do it with my Calcualte Normals function but SOMEONE appears to have deleted it from the code archives. Or if it is there it's not in any of the graphics or algorithm categories and there's no search function. I'll re-add it and get back to you.


sswift(Posted 2004) [#11]
Okay, rather than just add the normal function tot he code arhcives, I added that, and then went ahead and deleted the uneccessary stuff for you to create a function to calculate face normals.

Here ya go:

http://www.blitzbasic.com/codearcs/codearcs.php?code=976


Odds On(Posted 2004) [#12]
Hi sswift,

I am calculating the triangle normal and not the vertex normal. That's where the problem occurs... because I'm looping through the triangles of the mesh and not the vertices all I know about the current 3 vertices are their indexes. If I then want to set properties in the vertex type I have to loop through the Type until I find a match.

To give you a basic idea of what I'm trying to do, I want to visually represent every vertex of a mesh but ignore the back facing ones. I tried doing it by using a mesh but keeping the scale constant was too difficult due to objects to the left and right appearing bigger due to perspective etc.


sswift(Posted 2004) [#13]
I thinky ou can do what you want if you take my vertex normal calculation function, also in the code archives, in the same category as the above function, and modify it so that in the triangle loop, it doesn't store the face normal, but instead acesses an array of vertices, say Dim VertexFrontFace(Surface, Index) and sets that value to true for each vertex of a triangle if that triangle is found to be front facing.

Then only those vertcies which are connected to no triangles which are front facing will be set to false.


sswift(Posted 2004) [#14]
Here, I coded a function to do that for ya.

http://www.blitzbasic.com/codearcs/codearcs.php?code=977


Odds On(Posted 2004) [#15]
That's a nice bit of code sswift, but don't mesh surface indexes start from 1?

I think I'm gonna have to scrub the whole backface culling method because I forgot to take into account that more complex meshes will have front facing polygons behind other polygons and they will show up when I don't want them to.

The best way I can think of is to simply position a pivot at each vertex, offset it a bit in the direction of the vertex normal and then do an EntityVisible check from the camera to the pivot to see if it should be rendered.


Odds On(Posted 2004) [#16]
I just tried doing that and as I expected it was far too slow even on a reasonably low polygon mesh. I think I'll probably have to use a mesh to represent the vertex.. hopefully the surface count won't make it too slow.


sswift(Posted 2004) [#17]
" That's a nice bit of code sswift, but don't mesh surface indexes start from 1?"

I knew there was a reason I started that loop from 1... I changed it cause I usualyl start loops from 0. :-)


Shambler(Posted 2004) [#18]
Why on earth would you want to backface cull in software? It's slower.


darklordz(Posted 2004) [#19]
he has he reasons if ya read the topic u might find out why...