Akucuki

Cube barycentric test

Mar 17th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.50 KB | Source Code | 0 0
  1. class MyGdxGame : ApplicationAdapter() {
  2.     private lateinit var environment: Environment
  3.     private lateinit var cam: Camera
  4.     private lateinit var camController: CameraInputController
  5.     private lateinit var modelBatch: ModelBatch
  6.     private lateinit var modelBuilder: ModelBuilder
  7.     private lateinit var pointModel: Model
  8.     private lateinit var pointInstance: ModelInstance
  9.  
  10.     override fun create() {
  11.         environment = Environment()
  12.         environment.set(ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f))
  13.  
  14.         modelBuilder = ModelBuilder()
  15.         modelBatch = ModelBatch()
  16.  
  17.         cam = PerspectiveCamera(60f, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
  18.         cam.position.set(5f, 5f, 5f)
  19.         cam.lookAt(0f, 0f, 0f)
  20.         cam.near = 0.1f
  21.         cam.far = 100f
  22.         cam.update()
  23.  
  24.         camController = CameraInputController(cam)
  25.         Gdx.input.inputProcessor = camController
  26.  
  27.         pointModel = modelBuilder.createBox(
  28.             0.02f, 0.02f, 0.02f,
  29.             Material(ColorAttribute.createDiffuse(Color.WHITE)),
  30.             VertexAttributes.Usage.Position.toLong() or VertexAttributes.Usage.Normal.toLong()
  31.         )
  32.         pointInstance = ModelInstance(pointModel)
  33.     }
  34.  
  35.     override fun render() {
  36.         camController.update()
  37.  
  38.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT or GL20.GL_DEPTH_BUFFER_BIT)
  39.  
  40.         modelBatch.begin(cam)
  41.  
  42.         val a = Vector3(0f, 0f, 0f)
  43.         val b = Vector3(1f, 0f, 0f)
  44.         val c = Vector3(0f, 1f, 0f)
  45.  
  46.         val gridSize = 10
  47.         val stepSize = 0.1f
  48.  
  49.         for (i in -gridSize..gridSize) {
  50.             for (j in -gridSize..gridSize) {
  51.                 for (k in -gridSize..gridSize) {
  52.  
  53.                     val point = Vector3(i * stepSize, j * stepSize, k * stepSize)
  54.                     val barycentricCoords = toBarycoord3D(point, a, b, c)
  55.  
  56. //                    if (GeometryUtils.barycoordInsideTriangle(Vector2(barycentricCoords.x, barycentricCoords.y))) {
  57.                         val color = Color(
  58.                             barycentricCoords.x,
  59.                             barycentricCoords.y,
  60.                             barycentricCoords.z,
  61.                             1f
  62.                         )
  63.                         renderPoint(point, color)
  64. //                    }
  65.                 }
  66.             }
  67.         }
  68.  
  69.         modelBatch.end()
  70.     }
  71.  
  72.     fun toBarycoord3D(
  73.         point: Vector3,
  74.         a: Vector3,
  75.         b: Vector3,
  76.         c: Vector3
  77.     ): Vector3 {
  78.         val v0 = Vector3().set(b).sub(a)
  79.         val v1 = Vector3().set(c).sub(a)
  80.         val v2 = Vector3().set(point).sub(a)
  81.  
  82.         val d00 = v0.dot(v0)
  83.         val d01 = v0.dot(v1)
  84.         val d11 = v1.dot(v1)
  85.         val d20 = v2.dot(v0)
  86.         val d21 = v2.dot(v1)
  87.  
  88.         val denom = d00 * d11 - d01 * d01
  89.         val barycentricOut = Vector3()
  90.         barycentricOut.x = (d11 * d20 - d01 * d21) / denom
  91.         barycentricOut.y = (d00 * d21 - d01 * d20) / denom
  92.         barycentricOut.z = 1.0f - barycentricOut.x - barycentricOut.y
  93.  
  94.         return barycentricOut
  95.     }
  96.  
  97.     private fun renderPoint(position: Vector3, color: Color) {
  98.         (pointInstance.materials[0].get(ColorAttribute.Diffuse) as ColorAttribute).color.set(color)
  99.         pointInstance.transform.setTranslation(position)
  100.  
  101.         modelBatch.render(pointInstance, environment)
  102.     }
  103.  
  104.     override fun dispose() {
  105.         modelBatch.dispose()
  106.         pointModel.dispose()
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment