2021-03-25 18:53:13 +03:00
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* .obj viewer
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Example .obj model of V from SurmanPP
|
|
|
|
*
|
|
|
|
* HOW TO COMPILE SHADERS:
|
2021-11-29 19:26:32 +03:00
|
|
|
* Run `v shader .` in this directory to compile the shaders.
|
|
|
|
* For more info and help with shader compilation see `docs.md` and `v help shader`.
|
2021-03-25 18:53:13 +03:00
|
|
|
*
|
|
|
|
* ALTERNATIVE .OBJ MODELS:
|
|
|
|
* you can load alternative models putting them in the "assets/model" folder with or without their .mtl file.
|
|
|
|
* use the program help for further instructions.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - frame counter
|
|
|
|
**********************************************************************/
|
|
|
|
import gg
|
|
|
|
import gg.m4
|
|
|
|
import gx
|
|
|
|
import math
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.gfx
|
|
|
|
import sokol.sgl
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import obj
|
|
|
|
|
|
|
|
// GLSL Include and functions
|
|
|
|
|
2021-04-19 19:01:47 +03:00
|
|
|
#flag -I @VMODROOT/.
|
2021-11-29 19:26:32 +03:00
|
|
|
#include "gouraud.h" # Should be generated with `v shader .` (see the instructions at the top of this file)
|
2021-04-20 17:16:35 +03:00
|
|
|
|
2022-01-02 21:36:01 +03:00
|
|
|
fn C.gouraud_shader_desc(gfx.Backend) &gfx.ShaderDesc
|
2021-03-25 18:53:13 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
win_width = 600
|
|
|
|
win_height = 600
|
|
|
|
bg_color = gx.white
|
|
|
|
)
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
gg &gg.Context = unsafe { nil }
|
2022-01-02 21:36:01 +03:00
|
|
|
texture gfx.Image
|
2021-03-25 18:53:13 +03:00
|
|
|
init_flag bool
|
|
|
|
frame_count int
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
mouse_x int = -1
|
|
|
|
mouse_y int = -1
|
|
|
|
scroll_y int // mouse wheel value
|
2021-03-25 18:53:13 +03:00
|
|
|
// time
|
2021-04-20 17:16:35 +03:00
|
|
|
ticks i64
|
2021-03-25 18:53:13 +03:00
|
|
|
// model
|
2021-04-20 17:16:35 +03:00
|
|
|
obj_part &obj.ObjPart
|
|
|
|
n_vertex u32
|
2021-03-25 18:53:13 +03:00
|
|
|
// init parameters
|
2021-04-20 17:16:35 +03:00
|
|
|
file_name string
|
2021-03-25 18:53:13 +03:00
|
|
|
single_material_flag bool
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Draw functions
|
|
|
|
******************************************************************************/
|
2021-04-07 21:39:23 +03:00
|
|
|
[inline]
|
2021-03-25 18:53:13 +03:00
|
|
|
fn vec4(x f32, y f32, z f32, w f32) m4.Vec4 {
|
2021-04-20 17:16:35 +03:00
|
|
|
return m4.Vec4{
|
|
|
|
e: [x, y, z, w]!
|
|
|
|
}
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn calc_matrices(w f32, h f32, rx f32, ry f32, in_scale f32, pos m4.Vec4) obj.Mats {
|
2021-04-20 17:16:35 +03:00
|
|
|
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))
|
2021-03-25 18:53:13 +03:00
|
|
|
view_proj := view * proj
|
|
|
|
|
|
|
|
rxm := m4.rotate(m4.rad(rx), vec4(f32(1), 0, 0, 0))
|
|
|
|
rym := m4.rotate(m4.rad(ry), vec4(f32(0), 1, 0, 0))
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
model_pos := m4.unit_m4().translate(pos)
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
model_m := (rym * rxm) * model_pos
|
2021-03-25 18:53:13 +03:00
|
|
|
scale_m := m4.scale(vec4(in_scale, in_scale, in_scale, 1))
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
mv := scale_m * model_m // model view
|
2021-03-25 18:53:13 +03:00
|
|
|
nm := mv.inverse().transpose() // normal matrix
|
2021-04-20 17:16:35 +03:00
|
|
|
mvp := mv * view_proj // model view projection
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
return obj.Mats{
|
|
|
|
mv: mv
|
|
|
|
mvp: mvp
|
|
|
|
nm: nm
|
|
|
|
}
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_model(app App, model_pos m4.Vec4) u32 {
|
|
|
|
if app.init_flag == false {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := gg.window_size_real_pixels()
|
2021-04-20 17:16:35 +03:00
|
|
|
dw := ws.width / 2
|
|
|
|
dh := ws.height / 2
|
2021-03-25 18:53:13 +03:00
|
|
|
|
|
|
|
mut scale := f32(1)
|
|
|
|
if app.obj_part.radius > 1 {
|
2021-04-20 17:16:35 +03:00
|
|
|
scale = 1 / (app.obj_part.radius)
|
2021-03-25 18:53:13 +03:00
|
|
|
} else {
|
|
|
|
scale = app.obj_part.radius
|
|
|
|
}
|
|
|
|
scale *= 3
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
// *** vertex shader uniforms ***
|
|
|
|
rot := [f32(app.mouse_y), f32(app.mouse_x)]
|
2021-04-20 17:16:35 +03:00
|
|
|
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)
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
mut tmp_vs_param := obj.Tmp_vs_param{
|
2021-04-20 17:16:35 +03:00
|
|
|
mv: mats.mv
|
|
|
|
mvp: mats.mvp
|
|
|
|
nm: mats.nm
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
// *** fragment shader uniforms ***
|
|
|
|
time_ticks := f32(time.ticks() - app.ticks) / 1000
|
2021-04-20 17:16:35 +03:00
|
|
|
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)
|
2021-03-25 18:53:13 +03:00
|
|
|
|
|
|
|
mut tmp_fs_params := obj.Tmp_fs_param{}
|
2023-05-25 16:54:46 +03:00
|
|
|
tmp_fs_params.light = m4.vec3(x_light, radius_light, z_light)
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
sd := obj.Shader_data{
|
2021-04-25 21:40:38 +03:00
|
|
|
vs_data: unsafe { &tmp_vs_param }
|
2021-04-20 17:16:35 +03:00
|
|
|
vs_len: int(sizeof(tmp_vs_param))
|
2021-04-25 21:40:38 +03:00
|
|
|
fs_data: unsafe { &tmp_fs_params }
|
2021-04-20 17:16:35 +03:00
|
|
|
fs_len: int(sizeof(tmp_fs_params))
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 21:39:23 +03:00
|
|
|
return app.obj_part.bind_and_draw_all(sd)
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(mut app App) {
|
|
|
|
ws := gg.window_size_real_pixels()
|
|
|
|
|
|
|
|
// clear
|
2022-01-02 21:36:01 +03:00
|
|
|
mut color_action := gfx.ColorAttachmentAction{
|
|
|
|
action: .clear
|
|
|
|
value: gfx.Color{
|
2021-04-07 21:39:23 +03:00
|
|
|
r: 0.0
|
|
|
|
g: 0.0
|
|
|
|
b: 0.0
|
|
|
|
a: 1.0
|
|
|
|
}
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2022-01-02 21:36:01 +03:00
|
|
|
mut pass_action := gfx.PassAction{}
|
2021-03-25 18:53:13 +03:00
|
|
|
pass_action.colors[0] = color_action
|
|
|
|
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
|
|
|
|
|
|
|
|
// render the data
|
|
|
|
draw_start_glsl(app)
|
|
|
|
draw_model(app, m4.Vec4{})
|
2023-05-25 16:54:46 +03:00
|
|
|
// uncomment if you want a raw benchmark mode
|
2021-04-20 17:16:35 +03:00
|
|
|
/*
|
2021-03-25 18:53:13 +03:00
|
|
|
mut n_vertex_drawn := u32(0)
|
|
|
|
n_x_obj := 20
|
|
|
|
|
|
|
|
for x in 0..n_x_obj {
|
|
|
|
for z in 0..30 {
|
|
|
|
for y in 0..4 {
|
|
|
|
n_vertex_drawn += draw_model(app, m4.Vec4{e:[f32((x-(n_x_obj>>1))*3),-3 + y*3,f32(-6*z),1]!})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 17:16:35 +03:00
|
|
|
*/
|
2021-03-25 18:53:13 +03:00
|
|
|
draw_end_glsl(app)
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
// println("v:$n_vertex_drawn")
|
2021-03-25 18:53:13 +03:00
|
|
|
app.frame_count++
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
fn draw_start_glsl(app App) {
|
2021-03-25 18:53:13 +03:00
|
|
|
if app.init_flag == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws := gg.window_size_real_pixels()
|
|
|
|
gfx.apply_viewport(0, 0, ws.width, ws.height, true)
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
fn draw_end_glsl(app App) {
|
2021-03-25 18:53:13 +03:00
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Init / Cleanup
|
|
|
|
******************************************************************************/
|
|
|
|
fn my_init(mut app App) {
|
|
|
|
mut object := &obj.ObjPart{}
|
|
|
|
obj_file_lines := obj.read_lines_from_file(app.file_name)
|
|
|
|
object.parse_obj_buffer(obj_file_lines, app.single_material_flag)
|
|
|
|
object.summary()
|
|
|
|
app.obj_part = object
|
|
|
|
|
|
|
|
// set max vertices,
|
|
|
|
// for a large number of the same type of object it is better use the instances!!
|
|
|
|
desc := sapp.create_desc()
|
|
|
|
gfx.setup(&desc)
|
2022-01-03 16:05:24 +03:00
|
|
|
sgl_desc := sgl.Desc{
|
2021-03-25 18:53:13 +03:00
|
|
|
max_vertices: 128 * 65536
|
|
|
|
}
|
|
|
|
sgl.setup(&sgl_desc)
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
// 1x1 pixel white, default texture
|
|
|
|
unsafe {
|
|
|
|
tmp_txt := malloc(4)
|
2022-04-15 14:58:56 +03:00
|
|
|
tmp_txt[0] = u8(0xFF)
|
|
|
|
tmp_txt[1] = u8(0xFF)
|
|
|
|
tmp_txt[2] = u8(0xFF)
|
|
|
|
tmp_txt[3] = u8(0xFF)
|
2021-03-25 18:53:13 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup(mut app App) {
|
2021-04-20 17:16:35 +03:00
|
|
|
/*
|
2021-03-25 18:53:13 +03:00
|
|
|
for _, mat in app.obj_part.texture {
|
|
|
|
obj.destroy_texture(mat)
|
|
|
|
}
|
2021-04-20 17:16:35 +03:00
|
|
|
*/
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* events handling
|
|
|
|
******************************************************************************/
|
|
|
|
fn my_event_manager(mut ev gg.Event, mut app App) {
|
|
|
|
if ev.typ == .mouse_move {
|
|
|
|
app.mouse_x = int(ev.mouse_x)
|
|
|
|
app.mouse_y = int(ev.mouse_y)
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
if ev.scroll_y != 0 {
|
|
|
|
app.scroll_y += int(ev.scroll_y)
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
if ev.typ == .touches_began || ev.typ == .touches_moved {
|
|
|
|
if ev.num_touches > 0 {
|
|
|
|
touch_point := ev.touches[0]
|
|
|
|
app.mouse_x = int(touch_point.pos_x)
|
|
|
|
app.mouse_y = int(touch_point.pos_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Main
|
|
|
|
******************************************************************************/
|
|
|
|
fn main() {
|
2021-04-20 17:16:35 +03:00
|
|
|
/*
|
2021-03-25 18:53:13 +03:00
|
|
|
obj.tst()
|
|
|
|
exit(0)
|
2021-04-20 17:16:35 +03:00
|
|
|
*/
|
2021-03-25 18:53:13 +03:00
|
|
|
|
|
|
|
// App init
|
|
|
|
mut app := &App{
|
2021-04-20 17:16:35 +03:00
|
|
|
gg: 0
|
2021-03-25 18:53:13 +03:00
|
|
|
obj_part: 0
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
app.file_name = 'v.obj_' // default object is the v logo
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
app.single_material_flag = false
|
|
|
|
$if !android {
|
|
|
|
if os.args.len > 3 || (os.args.len >= 2 && os.args[1] in ['-h', '--help', '\\?', '-?']) {
|
|
|
|
eprintln('Usage:\nshow_obj [file_name:string] [single_material_flag:(true|false)]\n')
|
|
|
|
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".')
|
2021-04-20 17:16:35 +03:00
|
|
|
eprintln("single_material_flag: if true the viewer use for all the model's parts the default material\n")
|
2021-03-25 18:53:13 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
|
|
|
if os.args.len >= 2 {
|
2021-03-25 18:53:13 +03:00
|
|
|
app.file_name = os.args[1]
|
|
|
|
}
|
|
|
|
if os.args.len >= 3 {
|
|
|
|
app.single_material_flag = os.args[2].bool()
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
println('Loading model: ${app.file_name}')
|
|
|
|
println('Using single material: ${app.single_material_flag}')
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
app.gg = gg.new_context(
|
|
|
|
width: win_width
|
|
|
|
height: win_height
|
|
|
|
create_window: true
|
|
|
|
window_title: 'V Wavefront OBJ viewer - Use the mouse wheel to zoom'
|
|
|
|
user_data: app
|
|
|
|
bg_color: bg_color
|
|
|
|
frame_fn: frame
|
|
|
|
init_fn: my_init
|
|
|
|
cleanup_fn: cleanup
|
|
|
|
event_fn: my_event_manager
|
|
|
|
)
|
|
|
|
|
|
|
|
app.ticks = time.ticks()
|
|
|
|
app.gg.run()
|
2021-06-24 11:07:09 +03:00
|
|
|
}
|