1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: format most examples and tutorials, add them to v test-cleancode (#9826)

This commit is contained in:
Lukas Neubert
2021-04-20 16:16:35 +02:00
committed by GitHub
parent dff50686d6
commit 16e79bc3ca
38 changed files with 471 additions and 433 deletions

View File

@@ -9,6 +9,7 @@
* TODO:
**********************************************************************/
module obj
import sokol.gfx
import gg.m4
import math
@@ -46,10 +47,10 @@ pub fn destroy_texture(sg_img C.sg_image) {
}
pub fn load_texture(file_name string) C.sg_image {
buffer := obj.read_bytes_from_file(file_name)
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!")
eprintln('Texure file: [$file_name] ERROR!')
exit(0)
}
res := create_texture(int(img.width), int(img.height), img.data)
@@ -82,7 +83,7 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
// index buffer
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
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{
@@ -90,8 +91,8 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
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
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{}
@@ -99,10 +100,10 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
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_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
@@ -130,7 +131,7 @@ pub fn (mut obj_part ObjPart) create_pipeline(in_part []int, shader C.sg_shader,
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!')
// println('Buffers part [$in_part] init done!')
return res
}
@@ -151,10 +152,10 @@ pub fn (mut obj_part ObjPart) init_render_data(texture C.sg_image) {
}
}
obj_part.rend_data.clear()
//println("Material dict: ${obj_part.mat_map.keys()}")
// println("Material dict: ${obj_part.mat_map.keys()}")
for k, v in part_dict {
//println("$k => Parts $v")
// println("$k => Parts $v")
mut txt := texture
@@ -164,22 +165,22 @@ pub fn (mut obj_part ObjPart) init_render_data(texture C.sg_image) {
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")
// println("Texture [${file_name}] => from CACHE")
} else {
txt = obj.load_texture(file_name)
txt = load_texture(file_name)
obj_part.texture[file_name] = txt
//println("Texture [${file_name}] => LOADED")
// 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])
// 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.")
// println("Texture array len: ${obj_part.texture.len}")
// println("Calc bounding box.")
obj_part.calc_bbox()
println("init_render_data DONE!")
println('init_render_data DONE!')
}
pub fn (obj_part ObjPart) bind_and_draw(rend_data_index int, in_data Shader_data) u32 {
@@ -187,7 +188,7 @@ pub fn (obj_part ObjPart) bind_and_draw(rend_data_index int, in_data Shader_data
mut part_render_data := obj_part.rend_data[rend_data_index]
// pass light position
mut tmp_fs_params := obj.Tmp_fs_param{}
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 {
@@ -246,27 +247,47 @@ pub fn (obj_part ObjPart) bind_and_draw(rend_data_index int, in_data Shader_data
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}")
// println("Parts: ${obj_part.rend_data.len}")
for i, _ in obj_part.rend_data {
n_vert += obj_part.bind_and_draw(i,in_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]!}
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.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] }
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}")
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}")
}