Question about triangle face direction

Blitz3D Forums/Blitz3D Beginners Area/Question about triangle face direction

VPellen(Posted 2005) [#1]
I've been trying for quite some time now to figure out what determines which way a triangle will face when you create it. I've run the search function a few times, and tried to figure it out myself, but no luck so far.

Whilst the manual states that the triangle will be visible if you connect the vertices to "form a clockwise pattern relative to the viewer", this makes no sense to me; For starters, the "viewer" doesn't -exist- in 3D space in any form other than the camera; And the camera position and direction does not appear to effect the direction the triangle faces.

Perhaps I'm just dense, but I'm failing to see the logic. An explanation would be greatly appreciated.

Thank you.

Edit: Never mind, I think I finally figured it out. One of those "Oh, Duh." moments. Haven't had one of those in a while. Thanks for all the help.


Beaker(Posted 2005) [#2]
If your camera is in front of the triangle (ie. can see it) then the vertices will be clockwise. If the camera is behind the triangle (ie. can't see it) then the same vertices will be counter-clockwise.

You can use the cross-product to check if the triangle can be seen from the current cameras position.


Difference(Posted 2005) [#3]
Put your triangle at 0,0,0
Put your camera at 0,0,-10

make your camera look at 0,0,0

now you are on the negative z axis looking at an xy plane.
y positive is up

this tri is cw:
(-1,0,0) - (0,1,0) - (1,0,0)

this tri is ccw:
(-1,0,0) - (0,-1,0) - (1,0,0)
Graphics3D 500,500,0,2

cam = CreateCamera()
PositionEntity cam , 0,0,-2
m=CreateMesh()

PointEntity cam,m

s = CreateSurface(m)

v0 = AddVertex(s,-1,0,0)
v1 = AddVertex(s,0,Sqr(2),0)
v2 = AddVertex(s,1,0,0)

;cw:
AddTriangle s,v0,v1,v2

;ccw:
;AddTriangle s,v0,v2,v1

While Not KeyHit(1)
	RenderWorld 
	Flip
Wend



Sledge(Posted 2005) [#4]

Whilst the manual states that the triangle will be visible if you connect the vertices to "form a clockwise pattern relative to the viewer", this makes no sense to me; For starters, the "viewer" doesn't -exist- in 3D space in any form other than the camera; And the camera position and direction does not appear to effect the direction the triangle faces.



Defining the position of verts and the order they are joined to form triangles is purely conceptual and relative to the local origin of their object, the camera has nothing to do with that process whatsoever. However, when you eventually *do* have a camera, which triangles get rendered will depend on the direction they face relative to that camera, and the direction they face will be dependant on the position and orientation of the object in question.

So, the manual's comments are there to explain the rendering rules you must keep in mind when describing your objects. For example, the triangles on the opposing sides of a cube must be described in opposite directions for them both to face outwards (the "front" ones clockwise, the "rear" ones counterclockwise).


Shambler(Posted 2005) [#5]
Even though no viewer or camera exists yet we need a way of telling Blitz which side of the triangle will be visible.

The order clockwise/anticlockwise of the vertices is important because the order changes depending on whether you are looking at the front or back of the triangle.

You decide which is the front by imagining yourself standing in front of the triangle and the telling Blitz the vertices in a clockwise order 'From your imaginary viewpoint'.

Do it right and when the camera is created and placed at the position you were imagining then it will see the triangle too and the triangle will be drawn on the screen.


VPellen(Posted 2005) [#6]
Defining the position of verts and the order they are joined to form triangles is purely conceptual and relative to the local origin of their object, the camera has nothing to do with that process whatsoever.


Ahh, now this comes closer to answering my question. What I really want to know is not a how, but a why. I'm looking for the logic behind what defines which way the triangle faces.

How does blitz read the order in which the triangle is created, and use that to define which way the triangle faces?

That may have been confusing. I think my original post was too vague on what exactly I need to know; I think this post may be a bit confusing too. I'll try and work out how to word it better. (or better yet, solve the problem myself.)


Shambler(Posted 2005) [#7]

How does blitz read the order in which the triangle is created, and use that to define which way the triangle faces?



Well, when you do an AddTriangle (surface,v0,v1,v2) Blitz assumes that v0-v1-v2 are in clockwise order.

Why is this done?...
If you have an object made of triangles ( take a classic tea pot shape for example ) it is obvious to you and me which direction each triangle should face ( after all it is a solid object so should look like one ) but a programming language needs some way of being told which way each individual triangle is facing.

Of course apart from making sure an object is drawn correctly, knowing which way a triangle is facing also allows the renderer to throw away triangles facing away from the camera and so speed up rendering greatly.

For an in depth explanation you could look at winding order...thats the technical term for which way a triangles vertices are ordered.


Difference(Posted 2005) [#8]
Ok, the REAL answer is:

You see the triangle when its normal vector is facing towards the camera.


Difference(Posted 2005) [#9]
...unless you use EntityFX 16, in witch case both sides are visible...


Sledge(Posted 2005) [#10]

Well, when you do an AddTriangle (surface,v0,v1,v2) Blitz assumes that v0-v1-v2 are in clockwise order.



No it doesn't. The order will be clockwise or counterclockwise depending on the position of the verts and the order you declare them (within AddTriangle). For example if you have the following three verts...

v00=AddVertex(triSurface,-1,-1,0)
v01=AddVertex(triSurface, 0, 1,0)
v02=AddVertex(triSurface, 1,-1,0)

...then v00 will be at the bottom left, v01 in the middle at the top and v02 at the bottom right.

AddTriangle(triSurface,v00,v01,v02) would descibe it in a clockwise fashion; AddTriangle(triSurface,v02,v01,v00) counterckockwise. In one instance it would face "outwards", the other "inwards".

Observe:



PowerPC603(Posted 2005) [#11]
I created a 3D maze program, and I created the maze entirely with Blitz's command set (I've even set the UV's internally).

First I drew the vertices, triangles and walls on paper and I decided from which point they should be visible.

I drew the outlines for each kind of corridor, and the walls should be visible from the middle of the corridor.

So I placed a dot on the paper where the player would stand when he's playing the game and drew one of the walls in front of that point.
That way, I could easily see in which order I should connect the vertices, keeping in mind that they should be connected in a clockwise manner, as seen from the point where the player will stand while facing the wall.
It doesn't matter in which order you create the vertices, only the order in which you connect them together.

If you want to take a look at the entire source-code:
http://users.pandora.be/vge/downloads/Maze3DComplete.zip
Source-code comes with comments all over the place.
The creation of each kind of corridor is located in the file "Includes\CreateWallTypes.bb"


Shambler(Posted 2005) [#12]

AddTriangle(triSurface,v00,v01,v02) would descibe it in a clockwise fashion; AddTriangle(triSurface,v02,v01,v00) counterckockwise. In one instance it would face "outwards", the other "inwards".



And the reason that it changes the direction it is facing is because Blitz 'assumes' they are in clockwise direction.


Sledge(Posted 2005) [#13]
Oh, I see what you're getting at - to me that's backwards-double-talk ;) Internally, I imagine an object's tringles being either fixed as clockwise or counterclockwise relative to my static mind's eye, which is of course equal to each of those triangles counting as being clockwise from a series of variable points of view. So I guess it just depends on how someone's brain works - either of us could be misleading to the wrong ears, therefore it's good to have covered both ways of thinking.