-
Hello, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
You need 4 vertices but 6 indices. You can make the rectangle dynamic either by modifying the vertices every frame or more simply by setting a transform on the Renderable object. |
Beta Was this translation helpful? Give feedback.
-
Hi, Thanks you @cx20 for your sample, and @romainguy thanks you for support. I tried to change my code to be the most similar to @cx20 and for others people this is my final code. I have this : private fun createMesh() {
val intSize = 4
val floatSize = 4
val shortSize = 2
// A vertex is a position + a color:
// 4 floats for XYZ position, 1 integer for color
val vertexPositionSize = 3 * floatSize
val vertexColorSize = 1 * intSize
// Define a vertex and a function to put a vertex in a ByteBuffer
data class Vertex(val x: Float, val y: Float, val z: Float)
data class Color(val color: Int)
fun ByteBuffer.put(v: Vertex): ByteBuffer {
putFloat(v.x)
putFloat(v.y)
putFloat(v.z)
return this
}
fun ByteBuffer.putColor(v: Color): ByteBuffer {
putInt(v.color)
return this
}
val vertexCount = 4
val vertexData = ByteBuffer.allocate(vertexCount * vertexPositionSize)
// It is important to respect the native byte order
//Here you can put your dynamic coordinates too
.order(ByteOrder.nativeOrder())
.put(Vertex(-0.5f, 0.5f, 0.0f))
.put(Vertex(0.5f, 0.5f, 0.0f))
.put(Vertex(0.5f,-0.5f, 0.0f))
.put(Vertex(-0.5f,-0.5f, 0.0f))
// Make sure the cursor is pointing in the right place in the byte buffer
.flip()
val vertexColor = ByteBuffer.allocate(vertexCount * vertexColorSize)
// It is important to respect the native byte order
.order(ByteOrder.nativeOrder())
.putColor(Color(0xff0000ff.toInt()))
.putColor(Color(0xff00ff00.toInt()))
.putColor(Color(0xffff0000.toInt()))
.putColor(Color(0xff00ffff.toInt()))
// Make sure the cursor is pointing in the right place in the byte buffer
.flip()
// Declare the layout of our mesh
vertexBuffer = VertexBuffer.Builder()
.bufferCount(2)
.vertexCount(4)
// Because we interleave position and color data we must specify offset and stride
// We could use de-interleaved data by declaring two buffers and giving each
// attribute a different buffer index
.attribute(
VertexBuffer.VertexAttribute.POSITION,
0,
VertexBuffer.AttributeType.FLOAT3,
0,
12
)
.attribute(
VertexBuffer.VertexAttribute.COLOR,
1,
VertexBuffer.AttributeType.UBYTE4,
0,
4
)
// We store colors as unsigned bytes but since we want values between 0 and 1
// in the material (shaders), we must mark the attribute as normalized
.normalized(VertexBuffer.VertexAttribute.COLOR)
.build(mEngine)
// Feed the vertex data to the mesh
// We only set 1 buffer because the data is interleaved
vertexBuffer.setBufferAt(mEngine, 0, vertexData)
vertexBuffer.setBufferAt(mEngine, 1, vertexColor)
// Create the indices
val indexData = ByteBuffer.allocate(vertexCount * shortSize)
.order(ByteOrder.nativeOrder())
.putShort(0)
.putShort(1)
.putShort(2)
.putShort(0)
.putShort(2)
.putShort(3)
.flip()
indexBuffer = IndexBuffer.Builder()
.indexCount(6)
.bufferType(IndexBuffer.Builder.IndexType.USHORT)
.build(mEngine)
indexBuffer.setBuffer(mEngine, indexData)
} This is my square : https://drive.google.com/file/d/1z-B4Equ-S79gsbqxM2QZkxJdGAQOlmS0/view?usp=sharing Wow a lot of time to draw it but I'm very happy now, so thanks you both of them |
Beta Was this translation helpful? Give feedback.
You need 4 vertices but 6 indices. You can make the rectangle dynamic either by modifying the vertices every frame or more simply by setting a transform on the Renderable object.