Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyGdxGame : ApplicationAdapter() {
- private lateinit var environment: Environment
- private lateinit var cam: Camera
- private lateinit var camController: CameraInputController
- private lateinit var modelBatch: ModelBatch
- private lateinit var modelBuilder: ModelBuilder
- private lateinit var pointModel: Model
- private lateinit var pointInstance: ModelInstance
- override fun create() {
- environment = Environment()
- environment.set(ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f))
- modelBuilder = ModelBuilder()
- modelBatch = ModelBatch()
- cam = PerspectiveCamera(60f, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
- cam.position.set(5f, 5f, 5f)
- cam.lookAt(0f, 0f, 0f)
- cam.near = 0.1f
- cam.far = 100f
- cam.update()
- camController = CameraInputController(cam)
- Gdx.input.inputProcessor = camController
- pointModel = modelBuilder.createBox(
- 0.02f, 0.02f, 0.02f,
- Material(ColorAttribute.createDiffuse(Color.WHITE)),
- VertexAttributes.Usage.Position.toLong() or VertexAttributes.Usage.Normal.toLong()
- )
- pointInstance = ModelInstance(pointModel)
- }
- override fun render() {
- camController.update()
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT or GL20.GL_DEPTH_BUFFER_BIT)
- modelBatch.begin(cam)
- val a = Vector3(0f, 0f, 0f)
- val b = Vector3(1f, 0f, 0f)
- val c = Vector3(0f, 1f, 0f)
- val gridSize = 10
- val stepSize = 0.1f
- for (i in -gridSize..gridSize) {
- for (j in -gridSize..gridSize) {
- for (k in -gridSize..gridSize) {
- val point = Vector3(i * stepSize, j * stepSize, k * stepSize)
- val barycentricCoords = toBarycoord3D(point, a, b, c)
- // if (GeometryUtils.barycoordInsideTriangle(Vector2(barycentricCoords.x, barycentricCoords.y))) {
- val color = Color(
- barycentricCoords.x,
- barycentricCoords.y,
- barycentricCoords.z,
- 1f
- )
- renderPoint(point, color)
- // }
- }
- }
- }
- modelBatch.end()
- }
- fun toBarycoord3D(
- point: Vector3,
- a: Vector3,
- b: Vector3,
- c: Vector3
- ): Vector3 {
- val v0 = Vector3().set(b).sub(a)
- val v1 = Vector3().set(c).sub(a)
- val v2 = Vector3().set(point).sub(a)
- val d00 = v0.dot(v0)
- val d01 = v0.dot(v1)
- val d11 = v1.dot(v1)
- val d20 = v2.dot(v0)
- val d21 = v2.dot(v1)
- val denom = d00 * d11 - d01 * d01
- val barycentricOut = Vector3()
- barycentricOut.x = (d11 * d20 - d01 * d21) / denom
- barycentricOut.y = (d00 * d21 - d01 * d20) / denom
- barycentricOut.z = 1.0f - barycentricOut.x - barycentricOut.y
- return barycentricOut
- }
- private fun renderPoint(position: Vector3, color: Color) {
- (pointInstance.materials[0].get(ColorAttribute.Diffuse) as ColorAttribute).color.set(color)
- pointInstance.transform.setTranslation(position)
- modelBatch.render(pointInstance, environment)
- }
- override fun dispose() {
- modelBatch.dispose()
- pointModel.dispose()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment