Hi!
From almost a year I've been playing with Armory, Iron and Kha, to try to understand how this amazing set of technologies work during my incursion in the incredible interesting game programming world — and now I'm stuck with something I don't understand.
I'm trying to modify this example → https://github.com/luboslenco/kha3d_examples/blob/master/tutorial02/Sources/Empty.hx — simply adding another triangle to both the vertices and indices arrays, to try to paint both of them setting the buffers only once (relevant modified lines are only the addition to vertices and indices and the lines above 107 Draw! commet):
package;
import kha.Framebuffer;
import kha.Color;
import kha.Shaders;
import kha.graphics4.PipelineState;
import kha.graphics4.VertexStructure;
import kha.graphics4.VertexBuffer;
import kha.graphics4.IndexBuffer;
import kha.graphics4.FragmentShader;
import kha.graphics4.VertexShader;
import kha.graphics4.VertexData;
import kha.graphics4.Usage;
class Empty {
// An array of 3 vectors representing 3 vertices to form a triangle
static var vertices:Array<Float> = [
-1.0, -1.0, 0.0, // Bottom-left
1.0, -1.0, 0.0, // Bottom-right
0.0, 1.0, 0.0, // Top
-1.0, 1.0, 0.0, // Bottom-left
0.0, -1.0, -1.0, // Bottom-right
0.0, 1.0, 0.0, // Top
];
// Indices for our triangle, these will point to vertices above
static var indices:Array<Int> = [
0, // Bottom-left
1, // Bottom-right
2, // Top
3, // Bottom-left
4, // Bottom-right
5, // Top
];
var vertexBuffer:VertexBuffer;
var indexBuffer:IndexBuffer;
var pipeline:PipelineState;
public function new() {
// Define vertex structure
var structure = new VertexStructure();
structure.add("pos", VertexData.Float3);
// Save length - we only store position in vertices for now
// Eventually there will be texture coords, normals,...
var structureLength = 3;
// Compile pipeline state
// Shaders are located in 'Sources/Shaders' directory
// and Kha includes them automatically
pipeline = new PipelineState();
pipeline.inputLayout = [structure];
pipeline.fragmentShader = Shaders.simple_frag;
pipeline.vertexShader = Shaders.simple_vert;
pipeline.colorAttachmentCount = 1;
pipeline.colorAttachments[0] = kha.graphics4.TextureFormat.RGBA32;
pipeline.depthStencilAttachment = kha.graphics4.DepthStencilFormat.Depth16;
pipeline.compile();
// Create vertex buffer
vertexBuffer = new VertexBuffer(
Std.int(vertices.length / 3), // Vertex count - 3 floats per vertex
structure, // Vertex structure
Usage.StaticUsage // Vertex data will stay the same
);
// Copy vertices to vertex buffer
var vbData = vertexBuffer.lock();
for (i in 0...vbData.length) {
vbData.set(i, vertices[i]);
}
vertexBuffer.unlock();
// Create index buffer
indexBuffer = new IndexBuffer(
indices.length, // 3 indices for our triangle
Usage.StaticUsage // Index data will stay the same
);
// Copy indices to index buffer
var iData = indexBuffer.lock();
for (i in 0...iData.length) {
iData[i] = indices[i];
}
indexBuffer.unlock();
}
public function render(frames:Array<Framebuffer>) {
// A graphics object which lets us perform 3D operations
var g = frames[0].g4;
// Begin rendering
g.begin();
// Clear screen to black
g.clear(Color.Black);
// Bind state we want to draw with
g.setPipeline(pipeline);
// Bind data we want to draw
g.setVertexBuffer(vertexBuffer);
g.setIndexBuffer(indexBuffer);
// Draw!
g.drawIndexedVertices(0, 3);
//g.setVertexBuffer(vertexBuffer);
g.drawIndexedVertices(3, 3);
// End rendering
g.end();
}
}
But if I don't discomment the setVertexBuffer
line between the two drawIndexedVertices
, only the first triangle gets painted — or I can choose to draw the second one if I comment the first drawIndexedVertices
.
So... what am I doing wrong? The thing tha makes it more strange to me, is that I can see this working fine, and I don't find the relevant differences → https://github.com/armory3d/iron/blob/master/Sources/iron/data/MeshBatch.hx#L72;L97 .
Thanks a lot for your time