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

gg: add gg.m4, use it in the sokol GLSL examples (#8755)

This commit is contained in:
penguindark 2021-02-15 14:40:28 +01:00 committed by GitHub
parent e3649ec4d3
commit 325aef6d41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 3934 additions and 6047 deletions

View File

@ -0,0 +1,44 @@
name: Sokol Shader Examples
on:
push:
branches:
- master
paths-ignore:
- "**.md"
pull_request:
branches:
- master
paths-ignore:
- "**.md"
jobs:
sokol-shaders-can-be-compiled:
runs-on: ubuntu-20.04
timeout-minutes: 30
env:
VFLAGS: -cc tcc -no-retry-compilation
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update;
sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libxi-dev libxcursor-dev libasound2-dev xfonts-75dpi xfonts-base
- name: Build v
run: |
echo $VFLAGS
make -j4
./v -cg -cflags -Werror -o v cmd/v
- name: Shader examples can be build
run: |
wget https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
tar -xf pre-feb2021-api-changes.tar.gz
for f in examples/sokol/02_cubes_glsl/cube_glsl \
examples/sokol/03_march_tracing_glsl/rt_glsl \
examples/sokol/04_multi_shader_glsl/rt_glsl_puppy \
examples/sokol/04_multi_shader_glsl/rt_glsl_march \
; do \
echo "compiling shader $f.glsl ..."; \
sokol-tools-bin-pre-feb2021-api-changes/bin/linux/sokol-shdc --input $f.glsl --output $f.h --slang glsl330 ; \
done
for vfile in examples/sokol/0?*/*.v; do echo "compiling $vfile ..."; ./v $vfile ; done

View File

@ -128,6 +128,13 @@ pub fn new_test_session(_vargs string) TestSession {
skip_files << 'examples/database/orm.v' // try fix it
}
}
if testing.github_job != 'sokol-shaders-can-be-compiled' {
// These examples need .h files that are produced from the supplied .glsl files,
// using by the shader compiler tools in https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
skip_files << 'examples/sokol/02_cubes_glsl/cube_glsl.v'
skip_files << 'examples/sokol/03_march_tracing_glsl/rt_glsl.v'
skip_files << 'examples/sokol/04_multi_shader_glsl/rt_glsl.v'
}
if testing.github_job != 'ubuntu-tcc' {
skip_files << 'examples/wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases
// the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now

View File

@ -0,0 +1,426 @@
/**********************************************************************
*
* Sokol 3d cube demo
*
* 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:
* - add instancing
* - add an exampel with shaders
**********************************************************************/
import gg
import gx
import math
import sokol.sapp
import sokol.gfx
import sokol.sgl
const (
win_width = 800
win_height = 800
bg_color = gx.white
)
struct App {
mut:
gg &gg.Context
pip_3d C.sgl_pipeline
texture C.sg_image
init_flag bool
frame_count int
mouse_x int = -1
mouse_y int = -1
}
/******************************************************************************
*
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf byteptr) 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
}
// commen if .dynamic is enabled
img_desc.content.subimage[0][0] = C.sg_subimage_content{
ptr: buf
size: sz
}
sg_img := C.sg_make_image(&img_desc)
return sg_img
}
fn destroy_texture(sg_img C.sg_image) {
C.sg_destroy_image(sg_img)
}
// Use only if usage: .dynamic is enabled
fn update_text_texture(sg_img C.sg_image, w int, h int, buf byteptr) {
sz := w * h * 4
mut tmp_sbc := C.sg_image_content{}
tmp_sbc.subimage[0][0] = C.sg_subimage_content{
ptr: buf
size: sz
}
C.sg_update_image(sg_img, &tmp_sbc)
}
/******************************************************************************
*
* Draw functions
*
******************************************************************************/
fn draw_triangle() {
sgl.defaults()
sgl.begin_triangles()
sgl.v2f_c3b(0.0, 0.5, 255, 0, 0)
sgl.v2f_c3b(-0.5, -0.5, 0, 0, 255)
sgl.v2f_c3b(0.5, -0.5, 0, 255, 0)
sgl.end()
}
// vertex specification for a cube with colored sides and texture coords
fn cube() {
sgl.begin_quads()
// edge color
sgl.c3f(1.0, 0.0, 0.0)
// edge coord
// x,y,z, texture cord: u,v
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, 1.0)
sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(0.0, 1.0, 0.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f(1.0, -1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(1.0, 1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, -1.0, -1.0)
sgl.c3f(0.0, 0.0, 1.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(1.0, 0.5, 0.0)
sgl.v3f_t2f(1.0, -1.0, 1.0, -1.0, 1.0)
sgl.v3f_t2f(1.0, -1.0, -1.0, 1.0, 1.0)
sgl.v3f_t2f(1.0, 1.0, -1.0, 1.0, -1.0)
sgl.v3f_t2f(1.0, 1.0, 1.0, -1.0, -1.0)
sgl.c3f(0.0, 0.5, 1.0)
sgl.v3f_t2f(1.0, -1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f(1.0, -1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(-1.0, -1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, -1.0, -1.0)
sgl.c3f(1.0, 0.0, 0.5)
sgl.v3f_t2f(-1.0, 1.0, -1.0, -1.0, 1.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 1.0, 1.0)
sgl.v3f_t2f(1.0, 1.0, 1.0, 1.0, -1.0)
sgl.v3f_t2f(1.0, 1.0, -1.0, -1.0, -1.0)
sgl.end()
}
fn draw_cubes(app App) {
rot := [f32(1.0) * (app.frame_count % 360), 0.5 * f32(app.frame_count % 360)]
// rot := [f32(app.mouse_x), f32(app.mouse_y)]
sgl.defaults()
sgl.load_pipeline(app.pip_3d)
sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0)
sgl.matrix_mode_modelview()
sgl.translate(0.0, 0.0, -12.0)
sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube()
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube()
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
cube()
sgl.pop_matrix()
sgl.pop_matrix()
}
fn cube_t(r f32, g f32, b f32) {
sgl.begin_quads()
// edge color
sgl.c3f(r, g, b)
// edge coord
// x,y,z, texture cord: u,v
sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0, 0.25)
sgl.v3f_t2f(1.0, 1.0, -1.0, 0.25, 0.25)
sgl.v3f_t2f(1.0, -1.0, -1.0, 0.25, 0.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0)
sgl.c3f(r, g, b)
sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.0, 0.25)
sgl.v3f_t2f(1.0, -1.0, 1.0, 0.25, 0.25)
sgl.v3f_t2f(1.0, 1.0, 1.0, 0.25, 0.0)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.0, 0.0)
sgl.c3f(r, g, b)
sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.0, 0.25)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25)
sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.25, 0.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0)
sgl.c3f(r, g, b)
sgl.v3f_t2f(1.0, -1.0, 1.0, 0.0, 0.25)
sgl.v3f_t2f(1.0, -1.0, -1.0, 0.25, 0.25)
sgl.v3f_t2f(1.0, 1.0, -1.0, 0.25, 0.0)
sgl.v3f_t2f(1.0, 1.0, 1.0, 0.0, 0.0)
sgl.c3f(r, g, b)
sgl.v3f_t2f(1.0, -1.0, -1.0, 0.0, 0.25)
sgl.v3f_t2f(1.0, -1.0, 1.0, 0.25, 0.25)
sgl.v3f_t2f(-1.0, -1.0, 1.0, 0.25, 0.0)
sgl.v3f_t2f(-1.0, -1.0, -1.0, 0.0, 0.0)
sgl.c3f(r, g, b)
sgl.v3f_t2f(-1.0, 1.0, -1.0, 0.0, 0.25)
sgl.v3f_t2f(-1.0, 1.0, 1.0, 0.25, 0.25)
sgl.v3f_t2f(1.0, 1.0, 1.0, 0.25, 0.0)
sgl.v3f_t2f(1.0, 1.0, -1.0, 0.0, 0.0)
sgl.end()
}
fn draw_texture_cubes(app App) {
rot := [f32(app.mouse_x), f32(app.mouse_y)]
sgl.defaults()
sgl.load_pipeline(app.pip_3d)
sgl.enable_texture()
sgl.texture(app.texture)
sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 100.0)
sgl.matrix_mode_modelview()
sgl.translate(0.0, 0.0, -12.0)
sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_t(1, 1, 1)
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_t(1, 1, 1)
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-3.0 * sgl.rad(2 * rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(3.0 * sgl.rad(2 * rot[1]), 0.0, 0.0, 1.0)
cube_t(1, 1, 1)
sgl.pop_matrix()
sgl.pop_matrix()
sgl.disable_texture()
}
fn cube_field(app App) {
rot := [f32(app.mouse_x), f32(app.mouse_y)]
xyz_sz := f32(2.0)
field_size := 20
sgl.defaults()
sgl.load_pipeline(app.pip_3d)
sgl.enable_texture()
sgl.texture(app.texture)
sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0)
sgl.matrix_mode_modelview()
sgl.translate(field_size, 0.0, -120.0)
sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0)
// draw field_size*field_size cubes
for y in 0 .. field_size {
for x in 0 .. field_size {
sgl.push_matrix()
z := f32(math.cos(f32(x * 2) / field_size) * math.sin(f32(y * 2) / field_size) * xyz_sz) * (xyz_sz * 5)
sgl.translate(x * xyz_sz, z, y * xyz_sz)
cube_t(f32(f32(x) / field_size), f32(f32(y) / field_size), 1)
sgl.pop_matrix()
}
}
sgl.disable_texture()
}
fn frame(mut app App) {
ws := gg.window_size()
ratio := f32(ws.width) / ws.height
dw := ws.width
dh := ws.height
ww := int(dh / 3) // not a bug
hh := int(dh / 3)
x0 := int(f32(dw) * 0.05)
// x1 := dw/2
y0 := 0
y1 := int(f32(dh) * 0.5)
app.gg.begin()
// sgl.defaults()
// 2d triangle
sgl.viewport(x0, y0, ww, hh, true)
draw_triangle()
// colored cubes with viewport
sgl.viewport(x0, y1, ww, hh, true)
draw_cubes(app)
// textured cubed with viewport
sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true)
draw_texture_cubes(app)
// textured field of cubes with viewport
sgl.viewport(0, int(dh / 5), dw, int(dh * ratio), true)
cube_field(app)
app.frame_count++
app.gg.end()
}
/******************************************************************************
*
* Init / Cleanup
*
******************************************************************************/
fn my_init(mut app App) {
app.init_flag = true
// 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)
sgl_desc := C.sgl_desc_t{
max_vertices: 50 * 65536
}
sgl.setup(&sgl_desc)
// 3d pipeline
mut pipdesc := C.sg_pipeline_desc{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }
pipdesc.blend.enabled = true
pipdesc.blend.src_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
pipdesc.blend.dst_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
depth_compare_func: gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
}
pipdesc.rasterizer = C.sg_rasterizer_state{
cull_mode: .back
}
app.pip_3d = sgl.make_pipeline(&pipdesc)
// create chessboard texture 256*256 RGBA
w := 256
h := 256
sz := w * h * 4
tmp_txt := unsafe { malloc(sz) }
mut i := 0
for i < sz {
unsafe {
y := (i >> 0x8) >> 5 // 8 cell
x := (i & 0xFF) >> 5 // 8 cell
// upper left corner
if x == 0 && y == 0 {
tmp_txt[i] = byte(0xFF)
tmp_txt[i + 1] = byte(0)
tmp_txt[i + 2] = byte(0)
tmp_txt[i + 3] = byte(0xFF)
}
// low right corner
else if x == 7 && y == 7 {
tmp_txt[i] = byte(0)
tmp_txt[i + 1] = byte(0xFF)
tmp_txt[i + 2] = byte(0)
tmp_txt[i + 3] = byte(0xFF)
} else {
col := if ((x + y) & 1) == 1 { 0xFF } else { 0 }
tmp_txt[i] = byte(col) // red
tmp_txt[i + 1] = byte(col) // green
tmp_txt[i + 2] = byte(col) // blue
tmp_txt[i + 3] = byte(0xFF) // alpha
}
i += 4
}
}
unsafe {
app.texture = create_texture(w, h, tmp_txt)
free(tmp_txt)
}
}
fn cleanup(mut app App) {
gfx.shutdown()
}
/******************************************************************************
*
* event
*
******************************************************************************/
fn my_event_manager(mut ev sapp.Event, mut app App) {
if ev.typ == .mouse_move {
app.mouse_x = int(ev.mouse_x)
app.mouse_y = int(ev.mouse_y)
}
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() {
// App init
mut app := &App{
gg: 0
}
app.gg = gg.new_context(
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: '3D Cube Demo'
user_data: app
bg_color: bg_color
frame_fn: frame
init_fn: my_init
cleanup_fn: cleanup
event_fn: my_event_manager
)
app.gg.run()
}

View File

@ -0,0 +1,95 @@
//------------------------------------------------------------------------------
// Shader code for texcube-sapp sample.
//
// NOTE: This source file also uses the '#pragma sokol' form of the
// custom tags.
//------------------------------------------------------------------------------
//#pragma sokol @ctype mat4 my_mat4
#pragma sokol @vs vs
uniform vs_params {
mat4 mvp;
};
in vec4 pos;
in vec4 color0;
in vec2 texcoord0;
out vec4 color;
out vec2 uv;
void main() {
gl_Position = mvp * pos;
color = color0;
uv = texcoord0;
}
#pragma sokol @end
#pragma sokol @fs fs
uniform sampler2D tex;
uniform fs_params {
vec2 text_res;
float iTime;
};
in vec4 color;
in vec2 uv;
out vec4 frag_color;
//*********************************************************
// RAY TRACE
// original code from: https://www.shadertoy.com/view/ldS3DW
//*********************************************************
float sphere(vec3 ray, vec3 dir, vec3 center, float radius)
{
vec3 rc = ray-center;
float c = dot(rc, rc) - (radius*radius);
float b = dot(dir, rc);
float d = b*b - c;
float t = -b - sqrt(abs(d));
float st = step(0.0, min(t,d));
return mix(-1.0, t, st);
}
vec3 background(float t, vec3 rd)
{
vec3 light = normalize(vec3(sin(t), 0.6, cos(t)));
float sun = max(0.0, dot(rd, light));
float sky = max(0.0, dot(rd, vec3(0.0, 1.0, 0.0)));
float ground = max(0.0, -dot(rd, vec3(0.0, 1.0, 0.0)));
return (pow(sun, 256.0)+0.2*pow(sun, 2.0))*vec3(2.0, 1.6, 1.0) +
pow(ground, 0.5)*vec3(0.4, 0.3, 0.2) +
pow(sky, 1.0)*vec3(0.5, 0.6, 0.7);
}
vec4 mainImage(vec2 fragCoord)
{
vec2 uv = (fragCoord-vec2(0.4,0.4))*2.0;
//vec2 uv = (-1.0 + 2.0*fc.xy / text_res.xy) * vec2(text_res.x/text_res.y, 1.0);
vec3 ro = vec3(0.0, 0.0, -3.0);
vec3 rd = normalize(vec3(uv, 1.0));
vec3 p = vec3(0.0, 0.0, 0.0);
float t = sphere(ro, rd, p, 1.0);
vec3 nml = normalize(p - (ro+rd*t));
vec3 bgCol = background(iTime, rd);
rd = reflect(rd, nml);
vec3 col = background(iTime, rd) * vec3(0.9, 0.8, 1.0);
vec4 fragColor = vec4( mix(bgCol, col, step(0.0, t)), 1.0 );
return fragColor;
}
//*********************************************************
//*********************************************************
void main() {
vec4 c = color;
vec4 txt = texture(tex, uv/4.0);
c = txt * c;
vec4 col_ray = mainImage(uv);
float txt_mix = mod(iTime,5);
frag_color = c*txt_mix*0.1 + col_ray ;
}
#pragma sokol @end
#pragma sokol @program cube vs fs

View File

@ -6,17 +6,44 @@
* Use of this source code is governed by an MIT license
* that can be found in the LICENSE file.
*
* HOW TO COMPILE SHADERS:
* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
* ( also look at https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md )
* - compile the .glsl shader with:
* linux : sokol-shdc --input cube_glsl.glsl --output cube_glsl.h --slang glsl330
* windows: sokol-shdc.exe --input cube_glsl.glsl --output cube_glsl.h --slang glsl330
*
* --slang parameter can be:
* - glsl330: desktop GL
* - glsl100: GLES2 / WebGL
* - glsl300es: GLES3 / WebGL2
* - hlsl4: D3D11
* - hlsl5: D3D11
* - metal_macos: Metal on macOS
* - metal_ios: Metal on iOS device
* - metal_sim: Metal on iOS simulator
* - wgpu: WebGPU
*
* you can have multiple platforms at the same time passing prameter like this: --slang glsl330:hlsl5:metal_macos
* for further infos have a look at the sokol shader tool docs.
*
* TODO:
* - add instancing
* - add an exampel with shaders
**********************************************************************/
import gg
import gx
import math
//import math
import sokol.sapp
import sokol.gfx
import sokol.sgl
import time
import gg.m4
// GLSL Include and functions
#flag -I @VROOT/.
#include "cube_glsl.h" #Please use sokol-shdc to generate the necessary cube_glsl.h file from cube_glsl.glsl (see the instructions at the top of this file)
fn C.cube_shader_desc() &C.sg_shader_desc
const (
win_width = 800
@ -34,6 +61,13 @@ mut:
mouse_x int = -1
mouse_y int = -1
// glsl
cube_pip_glsl C.sg_pipeline
cube_bind C.sg_bindings
// time
ticks i64
}
/******************************************************************************
@ -55,7 +89,7 @@ fn create_texture(w int, h int, buf byteptr) C.sg_image{
label: &byte(0)
d3d11_texture: 0
}
// commen if .dynamic is enabled
// comment if .dynamic is enabled
img_desc.content.subimage[0][0] = C.sg_subimage_content{
ptr: buf
size: sz
@ -164,7 +198,7 @@ fn draw_cubes(app App) {
sgl.pop_matrix()
}
fn cube_t(r f32,g f32,b f32) {
fn cube_texture(r f32,g f32,b f32) {
sgl.begin_quads()
// edge color
sgl.c3f(r, g, b)
@ -202,6 +236,166 @@ fn cube_t(r f32,g f32,b f32) {
sgl.end()
}
/*
Cube vertex buffer with packed vertex formats for color and texture coords.
Note that a vertex format which must be portable across all
backends must only use the normalized integer formats
(BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted
to floating point formats in the vertex shader inputs.
The reason is that D3D11 cannot convert from non-normalized
formats to floating point inputs (only to integer inputs),
and WebGL2 / GLES2 don't support integer vertex shader inputs.
*/
struct Vertex_t {
x f32
y f32
z f32
color u32
//u u16
//v u16
u f32
v f32
}
fn init_cube_glsl(mut app App) {
/* cube vertex buffer */
//d := u16(32767/8) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0) //0.05)
c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, 1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, -1.0, c, d, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc))}
vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t))
vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer
//vert_buffer_desc.usage = .immutable
vert_buffer_desc.label = "cube-vertices".str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
indices := [
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
]
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
shader := gfx.make_shader(C.cube_shader_desc())
mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated bysokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
//pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
pipdesc.index_type = .uint16
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
depth_compare_func : gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
}
pipdesc.rasterizer = C.sg_rasterizer_state {
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
app.cube_bind.vertex_buffers[0] = vbuf
app.cube_bind.index_buffer = ibuf
app.cube_bind.fs_images[C.SLOT_tex] = app.texture
app.cube_pip_glsl = gfx.make_pipeline(&pipdesc)
println("GLSL init DONE!")
}
fn draw_cube_glsl(app App){
if app.init_flag == false {
return
}
rot := [f32(app.mouse_y), f32(app.mouse_x)]
ws := gg.window_size()
//ratio := f32(ws.width)/ws.height
dw := f32(ws.width/2)
dh := f32(ws.height/2)
tr_matrix := m4.calc_tr_matrices(dw, dh, rot[0], rot[1] ,2.0)
gfx.apply_viewport(ws.width/2, 0, ws.width/2, ws.height/2, true)
// apply the pipline and bindings
gfx.apply_pipeline(app.cube_pip_glsl)
gfx.apply_bindings(app.cube_bind)
//***************
// Uniforms
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &tr_matrix, 4*16 )
// fs uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut text_res := [
f32(512), 512, // x,y resolution to pass to FS
time_ticks, // time as f32
0 // padding 4 Bytes == 1 f32
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &text_res, 4*4 )
gfx.draw(0, (3*2)*6, 1)
gfx.end_pass()
gfx.commit()
}
fn draw_texture_cubes(app App) {
rot := [f32(app.mouse_x), f32(app.mouse_y)]
sgl.defaults()
@ -217,58 +411,25 @@ fn draw_texture_cubes(app App) {
sgl.translate(0.0, 0.0, -12.0)
sgl.rotate(sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_t(1,1,1)
cube_texture(1,1,1)
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-2.0 * sgl.rad(rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(-2.0 * sgl.rad(rot[1]), 0.0, 1.0, 0.0)
cube_t(1,1,1)
cube_texture(1,1,1)
sgl.push_matrix()
sgl.translate(0.0, 0.0, 3.0)
sgl.scale(0.5, 0.5, 0.5)
sgl.rotate(-3.0 * sgl.rad(2*rot[0]), 1.0, 0.0, 0.0)
sgl.rotate(3.0 * sgl.rad(2*rot[1]), 0.0, 0.0, 1.0)
cube_t(1,1,1)
cube_texture(1,1,1)
sgl.pop_matrix()
sgl.pop_matrix()
sgl.disable_texture()
}
fn cube_field(app App){
rot := [f32(app.mouse_x), f32(app.mouse_y)]
xyz_sz := f32(2.0)
field_size := 20
sgl.defaults()
sgl.load_pipeline(app.pip_3d)
sgl.enable_texture()
sgl.texture(app.texture)
sgl.matrix_mode_projection()
sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0)
sgl.matrix_mode_modelview()
sgl.translate(field_size, 0.0, -120.0)
sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0)
sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0)
// draw field_size*field_size cubes
for y in 0..field_size {
for x in 0..field_size {
sgl.push_matrix()
z := f32(math.cos(f32(x*2)/field_size)*math.sin(f32(y*2)/field_size)*xyz_sz)*(xyz_sz*5)
sgl.translate(x*xyz_sz, z, y*xyz_sz)
cube_t(f32(f32(x)/field_size), f32(f32(y)/field_size),1)
sgl.pop_matrix()
}
}
sgl.disable_texture()
}
fn frame(mut app App) {
ws := gg.window_size()
ratio := f32(ws.width)/ws.height
@ -281,8 +442,10 @@ fn frame(mut app App) {
y0 := 0
y1 := int(f32(dh) * 0.5)
//app.gg.begin()
app.gg.begin()
//sgl.defaults()
sgl.defaults()
// 2d triangle
sgl.viewport(x0, y0, ww, hh, true)
@ -296,13 +459,26 @@ fn frame(mut app App) {
sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true)
draw_texture_cubes(app)
// textured field of cubes with viewport
sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true)
cube_field(app)
app.gg.end()
// clear
mut color_action := C.sg_color_attachment_action{
action: gfx.Action(C.SG_ACTION_DONTCARE) //C.SG_ACTION_CLEAR)
}
color_action.val[0] = 1
color_action.val[1] = 1
color_action.val[2] = 1
color_action.val[3] = 1.0
mut pass_action := C.sg_pass_action{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
// glsl cube
draw_cube_glsl(app)
app.frame_count++
app.gg.end()
}
/******************************************************************************
@ -311,7 +487,7 @@ fn frame(mut app App) {
*
******************************************************************************/
fn my_init(mut app App) {
app.init_flag = true
// set max vertices,
// for a large number of the same type of object it is better use the instances!!
@ -341,7 +517,7 @@ fn my_init(mut app App) {
w := 256
h := 256
sz := w * h * 4
tmp_txt := unsafe {malloc(sz)}
tmp_txt := unsafe{ malloc(sz) }
mut i := 0
for i < sz {
unsafe {
@ -361,7 +537,7 @@ fn my_init(mut app App) {
tmp_txt[i+2] = byte(0)
tmp_txt[i+3] = byte(0xFF)
} else {
col := if ((x+y) & 1) == 1 {0xFF} else {0}
col := if ((x+y) & 1) == 1 {0xFF} else {128}
tmp_txt[i ] = byte(col) // red
tmp_txt[i+1] = byte(col) // green
tmp_txt[i+2] = byte(col) // blue
@ -370,10 +546,12 @@ fn my_init(mut app App) {
i += 4
}
}
unsafe {
app.texture = create_texture(w, h, tmp_txt)
free(tmp_txt)
}
app.texture = create_texture(w, h, tmp_txt)
unsafe{ free(tmp_txt) }
// glsl
init_cube_glsl(mut app)
app.init_flag = true
}
fn cleanup(mut app App) {
@ -404,12 +582,17 @@ fn my_event_manager(mut ev sapp.Event, mut app App) {
* Main
*
******************************************************************************/
[console]
fn main(){
// App init
mut app := &App{
gg: 0
}
mut a := [5]int{}
a[0]=2
println(a)
app.gg = gg.new_context({
width: win_width
height: win_height
@ -424,5 +607,6 @@ fn main(){
event_fn: my_event_manager
})
app.ticks = time.ticks()
app.gg.run()
}

View File

@ -4,7 +4,7 @@
// NOTE: This source file also uses the '#pragma sokol' form of the
// custom tags.
//------------------------------------------------------------------------------
#pragma sokol @ctype mat4 hmm_mat4
//#pragma sokol @ctype mat4 hmm_mat4
#pragma sokol @vs vs
uniform vs_params {

View File

@ -7,10 +7,11 @@
* that can be found in the LICENSE file.
*
* HOW TO COMPILE SHADERS:
* - donwload sokol shader tool from: https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md
* - compile shader with:
* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
* ( also look at https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md )
* - compile the .glsl shader with:
* linux : sokol-shdc --input rt_glsl.glsl --output rt_glsl.h --slang glsl330
* windows: sokol-shdc.exe --input rt_glsl.glsl --output rt_glsl.h --slang glsl330
* windows: sokol-shdc.exe --input rt_glsl.glsl --output rt_glsl.h --slang glsl330
*
* --slang parameter can be:
* - glsl330: desktop GL
@ -29,7 +30,9 @@
* TODO:
* - frame counter
**********************************************************************/
import gg
import gg.m4
import gx
//import math
@ -39,6 +42,12 @@ import sokol.sgl
import time
// GLSL Include and functions
#flag -I @VROOT/.
#include "rt_glsl.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.rt_shader_desc() &C.sg_shader_desc
const (
win_width = 800
win_height = 800
@ -54,28 +63,15 @@ mut:
mouse_x int = -1
mouse_y int = -1
// glsl
cube_pip_glsl C.sg_pipeline
cube_bind C.sg_bindings
// time
ticks i64
}
/******************************************************************************
*
* GLSL Include and functions
*
******************************************************************************/
#flag -I @VROOT/.
#include "HandmadeMath.h"
#include "rt_glsl.h"
#include "default_include.h"
fn C.rt_shader_desc() &C.sg_shader_desc
fn C.calc_matrices(res voidptr,w f32, h f32, rx f32, ry f32, scale f32)
/******************************************************************************
*
* Texture functions
@ -141,12 +137,12 @@ struct Vertex_t {
y f32
z f32
color u32
//u u16 // for compatibility with D3D11
//v u16 // for compatibility with D3D11
u f32
v f32
}
}
fn init_cube_glsl(mut app App) {
/* cube vertex buffer */
@ -154,7 +150,7 @@ fn init_cube_glsl(mut app App) {
d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, d},
@ -194,7 +190,7 @@ fn init_cube_glsl(mut app App) {
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
indices := [
u16(0), 1, 2, 0, 2, 3,
@ -204,7 +200,7 @@ fn init_cube_glsl(mut app App) {
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
]
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = indices.len * int(sizeof(u16))
@ -212,23 +208,23 @@ fn init_cube_glsl(mut app App) {
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
shader := gfx.make_shader(C.rt_shader_desc())
mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_pos, C.ATTR_vs_color0, C.ATTR_vs_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .float2 // u,v as f32
//pipdesc.layout.attrs[C.ATTR_vs_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
pipdesc.index_type = .uint16
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
depth_compare_func : gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
@ -237,7 +233,7 @@ fn init_cube_glsl(mut app App) {
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
app.cube_bind.vertex_buffers[0] = vbuf
app.cube_bind.index_buffer = ibuf
app.cube_bind.fs_images[C.SLOT_tex] = app.texture
@ -245,6 +241,21 @@ fn init_cube_glsl(mut app App) {
println("GLSL init DONE!")
}
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4{
proj := m4.perspective(60, w/h, 0.01, 10.0)
view := m4.look_at(m4.Vec4{e:[f32(0.0),0.0,6,0]!}, m4.Vec4{e:[f32(0),0,0,0]!}, m4.Vec4{e:[f32(0),1.0,0,0]!})
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), m4.Vec4{e:[f32(1),0,0,0]!})
rym := m4.rotate(m4.rad(ry), m4.Vec4{e:[f32(0),1,0,0]!})
model := rym * rxm
scale_m := m4.scale(m4.Vec4{e:[in_scale, in_scale, in_scale, 1]!})
res := (scale_m * model)* view_proj
return res
}
fn draw_cube_glsl(app App){
if app.init_flag == false {
return
@ -254,26 +265,24 @@ fn draw_cube_glsl(app App){
ratio := f32(ws.width) / ws.height
dw := f32(ws.width / 2)
dh := f32(ws.height / 2)
mut res := [f32(0),0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]!
// use the following commented lines to rotate the 3d glsl cube
// rot := [f32(app.mouse_y), f32(app.mouse_x)]
// C.calc_matrices( voidptr(&res), dw, dh, rot[0], rot[1] ,2.3)
C.calc_matrices( voidptr(&res), dw, dh, 0, 0 ,2.3)
// calc_tr_matrices(dw, dh, rot[0], rot[1] ,2.3)
tr_matrix := calc_tr_matrices(dw, dh, 0, 0 ,2.3)
gfx.apply_viewport(0, 0, ws.width, ws.height, true)
// apply the pipline and bindings
gfx.apply_pipeline(app.cube_pip_glsl)
gfx.apply_bindings(app.cube_bind)
//***************
// Uniforms
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &res, int(sizeof(res)) )
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params, &tr_matrix, 4*16 )
// fragment shader uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [
@ -282,10 +291,10 @@ fn draw_cube_glsl(app App){
ws.height - app.mouse_y*2, // mouse y scaled
time_ticks, // time as f32
app.frame_count, // frame count
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params, &tmp_fs_params, int(sizeof(tmp_fs_params)))
// 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw
gfx.draw(0, (3 * 2) * 6, 1)
gfx.end_pass()
@ -306,11 +315,11 @@ fn frame(mut app App) {
mut pass_action := C.sg_pass_action{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
// glsl cube
draw_cube_glsl(app)
app.frame_count++
}
}
/******************************************************************************
*
@ -331,7 +340,7 @@ fn my_init(mut app App) {
w := 256
h := 256
sz := w * h * 4
tmp_txt := unsafe {malloc(sz)}
tmp_txt := unsafe { malloc(sz) }
mut i := 0
for i < sz {
unsafe {
@ -418,6 +427,6 @@ fn main(){
event_fn: my_event_manager
})
app.ticks = time.ticks()
app.ticks = time.ticks()
app.gg.run()
}

View File

@ -0,0 +1,625 @@
/**********************************************************************
*
* Sokol 3d cube multishader demo
*
* 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.
*
* HOW TO COMPILE SHADERS:
* - download the sokol shader convertor tool from https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
* ( also look at https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md )
* - compile the .glsl shared file with:
* linux : sokol-shdc --input rt_glsl_puppy.glsl --output rt_glsl_puppy.h --slang glsl330
sokol-shdc --input rt_glsl_march.glsl --output rt_glsl_march.h --slang glsl330
* windows: sokol-shdc.exe --input rt_glsl_puppy.glsl --output rt_glsl_puppy.h --slang glsl330
* sokol-shdc.exe --input rt_glsl_march.glsl --output rt_glsl_march.h --slang glsl330
*
* --slang parameter can be:
* - glsl330: desktop GL
* - glsl100: GLES2 / WebGL
* - glsl300es: GLES3 / WebGL2
* - hlsl4: D3D11
* - hlsl5: D3D11
* - metal_macos: Metal on macOS
* - metal_ios: Metal on iOS device
* - metal_sim: Metal on iOS simulator
* - wgpu: WebGPU
*
* you can have multiple platforms at the same time passing parameters like this: --slang glsl330:hlsl5:metal_macos
* for further infos have a look at the sokol shader tool docs.
*
* TODO:
* - frame counter
**********************************************************************/
import gg
import gg.m4
import gx
//import math
import sokol.sapp
import sokol.gfx
import sokol.sgl
import time
// GLSL Include and functions
#flag -I @VROOT/.
#include "rt_glsl_march.h" #Please use sokol-shdc to generate the necessary rt_glsl_march.h file from rt_glsl_march.glsl (see the instructions at the top of this file)
#include "rt_glsl_puppy.h" #Please use sokol-shdc to generate the necessary rt_glsl_puppy.h file from rt_glsl_puppy.glsl (see the instructions at the top of this file)
fn C.rt_march_shader_desc() &C.sg_shader_desc
fn C.rt_puppy_shader_desc() &C.sg_shader_desc
const (
win_width = 800
win_height = 800
bg_color = gx.white
)
struct App {
mut:
gg &gg.Context
texture C.sg_image
init_flag bool
frame_count int
mouse_x int = -1
mouse_y int = -1
mouse_down bool
// glsl
cube_pip_glsl C.sg_pipeline
cube_bind C.sg_bindings
pipe map[string]C.sg_pipeline
bind map[string]C.sg_bindings
// time
ticks i64
}
/******************************************************************************
*
* Texture functions
*
******************************************************************************/
fn create_texture(w int, h int, buf byteptr) 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.content.subimage[0][0] = C.sg_subimage_content{
ptr: buf
size: sz
}
sg_img := C.sg_make_image(&img_desc)
return sg_img
}
fn destroy_texture(sg_img C.sg_image){
C.sg_destroy_image(sg_img)
}
// Use only if usage: .dynamic is enabled
fn update_text_texture(sg_img C.sg_image, w int, h int, buf byteptr){
sz := w * h * 4
mut tmp_sbc := C.sg_image_content{}
tmp_sbc.subimage[0][0] = C.sg_subimage_content {
ptr: buf
size: sz
}
C.sg_update_image(sg_img, &tmp_sbc)
}
/******************************************************************************
*
* Draw functions
*
******************************************************************************/
/*
Cube vertex buffer with packed vertex formats for color and texture coords.
Note that a vertex format which must be portable across all
backends must only use the normalized integer formats
(BYTE4N, UBYTE4N, SHORT2N, SHORT4N), which can be converted
to floating point formats in the vertex shader inputs.
The reason is that D3D11 cannot convert from non-normalized
formats to floating point inputs (only to integer inputs),
and WebGL2 / GLES2 don't support integer vertex shader inputs.
*/
struct Vertex_t {
x f32
y f32
z f32
color u32
//u u16 // for compatibility with D3D11
//v u16 // for compatibility with D3D11
u f32
v f32
}
// march shader init
fn init_cube_glsl_m(mut app App) {
/* cube vertex buffer */
//d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, 1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, -1.0, c, d, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc))}
vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t))
vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
indices := [
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
/*
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
*/
]
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
shader := gfx.make_shader(C.rt_march_shader_desc())
mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_m_pos, C.ATTR_vs_m_color0, C.ATTR_vs_m_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_m_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_m_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .float2 // u,v as f32
//pipdesc.layout.attrs[C.ATTR_vs_m_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
pipdesc.index_type = .uint16
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
depth_compare_func : gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
}
pipdesc.rasterizer = C.sg_rasterizer_state {
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
mut bind := C.sg_bindings{}
unsafe {C.memset(&bind, 0, sizeof(bind))}
bind.vertex_buffers[0] = vbuf
bind.index_buffer = ibuf
bind.fs_images[C.SLOT_tex] = app.texture
app.bind['march'] = bind
app.pipe['march'] = gfx.make_pipeline(&pipdesc)
println("GLSL March init DONE!")
}
// putty shader init
fn init_cube_glsl_p(mut app App) {
/* cube vertex buffer */
//d := u16(32767) // for compatibility with D3D11, 32767 stand for 1
d := f32(1.0)
c := u32(0xFFFFFF_FF) // color RGBA8
vertices := [
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, 1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, -1.0, c, d, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, d},
Vertex_t{-1.0, -1.0, 1.0, c, 0, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{ 1.0, 1.0, -1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, 1.0, c, 0, d},
Vertex_t{-1.0, -1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, -1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, -1.0, 1.0, c, d, d},
Vertex_t{ 1.0, -1.0, -1.0, c, 0, d},
Vertex_t{-1.0, 1.0, -1.0, c, 0, 0},
Vertex_t{-1.0, 1.0, 1.0, c, d, 0},
Vertex_t{ 1.0, 1.0, 1.0, c, d, d},
Vertex_t{ 1.0, 1.0, -1.0, c, 0, d},
]
mut vert_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&vert_buffer_desc, 0, sizeof(vert_buffer_desc))}
vert_buffer_desc.size = vertices.len * int(sizeof(Vertex_t))
vert_buffer_desc.content = byteptr(vertices.data)
vert_buffer_desc.@type = .vertexbuffer
vert_buffer_desc.label = "cube-vertices".str
vbuf := gfx.make_buffer(&vert_buffer_desc)
/* create an index buffer for the cube */
indices := [
/*
u16(0), 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
*/
u16(14), 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
]
mut index_buffer_desc := C.sg_buffer_desc{}
unsafe {C.memset(&index_buffer_desc, 0, sizeof(index_buffer_desc))}
index_buffer_desc.size = indices.len * int(sizeof(u16))
index_buffer_desc.content = byteptr(indices.data)
index_buffer_desc.@type = .indexbuffer
index_buffer_desc.label = "cube-indices".str
ibuf := gfx.make_buffer(&index_buffer_desc)
/* create shader */
shader := gfx.make_shader(C.rt_puppy_shader_desc())
mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.layout.buffers[0].stride = int(sizeof(Vertex_t))
// the constants [C.ATTR_vs_p_pos, C.ATTR_vs_p_color0, C.ATTR_vs_p_texcoord0] are generated by sokol-shdc
pipdesc.layout.attrs[C.ATTR_vs_p_pos ].format = .float3 // x,y,z as f32
pipdesc.layout.attrs[C.ATTR_vs_p_color0 ].format = .ubyte4n // color as u32
pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .float2 // u,v as f32
//pipdesc.layout.attrs[C.ATTR_vs_p_texcoord0].format = .short2n // u,v as u16
pipdesc.shader = shader
pipdesc.index_type = .uint16
pipdesc.depth_stencil = C.sg_depth_stencil_state{
depth_write_enabled: true
depth_compare_func : gfx.CompareFunc(C.SG_COMPAREFUNC_LESS_EQUAL)
}
pipdesc.rasterizer = C.sg_rasterizer_state {
cull_mode: .back
}
pipdesc.label = "glsl_shader pipeline".str
mut bind := C.sg_bindings{}
unsafe {C.memset(&bind, 0, sizeof(bind))}
bind.vertex_buffers[0] = vbuf
bind.index_buffer = ibuf
bind.fs_images[C.SLOT_tex] = app.texture
app.bind['puppy'] = bind
app.pipe['puppy'] = gfx.make_pipeline(&pipdesc)
println("GLSL Puppy init DONE!")
}
fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) m4.Mat4{
proj := m4.perspective(60, w/h, 0.01, 10.0)
view := m4.look_at(m4.Vec4{e:[f32(0.0),0.0,6,0]!}, m4.Vec4{e:[f32(0),0,0,0]!}, m4.Vec4{e:[f32(0),1.0,0,0]!})
view_proj := view * proj
rxm := m4.rotate(m4.rad(rx), m4.Vec4{e:[f32(1),0,0,0]!})
rym := m4.rotate(m4.rad(ry), m4.Vec4{e:[f32(0),1,0,0]!})
model := rym * rxm
scale_m := m4.scale(m4.Vec4{e:[in_scale, in_scale, in_scale, 1]!})
res := (scale_m * model)* view_proj
return res
}
// march triangles draw
fn draw_cube_glsl_m(app App){
if app.init_flag == false {
return
}
ws := gg.window_size()
ratio := f32(ws.width) / ws.height
dw := f32(ws.width / 2)
dh := f32(ws.height / 2)
rot := [f32(app.mouse_y), f32(app.mouse_x)]
tr_matrix := calc_tr_matrices(dw, dh, rot[0], rot[1], 2.3)
gfx.apply_pipeline(app.pipe['march'])
gfx.apply_bindings(app.bind['march'])
//***************
// Uniforms
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params_m, &tr_matrix, 4*16 )
// fragment shader uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [
f32(ws.width), ws.height * ratio, // x,y resolution to pass to FS
0,0, // dont send mouse position
//app.mouse_x, // mouse x
//ws.height - app.mouse_y*2, // mouse y scaled
time_ticks, // time as f32
app.frame_count, // frame count
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_m, &tmp_fs_params, int(sizeof(tmp_fs_params)))
// 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw
gfx.draw(0, (3 * 2) * 3, 1)
}
// puppy triangles draw
fn draw_cube_glsl_p(app App){
if app.init_flag == false {
return
}
ws := gg.window_size()
ratio := f32(ws.width) / ws.height
dw := f32(ws.width / 2)
dh := f32(ws.height / 2)
rot := [f32(app.mouse_y), f32(app.mouse_x)]
tr_matrix := calc_tr_matrices(dw, dh, rot[0], rot[1], 2.3)
// apply the pipline and bindings
gfx.apply_pipeline(app.pipe['puppy'])
gfx.apply_bindings(app.bind['puppy'])
//***************
// Uniforms
//***************
// passing the view matrix as uniform
// res is a 4x4 matrix of f32 thus: 4*16 byte of size
gfx.apply_uniforms(C.SG_SHADERSTAGE_VS, C.SLOT_vs_params_p, &tr_matrix, 4*16 )
// fragment shader uniforms
time_ticks := f32(time.ticks() - app.ticks) / 1000
mut tmp_fs_params := [
f32(ws.width), ws.height * ratio, // x,y resolution to pass to FS
0,0, // dont send mouse position
//app.mouse_x, // mouse x
//ws.height - app.mouse_y*2, // mouse y scaled
time_ticks, // time as f32
app.frame_count, // frame count
0,0 // padding bytes , see "fs_params" struct paddings in rt_glsl.h
]!
gfx.apply_uniforms(C.SG_SHADERSTAGE_FS, C.SLOT_fs_params_p, &tmp_fs_params, int(sizeof(tmp_fs_params)))
// 3 vertices for triangle * 2 triangles per face * 6 faces = 36 vertices to draw
gfx.draw(0, (3 * 2) * 3, 1)
}
fn draw_start_glsl(app App){
if app.init_flag == false {
return
}
ws := gg.window_size()
//ratio := f32(ws.width) / ws.height
//dw := f32(ws.width / 2)
//dh := f32(ws.height / 2)
gfx.apply_viewport(0, 0, ws.width, ws.height, true)
}
fn draw_end_glsl(app App){
gfx.end_pass()
gfx.commit()
}
fn frame(mut app App) {
ws := gg.window_size()
// clear
mut color_action := C.sg_color_attachment_action{
action: gfx.Action(C.SG_ACTION_CLEAR)
}
color_action.val[0] = 0
color_action.val[1] = 0
color_action.val[2] = 0
color_action.val[3] = 1.0
mut pass_action := C.sg_pass_action{}
pass_action.colors[0] = color_action
gfx.begin_default_pass(&pass_action, ws.width, ws.height)
/*
// glsl cube
if app.frame_count % 1 == 1{
draw_cube_glsl_m(app)
} else {
draw_cube_glsl_p(app)
}
*/
draw_start_glsl(app)
draw_cube_glsl_m(app)
draw_cube_glsl_p(app)
draw_end_glsl(app)
app.frame_count++
}
/******************************************************************************
*
* Init / Cleanup
*
******************************************************************************/
fn my_init(mut app App) {
// 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)
sgl_desc := C.sgl_desc_t{
max_vertices: 50 * 65536
}
sgl.setup(&sgl_desc)
// create chessboard texture 256*256 RGBA
w := 256
h := 256
sz := w * h * 4
tmp_txt := unsafe { malloc(sz) }
mut i := 0
for i < sz {
unsafe {
y := (i >> 0x8) >> 5 // 8 cell
x := (i & 0xFF) >> 5 // 8 cell
// upper left corner
if x==0 && y==0 {
tmp_txt[i ] = byte(0xFF)
tmp_txt[i+1] = byte(0)
tmp_txt[i+2] = byte(0)
tmp_txt[i+3] = byte(0xFF)
}
// low right corner
else if x==7 && y==7 {
tmp_txt[i ] = byte(0)
tmp_txt[i+1] = byte(0xFF)
tmp_txt[i+2] = byte(0)
tmp_txt[i+3] = byte(0xFF)
} else {
col := if ((x+y) & 1) == 1 {0xFF} else {128}
tmp_txt[i ] = byte(col) // red
tmp_txt[i+1] = byte(col) // green
tmp_txt[i+2] = byte(col) // blue
tmp_txt[i+3] = byte(0xFF) // alpha
}
i += 4
}
}
app.texture = create_texture(w, h, tmp_txt)
unsafe { free(tmp_txt) }
// glsl
init_cube_glsl_m(mut app)
init_cube_glsl_p(mut app)
app.init_flag = true
}
fn cleanup(mut app App) {
gfx.shutdown()
}
/******************************************************************************
*
* event
*
******************************************************************************/
fn my_event_manager(mut ev sapp.Event, mut app App) {
if ev.typ == .mouse_down{
app.mouse_down = true
}
if ev.typ == .mouse_up{
app.mouse_down = false
}
if app.mouse_down == true && ev.typ == .mouse_move {
app.mouse_x = int(ev.mouse_x)
app.mouse_y = int(ev.mouse_y)
}
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
*
******************************************************************************/
[console]
fn main(){
// App init
mut app := &App{
gg: 0
}
app.gg = gg.new_context({
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: '3D Ray Marching Cube'
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()
}

View File

@ -0,0 +1,695 @@
//------------------------------------------------------------------------------
// Shader code for texcube-sapp sample.
//
// NOTE: This source file also uses the '#pragma sokol' form of the
// custom tags.
//------------------------------------------------------------------------------
//#pragma sokol @ctype mat4 hmm_mat4
#pragma sokol @vs vs_m
uniform vs_params_m {
mat4 mvp;
};
in vec4 pos;
in vec4 color0;
in vec2 texcoord0;
out vec4 color;
out vec2 uv;
void main() {
gl_Position = mvp * pos;
color = color0;
uv = texcoord0;
}
#pragma sokol @end
#pragma sokol @fs fs_m
uniform sampler2D tex;
uniform fs_params_m {
vec2 iResolution;
vec2 iMouse;
float iTime;
float iFrame;
};
in vec4 color;
in vec2 uv;
out vec4 frag_color;
// change to 0 to 4 to increment the AntiAliasing,
// increase AA will SLOW the rendering!!
#define AA 1
//*********************************************************
// Ray Marching
// original code from: https://www.shadertoy.com/view/Xds3zN
//*********************************************************
// The MIT License
// Copyright © 2013 Inigo Quilez
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// A list of useful distance function to simple primitives. All
// these functions (except for ellipsoid) return an exact
// euclidean distance, meaning they produce a better SDF than
// what you'd get if you were constructing them from boolean
// operations.
//
// More info here:
//
// https://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
//------------------------------------------------------------------
float dot2( in vec2 v ) { return dot(v,v); }
float dot2( in vec3 v ) { return dot(v,v); }
float ndot( in vec2 a, in vec2 b ) { return a.x*b.x - a.y*b.y; }
float sdPlane( vec3 p )
{
return p.y;
}
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
float sdBoundingBox( vec3 p, vec3 b, float e )
{
p = abs(p )-b;
vec3 q = abs(p+e)-e;
return min(min(
length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
}
float sdEllipsoid( in vec3 p, in vec3 r ) // approximated
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
float sdTorus( vec3 p, vec2 t )
{
return length( vec2(length(p.xz)-t.x,p.y) )-t.y;
}
float sdCappedTorus(in vec3 p, in vec2 sc, in float ra, in float rb)
{
p.x = abs(p.x);
float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy);
return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb;
}
float sdHexPrism( vec3 p, vec2 h )
{
vec3 q = abs(p);
const vec3 k = vec3(-0.8660254, 0.5, 0.57735);
p = abs(p);
p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
vec2 d = vec2(
length(p.xy - vec2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x),
p.z-h.y );
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdOctogonPrism( in vec3 p, in float r, float h )
{
const vec3 k = vec3(-0.9238795325, // sqrt(2+sqrt(2))/2
0.3826834323, // sqrt(2-sqrt(2))/2
0.4142135623 ); // sqrt(2)-1
// reflections
p = abs(p);
p.xy -= 2.0*min(dot(vec2( k.x,k.y),p.xy),0.0)*vec2( k.x,k.y);
p.xy -= 2.0*min(dot(vec2(-k.x,k.y),p.xy),0.0)*vec2(-k.x,k.y);
// polygon side
p.xy -= vec2(clamp(p.x, -k.z*r, k.z*r), r);
vec2 d = vec2( length(p.xy)*sign(p.y), p.z-h );
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{
vec3 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
float sdRoundCone( in vec3 p, in float r1, float r2, float h )
{
vec2 q = vec2( length(p.xz), p.y );
float b = (r1-r2)/h;
float a = sqrt(1.0-b*b);
float k = dot(q,vec2(-b,a));
if( k < 0.0 ) return length(q) - r1;
if( k > a*h ) return length(q-vec2(0.0,h)) - r2;
return dot(q, vec2(a,b) ) - r1;
}
float sdRoundCone(vec3 p, vec3 a, vec3 b, float r1, float r2)
{
// sampling independent computations (only depend on shape)
vec3 ba = b - a;
float l2 = dot(ba,ba);
float rr = r1 - r2;
float a2 = l2 - rr*rr;
float il2 = 1.0/l2;
// sampling dependant computations
vec3 pa = p - a;
float y = dot(pa,ba);
float z = y - l2;
float x2 = dot2( pa*l2 - ba*y );
float y2 = y*y*l2;
float z2 = z*z*l2;
// single square root!
float k = sign(rr)*rr*rr*x2;
if( sign(z)*a2*z2 > k ) return sqrt(x2 + z2) *il2 - r2;
if( sign(y)*a2*y2 < k ) return sqrt(x2 + y2) *il2 - r1;
return (sqrt(x2*a2*il2)+y*rr)*il2 - r1;
}
float sdTriPrism( vec3 p, vec2 h )
{
const float k = sqrt(3.0);
h.x *= 0.5*k;
p.xy /= h.x;
p.x = abs(p.x) - 1.0;
p.y = p.y + 1.0/k;
if( p.x+k*p.y>0.0 ) p.xy=vec2(p.x-k*p.y,-k*p.x-p.y)/2.0;
p.x -= clamp( p.x, -2.0, 0.0 );
float d1 = length(p.xy)*sign(-p.y)*h.x;
float d2 = abs(p.z)-h.y;
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}
// vertical
float sdCylinder( vec3 p, vec2 h )
{
vec2 d = abs(vec2(length(p.xz),p.y)) - h;
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
// arbitrary orientation
float sdCylinder(vec3 p, vec3 a, vec3 b, float r)
{
vec3 pa = p - a;
vec3 ba = b - a;
float baba = dot(ba,ba);
float paba = dot(pa,ba);
float x = length(pa*baba-ba*paba) - r*baba;
float y = abs(paba-baba*0.5)-baba*0.5;
float x2 = x*x;
float y2 = y*y*baba;
float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0));
return sign(d)*sqrt(abs(d))/baba;
}
// vertical
float sdCone( in vec3 p, in vec2 c, float h )
{
vec2 q = h*vec2(c.x,-c.y)/c.y;
vec2 w = vec2( length(p.xz), p.y );
vec2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 );
vec2 b = w - q*vec2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 );
float k = sign( q.y );
float d = min(dot( a, a ),dot(b, b));
float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) );
return sqrt(d)*sign(s);
}
float sdCappedCone( in vec3 p, in float h, in float r1, in float r2 )
{
vec2 q = vec2( length(p.xz), p.y );
vec2 k1 = vec2(r2,h);
vec2 k2 = vec2(r2-r1,2.0*h);
vec2 ca = vec2(q.x-min(q.x,(q.y < 0.0)?r1:r2), abs(q.y)-h);
vec2 cb = q - k1 + k2*clamp( dot(k1-q,k2)/dot2(k2), 0.0, 1.0 );
float s = (cb.x < 0.0 && ca.y < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(dot2(ca),dot2(cb)) );
}
float sdCappedCone(vec3 p, vec3 a, vec3 b, float ra, float rb)
{
float rba = rb-ra;
float baba = dot(b-a,b-a);
float papa = dot(p-a,p-a);
float paba = dot(p-a,b-a)/baba;
float x = sqrt( papa - paba*paba*baba );
float cax = max(0.0,x-((paba<0.5)?ra:rb));
float cay = abs(paba-0.5)-0.5;
float k = rba*rba + baba;
float f = clamp( (rba*(x-ra)+paba*baba)/k, 0.0, 1.0 );
float cbx = x-ra - f*rba;
float cby = paba - f;
float s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
return s*sqrt( min(cax*cax + cay*cay*baba,
cbx*cbx + cby*cby*baba) );
}
// c is the sin/cos of the desired cone angle
float sdSolidAngle(vec3 pos, vec2 c, float ra)
{
vec2 p = vec2( length(pos.xz), pos.y );
float l = length(p) - ra;
float m = length(p - c*clamp(dot(p,c),0.0,ra) );
return max(l,m*sign(c.y*p.x-c.x*p.y));
}
float sdOctahedron(vec3 p, float s)
{
p = abs(p);
float m = p.x + p.y + p.z - s;
// exact distance
#if 0
vec3 o = min(3.0*p - m, 0.0);
o = max(6.0*p - m*2.0 - o*3.0 + (o.x+o.y+o.z), 0.0);
return length(p - s*o/(o.x+o.y+o.z));
#endif
// exact distance
#if 1
vec3 q;
if( 3.0*p.x < m ) q = p.xyz;
else if( 3.0*p.y < m ) q = p.yzx;
else if( 3.0*p.z < m ) q = p.zxy;
else return m*0.57735027;
float k = clamp(0.5*(q.z-q.y+s),0.0,s);
return length(vec3(q.x,q.y-s+k,q.z-k));
#endif
// bound, not exact
#if 0
return m*0.57735027;
#endif
}
float sdPyramid( in vec3 p, in float h )
{
float m2 = h*h + 0.25;
// symmetry
p.xz = abs(p.xz);
p.xz = (p.z>p.x) ? p.zx : p.xz;
p.xz -= 0.5;
// project into face plane (2D)
vec3 q = vec3( p.z, h*p.y - 0.5*p.x, h*p.x + 0.5*p.y);
float s = max(-q.x,0.0);
float t = clamp( (q.y-0.5*p.z)/(m2+0.25), 0.0, 1.0 );
float a = m2*(q.x+s)*(q.x+s) + q.y*q.y;
float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t);
float d2 = min(q.y,-q.x*m2-q.y*0.5) > 0.0 ? 0.0 : min(a,b);
// recover 3D and scale, and add sign
return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));
}
// la,lb=semi axis, h=height, ra=corner
float sdRhombus(vec3 p, float la, float lb, float h, float ra)
{
p = abs(p);
vec2 b = vec2(la,lb);
float f = clamp( (ndot(b,b-2.0*p.xz))/dot(b,b), -1.0, 1.0 );
vec2 q = vec2(length(p.xz-0.5*b*vec2(1.0-f,1.0+f))*sign(p.x*b.y+p.z*b.x-b.x*b.y)-ra, p.y-h);
return min(max(q.x,q.y),0.0) + length(max(q,0.0));
}
//------------------------------------------------------------------
vec2 opU( vec2 d1, vec2 d2 )
{
return (d1.x<d2.x) ? d1 : d2;
}
//------------------------------------------------------------------
#define ZERO (min(int(iFrame),0))
//------------------------------------------------------------------
vec2 map( in vec3 pos )
{
vec2 res = vec2( 1e10, 0.0 );
{
res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.25, 0.0), 0.25 ), 26.9 ) );
}
// bounding box
if( sdBox( pos-vec3(0.0,0.3,-1.0),vec3(0.35,0.3,2.5) )<res.x )
{
// more primitives
res = opU( res, vec2( sdBoundingBox( pos-vec3( 0.0,0.25, 0.0), vec3(0.3,0.25,0.2), 0.025 ), 16.9 ) );
res = opU( res, vec2( sdTorus( (pos-vec3( 0.0,0.30, 1.0)).xzy, vec2(0.25,0.05) ), 25.0 ) );
res = opU( res, vec2( sdCone( pos-vec3( 0.0,0.45,-1.0), vec2(0.6,0.8),0.45 ), 55.0 ) );
res = opU( res, vec2( sdCappedCone( pos-vec3( 0.0,0.25,-2.0), 0.25, 0.25, 0.1 ), 13.67 ) );
res = opU( res, vec2( sdSolidAngle( pos-vec3( 0.0,0.00,-3.0), vec2(3,4)/5.0, 0.4 ), 49.13 ) );
}
// bounding box
if( sdBox( pos-vec3(1.0,0.3,-1.0),vec3(0.35,0.3,2.5) )<res.x )
{
// more primitives
res = opU( res, vec2( sdCappedTorus((pos-vec3( 1.0,0.30, 1.0))*vec3(1,-1,1), vec2(0.866025,-0.5), 0.25, 0.05), 8.5) );
res = opU( res, vec2( sdBox( pos-vec3( 1.0,0.25, 0.0), vec3(0.3,0.25,0.1) ), 3.0 ) );
res = opU( res, vec2( sdCapsule( pos-vec3( 1.0,0.00,-1.0),vec3(-0.1,0.1,-0.1), vec3(0.2,0.4,0.2), 0.1 ), 31.9 ) );
res = opU( res, vec2( sdCylinder( pos-vec3( 1.0,0.25,-2.0), vec2(0.15,0.25) ), 8.0 ) );
res = opU( res, vec2( sdHexPrism( pos-vec3( 1.0,0.2,-3.0), vec2(0.2,0.05) ), 18.4 ) );
}
// bounding box
if( sdBox( pos-vec3(-1.0,0.35,-1.0),vec3(0.35,0.35,2.5))<res.x )
{
// more primitives
res = opU( res, vec2( sdPyramid( pos-vec3(-1.0,-0.6,-3.0), 1.0 ), 13.56 ) );
res = opU( res, vec2( sdOctahedron( pos-vec3(-1.0,0.15,-2.0), 0.35 ), 23.56 ) );
res = opU( res, vec2( sdTriPrism( pos-vec3(-1.0,0.15,-1.0), vec2(0.3,0.05) ),43.5 ) );
res = opU( res, vec2( sdEllipsoid( pos-vec3(-1.0,0.25, 0.0), vec3(0.2, 0.25, 0.05) ), 43.17 ) );
res = opU( res, vec2( sdRhombus( (pos-vec3(-1.0,0.34, 1.0)).xzy, 0.15, 0.25, 0.04, 0.08 ),17.0 ) );
}
// bounding box
if( sdBox( pos-vec3(2.0,0.3,-1.0),vec3(0.35,0.3,2.5) )<res.x )
{
// more primitives
res = opU( res, vec2( sdOctogonPrism(pos-vec3( 2.0,0.2,-3.0), 0.2, 0.05), 51.8 ) );
res = opU( res, vec2( sdCylinder( pos-vec3( 2.0,0.15,-2.0), vec3(0.1,-0.1,0.0), vec3(-0.2,0.35,0.1), 0.08), 31.2 ) );
res = opU( res, vec2( sdCappedCone( pos-vec3( 2.0,0.10,-1.0), vec3(0.1,0.0,0.0), vec3(-0.2,0.40,0.1), 0.15, 0.05), 46.1 ) );
res = opU( res, vec2( sdRoundCone( pos-vec3( 2.0,0.15, 0.0), vec3(0.1,0.0,0.0), vec3(-0.1,0.35,0.1), 0.15, 0.05), 51.7 ) );
res = opU( res, vec2( sdRoundCone( pos-vec3( 2.0,0.20, 1.0), 0.2, 0.1, 0.3 ), 37.0 ) );
}
return res;
}
// http://iquilezles.org/www/articles/boxfunctions/boxfunctions.htm
vec2 iBox( in vec3 ro, in vec3 rd, in vec3 rad )
{
vec3 m = 1.0/rd;
vec3 n = m*ro;
vec3 k = abs(m)*rad;
vec3 t1 = -n - k;
vec3 t2 = -n + k;
return vec2( max( max( t1.x, t1.y ), t1.z ),
min( min( t2.x, t2.y ), t2.z ) );
}
vec2 raycast( in vec3 ro, in vec3 rd )
{
vec2 res = vec2(-1.0,-1.0);
float tmin = 1.0;
float tmax = 20.0;
// raytrace floor plane
float tp1 = (0.0-ro.y)/rd.y;
if( tp1>0.0 )
{
tmax = min( tmax, tp1 );
res = vec2( tp1, 1.0 );
}
//else return res;
// raymarch primitives
vec2 tb = iBox( ro-vec3(0.0,0.4,-0.5), rd, vec3(2.5,0.41,3.0) );
if( tb.x<tb.y && tb.y>0.0 && tb.x<tmax)
{
//return vec2(tb.x,2.0);
tmin = max(tb.x,tmin);
tmax = min(tb.y,tmax);
float t = tmin;
for( int i=0; i<70 && t<tmax; i++ )
{
vec2 h = map( ro+rd*t );
if( abs(h.x)<(0.0001*t) )
{
res = vec2(t,h.y);
break;
}
t += h.x;
}
}
return res;
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
{
// bounding volume
float tp = (0.8-ro.y)/rd.y; if( tp>0.0 ) tmax = min( tmax, tp );
float res = 1.0;
float t = mint;
for( int i=ZERO; i<24; i++ )
{
float h = map( ro + rd*t ).x;
float s = clamp(8.0*h/t,0.0,1.0);
res = min( res, s*s*(3.0-2.0*s) );
t += clamp( h, 0.02, 0.2 );
if( res<0.004 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos )
{
#if 0
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
#else
// inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times
vec3 n = vec3(0.0);
for( int i=ZERO; i<4; i++ )
{
vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0);
n += e*map(pos+0.0005*e).x;
//if( n.x+n.y+n.z>100.0 ) break;
}
return normalize(n);
#endif
}
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = 0.0;
float sca = 1.0;
for( int i=ZERO; i<5; i++ )
{
float h = 0.01 + 0.12*float(i)/4.0;
float d = map( pos + h*nor ).x;
occ += (h-d)*sca;
sca *= 0.95;
if( occ>0.35 ) break;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y);
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p, in vec2 dpdx, in vec2 dpdy )
{
// filter kernel
vec2 w = abs(dpdx)+abs(dpdy) + 0.001;
// analytical integral (box filter)
vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w;
// xor pattern
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd, in vec3 rdx, in vec3 rdy )
{
// background
vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3;
// raycast scene
vec2 res = raycast(ro,rd);
float t = res.x;
float m = res.y;
if( m>-0.5 )
{
vec3 pos = ro + t*rd;
vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos );
vec3 ref = reflect( rd, nor );
// material
col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) );
float ks = 1.0;
if( m<1.5 )
{
// project pixel footprint into the plane
vec3 dpdx = ro.y*(rd/rd.y-rdx/rdx.y);
vec3 dpdy = ro.y*(rd/rd.y-rdy/rdy.y);
float f = checkersGradBox( 3.0*pos.xz, 3.0*dpdx.xz, 3.0*dpdy.xz );
col = 0.15 + f*vec3(0.05);
ks = 0.4;
}
// lighting
float occ = calcAO( pos, nor );
vec3 lin = vec3(0.0);
// sun
{
vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) );
vec3 hal = normalize( lig-rd );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
//if( dif>0.0001 )
dif *= calcSoftshadow( pos, lig, 0.02, 2.5 );
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0);
spe *= dif;
spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0);
lin += col*2.20*dif*vec3(1.30,1.00,0.70);
lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks;
}
// sky
{
float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 ));
dif *= occ;
float spe = smoothstep( -0.2, 0.2, ref.y );
spe *= dif;
spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 );
//if( spe>0.001 )
spe *= calcSoftshadow( pos, ref, 0.02, 2.5 );
lin += col*0.60*dif*vec3(0.40,0.60,1.15);
lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks;
}
// back
{
float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
dif *= occ;
lin += col*0.55*dif*vec3(0.25,0.25,0.25);
}
// sss
{
float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0);
dif *= occ;
lin += col*0.25*dif*vec3(1.00,1.00,1.00);
}
col = lin;
col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) );
}
return vec3( clamp(col,0.0,1.0) );
}
mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = ( cross(cu,cw) );
return mat3( cu, cv, cw );
}
vec4 mainImage( vec2 fragCoord )
{
vec2 mo = iMouse.xy/iResolution.xy;
float time = 32.0 + iTime*1.5;
// camera
vec3 ta = vec3( 0.5, -0.5, -0.6 );
vec3 ro = ta + vec3( 4.5*cos(0.1*time + 7.0*mo.x), 1.3 + 2.0*mo.y, 4.5*sin(0.1*time + 7.0*mo.x) );
// camera-to-world transformation
mat3 ca = setCamera( ro, ta, 0.0 );
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (2.0*(fragCoord+o)-iResolution.xy)/iResolution.y;
#else
vec2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
#endif
// focal length
const float fl = 2.5;
// ray direction
vec3 rd = ca * normalize( vec3(p,fl) );
// ray differentials
vec2 px = (2.0*(fragCoord+vec2(1.0,0.0))-iResolution.xy)/iResolution.y;
vec2 py = (2.0*(fragCoord+vec2(0.0,1.0))-iResolution.xy)/iResolution.y;
vec3 rdx = ca * normalize( vec3(px,fl) );
vec3 rdy = ca * normalize( vec3(py,fl) );
// render
vec3 col = render( ro, rd, rdx, rdy );
// gain
// col = col*3.0/(2.5+col);
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
//fragColor = vec4( tot, 1.0 );
return vec4( tot, 1.0 );
}
//*********************************************************
// END Ray Marching
//*********************************************************
void main() {
vec4 c = color;
vec4 txt = texture(tex, uv);
c = txt * c;
vec2 uv1 = uv * iResolution;
vec4 col_ray = mainImage(uv1);
// use this to mix the chessboart texture with the ray marching
//frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ;
frag_color = c*0.00001 + col_ray ;
}
#pragma sokol @end
#pragma sokol @program rt_march vs_m fs_m

View File

@ -0,0 +1,568 @@
//------------------------------------------------------------------------------
// Shader code for texcube-sapp sample.
//
// NOTE: This source file also uses the '#pragma sokol' form of the
// custom tags.
//------------------------------------------------------------------------------
//#pragma sokol @ctype mat4 hmm_mat4
#pragma sokol @vs vs_p
uniform vs_params_p {
mat4 mvp;
};
in vec4 pos;
in vec4 color0;
in vec2 texcoord0;
out vec4 color;
out vec2 uv;
void main() {
gl_Position = mvp * pos;
color = color0;
uv = texcoord0;
}
#pragma sokol @end
#pragma sokol @fs fs_p
uniform sampler2D tex;
uniform fs_params_p {
vec2 iResolution;
vec2 iMouse;
float iTime;
float iFrame;
};
in vec4 color;
in vec2 uv;
out vec4 frag_color;
// change to 0 to 4 to increment the AntiAliasing,
// increase AA will SLOW the rendering!!
#define AA 1
//*********************************************************
// Ray Marching
// original code from: https://www.shadertoy.com/view/Xds3zN
//*********************************************************
// Created by inigo quilez - iq/2019
// I share this piece (art and code) here in Shadertoy and through its Public API, only for educational purposes.
// You cannot use, share or host this piece or modifications of it as part of your own commercial or non-commercial product, website or project.
// You can share a link to it or an unmodified screenshot of it provided you attribute "by Inigo Quilez, @iquilezles and iquilezles.org".
// If you are a teacher, lecturer, educator or similar and these conditions are too restrictive for your needs, please contact me and we'll work it out.
// An animation test - a happy and blobby creature
// jumping and looking around. It gets off-model very
// often, but it looks good enough I think.
//
// Making-of with math/shader/art explanations (6 hours
// long): https://www.youtube.com/watch?v=Cfe5UQ-1L9Q
//
// Video capture: https://www.youtube.com/watch?v=s_UOFo2IULQ
//
// Buy a metal print here: https://www.redbubble.com/i/metal-print/Happy-Jumping-by-InigoQuilez/43594745.0JXQP
//------------------------------------------------------------------
// http://iquilezles.org/www/articles/smin/smin.htm
float smin( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return min(a, b) - h*h*0.25/k;
}
// http://iquilezles.org/www/articles/smin/smin.htm
vec2 smin( vec2 a, vec2 b, float k )
{
float h = clamp( 0.5+0.5*(b.x-a.x)/k, 0.0, 1.0 );
return mix( b, a, h ) - k*h*(1.0-h);
}
// http://iquilezles.org/www/articles/smin/smin.htm
float smax( float a, float b, float k )
{
float h = max(k-abs(a-b),0.0);
return max(a, b) + h*h*0.25/k;
}
// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}
// http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
float sdEllipsoid( in vec3 p, in vec3 r ) // approximated
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
vec2 sdStick(vec3 p, vec3 a, vec3 b, float r1, float r2) // approximated
{
vec3 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return vec2( length( pa - ba*h ) - mix(r1,r2,h*h*(3.0-2.0*h)), h );
}
// http://iquilezles.org/www/articles/smin/smin.htm
vec4 opU( vec4 d1, vec4 d2 )
{
return (d1.x<d2.x) ? d1 : d2;
}
//------------------------------------------------------------------
#define ZERO (min(int(iFrame),0))
//------------------------------------------------------------------
float href;
float hsha;
vec4 map( in vec3 pos, float atime )
{
hsha = 1.0;
float t1 = fract(atime);
float t4 = abs(fract(atime*0.5)-0.5)/0.5;
float p = 4.0*t1*(1.0-t1);
float pp = 4.0*(1.0-2.0*t1); // derivative of p
vec3 cen = vec3( 0.5*(-1.0 + 2.0*t4),
pow(p,2.0-p) + 0.1,
floor(atime) + pow(t1,0.7) -1.0 );
// body
vec2 uu = normalize(vec2( 1.0, -pp ));
vec2 vv = vec2(-uu.y, uu.x);
float sy = 0.5 + 0.5*p;
float compress = 1.0-smoothstep(0.0,0.4,p);
sy = sy*(1.0-compress) + compress;
float sz = 1.0/sy;
vec3 q = pos - cen;
float rot = -0.25*(-1.0 + 2.0*t4);
float rc = cos(rot);
float rs = sin(rot);
q.xy = mat2x2(rc,rs,-rs,rc)*q.xy;
vec3 r = q;
href = q.y;
q.yz = vec2( dot(uu,q.yz), dot(vv,q.yz) );
vec4 res = vec4( sdEllipsoid( q, vec3(0.25, 0.25*sy, 0.25*sz) ), 2.0, 0.0, 1.0 );
if( res.x-1.0 < pos.y ) // bounding volume
{
float t2 = fract(atime+0.8);
float p2 = 0.5-0.5*cos(6.2831*t2);
r.z += 0.05-0.2*p2;
r.y += 0.2*sy-0.2;
vec3 sq = vec3( abs(r.x), r.yz );
// head
vec3 h = r;
float hr = sin(0.791*atime);
hr = 0.7*sign(hr)*smoothstep(0.5,0.7,abs(hr));
h.xz = mat2x2(cos(hr),sin(hr),-sin(hr),cos(hr))*h.xz;
vec3 hq = vec3( abs(h.x), h.yz );
float d = sdEllipsoid( h-vec3(0.0,0.20,0.02), vec3(0.08,0.2,0.15) );
float d2 = sdEllipsoid( h-vec3(0.0,0.21,-0.1), vec3(0.20,0.2,0.20) );
d = smin( d, d2, 0.1 );
res.x = smin( res.x, d, 0.1 );
// belly wrinkles
{
float yy = r.y-0.02-2.5*r.x*r.x;
res.x += 0.001*sin(yy*120.0)*(1.0-smoothstep(0.0,0.1,abs(yy)));
}
// arms
{
vec2 arms = sdStick( sq, vec3(0.18-0.06*hr*sign(r.x),0.2,-0.05), vec3(0.3+0.1*p2,-0.2+0.3*p2,-0.15), 0.03, 0.06 );
res.xz = smin( res.xz, arms, 0.01+0.04*(1.0-arms.y)*(1.0-arms.y)*(1.0-arms.y) );
}
// ears
{
float t3 = fract(atime+0.9);
float p3 = 4.0*t3*(1.0-t3);
vec2 ear = sdStick( hq, vec3(0.15,0.32,-0.05), vec3(0.2+0.05*p3,0.2+0.2*p3,-0.07), 0.01, 0.04 );
res.xz = smin( res.xz, ear, 0.01 );
}
// mouth
{
d = sdEllipsoid( h-vec3(0.0,0.15+4.0*hq.x*hq.x,0.15), vec3(0.1,0.04,0.2) );
res.w = 0.3+0.7*clamp( d*150.0,0.0,1.0);
res.x = smax( res.x, -d, 0.03 );
}
// legs
{
float t6 = cos(6.2831*(atime*0.5+0.25));
float ccc = cos(1.57*t6*sign(r.x));
float sss = sin(1.57*t6*sign(r.x));
vec3 base = vec3(0.12,-0.07,-0.1); base.y -= 0.1/sy;
vec2 legs = sdStick( sq, base, base + vec3(0.2,-ccc,sss)*0.2, 0.04, 0.07 );
res.xz = smin( res.xz, legs, 0.07 );
}
// eye
{
float blink = pow(0.5+0.5*sin(2.1*iTime),20.0);
float eyeball = sdSphere(hq-vec3(0.08,0.27,0.06),0.065+0.02*blink);
res.x = smin( res.x, eyeball, 0.03 );
vec3 cq = hq-vec3(0.1,0.34,0.08);
cq.xy = mat2x2(0.8,0.6,-0.6,0.8)*cq.xy;
d = sdEllipsoid( cq, vec3(0.06,0.03,0.03) );
res.x = smin( res.x, d, 0.03 );
float eo = 1.0-0.5*smoothstep(0.01,0.04,length((hq.xy-vec2(0.095,0.285))*vec2(1.0,1.1)));
res = opU( res, vec4(sdSphere(hq-vec3(0.08,0.28,0.08),0.060),3.0,0.0,eo));
res = opU( res, vec4(sdSphere(hq-vec3(0.075,0.28,0.102),0.0395),4.0,0.0,1.0));
}
}
// ground
float fh = -0.1 - 0.05*(sin(pos.x*2.0)+sin(pos.z*2.0));
float t5f = fract(atime+0.05);
float t5i = floor(atime+0.05);
float bt4 = abs(fract(t5i*0.5)-0.5)/0.5;
vec2 bcen = vec2( 0.5*(-1.0+2.0*bt4),t5i+pow(t5f,0.7)-1.0 );
float k = length(pos.xz-bcen);
float tt = t5f*15.0-6.2831 - k*3.0;
fh -= 0.1*exp(-k*k)*sin(tt)*exp(-max(tt,0.0)/2.0)*smoothstep(0.0,0.01,t5f);
float d = pos.y - fh;
// bubbles
{
vec3 vp = vec3( mod(abs(pos.x),3.0)-1.5,pos.y,mod(pos.z+1.5,3.0)-1.5);
vec2 id = vec2( floor(pos.x/3.0), floor((pos.z+1.5)/3.0) );
float fid = id.x*11.1 + id.y*31.7;
float fy = fract(fid*1.312+atime*0.1);
float y = -1.0+4.0*fy;
vec3 rad = vec3(0.7,1.0+0.5*sin(fid),0.7);
rad -= 0.1*(sin(pos.x*3.0)+sin(pos.y*4.0)+sin(pos.z*5.0));
float siz = 4.0*fy*(1.0-fy);
float d2 = sdEllipsoid( vp-vec3(0.5,y,0.0), siz*rad );
d2 -= 0.03*smoothstep(-1.0,1.0,sin(18.0*pos.x)+sin(18.0*pos.y)+sin(18.0*pos.z));
d2 *= 0.6;
d2 = min(d2,2.0);
d = smin( d, d2, 0.32 );
if( d<res.x ) { res = vec4(d,1.0,0.0,1.0); hsha=sqrt(siz); }
}
// candy
{
float fs = 5.0;
vec3 qos = fs*vec3(pos.x, pos.y-fh, pos.z );
vec2 id = vec2( floor(qos.x+0.5), floor(qos.z+0.5) );
vec3 vp = vec3( fract(qos.x+0.5)-0.5,qos.y,fract(qos.z+0.5)-0.5);
vp.xz += 0.1*cos( id.x*130.143 + id.y*120.372 + vec2(0.0,2.0) );
float den = sin(id.x*0.1+sin(id.y*0.091))+sin(id.y*0.1);
float fid = id.x*0.143 + id.y*0.372;
float ra = smoothstep(0.0,0.1,den*0.1+fract(fid)-0.95);
d = sdSphere( vp, 0.35*ra )/fs;
if( d<res.x ) res = vec4(d,5.0,qos.y,1.0);
}
return res;
}
vec4 raycast( in vec3 ro, in vec3 rd, float time )
{
vec4 res = vec4(-1.0,-1.0,0.0,1.0);
float tmin = 0.5;
float tmax = 20.0;
#if 1
// raytrace bounding plane
float tp = (3.5-ro.y)/rd.y;
if( tp>0.0 ) tmax = min( tmax, tp );
#endif
// raymarch scene
float t = tmin;
for( int i=0; i<256 && t<tmax; i++ )
{
vec4 h = map( ro+rd*t, time );
if( abs(h.x)<(0.0005*t) )
{
res = vec4(t,h.yzw);
break;
}
t += h.x;
}
return res;
}
// http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float calcSoftshadow( in vec3 ro, in vec3 rd, float time )
{
float res = 1.0;
float tmax = 12.0;
#if 1
float tp = (3.5-ro.y)/rd.y; // raytrace bounding plane
if( tp>0.0 ) tmax = min( tmax, tp );
#endif
float t = 0.02;
for( int i=0; i<50; i++ )
{
float h = map( ro + rd*t, time ).x;
res = min( res, mix(1.0,16.0*h/t, hsha) );
t += clamp( h, 0.05, 0.40 );
if( res<0.005 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 calcNormal( in vec3 pos, float time )
{
#if 0
vec2 e = vec2(1.0,-1.0)*0.5773*0.001;
return normalize( e.xyy*map( pos + e.xyy, time ).x +
e.yyx*map( pos + e.yyx, time ).x +
e.yxy*map( pos + e.yxy, time ).x +
e.xxx*map( pos + e.xxx, time ).x );
#else
// inspired by tdhooper and klems - a way to prevent the compiler from inlining map() 4 times
vec3 n = vec3(0.0);
for( int i=ZERO; i<4; i++ )
{
vec3 e = 0.5773*(2.0*vec3((((i+3)>>1)&1),((i>>1)&1),(i&1))-1.0);
n += e*map(pos+0.001*e,time).x;
}
return normalize(n);
#endif
}
float calcOcclusion( in vec3 pos, in vec3 nor, float time )
{
float occ = 0.0;
float sca = 1.0;
for( int i=ZERO; i<5; i++ )
{
float h = 0.01 + 0.11*float(i)/4.0;
vec3 opos = pos + h*nor;
float d = map( opos, time ).x;
occ += (h-d)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 2.0*occ, 0.0, 1.0 );
}
vec3 render( in vec3 ro, in vec3 rd, float time )
{
// sky dome
vec3 col = vec3(0.5, 0.8, 0.9) - max(rd.y,0.0)*0.5;
// sky clouds
vec2 uv = 1.5*rd.xz/rd.y;
float cl = 1.0*(sin(uv.x)+sin(uv.y)); uv *= mat2(0.8,0.6,-0.6,0.8)*2.1;
cl += 0.5*(sin(uv.x)+sin(uv.y));
col += 0.1*(-1.0+2.0*smoothstep(-0.1,0.1,cl-0.4));
// sky horizon
col = mix( col, vec3(0.5, 0.7, .9), exp(-10.0*max(rd.y,0.0)) );
// scene geometry
vec4 res = raycast(ro,rd, time);
if( res.y>-0.5 )
{
float t = res.x;
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos, time );
vec3 ref = reflect( rd, nor );
float focc = res.w;
// material
col = vec3(0.2);
float ks = 1.0;
if( res.y>4.5 ) // candy
{
col = vec3(0.14,0.048,0.0);
vec2 id = floor(5.0*pos.xz+0.5);
col += 0.036*cos((id.x*11.1+id.y*37.341) + vec3(0.0,1.0,2.0) );
col = max(col,0.0);
focc = clamp(4.0*res.z,0.0,1.0);
}
else if( res.y>3.5 ) // eyeball
{
col = vec3(0.0);
}
else if( res.y>2.5 ) // iris
{
col = vec3(0.4);
}
else if( res.y>1.5 ) // body
{
col = mix(vec3(0.144,0.09,0.0036),vec3(0.36,0.1,0.04),res.z*res.z);
col = mix(col,vec3(0.14,0.09,0.06)*2.0, (1.0-res.z)*smoothstep(-0.15, 0.15, -href));
}
else // terrain
{
// base green
col = vec3(0.05,0.09,0.02);
float f = 0.2*(-1.0+2.0*smoothstep(-0.2,0.2,sin(18.0*pos.x)+sin(18.0*pos.y)+sin(18.0*pos.z)));
col += f*vec3(0.06,0.06,0.02);
ks = 0.5 + pos.y*0.15;
// footprints
vec2 mp = vec2(pos.x-0.5*(mod(floor(pos.z+0.5),2.0)*2.0-1.0), fract(pos.z+0.5)-0.5 );
float mark = 1.0-smoothstep(0.1, 0.5, length(mp));
mark *= smoothstep(0.0, 0.1, floor(time) - floor(pos.z+0.5) );
col *= mix( vec3(1.0), vec3(0.5,0.5,0.4), mark );
ks *= 1.0-0.5*mark;
}
// lighting (sun, sky, bounce, back, sss)
float occ = calcOcclusion( pos, nor, time )*focc;
float fre = clamp(1.0+dot(nor,rd),0.0,1.0);
vec3 sun_lig = normalize( vec3(0.6, 0.35, 0.5) );
float sun_dif = clamp(dot( nor, sun_lig ), 0.0, 1.0 );
vec3 sun_hal = normalize( sun_lig-rd );
float sun_sha = calcSoftshadow( pos, sun_lig, time );
float sun_spe = ks*pow(clamp(dot(nor,sun_hal),0.0,1.0),8.0)*sun_dif*(0.04+0.96*pow(clamp(1.0+dot(sun_hal,rd),0.0,1.0),5.0));
float sky_dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 ));
float sky_spe = ks*smoothstep( 0.0, 0.5, ref.y )*(0.04+0.96*pow(fre,4.0));
float bou_dif = sqrt(clamp( 0.1-0.9*nor.y, 0.0, 1.0 ))*clamp(1.0-0.1*pos.y,0.0,1.0);
float bac_dif = clamp(0.1+0.9*dot( nor, normalize(vec3(-sun_lig.x,0.0,-sun_lig.z))), 0.0, 1.0 );
float sss_dif = fre*sky_dif*(0.25+0.75*sun_dif*sun_sha);
vec3 lin = vec3(0.0);
lin += sun_dif*vec3(8.10,6.00,4.20)*vec3(sun_sha,sun_sha*sun_sha*0.5+0.5*sun_sha,sun_sha*sun_sha);
lin += sky_dif*vec3(0.50,0.70,1.00)*occ;
lin += bou_dif*vec3(0.20,0.70,0.10)*occ;
lin += bac_dif*vec3(0.45,0.35,0.25)*occ;
lin += sss_dif*vec3(3.25,2.75,2.50)*occ;
col = col*lin;
col += sun_spe*vec3(9.90,8.10,6.30)*sun_sha;
col += sky_spe*vec3(0.20,0.30,0.65)*occ*occ;
col = pow(col,vec3(0.8,0.9,1.0) );
// fog
col = mix( col, vec3(0.5,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) );
}
return col;
}
mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = ( cross(cu,cw) );
return mat3( cu, cv, cw );
}
//void mainImage( out vec4 fragColor, in vec2 fragCoord )
vec4 mainImage( vec2 fragCoord )
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=ZERO; m<AA; m++ )
for( int n=ZERO; n<AA; n++ )
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
// time coordinate (motion blurred, shutter=0.5)
float d = 0.5+0.5*sin(fragCoord.x*147.0)*sin(fragCoord.y*131.0);
float time = iTime - 0.5*(1.0/24.0)*(float(m*AA+n)+d)/float(AA*AA);
#else
vec2 p = (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
float time = iTime;
#endif
time += -2.6;
time *= 0.9;
// camera
float cl = sin(0.5*time);
float an = 1.57 + 0.7*sin(0.15*time);
vec3 ta = vec3( 0.0, 0.65, -0.6+time*1.0 - 0.4*cl);
vec3 ro = ta + vec3( 1.3*cos(an), -0.250, 1.3*sin(an) );
float ti = fract(time-0.15);
ti = 4.0*ti*(1.0-ti);
ta.y += 0.15*ti*ti*(3.0-2.0*ti)*smoothstep(0.4,0.9,cl);
// camera bounce
float t4 = abs(fract(time*0.5)-0.5)/0.5;
float bou = -1.0 + 2.0*t4;
ro += 0.06*sin(time*12.0+vec3(0.0,2.0,4.0))*smoothstep( 0.85, 1.0, abs(bou) );
// camera-to-world rotation
mat3 ca = setCamera( ro, ta, 0.0 );
// ray direction
vec3 rd = ca * normalize( vec3(p,1.8) );
// render
vec3 col = render( ro, rd, time );
// color grading
col = col*vec3(1.11,0.89,0.79);
// compress
col = 1.35*col/(1.0+col);
// gamma
col = pow( col, vec3(0.4545) );
tot += col;
#if AA>1
}
tot /= float(AA*AA);
#endif
// s-surve
tot = clamp(tot,0.0,1.0);
tot = tot*tot*(3.0-2.0*tot);
// vignetting
vec2 q = fragCoord/iResolution.xy;
tot *= 0.5 + 0.5*pow(16.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.25);
// output
//fragColor = vec4( tot, 1.0 );
return vec4( tot, 1.0 );
}
//*********************************************************
// END Ray Marching
//*********************************************************
void main() {
vec4 c = color;
vec4 txt = texture(tex, uv);
c = txt * c;
vec2 uv1 = uv * iResolution;
vec4 col_ray = mainImage(uv1);
// use this to mix the chessboart texture with the ray marching
//frag_color = clamp(c*iMouse.y/512.0,0.0,1.0) * col_ray ;
frag_color = c*0.00001 + col_ray ;
}
#pragma sokol @end
#pragma sokol @program rt_puppy vs_p fs_p

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +0,0 @@
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"
void calc_matrices(void *res, float w, float h, float rx, float ry, float scale){
hmm_mat4 proj = HMM_Perspective(60.0f, w/h, 0.01f, 10.0f);
hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 0.0f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
//state.rx += 1.0f; state.ry += 2.0f;
hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f));
hmm_mat4 rym = HMM_Rotate(ry, HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
hmm_mat4 scale_mx = HMM_Scale(HMM_Vec3(scale, scale, scale));
model = HMM_MultiplyMat4(model, scale_mx);
hmm_mat4 tmp_res = HMM_MultiplyMat4(view_proj, model);
// copy the matrix to V
int i = 0;
float *p = &tmp_res.Elements[0];
while(i < 16){
((float*)(res))[i]= p[i];
i++;
}
}

File diff suppressed because it is too large Load Diff

105
vlib/gg/m4/graphic.v Normal file
View File

@ -0,0 +1,105 @@
/**********************************************************************
*
* Simply vector/matrix graphic utility
*
* 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 m4
import math
// Translate degrees to radians
[inline]
pub fn rad(deg f32) f32 {
return (math.pi / 180.0) * deg
}
// Translate radians to degrees
[inline]
pub fn deg(grad f32) f32 {
return (180.0 / math.pi) * grad
}
// Calculate the perspective matrix
[direct_array_access]
pub fn perspective(fov f32, ar f32, n f32, f f32) Mat4 {
unsafe {
ctan := f32(1.0 / math.tan(fov * (f32(math.pi) / 360.0))) // for the FOV we use 360 instead 180
return Mat4{ e: [
ctan / ar, 0, 0, 0,
0, ctan, 0, 0,
0, 0, (n + f) / (n - f), -1.0,
0, 0, (2.0 * n * f) / (n - f), 0,
]!
}
}
}
// Calculate the look-at matrix
[direct_array_access]
pub fn look_at(eye Vec4, center Vec4, up Vec4) Mat4 {
unsafe {
f := (center - eye).normalize3()
s := (f % up).normalize3()
u := (s % f)
return Mat4{ e: [
/* [0][0] */ s.e[0],
/* [0][1] */ u.e[0],
/* [0][2] */ - f.e[0],
/* [0][3] */ 0,
/* [1][1] */ s.e[1],
/* [1][1] */ u.e[1],
/* [1][2] */ - f.e[1],
/* [1][3] */ 0,
/* [2][0] */ s.e[2],
/* [2][1] */ u.e[2],
/* [2][2] */ - f.e[2],
/* [2][3] */ 0,
/* [3][0] */ - (s * eye),
/* [3][1] */ - (u * eye),
/* [3][2] */ f * eye,
/* [3][3] */ 1,
]!
}
}
}
/*
hmm_mat4 proj = HMM_Perspective(60.0f, w/h, 0.01f, 10.0f);
hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
//state.rx += 1.0f; state.ry += 2.0f;
hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f));
hmm_mat4 rym = HMM_Rotate(ry, HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
hmm_mat4 scale_mx = HMM_Scale(HMM_Vec3(scale, scale, scale));
model = HMM_MultiplyMat4(model, scale_mx);
hmm_mat4 tmp_res = HMM_MultiplyMat4(view_proj, model);
*/
// Get the complete transformation matrix for GLSL demos
pub fn calc_tr_matrices(w f32, h f32, rx f32, ry f32, in_scale f32) Mat4 {
proj := perspective(60, w / h, 0.01, 10.0)
view := look_at(Vec4{ e: [f32(0.0), 1.5, 6, 0]! }, Vec4{ e: [f32(0), 0, 0, 0]! }, Vec4{ e: [f32(0), 1.0, 0, 0]! })
view_proj := view * proj
rxm := rotate(rad(rx), Vec4{ e: [f32(1), 0, 0, 0]! })
rym := rotate(rad(ry), Vec4{ e: [f32(0), 1, 0, 0]! })
model := rym * rxm
scale_m := scale(Vec4{ e: [in_scale, in_scale, in_scale, 1]! })
res := (scale_m * model) * view_proj
return res
}

218
vlib/gg/m4/m4_test.v Normal file
View File

@ -0,0 +1,218 @@
import gg.m4
pub fn test_m4() {
unsafe {
// Test Mat4
mut a := m4.Mat4{ e: [
f32(0), 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
]!
}
mut b := m4.Mat4{}
mut c := m4.Mat4{}
// equal test
assert a.e == [
f32(0), 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
]!
// copy test
b.copy(a)
assert a.e == b.e
// test: transpose, scale
assert b.transpose().mul_scalar(2.0).mul_scalar(0.5).transpose().e == a.e
assert b.sum_all() == 120.0
// test rows/columns set/get
for i in 0 .. 4 {
b = m4.zero_m4()
b.set_row(i, m4.Vec4{ e: [f32(1.0), 2, 3, 4]! })
assert b.get_f(0, i) == 1.0
assert b.get_f(1, i) == 2.0
assert b.get_f(2, i) == 3.0
assert b.get_f(3, i) == 4.0
// println(b)
c = m4.zero_m4()
c.set_col(i, m4.Vec4{ e: [f32(1.0), 2, 3, 4]! })
assert c.get_f(i, 0) == 1.0
assert c.get_f(i, 1) == 2.0
assert c.get_f(i, 2) == 3.0
assert c.get_f(i, 3) == 4.0
// println(c)
}
}
}
fn test_swap_col_row() {
unsafe {
// swap_col / swap_row
b := m4.Mat4{ e: [
f32(1), 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
]!
}
b.swap_col(0, 2)
assert b.e == [
f32(3), 2, 1, 4,
7, 6, 5, 8,
11, 10, 9, 12,
15, 14, 13, 16,
]!
b = m4.Mat4{ e: [
f32(1), 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
]!
}
b.swap_row(0, 2)
assert b.e == [
f32(9), 10, 11, 12,
5, 6, 7, 8,
1, 2, 3, 4,
13, 14, 15, 16,
]!
}
}
fn test_sum_sub() {
unsafe {
// test sum/sub
b := m4.unit_m4()
c := m4.unit_m4()
assert m4.sub(m4.add(b, c), b).e == m4.unit_m4().e
assert (b + c - b).e == m4.unit_m4().e
}
}
fn test_transpose() {
unsafe {
b := m4.Mat4{ e: [
f32(0), 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
]!
}
assert b.transpose().transpose().e == b.e
}
}
fn test_multiplication() {
unsafe {
b := m4.Mat4{ e: [
f32(1), 0, 0, 0,
0, 2, 0, 0,
0, 0, 3, 0,
0, 0, 0, 4,
]!
}
c := m4.Mat4{ e: [
f32(1), 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16,
]!
}
assert (c * c).e == [
f32(90),100,110,120,
202,228,254,280,
314,356,398,440,
426,484,542,600,
]!
assert m4.mul(c, c).e == [
f32(90),100,110,120,
202,228,254,280,
314,356,398,440,
426,484,542,600,
]!
assert m4.mul(b, c).e == [
f32(1), 2, 3, 4,
10, 12, 14, 16,
27, 30, 33, 36,
52, 56, 60, 64,
]!
assert (b * c).e == [
f32(1), 2, 3, 4,
10, 12, 14, 16,
27, 30, 33, 36,
52, 56, 60, 64,
]!
assert m4.det(b) == 24
}
}
fn test_det() {
unsafe {
b := m4.Mat4{ e: [
f32(5), 6, 6, 8,
2, 2, 2, 8,
6, 6, 2, 8,
2, 3, 6, 7,
]!
}
assert m4.det(b) == -8
c := m4.Mat4{ e: [
f32(1), 8, 2, 3,
8, 2, 3, 1,
2, 3, 3, 2,
3, 1, 2, 4,
]!
}
// println("*** INVERSE ****")
// println(m4.mul(b.inverse(),b))
// println(m4.clean_small(m4.mul(c.inverse(),c)))
// println("****************")
assert m4.mul(b.inverse(), b).e == m4.unit_m4().e
assert m4.mul(c.inverse(), c).is_equal(m4.unit_m4())
}
}
fn test_vec4() {
// Test Vec4
// println("*** Vector4 ****")
mut v := m4.Vec4{[f32(1), 2, 3, 4]!}
assert v * v.inv() == 4
assert v.mul_scalar(1.0 / v.mod()).mod() == 1
assert v + m4.Vec4{ e: [f32(5), 6, 7, 8]! } == m4.Vec4{ e: [f32(6), 8, 10, 12]! }
assert v - m4.Vec4{ e: [f32(1), 2, 3, 4]! } == m4.Vec4{ e: [f32(0), 0, 0, 0]! }
assert v.mul_vec4(m4.Vec4{ e: [f32(2), 2, 2, 2]! }) == m4.Vec4{ e: [f32(2), 4, 6, 8]! }
assert v.normalize().mod() == 1
v = m4.Vec4{[f32(1), 2, 3, 0]!}
assert v.normalize3().mod3() - 1 < m4.precision
assert v.normalize3().mod() - 1 < m4.precision
// x y z
// 1 2 3 ==> -3 6 -3 0
// 4 5 6
// println(m4.Vec4{[f32(1),2,3,2]!} % m4.Vec4{[f32(4),5,6,2]!})
assert m4.Vec4{[f32(1), 2, 3, 0]!} % m4.Vec4{[f32(4), 5, 6, 0]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
assert m4.Vec4{[f32(1), 2, 3, 13]!} % m4.Vec4{[f32(4), 5, 6, 11]!} == m4.Vec4{[ f32(-3), 6, -3, 0, ]!}
// Rotation
// println("*** Rotation ****")
rotx := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(1.0), 0, 0, 0]! }).clean()
roty := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 1.0, 0, 0]! }).clean()
rotz := m4.rotate(m4.rad(-90), m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean()
// println( rotx )
// println( roty )
// println( rotz )
// println( m4.mul_vec(rotx, m4.Vec4{e:[f32(0),0,1,0]!}).clean())
assert m4.mul_vec(roty, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), 0.0, 1, 0]! }
assert m4.mul_vec(rotz, m4.Vec4{ e: [f32(1.0), 0.0, 0, 0]! }).clean() == m4.Vec4{ e: [f32(0), -1, 0, 0]! }
assert m4.mul_vec(rotx, m4.Vec4{ e: [f32(0), 0, 1, 0]! }).clean() == m4.Vec4{ e: [f32(0), 1, 0, 0]! }
// println("****************")
}

