Skip to content

Instantly share code, notes, and snippets.

@Antriel
Created May 8, 2019 19:49
Show Gist options
  • Save Antriel/21e9c53a5a5ba8ea12028f2500ebfc94 to your computer and use it in GitHub Desktop.
Save Antriel/21e9c53a5a5ba8ea12028f2500ebfc94 to your computer and use it in GitHub Desktop.
Iron Perspective Triangle
package;
import iron.math.Vec4;
import iron.App;
import iron.Scene;
import iron.RenderPath;
import iron.data.*;
import iron.data.SceneFormat;
import iron.object.Object;
class Main {
// Raw data used to create the scene
static var raw:TSceneFormat;
public static function main() {
// Init and create window
kha.System.start({ title: "Empty", width: 1280, height: 720 }, function(window:kha.Window) {
App.init(ready);
});
}
static function ready() {
// Define active render path
var path = new RenderPath();
// Commands to execute each frame
path.commands = function() {
// Draw to framebuffer
path.setTarget("");
// Clear color and depth
path.clearTarget(0xff696969, 1.0);
// Loop through visible meshes and draw "mesh" context
// "mesh" context is retrieved from materials attached to mesh
// If material with such context exists, mesh is drawn using this material
path.drawMeshes("mesh");
};
RenderPath.setActive(path);
// Create empty scene
raw = {
name: "Scene",
shader_datas: [],
material_datas: [],
mesh_datas: [],
camera_datas: [],
camera_ref: "Camera",
objects: []
};
Data.cachedSceneRaws.set(raw.name, raw);
addMeshShaderMaterial();
addCamera();
createMeshObject();
Scene.create(raw, function(_) {
// Scene.active.camera.P.setIdentity();
var t = Scene.active.camera.transform;
t.loc.set(0, 0, 0.5);
t.rot.fromTo(new Vec4(0, 0, -1), new Vec4(0, 0, 1));
t.buildMatrix();
// trace(Scene.active.camera.V);
trace(Scene.active.camera.P);
// trace(Scene.active.camera.VP);
// Scene.active.camera.transform.reset();
// Scene.active.camera.V.setIdentity();
// Scene.active.camera.P.setIdentity();
// Scene.active.camera.P._00 = 10000;
// Scene.active.camera.P._11 = 20000;
// Scene.active.camera.P._22 = -1;
// Scene.active.camera.P._23 = -1;
// Scene.active.camera.P._32 = -0;
trace(Scene.active.camera.P);
App.notifyOnUpdate(function() {
// t.loc.z -= 0.02;
// t.buildMatrix();
// Scene.active.camera.transform.rotate(new Vec4(0.1, 0.2, 0.3), 0.02);
});
});
}
static function addMeshShaderMaterial() {
// Create triangle mesh
// Build vertex buffer
// Using Short4Norm vertex data to save memory
// Short = 2 byte values ranging from -32768 to 32767
// Short4 = 4 components per vertex (1 component is padding here for 32bit alignment)
// Short4Norm = values are normalized to <-1, 1> range in the shader
var range = 32767;
var half = Std.int(range / 2);
var vb = new kha.arrays.Int16Array(12);
// vb[0] = -half; vb[1] = -half; vb[2 ] = 0; vb[3 ] = half; // Vertex 0 xyzw
// vb[4] = half; vb[5] = -half; vb[6 ] = 0; vb[7 ] = half; // Vertex 1 xyzw
// vb[8] = 0; vb[9] = half; vb[10] = 0; vb[11] = half; // Vertex 2 xyzw
vb[0] = -1; vb[1] = -1; vb[2 ] = 0; vb[3 ] = 1; // Vertex 0 xyzw
vb[4] = 1; vb[5] = -1; vb[6 ] = 0; vb[7 ] = 1; // Vertex 1 xyzw
vb[8] = 0; vb[9] = 1; vb[10] = 0; vb[11] = 1; // Vertex 2 xyzw
// Build triangle index buffer
var ib = new kha.arrays.Uint32Array(3);
ib[0] = 0; // Point to vertex 0
ib[1] = 1; // Point to vertex 1
ib[2] = 2; // Point to vertex 2
// Raw data used to create the mesh
var mesh:TMeshData = {
name: "TriangleMesh",
vertex_arrays: [
{ attrib: "pos", values: vb }
],
index_arrays: [
{ material: 0, values: ib }
],
// Apply scale to world matrix when drawing this mesh
scale_pos: 1.0
};
raw.mesh_datas.push(mesh);
// Create shader for our triangle mesh
var sh:TShaderData = {
name: "MyShader",
contexts: [
{
name: "mesh",
vertex_shader: "mesh.vert",
fragment_shader: "mesh.frag",
compare_mode: "less",
// cull_mode: "clockwise",
cull_mode: "none",
// depth_write: true,
depth_write: false,
constants: [
{ name: "color", type: "vec3" },
{ name: "WVP", type: "mat4", link: "_worldViewProjectionMatrix" }
// { name: "WVP", type: "mat4" }
],
vertex_elements: [
{ name: "pos", data: "short4norm" }
]
}
]
};
raw.shader_datas.push(sh);
// Pass as color uniform to the material
var col = new kha.arrays.Float32Array(3);
col[0] = 1.0; col[1] = 1.0; col[2] = 0.0;
// var WVP = kha.math.Matrix4.identity();
var md:TMaterialData = {
name: "MyMaterial",
shader: "MyShader",
contexts: [
{
name: "mesh",
bind_constants: [
{ name: "color", vec3: col },
// { name: "WVP", mat4: WVP }
]
}
]
};
raw.material_datas.push(md);
}
static function addCamera() {
var cd:TCameraData = {
name: "MyCamera",
// near_plane: 0.1,
near_plane: 0,
far_plane: 100.0,
frustum_culling: false,
fov: 0.85
};
raw.camera_datas.push(cd);
var co:TObj = {
name: "Camera",
type: "camera_object",
data_ref: "MyCamera",
transform: null
};
raw.objects.push(co);
}
static function createMeshObject() {
// Create new mesh object
var tri:TObj = {
name: "Triangle",
type: "mesh_object",
data_ref: "TriangleMesh",
material_refs: ["MyMaterial"],
transform: null
};
raw.objects.push(tri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment