Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
h4k1m0u / Wave.vert
Created April 8, 2023 11:37
Moving sinusoidal wave plane with GLSL
uniform float u_time;
void main() {
// animated wave shape using sin function
// x supposed in [0, 1] => 2pi*x in [0, 2pi], add time to shift curve/animate (angle = 2k*pi + t)
// TODO: shading doesn't work, as normals weren't precalculated
const float PI = 3.141592;
vec4 position_wave = v_position;
float angle = (2.0*PI * v_position.x) + u_time;
position_wave.y = sin(angle);
@h4k1m0u
h4k1m0u / Frustum.cpp
Last active March 27, 2023 21:47
Calculate equation of the 6 planes defining the camera frustum
// src/main.cpp
#include <iostream>
#include <glm/gtx/string_cast.hpp>
#include "frustum.hpp"
int main() {
// camera located at origin & looking down negative z-axis
glm::vec3 camera_position(0, 0, 0);
glm::vec3 camera_dir(0, 0, -1);
@h4k1m0u
h4k1m0u / distance_segments.py
Created March 24, 2023 22:17
Calculate the distance between two segments identified by a pair of extremity 2D points
# this script is meant to be run inside Blender
import math
from mathutils import Vector, geometry
def distance_point_to_segment(p, p0, p1):
"""
Project point p on line passing through points p0 and p1
Returns Distance between point p & its projection, if the latter belongs to segment p0-p1,
Infinity otherwise
"""
// file: src/main.js
import fragUrl from '../shaders/shader.frag';
// load file content of fragment shader
const fragContent = await fetch(fragUrl).then(res => res.text());
// attach fragment shader to canvas
const canvas = document.getElementById('canvas');
const sandbox = new GlslCanvas(canvas);
sandbox.load(fragContent);
@h4k1m0u
h4k1m0u / LibCURL-example.c
Created September 23, 2022 21:00
Get webpage content with libCURL & write it to file
// src/main.c
#include <curl/curl.h>
/**
* Get webpage content with libCURL & write it to file
* Inspired by: https://curl.se/libcurl/c/libcurl-tutorial.html
*/
int main() {
// set up program environment (init curl)
curl_global_init(CURL_GLOBAL_ALL);
@h4k1m0u
h4k1m0u / Jest-example.js
Last active September 11, 2022 20:54
Unit tests with Jest and Puppeteer
// file: src/chrome_screenshot.test.js
// grouped tests inside `describe()`
describe('Google', () => {
// run a function before any test
beforeAll(async () => {
await page.goto('https://google.com');
});
// it = test() - resolves() get value of fulfilled promise to compare it
@h4k1m0u
h4k1m0u / TS-example.ts
Last active November 26, 2022 14:51
Basic example for compiling Typescript with Webpack
// file: src/main.ts
import { sum } from './module';
//////////////////////////////////////////////////////
// Interface with an optional field
//////////////////////////////////////////////////////
interface Person {
name: string;
age: number;
@h4k1m0u
h4k1m0u / GTK-example.c
Created September 10, 2022 17:54
Basic example with GTK in C
// file: src/main.c
#include <gtk/gtk.h>
/* Called on button click */
static void on_click(GtkButton* button, gpointer user_data) {
g_print("Button was clicked!\n");
}
/* Called to show the window when app starts */
static void activate(GtkApplication* app, gpointer user_data) {
@h4k1m0u
h4k1m0u / Libuv-example.c
Last active September 4, 2022 15:44
Write string to a text file async using libuv
// file: src/main.c
#include <uv.h>
#include <stdio.h>
// global vars can be accessed from callback
int r;
uv_file handle;
// requests (operations) on files
uv_fs_t req_open;
@h4k1m0u
h4k1m0u / meson-ninja.md
Created August 27, 2022 20:25
Compile & install project managed with

Meson & ninja (its default build system) work the same way as Cmake & make:

$ meson setup <build-dir>        # = `meson <build-dir>` or `cmake ..`
$ meson compile -C <build-dir>   # = `ninja -C <build-dir>` or `make`
$ meson install -C <build-dir>   # = `ninja -C <build-dir> install` or `make install`
$ ninja -C <build-dir> uninstall # if target `uninstall` exists