Skip to content

Instantly share code, notes, and snippets.

#include "camera.hpp"
#include "../input.hpp"
#include "assert.h"
void Camera_View::calc_frustrum () {
// frustrum_corners set to cam space by perspective_matrix() or orthographic_matrix()
for (int i=0; i<8; ++i)
frustrum.corners[i] = cam_to_world * frustrum.corners[i];
struct ParametricOctreeTraverser {
Octree& octree;
// An Efficient Parametric Algorithm for Octree Traversal
// J. Revelles, C.Ure ̃na, M.Lastra
// http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=F6810427A1DC4136D615FBD178C1669C?doi=10.1.1.29.987&rep=rep1&type=pdf
static constexpr int3 child_offset_lut[8] = {
int3(0,0,0),
int3(1,0,0),
uniform isampler1D BlockTileInfo_texture;
uniform sampler2DArray tile_textures;
// BlockTileInfo format:
/*
struct BlockTileInfo {
int base_index;
// side is always at base_index
#define _CRT_SECURE_NO_WARNINGS
// img loading
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_BMP 1
#define STBI_ONLY_PNG 1
#define STBI_ONLY_TGA 1
#define STBI_ONLY_JPEG 1
#define GLFW_INCLUDE_VULKAN
#include "GLFW/glfw3.h"
#include "kissmath.hpp"
#include "assert.h"
#include <vector>
const bool enable_validation_layers = true;
const std::vector<char const*> validation_layers = {
"VK_LAYER_KHRONOS_validation"
};
// make ray relative to world texture
ray_pos += float(WORLD_SIZE/2 * CHUNK_SIZE);
// flip coordinate space for ray such that ray dir is all positive
// keep track of this flip via flipmask
ivec3 flipmask = mix(ivec3(0), ivec3(-1), lessThan(ray_dir, vec3(0.0)));
ray_pos *= mix(vec3(1), vec3(-1), lessThan(ray_dir, vec3(0.0)));
ray_dir = abs(ray_dir);
bool trace_ray (vec3 pos, vec3 dir, float max_dist, out Hit hit, bool sunray) {
// make ray relative to world texture and bring coordinates into [1.0, 2.0] float space
// for float mantissa optimization (avoid int -> float conversion for projection)
// basially treat float mantissa like integer for stepping but use the whole float for projection calculations
vec3 coord = pos + WORLD_SIZE_HALF;
coord = coord * INV_WORLD_SIZEf + 1.0; // to [1.0, 2.0]
coord = clamp(coord, 1.0, 2.0);
// flip coordinate space such that ray is always positive (simplifies stepping logic)
// keep track of flip via flipmask
// WORLD_SIZE = POT size of 3d texture with voxel data for world eg. #define WORLD_SIZE 512
// OCTREE_MIPS = number of mips of texture
const float WORLD_SIZEf = float(WORLD_SIZE);
const uint ROUNDMASK = -1;
const uint FLIPMASK = WORLD_SIZE-1;
//// pos is relative to world 3d texture
bool trace_ray (vec3 pos, vec3 dir, float max_dist, out Hit hit) {
// flip coordinate space such that ray is always positive (simplifies stepping logic)
// keep track of flip via flipmask
bool3 ray_neg = dir < 0.0;
float3 flipped = ray_neg ? -pos : pos;
int3 flipmask = ray_neg ? -1 : 0;
float3 absDir = abs(dir);
// Blocks of pixels
#define LOCAL_SIZE_X 8
#define LOCAL_SIZE_Y 8
// 12 cones for VCT around a hemisphere
#define LOCAL_SIZE_Z 12
#define NUM_CONES LOCAL_SIZE_Z
struct Geometry {