July 28, 2021, 04:28:59

Author Topic: [bb] Gradient Of a Triangle by Mr Snidesmin [ 1+ years ago ]  (Read 883 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Gradient Of a Triangle
Author : Mr Snidesmin
Posted : 1+ years ago

Description : I have written this for use with a function that splits a mesh into a number of surfaces, for various levels of gradient.  This is useful if you want to add a cliff texture to the steep triangles of a mesh etc.

The 'Instant cliffs' function is here:
<a href="codearcsc79a.html?code=1094" target="_blank">http://blitzbasic.com/codearcs/codearcs.php?code=1094[/url]

I'm sure you could think of plenty of other uses for finding a triangle's gradient though. . . :O)


Code :
Code: BlitzBasic
  1. Function TriGradient#(surface%, triangle%)
  2.         Local v0%,v1%,v2%
  3.         Local v0x#, v0y#, v0z#
  4.         Local v1x#, v1y#, v1z#
  5.         Local v2x#, v2y#, v2z#
  6.         Local tx#, ty#, tz#
  7.         Local m1#, m2#, c1#, c2#
  8.         Local ax#, ay#, az#
  9.         Local theta#, argument#, modulus#
  10.         Local i%
  11.        
  12.         ;Special case 1: Is the whole triangle parallel to xz-plane?
  13.         v0x = VertexX(surface, TriangleVertex(surface, triangle, 0))
  14.         v0y = VertexY(surface, TriangleVertex(surface, triangle, 0))
  15.         v0z = VertexZ(surface, TriangleVertex(surface, triangle, 0))
  16.         v1x = VertexX(surface, TriangleVertex(surface, triangle, 1))
  17.         v1y = VertexY(surface, TriangleVertex(surface, triangle, 1))
  18.         v1z = VertexZ(surface, TriangleVertex(surface, triangle, 1))
  19.         v2x = VertexX(surface, TriangleVertex(surface, triangle, 2))
  20.         v2y = VertexY(surface, TriangleVertex(surface, triangle, 2))
  21.         v2z = VertexZ(surface, TriangleVertex(surface, triangle, 2))
  22.         If v0y = v1y And v1y = v2y Then
  23.                 ;Yes, so gradient is 0
  24.                 Return 0
  25.         End If
  26.        
  27.         ;Step 1:
  28.         ;Sort out vertices in order of height (y-axis), v0=bottom, v1=middle, v2=top
  29.         For i=0 To 2
  30.         v% = TriangleVertex(surface, triangle, i)
  31.                 If v0=0 Then v0 = v
  32.                 If v2=0 Then v2 = v
  33.                
  34.                 If VertexY(surface,v) > VertexY(surface,v2) Then v2 = v
  35.                 If VertexY(surface,v) < VertexY(surface,v0) Then v0 = v
  36.     Next
  37.         For i=0 To 2
  38.         v% = TriangleVertex(surface, triangle, i)
  39.                 If v0<>v And v2<>v Then v1 = v
  40.     Next
  41.         v0x = VertexX(surface, v0)
  42.         v0y = VertexY(surface, v0)
  43.         v0z = VertexZ(surface, v0)
  44.         v1x = VertexX(surface, v1)
  45.         v1y = VertexY(surface, v1)
  46.         v1z = VertexZ(surface, v1)
  47.         v2x = VertexX(surface, v2)
  48.         v2y = VertexY(surface, v2)
  49.         v2z = VertexZ(surface, v2)
  50.                
  51.         ;Step 2: Translate lowest point To 0,0,0
  52.         tx = v0x
  53.         ty = v0y
  54.         tz = v0z
  55.         v0x = v0x - tx
  56.         v0y = v0y - ty
  57.         v0z = v0z - tz
  58.         v1x = v1x - tx
  59.         v1y = v1y - ty
  60.         v1z = v1z - tz
  61.         v2x = v2x - tx
  62.         v2y = v2y - ty
  63.         v2z = v2z - tz
  64.        
  65.         ;Special case 2: Is the line (v1,v2) is parallel to xz-plane?
  66.         If v1y = v2y Then
  67.                 ;Yes, so invert the triangle in the y-axis
  68.                 v0y = v1y
  69.                 v1y = 0
  70.                 v2y = 0
  71.                 ;Lowest point is now v2, highest is v0, so swap them:
  72.                 tx# = v2x
  73.                 ty# = v2y
  74.                 tz# = v2z
  75.                 v2x = v0x
  76.                 v2y = v0y
  77.                 v2z = v0z
  78.                 v0x = tx
  79.                 v0y = ty
  80.                 v0z = tz
  81.                 ;Re-do translation:
  82.                 v0x = v0x - tx
  83.                 v0y = v0y - ty
  84.                 v0z = v0z - tz
  85.                 v1x = v1x - tx
  86.                 v1y = v1y - ty
  87.                 v1z = v1z - tz
  88.                 v2x = v2x - tx
  89.                 v2y = v2y - ty
  90.                 v2z = v2z - tz
  91.         End If
  92.        
  93.        
  94.         ;Step 3: Find Point a, such that y(a) = 0 and y is on the line (v2, v1)
  95.         ;Line Equations:
  96.         ;y=m1x+c1
  97.         ;y=m2z+c2
  98.         m1 = (v2y-v1y) / (v2x-v1x)
  99.         m2 = (v2y-v1y) / (v2z-v1z)
  100.         c1 = v1y - m1 * v1x
  101.         c2 = v1y - m1 * v1z
  102.         ay = 0
  103.         ax = -c1/m1
  104.         az = -c2/m2
  105.        
  106.         ;Special case 3: is v1 at y=0
  107.         If v1y = 0 Then
  108.                 ;Yes, therefore a = v1
  109.                 ax = v1x
  110.                 ay = v1y
  111.                 az = v1z
  112.         End If
  113.        
  114.         ;Step 4. Rotate all points v0,v1,v2, a about y-axis so that point a is at x=0
  115.         theta# = -ATan2(az, ax) ;Angle to rotate
  116.         ;NOTE: Actually only v2's z-component will be used, so let's be lazy and only calculate v2z.
  117.         ;;Rotate v0
  118.         ;argument# = ATan2(v0z, v0x)
  119.         ;modulus# = Sqr(v0x^2 + v0z^2)
  120.         ;v0x = modulus * Cos(theta+argument)
  121.         ;v0z = modulus * Sin(theta+argument)
  122.         ;;Rotate v1
  123.         ;argument# = ATan2(v1z, v1x)
  124.         ;modulus# = Sqr(v1x^2 + v1z^2)
  125.         ;v1x = modulus * Cos(theta+argument)
  126.         ;v1z = modulus * Sin(theta+argument)
  127.         ;;Rotate v2
  128.         argument# = ATan2(v2z, v2x)
  129.         modulus# = Sqr(v2x^2 + v2z^2)
  130.         ;v2x = modulus * Cos(theta+argument)
  131.         v2z = modulus * Sin(theta+argument)
  132.         ;;Rotate a
  133.         ;argument# = ATan2(az, ax)
  134.         ;modulus# = Sqr(ax^2 + az^2)
  135.         ;ax = modulus * Cos(theta+argument)
  136.         ;az = modulus * Sin(theta+argument)
  137.        
  138.        
  139.         ;Step 5. Calculate the gradient, and return the value.
  140.         Return Abs(v2y / v2z)
  141. End Function
  142.  
  143.  
  144.  
  145. Function Test#(v0x#, v0y#, v0z#, v1x#, v1y#, v1z#, v2x#, v2y#, v2z#)
  146.         m% = CreateMesh()
  147.         s% = CreateSurface(m)
  148.         AddVertex s, v0x, v0y, v0z
  149.         AddVertex s, v1x, v1y, v1z
  150.         AddVertex s, v2x, v2y, v2z
  151.         AddTriangle s, 0,1,2
  152.         Return TriGradient(s, 0)
  153. End Function
  154.  
  155. Graphics3D 800, 600, 0, 2
  156. Print Test(0,0,0,   10,1,0,   20,0,0)
  157. WaitKey
  158. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal