-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[orx-mesh] Split MeshData types from orx-obj-loader
- Loading branch information
Showing
16 changed files
with
467 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
plugins { | ||
org.openrndr.extra.convention.`kotlin-multiplatform` | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(libs.openrndr.application) | ||
api(libs.openrndr.math) | ||
api(libs.openrndr.shape) | ||
implementation(project(":orx-shapes")) | ||
} | ||
} | ||
|
||
val jvmDemo by getting { | ||
dependencies { | ||
api(libs.openrndr.shape) | ||
implementation(project(":orx-shapes")) | ||
implementation(project(":orx-mesh-generators")) | ||
implementation(project(":orx-camera")) | ||
implementation(project(":orx-noise")) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.openrndr.extra.objloader | ||
|
||
interface ICompoundMeshData { | ||
val vertexData: IVertexData | ||
val compounds: Map<String, IMeshData> | ||
|
||
fun triangulate(): ICompoundMeshData | ||
} | ||
|
||
class CompoundMeshData( | ||
override val vertexData: VertexData, | ||
override val compounds: Map<String, MeshData> | ||
) : ICompoundMeshData { | ||
|
||
override fun triangulate(): CompoundMeshData { | ||
return CompoundMeshData(vertexData, compounds.mapValues { | ||
it.value.triangulate() | ||
}) | ||
} | ||
} | ||
|
||
class MutableCompoundMeshData( | ||
override val vertexData: MutableVertexData, | ||
override val compounds: MutableMap<String, MutableMeshData> | ||
) : ICompoundMeshData { | ||
|
||
override fun triangulate(): MutableCompoundMeshData { | ||
TODO("Not yet implemented") | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
orx-mesh/src/commonMain/kotlin/CompoundMeshDataExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.openrndr.extra.objloader | ||
|
||
import org.openrndr.draw.VertexBuffer | ||
import org.openrndr.draw.vertexBuffer | ||
|
||
fun ICompoundMeshData.toVertexBuffer(): VertexBuffer { | ||
val triangulated = this.triangulate() | ||
|
||
val triangleCount = triangulated.compounds.values.sumOf { it.polygons.size } | ||
|
||
val vertexBuffer = vertexBuffer(objVertexFormat, triangleCount * 3) | ||
|
||
var elementOffset = 0 | ||
for (compound in compounds) { | ||
compound.value.toVertexBuffer(elementOffset, vertexBuffer) | ||
elementOffset += compound.value.polygons.size * 3 | ||
} | ||
|
||
return vertexBuffer | ||
} | ||
|
||
fun ICompoundMeshData.flattenPolygons(): Map<String, List<IPolygon>> { | ||
return compounds.mapValues { it.value.flattenPolygons() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.openrndr.extra.objloader | ||
|
||
import kotlin.jvm.JvmRecord | ||
|
||
interface IMeshData { | ||
val vertexData: IVertexData | ||
val polygons: List<IIndexedPolygon> | ||
fun triangulate(): IMeshData | ||
fun flattenPolygons(): List<IPolygon> | ||
} | ||
|
||
@JvmRecord | ||
data class MeshData( | ||
override val vertexData: VertexData, | ||
override val polygons: List<IndexedPolygon>, | ||
) : IMeshData { | ||
override fun triangulate(): MeshData { | ||
return copy(polygons = polygons.flatMap { polygon -> polygon.triangulate(vertexData) }) | ||
} | ||
|
||
override fun flattenPolygons(): List<Polygon> { | ||
return polygons.map { ip -> | ||
ip.toPolygon(vertexData) | ||
} | ||
} | ||
} | ||
|
||
|
||
data class MutableMeshData( | ||
override val vertexData: MutableVertexData, | ||
override val polygons: MutableList<IndexedPolygon> | ||
) : IMeshData { | ||
override fun triangulate(): MutableMeshData { | ||
return copy(polygons = polygons.flatMap { it.triangulate(vertexData) }.toMutableList()) | ||
} | ||
|
||
override fun flattenPolygons(): List<Polygon> { | ||
return polygons.map { it.toPolygon(vertexData) } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.openrndr.extra.objloader | ||
|
||
import org.openrndr.draw.VertexBuffer | ||
import org.openrndr.draw.VertexFormat | ||
import org.openrndr.draw.vertexBuffer | ||
import org.openrndr.draw.vertexFormat | ||
import org.openrndr.math.Vector2 | ||
|
||
/** | ||
* The [VertexFormat] for a [VertexBuffer] with positions, normals and texture coordinates. | ||
*/ | ||
internal val objVertexFormat = vertexFormat { | ||
position(3) | ||
normal(3) | ||
textureCoordinate(2) | ||
} | ||
|
||
/** | ||
* Converts a [MeshData] instance into a [VertexBuffer] | ||
*/ | ||
fun IMeshData.toVertexBuffer(elementOffset: Int = 0, vertexBuffer: VertexBuffer? = null): VertexBuffer { | ||
val objects = triangulate().flattenPolygons() | ||
val triangleCount = objects.size | ||
val vertexBuffer = vertexBuffer ?: vertexBuffer(objVertexFormat, triangleCount * 3) | ||
|
||
vertexBuffer.put(elementOffset) { | ||
objects.forEach { | ||
|
||
for (i in it.positions.indices) { | ||
write(it.positions[i]) | ||
if (it.normals.isNotEmpty()) { | ||
write(it.normals[i]) | ||
} else { | ||
val d0 = it.positions[2] - it.positions[0] | ||
val d1 = it.positions[1] - it.positions[0] | ||
write(d0.normalized.cross(d1.normalized).normalized) | ||
} | ||
if (it.textureCoords.isNotEmpty()) { | ||
write(it.textureCoords[i]) | ||
} else { | ||
write(Vector2.ZERO) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
vertexBuffer.shadow.destroy() | ||
return vertexBuffer | ||
} |
Oops, something went wrong.