Code archives/3D Graphics - Mesh/TriSteepness
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This is a method of calculating the steepness (gradient) of a triangle by finding the slope of the edges of the triangle and returning the steepest one. It is substantially faster than a more complex method which I was using for a while that was posted in these archives. This takes about 0 - 1 milliseconds compared to 30 - 35 milliseconds using the more complex function. Of course, for the speed, some accuracy is sacrificed, but it is usually very accurate and the most I have seen it vary from the results of the more complex version is by about 0.07. Note: If the triangle is vertical this function will not work. | |||||
Function TriSteepness#(surface,index) vert0 = TriangleVertex(surface,index,0) vert1 = TriangleVertex(surface,index,1) vert2 = TriangleVertex(surface,index,2) vert0to1# = VertSteepness#(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,1)) vert0to2# = VertSteepness(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,2)) vert1to2# = VertSteepness#(surface,TriangleVertex(surface,index,1),surface,TriangleVertex(surface,index,2)) If vert0to1# => vert0to2# And vert0to1# => vert1to2# Then Return vert0to1# If vert0to2# => vert0to1# And vert0to2# => vert1to2# Then Return vert0to2# If vert1to2# => vert0to2# And vert1to2# => vert0to1# Then Return vert1to2# End Function Function VertDist#(surface,index,x#,Z#) xdist# = VertexX(surface,index) - x Zdist# = VertexZ(surface,index) - Z Dist# = Sqr(Xdist^2 + Zdist^2) Return Dist# End Function Function VertSteepness#(surface,index,surface2,index2) vertgroundist# = VertDist#(surface,index,VertexX(surface2,index2),VertexZ(surface2,index2)) vertupdist# = Abs(VertexY(surface2,index2) - VertexY(surface,index)) slope# = vertupdist#/vertgroundist# Return slope# End Function |
Comments
| ||
Better and quicker just to calculate the triangle Y normal component, rather than the steepest edge, like so ... Where >0 = Flat up , <0 = flat down, 0 = steepest / vertical Function TriangleNY#( s , t ) 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 ) if Ns > 0 Return Ny / Ns else return 0 endif End Function Stevie |
| ||
Oh cool, you learn something new everyday! |
Code Archives Forum