624
vlib/gg/m4/matrix.v Normal file
View File

@ -0,0 +1,624 @@
/**********************************************************************
*
* Simply vector/matrix utility
*
* 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 m4
import math
pub union Mat4 {
pub mut:
e [16]f32
f [4][4]f32
}
pub const precision = f32(10e-7)
// default precision for the module
/*********************************************************************
*
* Utility
*
*********************************************************************/
fn abs(a f32) f32 {
if a >= f32(0.0) {
return a
} else {
return -a
}
}
// String representation of the matrix
pub fn (x Mat4) str() string {
unsafe {
return '|${x.e[0]:-6.3},${x.e[1]:-6.3},${x.e[2]:-6.3},${x.e[3]:-6.3}|\n' +
'|${x.e[4]:-6.3},${x.e[5]:-6.3},${x.e[6]:-6.3},${x.e[7]:-6.3}|\n' +
'|${x.e[8]:-6.3},${x.e[9]:-6.3},${x.e[10]:-6.3},${x.e[11]:-6.3}|\n' +
'|${x.e[12]:-6.3},${x.e[13]:-6.3},${x.e[14]:-6.3},${x.e[15]:-6.3}|\n'
}
}
// Remove all the raw zeros
[direct_array_access]
pub fn (a Mat4) clean() Mat4 {
unsafe {
x := Mat4{}
for c, value in a.e {
if abs(value) < m4.precision {
x.e[c] = 0
} else {
x.e[c] = value
}
}
return x
}
}
// Sum all the elements of the matrix
[direct_array_access]
pub fn (x Mat4) sum_all() f32 {
unsafe {
res := f32(0)
for v in x.e {
res += v
}
return res
}
}
// Check if two matrix are equal using module precision
[direct_array_access]
pub fn (x Mat4) is_equal(y Mat4) bool {
unsafe {
for c, value in x.e {
if abs(value - y.e[c]) > m4.precision {
return false
}
}
return true
}
}
//-------------------------------------
// Set/Get values
//-------------------------------------
// Get an element of the matrix using [0..15] indexes, one dimension
pub fn (x Mat4) get_e(elem_index int) f32 {
unsafe {
return x.e[elem_index]
}
}
// Get an element of the matrix using [0..3][0..3] indexes, two dimension
pub fn (x Mat4) get_f(index_col int, index_row int) f32 {
unsafe {
return x.e[(index_row << 2) + index_col]
}
}
// Set an element of the matrix using [0..15] indexes, one dimension
pub fn (mut x Mat4) set_e(index int, value f32) {
unsafe {
x.e[index] = value
}
}
// Set an element of the matrix using [0..3][0..3] indexes, two dimension
pub fn (mut x Mat4) set_f(index_col int, index_row int, value f32) {
unsafe {
x.e[(index_row << 2) + index_col] = value
}
}
// Copy a matrix elements from another matrix
[direct_array_access]
pub fn (mut x Mat4) copy(y Mat4) {
unsafe {
x.e = [
y.e[0], y.e[1], y.e[2], y.e[3],
y.e[4], y.e[5], y.e[6], y.e[7],
y.e[8], y.e[9], y.e[10], y.e[11],
y.e[12], y.e[13], y.e[14], y.e[15],
]!
}
}
// Set the trace of the matrix using a vec4
[direct_array_access]
pub fn (mut x Mat4) set_trace(v3 Vec4) {
unsafe {
x.e[0] = v3.e[0]
x.e[5] = v3.e[1]
x.e[10] = v3.e[2]
x.e[15] = v3.e[3]
}
}
// Get the trace of the matrix
[direct_array_access]
pub fn (x Mat4) get_trace() Vec4 {
unsafe {
return Vec4{ e: [ x.e[0], x.e[5], x.e[10], x.e[15], ]! }
}
}
// Set all the matrix elements to value
[direct_array_access]
pub fn (mut x Mat4) set_f32(value f32) {
unsafe {
x.e = [
value, value, value, value,
value, value, value, value,
value, value, value, value,
value, value, value, value,
]!
}
}
//-------------------------------------
// Rows/Column access
//-------------------------------------
// Set the row as the input vec4
[direct_array_access]
pub fn (mut x Mat4) set_row(row int, v3 Vec4) {
unsafe {
x.e[row * 4] = v3.e[0]
x.e[row * 4 + 1] = v3.e[1]
x.e[row * 4 + 2] = v3.e[2]
x.e[row * 4 + 3] = v3.e[3]
}
}
// Get a row from a matrix
[direct_array_access]
pub fn (x Mat4) get_row(row int) Vec4 {
unsafe {
return Vec4{
e: [
x.e[row * 4],
x.e[row * 4 + 1],
x.e[row * 4 + 2],
x.e[row * 4 + 3],
]!
}
}
}
// Set the column as the input vec4
[direct_array_access]
pub fn (mut x Mat4) set_col(col int, v3 Vec4) {
unsafe {
x.e[col] = v3.e[0]
x.e[col + 4] = v3.e[1]
x.e[col + 8] = v3.e[2]
x.e[col + 12] = v3.e[3]
}
}
// Get a column from a matrix
[direct_array_access]
pub fn (x Mat4) get_col(col int) Vec4 {
unsafe {
return Vec4{
e: [
x.e[col],
x.e[col + 4],
x.e[col + 8],
x.e[col + 12],
]!
}
}
}
// Swap two columns in the matrix
[direct_array_access]
pub fn (mut x Mat4) swap_col(col1 int, col2 int) {
unsafe {
v0 := x.e[col1]
v1 := x.e[col1 + 4]
v2 := x.e[col1 + 8]
v3 := x.e[col1 + 12]
x.e[col1] = x.e[col2]
x.e[col1 + 4] = x.e[col2 + 4]
x.e[col1 + 8] = x.e[col2 + 8]
x.e[col1 + 12] = x.e[col2 + 12]
x.e[col2] = v0
x.e[col2 + 4] = v1
x.e[col2 + 8] = v2
x.e[col2 + 12] = v3
}
}
// Swap two rows in the matrix
[direct_array_access]
pub fn (mut x Mat4) swap_row(row1 int, row2 int) {
unsafe {
v0 := x.e[row1 * 4]
v1 := x.e[row1 * 4 + 1]
v2 := x.e[row1 * 4 + 2]
v3 := x.e[row1 * 4 + 3]
x.e[row1 * 4] = x.e[row2 * 4]
x.e[row1 * 4 + 1] = x.e[row2 * 4 + 1]
x.e[row1 * 4 + 2] = x.e[row2 * 4 + 2]
x.e[row1 * 4 + 3] = x.e[row2 * 4 + 3]
x.e[row2 * 4] = v0
x.e[row2 * 4 + 1] = v1
x.e[row2 * 4 + 2] = v2
x.e[row2 * 4 + 3] = v3
}
}
//-------------------------------------
// Modify data
//-------------------------------------
// Transpose the matrix
[direct_array_access]
pub fn (x Mat4) transpose() Mat4 {
unsafe {
return Mat4{ e: [
x.e[0], x.e[4], x.e[8], x.e[12],
x.e[1], x.e[5], x.e[9], x.e[13],
x.e[2], x.e[6], x.e[10], x.e[14],
x.e[3], x.e[7], x.e[11], x.e[15],
]!
}
}
}
// Multiply the all the elements of the matrix by a scalar
[direct_array_access]
pub fn (x Mat4) mul_scalar(s f32) Mat4 {
unsafe {
return Mat4{ e: [
x.e[0] * s, x.e[1] * s, x.e[2] * s, x.e[3] * s,
x.e[4] * s, x.e[5] * s, x.e[6] * s, x.e[7] * s,
x.e[8] * s, x.e[9] * s, x.e[10] * s, x.e[11] * s,
x.e[12] * s, x.e[13] * s, x.e[14] * s, x.e[15] * s,
]!
}
}
}
/*********************************************************************
*
* Init/set
*
*********************************************************************/
// Return a zero matrix
[direct_array_access]
pub fn zero_m4() Mat4 {
unsafe {
return Mat4{ e: [
f32(0), 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
]!
}
}
}
// Return a unity matrix
[direct_array_access]
pub fn unit_m4() Mat4 {
unsafe {
return Mat4{ e: [
f32(1), 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]!
}
}
}
// Return a matrix initialized with value
[direct_array_access]
pub fn set_m4(value f32) Mat4 {
return Mat4{ e: [
value, value, value, value,
value, value, value, value,
value, value, value, value,
value, value, value, value,
]!
}
}
/*********************************************************************
*
* Math
*
*********************************************************************/
// Sum of matrix, operator +
[direct_array_access]
pub fn (a Mat4) + (b Mat4) Mat4 {
unsafe {
return Mat4{ e: [
a.e[0] + b.e[0], a.e[1] + b.e[1], a.e[2] + b.e[2], a.e[3] + b.e[3],
a.e[4] + b.e[4], a.e[5] + b.e[5], a.e[6] + b.e[6], a.e[7] + b.e[7],
a.e[8] + b.e[8], a.e[9] + b.e[9], a.e[10] + b.e[10], a.e[11] + b.e[11],
a.e[12] + b.e[12], a.e[13] + b.e[13], a.e[14] + b.e[14], a.e[15] + b.e[15],
]!
}
}
}
// Subtraction of matrix, operator -
[direct_array_access]
pub fn (a Mat4) - (b Mat4) Mat4 {
unsafe {
return Mat4{ e: [
a.e[0] - b.e[0], a.e[1] - b.e[1], a.e[2] - b.e[2], a.e[3] - b.e[3],
a.e[4] - b.e[4], a.e[5] - b.e[5], a.e[6] - b.e[6], a.e[7] - b.e[7],
a.e[8] - b.e[8], a.e[9] - b.e[9], a.e[10] - b.e[10], a.e[11] - b.e[11],
a.e[12] - b.e[12], a.e[13] - b.e[13], a.e[14] - b.e[14], a.e[15] - b.e[15],
]!
}
}
}
// Multiplication of matrix, operator *
[direct_array_access]
pub fn (a Mat4) * (b Mat4) Mat4 {
unsafe {
return Mat4{
e: [
/* [0][0] */ a.f[0][0] * b.f[0][0] + a.f[0][1] * b.f[1][0] + a.f[0][2] * b.f[2][0] + a.f[0][3] * b.f[3][0]
/* [0][1] */, a.f[0][0] * b.f[0][1] + a.f[0][1] * b.f[1][1] + a.f[0][2] * b.f[2][1] + a.f[0][3] * b.f[3][1]
/* [0][2] */, a.f[0][0] * b.f[0][2] + a.f[0][1] * b.f[1][2] + a.f[0][2] * b.f[2][2] + a.f[0][3] * b.f[3][2]
/* [0][3] */, a.f[0][0] * b.f[0][3] + a.f[0][1] * b.f[1][3] + a.f[0][2] * b.f[2][3] + a.f[0][3] * b.f[3][3]
/* [1][0] */, a.f[1][0] * b.f[0][0] + a.f[1][1] * b.f[1][0] + a.f[1][2] * b.f[2][0] + a.f[1][3] * b.f[3][0]
/* [1][1] */, a.f[1][0] * b.f[0][1] + a.f[1][1] * b.f[1][1] + a.f[1][2] * b.f[2][1] + a.f[1][3] * b.f[3][1]
/* [1][2] */, a.f[1][0] * b.f[0][2] + a.f[1][1] * b.f[1][2] + a.f[1][2] * b.f[2][2] + a.f[1][3] * b.f[3][2]
/* [1][3] */, a.f[1][0] * b.f[0][3] + a.f[1][1] * b.f[1][3] + a.f[1][2] * b.f[2][3] + a.f[1][3] * b.f[3][3]
/* [2][0] */, a.f[2][0] * b.f[0][0] + a.f[2][1] * b.f[1][0] + a.f[2][2] * b.f[2][0] + a.f[2][3] * b.f[3][0]
/* [2][1] */, a.f[2][0] * b.f[0][1] + a.f[2][1] * b.f[1][1] + a.f[2][2] * b.f[2][1] + a.f[2][3] * b.f[3][1]
/* [2][2] */, a.f[2][0] * b.f[0][2] + a.f[2][1] * b.f[1][2] + a.f[2][2] * b.f[2][2] + a.f[2][3] * b.f[3][2]
/* [2][3] */, a.f[2][0] * b.f[0][3] + a.f[2][1] * b.f[1][3] + a.f[2][2] * b.f[2][3] + a.f[2][3] * b.f[3][3]
/* [3][0] */, a.f[3][0] * b.f[0][0] + a.f[3][1] * b.f[1][0] + a.f[3][2] * b.f[2][0] + a.f[3][3] * b.f[3][0]
/* [3][1] */, a.f[3][0] * b.f[0][1] + a.f[3][1] * b.f[1][1] + a.f[3][2] * b.f[2][1] + a.f[3][3] * b.f[3][1]
/* [3][2] */, a.f[3][0] * b.f[0][2] + a.f[3][1] * b.f[1][2] + a.f[3][2] * b.f[2][2] + a.f[3][3] * b.f[3][2]
/* [3][3] */, a.f[3][0] * b.f[0][3] + a.f[3][1] * b.f[1][3] + a.f[3][2] * b.f[2][3] + a.f[3][3] * b.f[3][3],
]!
}
}
}
// Sum of matrix function
pub fn add(a Mat4, b Mat4) Mat4 {
unsafe {
return a + b
}
}
// Subtraction of matrix function
pub fn sub(a Mat4, b Mat4) Mat4 {
unsafe {
return a - b
}
}
// Multiplication of matrix function
pub fn mul(a Mat4, b Mat4) Mat4 {
unsafe {
return a * b
}
}
// Multiply a Matrix by a vector
[direct_array_access]
pub fn mul_vec(a Mat4, v Vec4) Vec4 {
unsafe {
return Vec4{ e: [
a.e[0] * v.e[0] + a.e[4] * v.e[1] + a.e[8] * v.e[2] + a.e[12] * v.e[3],
a.e[1] * v.e[0] + a.e[5] * v.e[1] + a.e[9] * v.e[2] + a.e[13] * v.e[3],
a.e[2] * v.e[0] + a.e[6] * v.e[1] + a.e[10] * v.e[2] + a.e[14] * v.e[3],
a.e[3] * v.e[0] + a.e[7] * v.e[1] + a.e[11] * v.e[2] + a.e[15] * v.e[3],
]!
}
}
}
// Calculate the determinant of the Matrix
[direct_array_access]
pub fn det(x Mat4) f32 {
unsafe {
mut t := [6]f32{}
x00 := x.f[0][0]
x10 := x.f[1][0]
x20 := x.f[2][0]
x30 := x.f[3][0]
x01 := x.f[0][1]
x11 := x.f[1][1]
x21 := x.f[2][1]
x31 := x.f[3][1]
x02 := x.f[0][2]
x12 := x.f[1][2]
x22 := x.f[2][2]
x32 := x.f[3][2]
x03 := x.f[0][3]
x13 := x.f[1][3]
x23 := x.f[2][3]
x33 := x.f[3][3]
t[0] = x22 * x33 - x23 * x32
t[1] = x12 * x33 - x13 * x32
t[2] = x12 * x23 - x13 * x22
t[3] = x02 * x33 - x03 * x32
t[4] = x02 * x23 - x03 * x22
t[5] = x02 * x13 - x03 * x12
return 0.0 +
x00 * (x11 * t[0] - x21 * t[1] + x31 * t[2]) -
x10 * (x01 * t[0] - x21 * t[3] + x31 * t[4]) +
x20 * (x01 * t[1] - x11 * t[3] + x31 * t[5]) -
x30 * (x01 * t[2] - x11 * t[4] + x21 * t[5])
}
}
// Calculate the inverse of the Matrix
[direct_array_access]
pub fn (x Mat4) inverse() Mat4 {
unsafe {
mut t := [6]f32{}
mut det := f32(0)
a := x.f[0][0]
b := x.f[1][0]
c := x.f[2][0]
d := x.f[3][0]
e := x.f[0][1]
f := x.f[1][1]
g := x.f[2][1]
h := x.f[3][1]
i := x.f[0][2]
j := x.f[1][2]
k := x.f[2][2]
l := x.f[3][2]
m := x.f[0][3]
n := x.f[1][3]
o := x.f[2][3]
p := x.f[3][3]
t[0] = k * p - o * l
t[1] = j * p - n * l
t[2] = j * o - n * k
t[3] = i * p - m * l
t[4] = i * o - m * k
t[5] = i * n - m * j
mut dest := Mat4{}
dest.f[0][0] = f * t[0] - g * t[1] + h * t[2]
dest.f[0][1] = -(e * t[0] - g * t[3] + h * t[4])
dest.f[0][2] = e * t[1] - f * t[3] + h * t[5]
dest.f[0][3] = -(e * t[2] - f * t[4] + g * t[5])
dest.f[1][0] = -(b * t[0] - c * t[1] + d * t[2])
dest.f[1][1] = a * t[0] - c * t[3] + d * t[4]
dest.f[1][2] = -(a * t[1] - b * t[3] + d * t[5])
dest.f[1][3] = a * t[2] - b * t[4] + c * t[5]
t[0] = g * p - o * h
t[1] = f * p - n * h
t[2] = f * o - n * g
t[3] = e * p - m * h
t[4] = e * o - m * g
t[5] = e * n - m * f
dest.f[2][0] = b * t[0] - c * t[1] + d * t[2]
dest.f[2][1] = -(a * t[0] - c * t[3] + d * t[4])
dest.f[2][2] = a * t[1] - b * t[3] + d * t[5]
dest.f[2][3] = -(a * t[2] - b * t[4] + c * t[5])
t[0] = g * l - k * h
t[1] = f * l - j * h
t[2] = f * k - j * g
t[3] = e * l - i * h
t[4] = e * k - i * g
t[5] = e * j - i * f
dest.f[3][0] = -(b * t[0] - c * t[1] + d * t[2])
dest.f[3][1] = a * t[0] - c * t[3] + d * t[4]
dest.f[3][2] = -(a * t[1] - b * t[3] + d * t[5])
dest.f[3][3] = a * t[2] - b * t[4] + c * t[5]
tmp := (a * dest.f[0][0] + b * dest.f[0][1] + c * dest.f[0][2] + d * dest.f[0][3])
if tmp != 0 {
det = f32(1.0) / tmp
}
return dest.mul_scalar(det)
}
}
/*********************************************************************
*
* Transformations
*
*********************************************************************/
// Get a rotation matrix using w as rotation axis vector, the angle is in radians
[direct_array_access]
pub fn rotate(angle f32, w Vec4) Mat4 {
unsafe {
cs := f32(math.cos(angle))
sn := f32(math.sin(angle))
cv := f32(1.0) - cs
axis := w.normalize3()
ax := axis.e[0]
ay := axis.e[1]
az := axis.e[2]
return Mat4{ e: [
/* [0][0] */ (ax * ax * cv) + cs
/* [0][1] */, (ax * ay * cv) + az * sn
/* [0][2] */, (ax * az * cv) - ay * sn
/* [0][3] */, 0
/* [1][0] */, (ay * ax * cv) - az * sn
/* [1][1] */, (ay * ay * cv) + cs
/* [1][2] */, (ay * az * cv) + ax * sn
/* [1][3] */, 0
/* [2][0] */, (az * ax * cv) + ay * sn
/* [2][1] */, (az * ay * cv) - ax * sn
/* [2][2] */, (az * az * cv) + cs
/* [2][3] */, 0
/* [3][0] */, 0
/* [3][1] */, 0
/* [3][2] */, 0
/* [3][3] */, 1,
]!
}
}
}
/*********************************************************************
*
* Graphic
*
*********************************************************************/
// Get a matrix translated by a vector w
[direct_array_access]
pub fn (x Mat4) translate(w Vec4) Mat4 {
unsafe {
return Mat4{ e: [
x.e[0], x.e[1], x.e[2], x.e[3] + w.e[0],
x.e[4], x.e[5], x.e[6], x.e[7] + w.e[1],
x.e[8], x.e[9], x.e[10], x.e[11] + w.e[2],
x.e[12], x.e[13], x.e[14], x.e[15],
]!
}
}
}
// Get a scale matrix, the scale vector is w, only xyz are evaluated.
[direct_array_access]
pub fn scale(w Vec4) Mat4 {
unsafe {
return Mat4{ e: [
w.e[0], 0, 0, 0,
0, w.e[1], 0, 0,
0, 0, w.e[2], 0,
0, 0, 0, 1,
]!
}
}
}

