Skip to content

Instantly share code, notes, and snippets.

View JuanDiegoMontoya's full-sized avatar
🧊

Jake Ryan JuanDiegoMontoya

🧊
View GitHub Profile
@JuanDiegoMontoya
JuanDiegoMontoya / License
Created August 6, 2021 23:47
This license applies to all my public gists.
MIT License
Copyright (c) 2021 Jake Ryan
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:
#pragma once
#include <concepts>
#include <utility>
template<std::invocable Fn>
class Defer
{
public:
constexpr Defer(Fn&& f) noexcept : f_(std::move(f)) {}
constexpr Defer(const Fn& f) : f_(f) {}
inline uint64_t xorshf96()
{
static thread_local uint64_t x = 123456789, y = 362436069, z = 521288629;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
const uint64_t t = x;
x = y;
y = z;
@JuanDiegoMontoya
JuanDiegoMontoya / volumetric.glsl
Last active February 4, 2021 02:39
Fragment shader which, given a shadow map, will generate volumetric light shafts in a scene. No optimizations included.
#version 460 core
layout (location = 0) in vec2 vTexCoord; // could be calculated in FS with screen-space coords
layout (location = 0) uniform sampler2D u_hdrBuffer; // final HDR scene image (before tonemapping)
layout (location = 1) uniform sampler2D gDepth; // scene depth buffer from eye PoV
layout (location = 2) uniform sampler2D shadowDepth;
layout (location = 3) uniform mat4 u_invViewProj;
layout (location = 4) uniform mat4 u_lightMatrix;
@JuanDiegoMontoya
JuanDiegoMontoya / Compress.h
Last active October 21, 2020 04:29
Compresses and uncompresses arrays of objects using zlib.
#pragma once
#include <span>
#include <cstddef>
#include <stdint.h>
#include <vector>
#include <zlib.h>
#include <memory>
namespace Compression
{
@JuanDiegoMontoya
JuanDiegoMontoya / StaticBuffer.cpp
Last active October 19, 2020 05:28
Simple abstraction for immutable buffers using DSA in OpenGL 4.5+.
#include <Graphics/GraphicsIncludes.h>
#include <Graphics/StaticBuffer.h>
StaticBuffer::StaticBuffer(const void* data, GLuint size, GLbitfield glflags)
{
glCreateBuffers(1, &rendererID_);
glNamedBufferStorage(rendererID_, size, data, glflags);
}
StaticBuffer::StaticBuffer(const StaticBuffer& other)
@JuanDiegoMontoya
JuanDiegoMontoya / DeltaEncoding.h
Created October 11, 2020 10:26
An implementation of delta encoding for signed integral types in C++20.
#pragma once
#include <span>
#include <vector>
#include <concepts>
namespace Compression
{
template<std::signed_integral T>
std::vector<T> EncodeDelta(std::span<T> array)
{
@JuanDiegoMontoya
JuanDiegoMontoya / RLE.h
Last active October 11, 2020 10:27
An implementation of run-length encoding for arbitrary types in C++20.
#pragma once
#include <span>
#include <vector>
namespace Compression
{
template<typename T>
struct RLEelement
{
uint32_t count{};
@JuanDiegoMontoya
JuanDiegoMontoya / water_frag.glsl
Last active March 3, 2021 01:55
Simple vertex+fragment shader for giving a mesh a water-like material. NOTE: this relies on the mesh being a vertically displaced planar mesh to work! This will not apply ripples, etc. itself!
#version 450
out vec4 FragColor;
in vec3 vPos;
in vec3 vNormal;
in vec2 vTexCoord;
uniform vec3 u_viewpos;
uniform float u_waterRefract; // water
@JuanDiegoMontoya
JuanDiegoMontoya / VoxelLit.cpp
Last active October 11, 2020 10:20
Flood fill (addition) in a voxel world. The code is ugly but should be somewhat self-explanatory.
// ref https://www.seedofandromeda.com/blogs/29-fast-flood-fill-lighting-in-a-blocky-voxel-game-pt-1
// wpos: world position
// nLight: new lighting value
// skipself: chunk updating thing
void ChunkManager::lightPropagateAdd(glm::ivec3 wpos, Light nLight, bool skipself)
{
// get existing light at the position
LightPtr L = GetLightPtr(wpos);
if (L)
{