Fix problem in UV coord generation of plane meshes

This commit is contained in:
Edwin Jakobs
2020-04-06 19:13:49 +02:00
parent 185c02176f
commit 0de7426017
4 changed files with 150 additions and 89 deletions

View File

@@ -0,0 +1,18 @@
sourceSets {
demo {
java {
srcDirs = ["src/demo/kotlin"]
compileClasspath += main.getCompileClasspath()
runtimeClasspath += main.getRuntimeClasspath()
}
}
}
dependencies {
demoImplementation(project(":orx-camera"))
demoImplementation("org.openrndr:openrndr-core:$openrndrVersion")
demoRuntimeOnly("org.openrndr:openrndr-gl3:$openrndrVersion")
demoRuntimeOnly("org.openrndr:openrndr-gl3-natives-$openrndrOS:$openrndrVersion")
demoImplementation(sourceSets.getByName("main").output)
}

View File

@@ -0,0 +1,35 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.colorBuffer
import org.openrndr.draw.shadeStyle
import org.openrndr.extras.camera.Orbital
import org.openrndr.extras.meshgenerators.boxMesh
fun main() {
application {
program {
val box = boxMesh(1.0, 1.0, 1.0)
val texture = colorBuffer(256, 256)
val s = texture.shadow
for (y in 0 until 256) {
for (x in 0 until 256) {
s[x, y] = ColorRGBa(x/256.0, y/256.0, 0.0, 1.0)
}
}
s.upload()
extend(Orbital())
extend {
drawer.background(ColorRGBa.PINK)
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = texture(p_texture, va_texCoord0.xy);
""".trimIndent()
parameter("texture", texture)
}
drawer.vertexBuffer(box, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -1,84 +1,84 @@
package org.openrndr.extras.meshgenerators
import org.openrndr.draw.VertexBuffer
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
fun planeMesh(center: Vector3,
right: Vector3,
forward: Vector3,
up: Vector3 = forward.cross(right).normalized,
width: Double = 1.0, height: Double = 1.0,
widthSegments: Int = 1, heightSegments: Int = 1): VertexBuffer {
val vertexCount = (widthSegments * heightSegments) * 6
val vb = meshVertexBuffer(vertexCount)
vb.put {
generatePlane(center, right, forward, up,
width, height, widthSegments, heightSegments, bufferWriter(this))
}
return vb
}
/**
* generates a finite plane with its center at (0,0,0) and spanning the xz-plane
*/
fun groundPlaneMesh(width: Double = 1.0,
height: Double = 1.0,
widthSegments: Int = 1,
heightSegments: Int = 1): VertexBuffer {
return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y,
width, height, widthSegments, heightSegments)
}
/**
* generates a finite plane with its center at (0,0,0) and spanning the xy-plane
*/
fun wallPlaneMesh(width: Double = 1.0,
height: Double = 1.0,
widthSegments: Int = 1,
heightSegments: Int = 1): VertexBuffer {
return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z,
width, height, widthSegments, heightSegments)
}
fun generatePlane(center: Vector3,
right: Vector3,
forward: Vector3,
up: Vector3 = forward.cross(right).normalized,
width: Double = 1.0, height: Double = 1.0,
widthSegments: Int = 1, heightSegments: Int = 2,
writer: VertexWriter) {
val forwardStep = forward.normalized * (height / heightSegments)
val rightStep = right.normalized * (width / widthSegments)
val corner = center - forward.normalized * (height*0.5) - right.normalized * (width * 0.5)
val step = Vector2(1.0 / width, 1.0 / height)
for (v in 0 until heightSegments) {
for (u in 0 until widthSegments) {
val uv00 = Vector2(u + 0.0, v + 0.0) * step
val uv01 = Vector2(u + 0.0, v + 1.0) * step
val uv10 = Vector2(u + 1.0, v + 0.0) * step
val uv11 = Vector2(u + 1.0, v + 1.0) * step
val c00 = corner + forwardStep * v.toDouble() + rightStep * u.toDouble()
val c01 = corner + forwardStep * (v + 1).toDouble() + rightStep * u.toDouble()
val c10 = corner + forwardStep * v.toDouble() + rightStep * (u + 1).toDouble()
val c11 = corner + forwardStep * (v + 1).toDouble() + rightStep * (u + 1).toDouble()
writer(c11, up, uv00)
writer(c10, up, uv10)
writer(c00, up, uv11)
writer(c00, up, uv11)
writer(c01, up, uv01)
writer(c11, up, uv00)
}
}
}
package org.openrndr.extras.meshgenerators
import org.openrndr.draw.VertexBuffer
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
fun planeMesh(center: Vector3,
right: Vector3,
forward: Vector3,
up: Vector3 = forward.cross(right).normalized,
width: Double = 1.0, height: Double = 1.0,
widthSegments: Int = 1, heightSegments: Int = 1): VertexBuffer {
val vertexCount = (widthSegments * heightSegments) * 6
val vb = meshVertexBuffer(vertexCount)
vb.put {
generatePlane(center, right, forward, up,
width, height, widthSegments, heightSegments, bufferWriter(this))
}
return vb
}
/**
* generates a finite plane with its center at (0,0,0) and spanning the xz-plane
*/
fun groundPlaneMesh(width: Double = 1.0,
height: Double = 1.0,
widthSegments: Int = 1,
heightSegments: Int = 1): VertexBuffer {
return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y,
width, height, widthSegments, heightSegments)
}
/**
* generates a finite plane with its center at (0,0,0) and spanning the xy-plane
*/
fun wallPlaneMesh(width: Double = 1.0,
height: Double = 1.0,
widthSegments: Int = 1,
heightSegments: Int = 1): VertexBuffer {
return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z,
width, height, widthSegments, heightSegments)
}
fun generatePlane(center: Vector3,
right: Vector3,
forward: Vector3,
up: Vector3 = forward.cross(right).normalized,
width: Double = 1.0, height: Double = 1.0,
widthSegments: Int = 1, heightSegments: Int = 2,
writer: VertexWriter) {
val forwardStep = forward.normalized * (height / heightSegments)
val rightStep = right.normalized * (width / widthSegments)
val corner = center - forward.normalized * (height*0.5) - right.normalized * (width * 0.5)
val step = Vector2(1.0 / widthSegments, 1.0 / heightSegments)
for (v in 0 until heightSegments) {
for (u in 0 until widthSegments) {
val uv00 = Vector2(u + 0.0, v + 0.0) * step
val uv01 = Vector2(u + 0.0, v + 1.0) * step
val uv10 = Vector2(u + 1.0, v + 0.0) * step
val uv11 = Vector2(u + 1.0, v + 1.0) * step
val c00 = corner + forwardStep * v.toDouble() + rightStep * u.toDouble()
val c01 = corner + forwardStep * (v + 1).toDouble() + rightStep * u.toDouble()
val c10 = corner + forwardStep * v.toDouble() + rightStep * (u + 1).toDouble()
val c11 = corner + forwardStep * (v + 1).toDouble() + rightStep * (u + 1).toDouble()
writer(c11, up, uv00)
writer(c10, up, uv10)
writer(c00, up, uv11)
writer(c00, up, uv11)
writer(c01, up, uv01)
writer(c11, up, uv00)
}
}
}