Save
Saving
  • D
    darmie

    I need a guide on how to use Graphics5.

    Here's my attempt below (I followed the Kha 3D tutorial by Lubos Lenco):

    package exp;
    
    import kha.Framebuffer;
    import kha.Color;
    import kha.Shaders;
    import kha.graphics5.RenderTarget;
    import kha.graphics5.TextureFormat;
    import kha.graphics4.DepthStencilFormat;
    import kha.graphics5.CommandList;
    import kha.graphics5.PipelineState;
    import kha.graphics5.VertexStructure;
    import kha.graphics5.VertexBuffer;
    import kha.graphics5.IndexBuffer;
    import kha.graphics5.FragmentShader;
    import kha.graphics5.VertexShader;
    import kha.graphics5.VertexData;
    import kha.graphics5.Usage;
    import kha.Image;
    
    class Scene {
    	public var width:Int;
    	public var height:Int;
    	public var commands:CommandList;
    	public var renderTarget:RenderTarget;
    
    	var vertexBuffer:VertexBuffer;
    	var indexBuffer:IndexBuffer;
    	var pipeline:PipelineState;
    
    	// 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
    	];
    	// Indices for our triangle, these will point to vertices above
    	static var indices:Array<Int> = [
    		0, // Bottom-left
    		1, // Bottom-right
    		2  // Top
    	];
    
    	public function new(width:Int, height:Int) {
    		this.width = width;
    		this.height = height;
    
    		this.commands = new CommandList();
    		this.renderTarget = new RenderTarget(width, height, 0, true, TextureFormat.RGBA32, NoDepthAndStencil, 0);
    
    		// 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 = cast Shaders.simple_frag;
    		pipeline.vertexShader = cast Shaders.simple_vert;
    		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 update() {}
    
    	public function render(frame:Framebuffer) {
    		// A graphics object which lets us perform 3D operations
    		var g = frame.g5;
    
    		// Begin rendering
    		g.renderTargetsInvertedY();
    		g.begin(renderTarget);
    		commands.begin();
    		// commands.setRenderTargets([renderTarget]);
    		// commands.renderTargetToFramebufferBarrier(renderTarget);
    
    		// Bind state we want to draw with
    		commands.setPipeline(pipeline);
    
    		// Bind data we want to draw
    		commands.setVertexBuffers([vertexBuffer], [1]);
    		commands.setIndexBuffer(indexBuffer);
    
    		// Draw!
    		commands.drawIndexedVertices();
    		// commands.clear(renderTarget, Color.Yellow);
    
    		commands.end();
    		// End rendering
    		g.end();
    		g.swapBuffers();
    	}
    }
    
    

    I tried to build on MacOS Mojave, but the app crashed.

    posted in Kode read more