240
vlib/gg/m4/vector.v Normal file
View File

@ -0,0 +1,240 @@
/**********************************************************************
*
* Simply vector/matrix utility
*
* 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 m4
import math
pub struct Vec4 {
pub mut:
e [4]f32
}
/*********************************************************************
*
* Utility
*
*********************************************************************/
pub fn (x Vec4) str() string {
return '|${x.e[0]:-6.3},${x.e[1]:-6.3},${x.e[2]:-6.3},${x.e[3]:-6.3}|\n'
}
// Remove all the raw zeros
[direct_array_access]
pub fn (a Vec4) clean() Vec4 {
unsafe {
x := Vec4{}
for c, value in a.e {
if abs(value) < precision {
x.e[c] = 0
} else {
x.e[c] = value
}
}
return x
}
}
// Set all elements to value
[direct_array_access]
pub fn (mut x Vec4) copy(value f32) {
x.e = [ value, value, value, value, ]!
}
// Scale the vector using a scalar
[direct_array_access]
pub fn (x Vec4) mul_scalar(value f32) Vec4 {
return Vec4{ e: [ x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value, ]! }
}
// Reciprocal of the vector
[direct_array_access]
pub fn (x Vec4) inv() Vec4 {
return Vec4{
e: [
if x.e[0] != 0 { 1.0 / x.e[0] } else { f32(0) },
if x.e[1] != 0 { 1.0 / x.e[1] } else { f32(0) },
if x.e[2] != 0 { 1.0 / x.e[2] } else { f32(0) },
if x.e[3] != 0 { 1.0 / x.e[3] } else { f32(0) },
]!
}
}
// Normalize the vector
[direct_array_access]
pub fn (x Vec4) normalize() Vec4 {
m := x.mod()
if m == 0 {
return zero_v4()
}
return Vec4{
e: [
x.e[0] * (1 / m),
x.e[1] * (1 / m),
x.e[2] * (1 / m),
x.e[3] * (1 / m),
]!
}
}
// Normalize only xyz, w set to 0
[direct_array_access]
pub fn (x Vec4) normalize3() Vec4 {
m := x.mod3()
if m == 0 {
return zero_v4()
}
return Vec4{
e: [
x.e[0] * (1 / m),
x.e[1] * (1 / m),
x.e[2] * (1 / m),
0,
]!
}
}
// Module of the vector xyzw
[direct_array_access]
pub fn (x Vec4) mod() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2] + x.e[3] * x.e[3]))
}
// Module for 3d vector xyz, w ignored
[direct_array_access]
pub fn (x Vec4) mod3() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2]))
}
/*********************************************************************
*
* Math
*
*********************************************************************/
// Return a zero vector
[direct_array_access]
pub fn zero_v4() Vec4 {
return Vec4{
e: [
f32(0),
0,
0,
0,
]!
}
}
// Return all one vector
[direct_array_access]
pub fn one_v4() Vec4 {
return Vec4{
e: [
f32(1),
1,
1,
1,
]!
}
}
// Return a blank vector
[direct_array_access]
pub fn blank_v4() Vec4 {
return Vec4{
e: [
f32(0),
0,
0,
1,
]!
}
}
// Set all elements to value
[direct_array_access]
pub fn set_v4(value f32) Vec4 {
return Vec4{
e: [
value,
value,
value,
value,
]!
}
}
// Sum of all the elements
[direct_array_access]
pub fn (x Vec4) sum() f32 {
return x.e[0] + x.e[1] + x.e[2] + x.e[3]
}
/*********************************************************************
*
* Operators
*
*********************************************************************/
// Addition
[direct_array_access]
pub fn (a Vec4) + (b Vec4) Vec4 {
return Vec4{
e: [
a.e[0] + b.e[0],
a.e[1] + b.e[1],
a.e[2] + b.e[2],
a.e[3] + b.e[3],
]!
}
}
// Subtraction
[direct_array_access]
pub fn (a Vec4) - (b Vec4) Vec4 {
return Vec4{
e: [
a.e[0] - b.e[0],
a.e[1] - b.e[1],
a.e[2] - b.e[2],
a.e[3] - b.e[3],
]!
}
}
// Dot product
[direct_array_access]
pub fn (a Vec4) * (b Vec4) f32 {
return a.e[0] * b.e[0] + a.e[1] * b.e[1] + a.e[2] * b.e[2] + a.e[3] * b.e[3]
}
// Cross product
[direct_array_access]
pub fn (a Vec4) % (b Vec4) Vec4 {
return Vec4{
e: [
(a.e[1] * b.e[2]) - (a.e[2] * b.e[1]),
(a.e[2] * b.e[0]) - (a.e[0] * b.e[2]),
(a.e[0] * b.e[1]) - (a.e[1] * b.e[0]),
0,
]!
}
}
// Components multiplication
[direct_array_access]
pub fn (x Vec4) mul_vec4(y Vec4) Vec4 {
return Vec4{
e: [
x.e[0] * y.e[0],
x.e[1] * y.e[1],
x.e[2] * y.e[2],
x.e[3] * y.e[3],
]!
}
}