Skip to content

Instantly share code, notes, and snippets.

View Shtille's full-sized avatar

Vladimir Shtille

  • Moscow
View GitHub Profile
@Shtille
Shtille / box_normal.cpp
Created March 9, 2018 18:21
Box (AABB) normal calculation for ray tracing for GLSL
vec3 BoxNormal(const Box box, const vec3 point)
{
vec3 center = (box.max + box.min) * 0.5;
vec3 size = (box.max - box.min) * 0.5;
vec3 pc = point - center;
// step(edge,x) : x < edge ? 0 : 1
vec3 normal = vec3(0.0);
normal += vec3(sign(pc.x), 0.0, 0.0) * step(abs(abs(pc.x) - size.x), kEpsilon);
normal += vec3(0.0, sign(pc.y), 0.0) * step(abs(abs(pc.y) - size.y), kEpsilon);
normal += vec3(0.0, 0.0, sign(pc.z)) * step(abs(abs(pc.z) - size.z), kEpsilon);
#version 330 core
out vec4 out_color;
in DATA
{
vec2 uv;
} fs_in;
struct Material {
@Shtille
Shtille / heightmap_altitude.cpp
Created October 17, 2018 16:56
Heightmap altitude and normal calculation for custom point
float MercatorNode::GetAltitude(float pos_x, float pos_z, bool accurate)
{
const float * height_data = map_tile_.GetHeightData();
if (height_data)
{
const float kMSM = static_cast<float>(mgn::terrain::GetMapSizeMax());
// Determine point relative position in tile
float tiles_per_side = static_cast<float>(1 << lod_);
float tiles_x = tiles_per_side * (pos_x / kMSM);
@Shtille
Shtille / pool_allocator_test.cpp
Last active September 9, 2019 16:30
Pool allocator with test program
#include <limits>
#include <iostream>
#include <set>
#include <vector>
#include <stddef.h>
#include <assert.h>
class StackLinkedList {
public:
struct Node {
Node* next;
@Shtille
Shtille / corellation.py
Last active March 11, 2020 23:44
Computes corellation koefficient between two arrays of values
import math
def average(x):
n = len(x)
s = 0
for xi in x:
s += xi
s = s / n
return s
@Shtille
Shtille / actionPromise.js
Last active April 20, 2020 13:49
Promise class for JavaScript.
/**
* Module describes classes ActionPromise and ActionPromiseList.
* Exceptionally useful when your executed code has asynchronous dependencies.
* Keeps the same execution order as promises have been added despite the server response.
*
* Here's an example of code:
* @code{.js}
* needAddress = true;
* needGeometry = true;
* requestAddress(function(){
@Shtille
Shtille / main.js
Created May 19, 2020 19:55
Sending analytics information to web server on exit
window.addEventListener('beforeunload', function (e) {
sendAnalyticsData();
});
function sendAnalyticsData() {
if (navigator && navigator.sendBeacon) {
navigator.sendBeacon(url);
} else {
$.ajax({
type: 'POST',
@Shtille
Shtille / download.js
Created June 2, 2020 22:16
Download object as JSON
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
URL.revokeObjectURL(a.href);
}
// Usage
var jsonObject = {
@Shtille
Shtille / downloadCsv.js
Last active June 9, 2020 14:05
Download object array as CSV file
getObjectRows : function(array) {
if (!Array.isArray(array))
return new Array();
if (array.length == 0)
return new Array();
var rows = new Array();
var namesRow = new Array();
var obj = array[0];
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && typeof obj[prop] !== "function" && typeof obj[prop] !== "object") {
@Shtille
Shtille / completer_eample.dart
Created October 9, 2020 17:06
Completer usage example
import 'dart:async';
Future<String> someFutureResult(){
final c = new Completer<String>();
// complete will be called in 3 seconds by the timer.
new Timer(Duration(seconds: 3), () => c.complete("you should see me second"));
return c.future;
}
main(){