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

@@ -44,7 +44,6 @@ import sokol.sapp
import sokol.gfx
import sokol.sgl
import time
import os
import obj
@@ -52,6 +51,7 @@ import obj
#flag -I @VMODROOT/.
#include "gouraud.h" #Please use sokol-shdc to generate the necessary rt_glsl.h file from rt_glsl.glsl (see the instructions at the top of this file)
fn C.gouraud_shader_desc(gfx.Backend) &C.sg_shader_desc
const (
@@ -67,19 +67,16 @@ mut:
init_flag bool
frame_count int
mouse_x int = -1
mouse_y int = -1
scroll_y int //mouse wheel value
mouse_x int = -1
mouse_y int = -1
scroll_y int // mouse wheel value
// time
ticks i64
ticks i64
// model
obj_part &obj.ObjPart
n_vertex u32
obj_part &obj.ObjPart
n_vertex u32
// init parameters
file_name string
file_name string
single_material_flag bool
}
@@ -88,12 +85,15 @@ mut:
******************************************************************************/
[inline]
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
return m4.Vec4{e:[x, y, z, w]!}
return m4.Vec4{
e: [x, y, z, w]!
}
}
fn calc_matrices(w f32, h f32, rx f32, ry f32, in_scale f32, pos m4.Vec4) obj.Mats {
proj := m4.perspective(60, w/h, 0.01, 100.0) // set far plane to 100 fro the zoom function
view := m4.look_at(vec4(f32(0.0) ,0 , 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1, 0, 0))
proj := m4.perspective(60, w / h, 0.01, 100.0) // set far plane to 100 fro the zoom function
view := m4.look_at(vec4(f32(0.0), 0, 6, 0), vec4(f32(0), 0, 0, 0), vec4(f32(0), 1,
0, 0))
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), vec4(f32(1), 0, 0, 0))
@@ -101,14 +101,18 @@ fn calc_matrices(w f32, h f32, rx f32, ry f32, in_scale f32, pos m4.Vec4) obj.Ma
model_pos := m4.unit_m4().translate(pos)
model_m := (rym * rxm) * model_pos
model_m := (rym * rxm) * model_pos
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
mv := scale_m * model_m // model view
mv := scale_m * model_m // model view
nm := mv.inverse().transpose() // normal matrix
mvp := mv * view_proj // model view projection
mvp := mv * view_proj // model view projection
return obj.Mats{mv:mv, mvp:mvp, nm:nm}
return obj.Mats{
mv: mv
mvp: mvp
nm: nm
}
}
fn draw_model(app App, model_pos m4.Vec4) u32 {
@@ -117,12 +121,12 @@ fn draw_model(app App, model_pos m4.Vec4) u32 {
}
ws := gg.window_size_real_pixels()
dw := ws.width/2
dh := ws.height/2
dw := ws.width / 2
dh := ws.height / 2
mut scale := f32(1)
if app.obj_part.radius > 1 {
scale = 1/(app.obj_part.radius)
scale = 1 / (app.obj_part.radius)
} else {
scale = app.obj_part.radius
}
@@ -130,29 +134,29 @@ fn draw_model(app App, model_pos m4.Vec4) u32 {
// *** vertex shader uniforms ***
rot := [f32(app.mouse_y), f32(app.mouse_x)]
mut zoom_scale := scale + f32(app.scroll_y) / (app.obj_part.radius*4)
mats := calc_matrices(dw, dh, rot[0], rot[1] , zoom_scale, model_pos)
mut zoom_scale := scale + f32(app.scroll_y) / (app.obj_part.radius * 4)
mats := calc_matrices(dw, dh, rot[0], rot[1], zoom_scale, model_pos)
mut tmp_vs_param := obj.Tmp_vs_param{
mv: mats.mv,
mvp: mats.mvp,
nm: mats.nm
mv: mats.mv
mvp: mats.mvp
nm: mats.nm
}
// *** fragment shader uniforms ***
time_ticks := f32(time.ticks() - app.ticks) / 1000
radius_light := f32(app.obj_part.radius)
x_light := f32(math.cos(time_ticks) * radius_light)
z_light := f32(math.sin(time_ticks) * radius_light)
radius_light := f32(app.obj_part.radius)
x_light := f32(math.cos(time_ticks) * radius_light)
z_light := f32(math.sin(time_ticks) * radius_light)
mut tmp_fs_params := obj.Tmp_fs_param{}
tmp_fs_params.ligth = m4.vec3(x_light, radius_light, z_light)
sd := obj.Shader_data{
vs_data: &tmp_vs_param
vs_len: int(sizeof(tmp_vs_param))
vs_len: int(sizeof(tmp_vs_param))
fs_data: &tmp_fs_params
fs_len: int(sizeof(tmp_fs_params))
fs_len: int(sizeof(tmp_fs_params))
}
return app.obj_part.bind_and_draw_all(sd)
@@ -179,8 +183,8 @@ fn frame(mut app App) {
// render the data
draw_start_glsl(app)
draw_model(app, m4.Vec4{})
// uncoment if you want a raw benchmark mode
/*
// uncoment if you want a raw benchmark mode
/*
mut n_vertex_drawn := u32(0)
n_x_obj := 20
@@ -191,14 +195,14 @@ fn frame(mut app App) {
}
}
}
*/
*/
draw_end_glsl(app)
//println("v:$n_vertex_drawn")
// println("v:$n_vertex_drawn")
app.frame_count++
}
fn draw_start_glsl(app App){
fn draw_start_glsl(app App) {
if app.init_flag == false {
return
}
@@ -206,7 +210,7 @@ fn draw_start_glsl(app App){
gfx.apply_viewport(0, 0, ws.width, ws.height, true)
}
fn draw_end_glsl(app App){
fn draw_end_glsl(app App) {
gfx.end_pass()
gfx.commit()
}
@@ -240,7 +244,6 @@ fn my_init(mut app App) {
app.texture = obj.create_texture(1, 1, tmp_txt)
free(tmp_txt)
}
// glsl
app.obj_part.init_render_data(app.texture)
app.init_flag = true
@@ -248,11 +251,11 @@ fn my_init(mut app App) {
fn cleanup(mut app App) {
gfx.shutdown()
/*
/*
for _, mat in app.obj_part.texture {
obj.destroy_texture(mat)
}
*/
*/
}
/******************************************************************************
@@ -280,20 +283,21 @@ fn my_event_manager(mut ev gg.Event, mut app App) {
/******************************************************************************
* Main
******************************************************************************/
[console] // is needed for easier diagnostics on windows
// is needed for easier diagnostics on windows
[console]
fn main() {
/*
/*
obj.tst()
exit(0)
*/
*/
// App init
mut app := &App{
gg: 0
gg: 0
obj_part: 0
}
app.file_name = "v.obj_" // default object is the v logo
app.file_name = 'v.obj_' // default object is the v logo
app.single_material_flag = false
$if !android {
@@ -302,7 +306,7 @@ fn main() {
eprintln('file_name : name of the .obj file, it must be in the folder "assets/models"')
eprintln(' if no file name is passed the default V logo will be showed.')
eprintln(' if you want custom models you can put them in the folder "assets/models".')
eprintln('single_material_flag: if true the viewer use for all the model\'s parts the default material\n')
eprintln("single_material_flag: if true the viewer use for all the model's parts the default material\n")
exit(0)
}
@@ -312,8 +316,8 @@ fn main() {
if os.args.len >= 3 {
app.single_material_flag = os.args[2].bool()
}
println("Loading model: $app.file_name")
println("Using single material: $app.single_material_flag")
println('Loading model: $app.file_name')
println('Using single material: $app.single_material_flag')
}
app.gg = gg.new_context(