Fix formatting of OBJLoader
This commit is contained in:
@@ -9,6 +9,8 @@ import org.openrndr.math.Vector3
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.MalformedURLException
|
import java.net.MalformedURLException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
class Triangle(val positions: Array<Vector3> = emptyArray(),
|
class Triangle(val positions: Array<Vector3> = emptyArray(),
|
||||||
val normals: Array<Vector3> = emptyArray(),
|
val normals: Array<Vector3> = emptyArray(),
|
||||||
@@ -31,23 +33,23 @@ fun bounds(triangles: List<Triangle>): Box {
|
|||||||
|
|
||||||
triangles.forEach {
|
triangles.forEach {
|
||||||
it.positions.forEach {
|
it.positions.forEach {
|
||||||
minX = Math.min(minX, it.x)
|
minX = min(minX, it.x)
|
||||||
minY = Math.min(minY, it.y)
|
minY = min(minY, it.y)
|
||||||
minZ = Math.min(minZ, it.z)
|
minZ = min(minZ, it.z)
|
||||||
|
|
||||||
maxX = Math.max(maxX, it.x)
|
maxX = max(maxX, it.x)
|
||||||
maxY = Math.max(maxY, it.y)
|
maxY = max(maxY, it.y)
|
||||||
maxZ = Math.max(maxZ, it.z)
|
maxZ = max(maxZ, it.z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Box(Vector3(minX, minY, minZ), maxX - minX, maxY - minY, maxZ - minZ)
|
return Box(Vector3(minX, minY, minZ), maxX - minX, maxY - minY, maxZ - minZ)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun List<Triangle>.vertexBuffer():VertexBuffer {
|
fun List<Triangle>.vertexBuffer(): VertexBuffer {
|
||||||
val vertexBuffer = vertexBuffer(objVertexFormat, size*3)
|
val vertexBuffer = vertexBuffer(objVertexFormat, size * 3)
|
||||||
vertexBuffer.put {
|
vertexBuffer.put {
|
||||||
this@vertexBuffer.forEach {
|
this@vertexBuffer.forEach {
|
||||||
for (i in 0 until it.positions.size) {
|
for (i in it.positions.indices) {
|
||||||
write(it.positions[i])
|
write(it.positions[i])
|
||||||
write(it.normals[i])
|
write(it.normals[i])
|
||||||
write(Vector2.ZERO)
|
write(Vector2.ZERO)
|
||||||
@@ -92,16 +94,16 @@ fun loadOBJasVertexBuffer(lines: List<String>): VertexBuffer {
|
|||||||
vertexBuffer.put {
|
vertexBuffer.put {
|
||||||
objects.entries.forEach {
|
objects.entries.forEach {
|
||||||
it.value.forEach {
|
it.value.forEach {
|
||||||
for (i in 0 until it.positions.size) {
|
for (i in it.positions.indices) {
|
||||||
write(it.positions[i])
|
write(it.positions[i])
|
||||||
if (it.normals.size > 0) {
|
if (it.normals.isNotEmpty()) {
|
||||||
write(it.normals[i])
|
write(it.normals[i])
|
||||||
} else {
|
} else {
|
||||||
val d0 = it.positions[2] - it.positions[0]
|
val d0 = it.positions[2] - it.positions[0]
|
||||||
val d1 = it.positions[1] - it.positions[0]
|
val d1 = it.positions[1] - it.positions[0]
|
||||||
write(d0.normalized.cross(d1.normalized).normalized)
|
write(d0.normalized.cross(d1.normalized).normalized)
|
||||||
}
|
}
|
||||||
if (it.textureCoords.size > 0) {
|
if (it.textureCoords.isNotEmpty()) {
|
||||||
write(it.textureCoords[i])
|
write(it.textureCoords[i])
|
||||||
} else {
|
} else {
|
||||||
write(Vector2.ZERO)
|
write(Vector2.ZERO)
|
||||||
@@ -145,36 +147,36 @@ fun loadOBJ(lines: List<String>): Map<String, List<Triangle>> {
|
|||||||
it.map { it.toIntOrNull() }
|
it.map { it.toIntOrNull() }
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i in 0 until indices.size-2) {
|
for (i in 0 until indices.size - 2) {
|
||||||
|
|
||||||
val attributes = indices[0].size
|
val attributes = indices[0].size
|
||||||
val o = i*2
|
val o = i * 2
|
||||||
val s = indices.size
|
val s = indices.size
|
||||||
|
|
||||||
val ps = if (attributes >= 1) arrayOf(
|
val ps = if (attributes >= 1) arrayOf(
|
||||||
indices[(0+o)%s][0]?.let { positions[it - 1] } ?: Vector3.ZERO,
|
indices[(0 + o) % s][0]?.let { positions[it - 1] } ?: Vector3.ZERO,
|
||||||
indices[(1+o)%s][0]?.let { positions[it - 1] } ?: Vector3.ZERO,
|
indices[(1 + o) % s][0]?.let { positions[it - 1] } ?: Vector3.ZERO,
|
||||||
indices[(2+o)%s][0]?.let { positions[it - 1] } ?: Vector3.ZERO)
|
indices[(2 + o) % s][0]?.let { positions[it - 1] } ?: Vector3.ZERO)
|
||||||
else
|
else
|
||||||
emptyArray()
|
emptyArray()
|
||||||
|
|
||||||
val tcs = if (attributes >= 2) arrayOf(
|
val tcs = if (attributes >= 2) arrayOf(
|
||||||
indices[(0+o)%s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO,
|
indices[(0 + o) % s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO,
|
||||||
indices[(1+o)%s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO,
|
indices[(1 + o) % s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO,
|
||||||
indices[(2+o)%s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO)
|
indices[(2 + o) % s][1]?.let { textureCoords[it - 1] } ?: Vector2.ZERO)
|
||||||
else
|
else
|
||||||
emptyArray()
|
emptyArray()
|
||||||
|
|
||||||
|
|
||||||
val ns = if (attributes >= 3) arrayOf(
|
val ns = if (attributes >= 3) arrayOf(
|
||||||
indices[(0+o)%s][2]?.let { normals[it - 1] } ?: Vector3.ZERO,
|
indices[(0 + o) % s][2]?.let { normals[it - 1] } ?: Vector3.ZERO,
|
||||||
indices[(1+o)%s][2]?.let { normals[it - 1] } ?: Vector3.ZERO,
|
indices[(1 + o) % s][2]?.let { normals[it - 1] } ?: Vector3.ZERO,
|
||||||
indices[(2+o)%s][2]?.let { normals[it - 1] } ?: Vector3.ZERO)
|
indices[(2 + o) % s][2]?.let { normals[it - 1] } ?: Vector3.ZERO)
|
||||||
else
|
else
|
||||||
emptyArray()
|
emptyArray()
|
||||||
|
|
||||||
activeMesh.add(Triangle(ps, ns, tcs))
|
activeMesh.add(Triangle(ps, ns, tcs))
|
||||||
if(meshes.isEmpty()) {
|
if (meshes.isEmpty()) {
|
||||||
meshes["no-name"] = activeMesh
|
meshes["no-name"] = activeMesh
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user