Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#33] Fix limitation of 3D path length #39

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package earth.worldwind.util.kgl
import android.opengl.GLES20
import java.nio.ByteBuffer
import java.nio.FloatBuffer
import java.nio.IntBuffer
import java.nio.ShortBuffer

class AndroidKgl : Kgl {
Expand Down Expand Up @@ -80,7 +81,8 @@ class AndroidKgl : Kgl {

override fun bufferData(target: Int, size: Int, sourceData: ShortArray, usage: Int, offset: Int) =
GLES20.glBufferData(target, size, ShortBuffer.wrap(sourceData, offset, size / 2), usage)

override fun bufferData(target: Int, size: Int, sourceData: IntArray, usage: Int, offset: Int) =
GLES20.glBufferData(target, size, IntBuffer.wrap(sourceData, offset, size / 4), usage)
override fun bufferData(target: Int, size: Int, sourceData: FloatArray, usage: Int, offset: Int) =
GLES20.glBufferData(target, size, FloatBuffer.wrap(sourceData, offset, size / 4), usage)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import earth.worldwind.geom.Matrix3
import earth.worldwind.geom.Vec3
import earth.worldwind.render.Color
import earth.worldwind.render.Texture
import earth.worldwind.render.buffer.AbstractBufferObject
import earth.worldwind.render.buffer.FloatBufferObject
import earth.worldwind.render.buffer.ShortBufferObject
import earth.worldwind.render.program.BasicShaderProgram

open class DrawShapeState internal constructor() {
Expand All @@ -15,7 +15,7 @@ open class DrawShapeState internal constructor() {

var program: BasicShaderProgram? = null
var vertexBuffer: FloatBufferObject? = null
var elementBuffer: ShortBufferObject? = null
var elementBuffer: AbstractBufferObject? = null
val vertexOrigin = Vec3()
var vertexStride = 0
var enableCullFace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package earth.worldwind.render.buffer

import earth.worldwind.draw.DrawContext
import earth.worldwind.util.kgl.GL_STATIC_DRAW

open class IntBufferObject(target: Int, array: IntArray, size: Int = array.size) : AbstractBufferObject(target, size * 4) {
protected var array: IntArray? = array

override fun release(dc: DrawContext) {
super.release(dc)
array = null // array can be non-null if the object has not been bound
}

override fun bindBuffer(dc: DrawContext): Boolean {
array?.let{ loadBuffer(dc) }.also { array = null }
return super.bindBuffer(dc)
}

override fun loadBufferObjectData(dc: DrawContext) {
array?.let { dc.gl.bufferData(target, byteCount, it, GL_STATIC_DRAW) }
}
}
28 changes: 14 additions & 14 deletions worldwind/src/commonMain/kotlin/earth/worldwind/shape/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import earth.worldwind.draw.DrawableSurfaceShape
import earth.worldwind.geom.*
import earth.worldwind.render.*
import earth.worldwind.render.buffer.FloatBufferObject
import earth.worldwind.render.buffer.ShortBufferObject
import earth.worldwind.render.buffer.IntBufferObject
import earth.worldwind.render.image.ImageOptions
import earth.worldwind.render.image.ResamplingMode
import earth.worldwind.render.image.WrapMode
Expand All @@ -27,9 +27,9 @@ open class Path @JvmOverloads constructor(
protected var vertexArray = FloatArray(0)
protected var vertexIndex = 0
// TODO Use ShortArray instead of mutableListOf<Short> to avoid unnecessary memory re-allocations
protected val interiorElements = mutableListOf<Short>()
protected val outlineElements = mutableListOf<Short>()
protected val verticalElements = mutableListOf<Short>()
protected val interiorElements = mutableListOf<Int>()
protected val outlineElements = mutableListOf<Int>()
protected val verticalElements = mutableListOf<Int>()
protected lateinit var vertexBufferKey: Any
protected lateinit var elementBufferKey: Any
protected val vertexOrigin = Vec3()
Expand Down Expand Up @@ -93,7 +93,7 @@ open class Path @JvmOverloads constructor(

// Assemble the drawable's OpenGL element buffer object.
drawState.elementBuffer = rc.getBufferObject(elementBufferKey) {
ShortBufferObject(GL_ELEMENT_ARRAY_BUFFER, (interiorElements + outlineElements + verticalElements).toShortArray())
IntBufferObject(GL_ELEMENT_ARRAY_BUFFER, (interiorElements + outlineElements + verticalElements).toIntArray())
}

// Configure the drawable's vertex texture coordinate attribute.
Expand All @@ -118,7 +118,7 @@ open class Path @JvmOverloads constructor(
drawState.lineWidth(activeAttributes.outlineWidth + if (isSurfaceShape) 0.5f else 0f)
drawState.drawElements(
GL_LINE_STRIP, outlineElements.size,
GL_UNSIGNED_SHORT, interiorElements.size * 2
GL_UNSIGNED_INT, interiorElements.size * Int.SIZE_BYTES
)
}

Expand All @@ -131,7 +131,7 @@ open class Path @JvmOverloads constructor(
drawState.lineWidth(activeAttributes.outlineWidth)
drawState.drawElements(
GL_LINES, verticalElements.size,
GL_UNSIGNED_SHORT, interiorElements.size * 2 + outlineElements.size * 2
GL_UNSIGNED_INT, (interiorElements.size + outlineElements.size) * Int.SIZE_BYTES
)
}

Expand All @@ -140,7 +140,7 @@ open class Path @JvmOverloads constructor(
drawState.color(if (rc.isPickMode) pickColor else activeAttributes.interiorColor)
drawState.drawElements(
GL_TRIANGLE_STRIP, interiorElements.size,
GL_UNSIGNED_SHORT, 0
GL_UNSIGNED_INT, 0
)
}

Expand Down Expand Up @@ -252,25 +252,25 @@ open class Path @JvmOverloads constructor(
vertexArray[vertexIndex++] = (latitude.inDegrees - vertexOrigin.y).toFloat()
vertexArray[vertexIndex++] = (altitude - vertexOrigin.z).toFloat()
vertexArray[vertexIndex++] = texCoord1d.toFloat()
outlineElements.add(vertex.toShort())
outlineElements.add(vertex)
} else {
vertexArray[vertexIndex++] = (point.x - vertexOrigin.x).toFloat()
vertexArray[vertexIndex++] = (point.y - vertexOrigin.y).toFloat()
vertexArray[vertexIndex++] = (point.z - vertexOrigin.z).toFloat()
vertexArray[vertexIndex++] = texCoord1d.toFloat()
outlineElements.add(vertex.toShort())
outlineElements.add(vertex)
if (isExtrude) {
point = rc.geographicToCartesian(latitude, longitude, 0.0, altitudeMode, this.point)
vertexArray[vertexIndex++] = (point.x - vertexOrigin.x).toFloat()
vertexArray[vertexIndex++] = (point.y - vertexOrigin.y).toFloat()
vertexArray[vertexIndex++] = (point.z - vertexOrigin.z).toFloat()
vertexArray[vertexIndex++] = 0f /*unused*/
interiorElements.add(vertex.toShort())
interiorElements.add(vertex.inc().toShort())
interiorElements.add(vertex)
interiorElements.add(vertex.inc())
}
if (isExtrude && !intermediate) {
verticalElements.add(vertex.toShort())
verticalElements.add(vertex.inc().toShort())
verticalElements.add(vertex)
verticalElements.add(vertex.inc())
}
}
return vertex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ interface Kgl {
fun createBuffer(): KglBuffer
fun bindBuffer(target: Int, buffer: KglBuffer)
fun bufferData(target: Int, size: Int, sourceData: ShortArray, usage: Int, offset: Int = 0)
fun bufferData(target: Int, size: Int, sourceData: IntArray, usage: Int, offset: Int = 0)
fun bufferData(target: Int, size: Int, sourceData: FloatArray, usage: Int, offset: Int = 0)
fun deleteBuffer(buffer: KglBuffer)

Expand Down
Loading