diff options
Diffstat (limited to 'v_windows/v/examples/sokol/06_obj_viewer/modules')
4 files changed, 1040 insertions, 0 deletions
diff --git a/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/obj.v b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/obj.v new file mode 100644 index 0000000..1d239a0 --- /dev/null +++ b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/obj.v @@ -0,0 +1,595 @@ +module obj + +/********************************************************************** +* +* .obj loader +* +* Copyright (c) 2021 Dario Deledda. All rights reserved. +* Use of this source code is governed by an MIT license +* that can be found in the LICENSE file. +* +* TODO: +**********************************************************************/ +import gg.m4 +import strconv + +enum F_state { + start + first + ints + decimals + exp_start + exp_sign + exp_int +} + +// read a int from a string +fn get_int(s string, start_index int) (int, int) { + mut i := start_index + mut res := 0 + mut sgn := 1 + + mut state := F_state.start + for true { + if i >= s.len { + break + } + c := s[i] + if state == .start { + match c { + `+` { + i++ + state = .ints + continue + } + `-` { + sgn = -1 + i++ + state = .ints + continue + } + `0`...`9` { + state = .ints + } + ` `, `\t` { + i++ + continue + } + else { // no number found + break + } + } + } + + if state == .ints { + match c { + `0`...`9` { + // println("$res => ${(int(c) - 48)}") + res = res * 10 + (int(c) - 48) + i++ + continue + } + else { + break + } + } + } + } + // println("---") + return res * sgn, i +} + +// reas a float number from a string +fn get_float(s string, start_index int) (f64, int) { + mut i1 := start_index //+ 1 + for i1 < s.len && s[i1] in [` `, `\t`] { + i1++ + } + mut i := i1 + for i < s.len { + if s[i] in [` `, `\t`] { + break + } + i++ + } + // println(" get_float: ($start_index,$i) [${s[start_index..i]}]") + // f_res := strconv.atof_quick(s[start_index..i]) + f_res := strconv.atof_quick(s[i1..i]) + return f_res, i +} + +// read 3 f32 in sequence from a string +fn parse_3f(row string, start_index int) m4.Vec4 { + // println(row) + mut i := start_index //+ 1 + mut f1 := f64(0) + mut f2 := f64(0) + f0, mut p := get_float(row, i) + // print("Here f0: $f0 $p ") + f1, p = get_float(row, p + 1) + // print("Here f1: $f1 $p ") + f2, p = get_float(row, p + 1) + // print("Here f2: $f2 $p ") + return m4.Vec4{ + e: [f32(f0), f32(f1), f32(f2), 1]! + } +} + +// reas a sequence of f32 from a string +fn (mut m ObjPart) parse_floats(row string, start_index int) m4.Vec4 { + mut i := start_index //+ 1 + mut res_f := f64(0) + mut res := m4.Vec4{ + e: [f32(0), 0, 0, 1]! + } + mut c := 0 + for true { + res_f, i = get_float(row, i) + unsafe { + res.e[c] = f32(res_f) + } + c++ + i++ + if i >= row.len { + break + } + } + return res +} + +// read and manage all the faes from an .obj file data +fn (mut p Part) parse_faces(row string, start_index int, obj ObjPart) { + mut i := start_index + 1 + mut res := [][3]int{} + mut v := 0 + mut t := 0 + mut n := 0 + // println("row: ${row[i..]}") + for true { + t = 0 + n = 0 + if i >= row.len { + break + } + mut c := row[i] + if (c > `9` || c < `0`) && c != `-` { + i++ + continue + } + v, i = get_int(row, i) + if i < row.len && row[i] == `/` { + if row[i + 1] != `/` { + t, i = get_int(row, i + 1) + if i < row.len && row[i] == `/` { + n, i = get_int(row, i + 1) + } + } else { + i++ + n, i = get_int(row, i + 1) + } + } + // manage negative indexes + // NOTE: not well suporeted now + if v < 0 { + // println("${obj.v.len} ${obj.v.len-c}") + v = obj.v.len - v + 1 + // exit(0) + } + if n < 0 { + n = obj.vn.len - n + 1 + } + if t < 0 { + t = obj.vt.len - t + 1 + } + res << [v - 1, n - 1, t - 1]! + } + // println("ok res: ${res}") + // println(p.faces.len) + p.faces << res +} + +// parse the obj file, if single_material is true it use only one default material +pub fn (mut obj_part ObjPart) parse_obj_buffer(rows []string, single_material bool) { + mut mat_count := 0 + mut row_count := 0 + default_part := Part{ + name: 'default part' + } + obj_part.part << default_part + // println("OBJ file has ${rows.len} rows") + for c, row in rows { + // println("$c $row") + mut i := 0 + row_count++ + for true { + if i >= row.len { + break + } + match row[i] { + `s` { + break + } + `m` { + if row[i..i + 6] == 'mtllib' { + obj_part.material_file = row[i + 7..].trim_space() + obj_part.load_materials() + } + break + } + `o`, `g` { + mut part := Part{} + part.name = row[i + 1..].trim_space() + obj_part.part << part + mat_count = 0 + break + } + `u` { + if single_material == false && row[i..i + 6] == 'usemtl' { + material := row[i + 7..].trim_space() + // println("material: $material") + // manage multiple materials in an part + if obj_part.part[obj_part.part.len - 1].material.len > 0 { + mat_count++ + mut part := Part{} + if mat_count > 1 { + li := obj_part.part[obj_part.part.len - 1].name.last_index('_m') or { + obj_part.part[obj_part.part.len - 1].name.len - 1 + } + part.name = obj_part.part[obj_part.part.len - 1].name[..li] + + '_m${mat_count:02}' + } else { + part.name = obj_part.part[obj_part.part.len - 1].name + '_m01' + } + obj_part.part << part + } + obj_part.part[obj_part.part.len - 1].material = material + } + break + } + `v` { + i++ + match row[i] { + // normals + `n` { + obj_part.vn << parse_3f(row, i + 2) + // println("Vertex line: $c") + break + } + // parameteres uvw + `p` { + obj_part.vp << parse_3f(row, i + 2) + // println("Vertex line: ${obj_part.vp.len}") + break + } + // texture uvw + `t` { + obj_part.vt << obj_part.parse_floats(row, i + 2) + // println("Vertex line: $c") + break + } + else { + obj_part.v << parse_3f(row, i + 1) + // println("$row => ${obj_part.v[obj_part.v.len-1]}") + break + } + } + } + `f` { + // println("$c $row") + obj_part.part[obj_part.part.len - 1].parse_faces(row, i, obj_part) + // println(obj_part.part[obj_part.part.len - 1].faces.len) + // println("Faces line: $c") + break + } + // end of the line, comments + `\n`, `#` { + break + } + else {} + } + i++ + } + // if c == 2 { break } + if c % 100000 == 0 && c > 0 { + println('$c rows parsed') + } + } + println('$row_count .obj Rows parsed') + // remove default part if empty + if obj_part.part.len > 1 && obj_part.part[0].faces.len == 0 { + obj_part.part = obj_part.part[1..] + } +} + +// load the materials if found the .mtl file +fn (mut obj_part ObjPart) load_materials() { + rows := read_lines_from_file(obj_part.material_file) + println('Material file [$obj_part.material_file] $rows.len Rows.') + for row in rows { + // println("$row") + mut i := 0 + for true { + if i >= row.len { + break + } + match row[i] { + `n` { + if row[i..i + 6] == 'newmtl' { + name := row[i + 6..].trim_space() + mut mat := Material{ + name: name + } + obj_part.mat << mat + break + } + } + `K` { + if row[i + 1] !in [`a`, `d`, `e`, `s`] { + break + } + k_name := row[i..i + 2] + i += 3 + value := parse_3f(row, i) + obj_part.mat[obj_part.mat.len - 1].ks[k_name] = value + break + } + `N` { + n_name := row[i..i + 2] + i += 3 + value, _ := get_float(row, i) + obj_part.mat[obj_part.mat.len - 1].ns[n_name] = f32(value) + break + } + `m` { + if row[i..i + 4] == 'map_' { + name := row[i..i + 6] + if (i + 7) < row.len { + file_name := row[i + 7..].trim_space() + obj_part.mat[obj_part.mat.len - 1].maps[name] = file_name + } + break + } + } + // trasparency + `d` { + if row[i + 1] == ` ` { + value, _ := get_float(row, i + 2) + obj_part.mat[obj_part.mat.len - 1].ns['Tr'] = f32(value) + } + } + `T` { + if row[i + 1] == `r` { + value, _ := get_float(row, i + 3) + obj_part.mat[obj_part.mat.len - 1].ns['Tr'] = f32(1.0 - value) + } + } + // end of the line, comments + `\n`, `#` { + break + } + ` `, `\t` { + i++ + continue + } + else { + break + } + } + i++ + } + } + + // create map material name => material index + for i, m in obj_part.mat { + if m.name !in obj_part.mat_map { + obj_part.mat_map[m.name] = i + } + } + + println('Material Loading Done!') +} + +//============================================================================== +// Sokol data +//============================================================================== + +// vertex data struct +pub struct Vertex_pnct { +pub mut: + x f32 // poistion + y f32 + z f32 + nx f32 // normal + ny f32 + nz f32 + color u32 = 0xFFFFFFFF // color + u f32 // uv + v f32 + // u u16 // for compatibility with D3D11 + // v u16 // for compatibility with D3D11 +} + +// struct used to pass the data to the sokol calls +pub struct Skl_buffer { +pub mut: + vbuf []Vertex_pnct + ibuf []u32 + n_vertex u32 +} + +// transforms data from .obj format to buffer ready to be used in the render +pub fn (mut obj_part ObjPart) get_buffer(in_part_list []int) Skl_buffer { + // in_part := 0 + mut v_count_index := 0 + mut out_buf := Skl_buffer{} + + mut cache := map[string]int{} + mut cache_hit := 0 + + // has_normals := obj_part.vn.len > 0 + // has_uvs := obj_part.vt.len > 0 + + for in_part in in_part_list { + part := obj_part.part[in_part] + for fc, face in part.faces { + // println("$fc $face") + // default 3 faces + mut v_seq := [0, 1, 2] + if face.len == 4 { + v_seq = [0, 1, 2, 0, 2, 3] + } + + // if big faces => use the fan of triangles as solution + // Note: this trick doesn't work with concave faces + if face.len > 4 { + v_seq = [] + mut i := 1 + for i < (face.len - 1) { + v_seq << 0 + v_seq << i + v_seq << (i + 1) + i++ + } + // println("BIG FACES! ${fc} ${face.len} v_seq:${v_seq.len}") + } + + // no vertex index, generate normals + if face[0][1] == -1 && face.len >= 3 { + mut v_count := 0 + v0 := face[v_count + 0][0] + v1 := face[v_count + 1][0] + v2 := face[v_count + 2][0] + + vec0 := obj_part.v[v2] - obj_part.v[v1] + vec1 := obj_part.v[v0] - obj_part.v[v1] + tmp_normal := vec0 % vec1 + + for v_count < face.len { + obj_part.vn << tmp_normal + obj_part.part[in_part].faces[fc][v_count][1] = obj_part.vn.len - 1 + v_count++ + } + } + + for vertex_index in v_seq { + // position + if vertex_index >= face.len { + continue + } + v_index := face[vertex_index][0] // vertex index + n_index := face[vertex_index][1] // normal index + t_index := face[vertex_index][2] // uv texture index + key := '${v_index}_${n_index}_$t_index' + if key !in cache { + cache[key] = v_count_index + mut pnct := Vertex_pnct{ + x: obj_part.v[v_index].e[0] + y: obj_part.v[v_index].e[1] + z: obj_part.v[v_index].e[2] + } + // normal + if n_index >= 0 { + pnct.nx = obj_part.vn[n_index].e[0] + pnct.ny = obj_part.vn[n_index].e[1] + pnct.nz = obj_part.vn[n_index].e[2] + } + // texture uv + if t_index >= 0 { + pnct.u = obj_part.vt[t_index].e[0] + pnct.v = obj_part.vt[t_index].e[1] + } + + out_buf.vbuf << pnct + out_buf.ibuf << u32(v_count_index) + v_count_index++ + } else { + // println("Cache used! $key") + out_buf.ibuf << u32(cache[key]) + cache_hit++ + } + } + } + } + + /* + println("------------") + for c1, x1 in out_buf.vbuf[..10] { + println("$c1 $x1") + } + println(out_buf.ibuf[..10]) + */ + // println("vbuf size: ${out_buf.vbuf.len} ibuf size: ${out_buf.ibuf.len} Cache hit: $cache_hit") + out_buf.n_vertex = u32(out_buf.ibuf.len) + return out_buf +} + +//============================================================================== +// Utility +//============================================================================== +// print on the console the summary of the .obj model loaded +pub fn (obj_part ObjPart) summary() { + println('---- Stats ----') + println('vertices: $obj_part.v.len') + println('normals : $obj_part.vn.len') + println('uv : $obj_part.vt.len') + println('parts : $obj_part.part.len') + // Parts + println('---- Parts ----') + for c, x in obj_part.part { + println('${c:3} [${x.name:-16}] mat:[${x.material:-10}] ${x.faces.len:7} faces') + } + // Materials + println('---- Materials ----') + println('Material dict: $obj_part.mat_map.keys()') + for c, mat in obj_part.mat { + println('${c:3} [${mat.name:-16}]') + for k, v in mat.ks { + print('$k = $v') + } + for k, v in mat.ns { + println('$k = $v') + } + for k, v in mat.maps { + println('$k = $v') + } + } +} + +// debug test function, do not remove. +pub fn tst() { + /* + //fname := "capsule.obj" + //fname := "Forklift.obj" + fname := "cube.obj" + //fname := "Orange Robot 3D ObjPart.obj" + + mut obj := ObjPart{} + buf := os.read_lines(fname) or { panic(err.msg) } + obj.parse_obj_buffer(buf) + obj.summary() + */ + /* + a :="f 7048 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7003" + mut f1 := 0 + mut f2 := 0 + f0,mut p := get_int(a,1) + f1, p = get_int(a,p) + f2, p = get_int(a,p) + println("res: ${f0} ${f1} ${f2}") + */ + /* + a :="v -0 0.107769 -0.755914" + println("${parse_3f(a,1)}") + */ + /* + ort := m4.ortho(0,300,0,200,0,0) + println(ort) + a := m4.vec3(0,0,0) + println("a: $a") + res := m4.mul_vec(ort, a) + println("res:\n${res}") + */ + s := 'K 1 1 1' + r := strconv.atof_quick(s[1..s.len - 1]) + println(r) +} diff --git a/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/rend.v b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/rend.v new file mode 100644 index 0000000..d4ea3bc --- /dev/null +++ b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/rend.v @@ -0,0 +1,297 @@ +/********************************************************************** +* +* .obj loader +* +* Copyright (c) 2021 Dario Deledda. All rights reserved. +* Use of this source code is governed by an MIT license +* that can be found in the LICENSE file. +* +* TODO: +**********************************************************************/ +module obj + +import sokol.gfx +import gg.m4 +import math +import stbi + +/****************************************************************************** +* Texture functions +******************************************************************************/ +pub fn create_texture(w int, h int, buf &byte) C.sg_image { + sz := w * h * 4 + mut img_desc := C.sg_image_desc{ + width: w + height: h + num_mipmaps: 0 + min_filter: .linear + mag_filter: .linear + // usage: .dynamic + wrap_u: .clamp_to_edge + wrap_v: .clamp_to_edge + label: &byte(0) + d3d11_texture: 0 + } + // comment if .dynamic is enabled + img_desc.data.subimage[0][0] = C.sg_range{ + ptr: buf + size: size_t(sz) + } + + sg_img := C.sg_make_image(&img_desc) + return sg_img +} + +pub fn destroy_texture(sg_img C.sg_image) { + C.sg_destroy_image(sg_img) +} + +pub fn load_texture(file_name string) C.sg_image { + buffer := read_bytes_from_file(file_name) + stbi.set_flip_vertically_on_load(true) + img := stbi.load_from_memory(buffer.data, buffer.len) or { + eprintln('Texure file: [$file_name] ERROR!') + exit(0) + } + res := create_texture(int(img.width), int(img.height), img.data) + img.free() + return res +} + +/****************************************************************************** +* Pipeline +******************************************************************************/ +pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader, texture C.sg_image) Render_data { + mut res := Render_data{} + obj_buf := obj_part.get_buffer(in_part) + res.n_vert = obj_buf.n_vertex + res.material = obj_part.part[in_part[0]].material + + // vertex buffer + mut vert_buffer_desc := C.sg_buffer_desc{ + label: 0 + } + unsafe { C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc)) } + + vert_buffer_desc.size = size_t(obj_buf.vbuf.len * int(sizeof(Vertex_pnct))) + vert_buffer_desc.data = C.sg_range{ + ptr: obj_buf.vbuf.data + size: size_t(obj_buf.vbuf.len * int(sizeof(Vertex_pnct))) + } + + vert_buffer_desc.@type = .vertexbuffer + vert_buffer_desc.label = 'vertbuf_part_${in_part:03}'.str + vbuf := gfx.make_buffer(&vert_buffer_desc) + + // index buffer + mut index_buffer_desc := C.sg_buffer_desc{ + label: 0 + } + unsafe { C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc)) } + + index_buffer_desc.size = size_t(obj_buf.ibuf.len * int(sizeof(u32))) + index_buffer_desc.data = C.sg_range{ + ptr: obj_buf.ibuf.data + size: size_t(obj_buf.ibuf.len * int(sizeof(u32))) + } + + index_buffer_desc.@type = .indexbuffer + index_buffer_desc.label = 'indbuf_part_${in_part:03}'.str + ibuf := gfx.make_buffer(&index_buffer_desc) + + mut pipdesc := C.sg_pipeline_desc{} + unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } + pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_pnct)) + + // the constants [C.ATTR_vs_a_Position, C.ATTR_vs_a_Color, C.ATTR_vs_a_Texcoord0] are generated by sokol-shdc + pipdesc.layout.attrs[C.ATTR_vs_a_Position].format = .float3 // x,y,z as f32 + pipdesc.layout.attrs[C.ATTR_vs_a_Normal].format = .float3 // x,y,z as f32 + pipdesc.layout.attrs[C.ATTR_vs_a_Color].format = .ubyte4n // color as u32 + pipdesc.layout.attrs[C.ATTR_vs_a_Texcoord0].format = .float2 // u,v as f32 + // pipdesc.layout.attrs[C.ATTR_vs_a_Texcoord0].format = .short2n // u,v as u16 + pipdesc.index_type = .uint32 + + color_state := C.sg_color_state{ + blend: C.sg_blend_state{ + enabled: true + src_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA) + dst_factor_rgb: gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA) + } + } + pipdesc.colors[0] = color_state + + pipdesc.depth = C.sg_depth_state{ + write_enabled: true + compare: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL) + } + pipdesc.cull_mode = .front + + pipdesc.label = 'pip_part_${in_part:03}'.str + + // shader + pipdesc.shader = shader + + res.bind.vertex_buffers[0] = vbuf + res.bind.index_buffer = ibuf + res.bind.fs_images[C.SLOT_tex] = texture + res.pipeline = gfx.make_pipeline(&pipdesc) + // println('Buffers part [$in_part] init done!') + + return res +} + +/****************************************************************************** +* Render functions +******************************************************************************/ +// agregate all the part by materials +pub fn (mut obj_part ObjPart) init_render_data(texture C.sg_image) { + // create shader + // One shader for all the model + shader := gfx.make_shader(C.gouraud_shader_desc(gfx.query_backend())) + + mut part_dict := map[string][]int{} + for i, p in obj_part.part { + if p.faces.len > 0 { + part_dict[p.material] << i + } + } + obj_part.rend_data.clear() + // println("Material dict: ${obj_part.mat_map.keys()}") + + for k, v in part_dict { + // println("$k => Parts $v") + + mut txt := texture + + if k in obj_part.mat_map { + mat_map := obj_part.mat[obj_part.mat_map[k]] + if 'map_Kd' in mat_map.maps { + file_name := mat_map.maps['map_Kd'] + if file_name in obj_part.texture { + txt = obj_part.texture[file_name] + // println("Texture [${file_name}] => from CACHE") + } else { + txt = load_texture(file_name) + obj_part.texture[file_name] = txt + // println("Texture [${file_name}] => LOADED") + } + } + } + // key := obj_part.texture.keys()[0] + // obj_part.rend_data << obj_part.create_pipeline(v, shader, obj_part.texture[key]) + obj_part.rend_data << obj_part.create_pipeline(v, shader, txt) + } + // println("Texture array len: ${obj_part.texture.len}") + // println("Calc bounding box.") + obj_part.calc_bbox() + println('init_render_data DONE!') +} + +pub fn (obj_part ObjPart) bind_and_draw(rend_data_index int, in_data Shader_data) u32 { + // apply the pipline and bindings + mut part_render_data := obj_part.rend_data[rend_data_index] + + // pass light position + mut tmp_fs_params := Tmp_fs_param{} + tmp_fs_params.ligth = in_data.fs_data.ligth + + if part_render_data.material in obj_part.mat_map { + mat_index := obj_part.mat_map[part_render_data.material] + mat := obj_part.mat[mat_index] + + // ambient + tmp_fs_params.ka = in_data.fs_data.ka + if 'Ka' in mat.ks { + tmp_fs_params.ka = mat.ks['Ka'] + } + + // specular + tmp_fs_params.ks = in_data.fs_data.ks + if 'Ks' in mat.ks { + tmp_fs_params.ks = mat.ks['Ks'] + } + + // specular exponent Ns + if 'Ns' in mat.ns { + tmp_fs_params.ks.e[3] = mat.ns['Ns'] / 1000.0 + } else { + // defautl value is 10 + tmp_fs_params.ks.e[3] = f32(10) / 1000.0 + } + + // diffuse + tmp_fs_params.kd = in_data.fs_data.kd + if 'Kd' in mat.ks { + tmp_fs_params.kd = mat.ks['Kd'] + } + + // alpha/transparency + if 'Tr' in mat.ns { + tmp_fs_params.kd.e[3] = mat.ns['Tr'] + } + } + + gfx.apply_pipeline(part_render_data.pipeline) + gfx.apply_bindings(part_render_data.bind) + + vs_uniforms_range := C.sg_range{ + ptr: in_data.vs_data + size: size_t(in_data.vs_len) + } + fs_uniforms_range := C.sg_range{ + ptr: unsafe { &tmp_fs_params } + size: size_t(in_data.fs_len) + } + + gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &vs_uniforms_range) + gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &fs_uniforms_range) + gfx.draw(0, int(part_render_data.n_vert), 1) + return part_render_data.n_vert +} + +pub fn (obj_part ObjPart) bind_and_draw_all(in_data Shader_data) u32 { + mut n_vert := u32(0) + // println("Parts: ${obj_part.rend_data.len}") + for i, _ in obj_part.rend_data { + n_vert += obj_part.bind_and_draw(i, in_data) + } + return n_vert +} + +pub fn (mut obj_part ObjPart) calc_bbox() { + obj_part.max = m4.Vec4{ + e: [f32(-math.max_f32), -math.max_f32, -math.max_f32, 0]! + } + obj_part.min = m4.Vec4{ + e: [f32(math.max_f32), math.max_f32, math.max_f32, 0]! + } + for v in obj_part.v { + if v.e[0] > obj_part.max.e[0] { + obj_part.max.e[0] = v.e[0] + } + if v.e[1] > obj_part.max.e[1] { + obj_part.max.e[1] = v.e[1] + } + if v.e[2] > obj_part.max.e[2] { + obj_part.max.e[2] = v.e[2] + } + + if v.e[0] < obj_part.min.e[0] { + obj_part.min.e[0] = v.e[0] + } + if v.e[1] < obj_part.min.e[1] { + obj_part.min.e[1] = v.e[1] + } + if v.e[2] < obj_part.min.e[2] { + obj_part.min.e[2] = v.e[2] + } + } + val1 := obj_part.max.mod3() + val2 := obj_part.min.mod3() + if val1 > val2 { + obj_part.radius = f32(val1) + } else { + obj_part.radius = f32(val2) + } + // println("BBox: ${obj_part.min} <=> ${obj_part.max}\nRadius: ${obj_part.radius}") +} diff --git a/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/struct.v b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/struct.v new file mode 100644 index 0000000..55a528b --- /dev/null +++ b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/struct.v @@ -0,0 +1,104 @@ +/********************************************************************** +* +* .obj loader +* +* Copyright (c) 2021 Dario Deledda. All rights reserved. +* Use of this source code is governed by an MIT license +* that can be found in the LICENSE file. +* +* TODO: +**********************************************************************/ +module obj + +import gg.m4 + +// part struct mantain the fae indexes list +pub struct Part { +pub mut: + faces [][][3]int // v n t index order, if -1 not available + name string + material string +} + +// materias struct, all Ks and Ns are stored as maps of string +pub struct Material { +pub mut: + name string + ks map[string]m4.Vec4 + ns map[string]f32 + maps map[string]string +} + +// render data used for the rendering +pub struct Render_data { +pub mut: + pipeline C.sg_pipeline + bind C.sg_bindings + n_vert u32 + material string +} + +// base object parts struct +pub struct ObjPart { +pub mut: + v []m4.Vec4 // position + vn []m4.Vec4 // normals + vp []m4.Vec4 // vertex params + vt []m4.Vec4 // textures + + name string + part []Part // parts of the ObjPart + mat []Material // list of the materials of the ObjPart + mat_map map[string]int // maping material name to its material index + texture map[string]C.sg_image // GPU loaded texture map + material_file string // .mtl file name for the .obj + + rend_data []Render_data // render data used for the rendering + + t_m m4.Mat4 = m4.unit_m4() // transform matrix for this ObjPart + // child []ObjPart // childs + // stats + min m4.Vec4 // min 3d position in the ObjPart + max m4.Vec4 // max 3d position in the ObjPart + radius f32 // bounding circle radius of the ObjPart +} + +// used in to pass the matrices to the shader +pub struct Mats { +pub mut: + mv m4.Mat4 + mvp m4.Mat4 + nm m4.Mat4 +} + +// data passed to the vertex shader +pub struct Tmp_vs_param { +pub mut: + mv m4.Mat4 + mvp m4.Mat4 + nm m4.Mat4 +} + +// data passed to the pixel shader +pub struct Tmp_fs_param { +pub mut: + ligth m4.Vec4 + ka m4.Vec4 = m4.Vec4{ + e: [f32(0.1), 0.0, 0.0, 1.0]! + } + kd m4.Vec4 = m4.Vec4{ + e: [f32(0.5), 0.5, 0.5, 1.0]! + } + ks m4.Vec4 = m4.Vec4{ + e: [f32(1.0), 1.0, 1.0, 1.0]! + } +} + +// shader data for the rendering +pub struct Shader_data { +pub mut: + vs_data &Tmp_vs_param + vs_len int + fs_data &Tmp_fs_param + fs_len int +} diff --git a/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/util.v b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/util.v new file mode 100644 index 0000000..a1e596a --- /dev/null +++ b/v_windows/v/examples/sokol/06_obj_viewer/modules/obj/util.v @@ -0,0 +1,44 @@ +module obj + +import os + +// read a file as single lines +pub fn read_lines_from_file(file_path string) []string { + mut path := '' + mut rows := []string{} + $if android { + path = 'models/' + file_path + bts := os.read_apk_asset(path) or { + eprintln('File [$path] NOT FOUND!') + return rows + } + rows = bts.bytestr().split_into_lines() + } $else { + path = os.resource_abs_path('assets/models/' + file_path) + rows = os.read_lines(path) or { + eprintln('File [$path] NOT FOUND! file_path: $file_path') + return rows + } + } + return rows +} + +// read a file as []byte +pub fn read_bytes_from_file(file_path string) []byte { + mut path := '' + mut buffer := []byte{} + $if android { + path = 'models/' + file_path + buffer = os.read_apk_asset(path) or { + eprintln('Texure file: [$path] NOT FOUND!') + exit(0) + } + } $else { + path = os.resource_abs_path('assets/models/' + file_path) + buffer = os.read_bytes(path) or { + eprintln('Texure file: [$path] NOT FOUND!') + exit(0) + } + } + return buffer +} |