Skip to content

Instantly share code, notes, and snippets.

@bysreg
bysreg / frustum_aabb_intersection.cpp
Last active December 3, 2017 16:30
frustum-aabb intersection test (the slow way)
bool Intersect(const Frustum& frustum, const AABB& aabb)
{
for (int plane_idx = 0; plane_idx < 6; ++plane_idx)
{
std::uint8_t plane_in = 0;
std::uint8_t plane_out = 0;
for (int x_idx = 0; x_idx < 2; ++x_idx)
{
for (int y_idx = 0; y_idx < 2; ++y_idx)
@bysreg
bysreg / plane_positive_negative_point.cpp
Created December 3, 2017 16:20
Get positive and negative point of AABB relative to a plane (a positive point is defined as a point that is most aligned with plane's normal whereas negative point is vice versa)
Vec3f GetPointPositive(const AABB& a, const Vec3f& v)
{
Vec3f ret(
(v.x > 0 ? a.max.x : a.min.x),
(v.y > 0 ? a.max.y : a.min.y),
(v.z > 0 ? a.max.z : a.min.z)
);
return ret;
}
@bysreg
bysreg / aabbops.cpp
Created September 30, 2017 22:11
AABB Intersection test
bool IsIntersect(const AABB& a, const AABB& b)
{
for(int i=0;i<3;++i)
{
if(a.min[i] <= b.max[i] && a.max[i] <= b.min[i])
return true;
}
return false;
}
@bysreg
bysreg / derived_object_registration.cpp
Created July 2, 2017 02:26
Derived Object Registration (using object_factory.hpp gist)
#include "object_factory.hpp"
class DerivedObject : public Object
{
public:
DerivedObject::DerivedObject()
{
// some code
}
};
@bysreg
bysreg / object_factory.hpp
Last active July 2, 2017 02:23
object_factory
class ObjectFactory
{
public:
using ObjectFactoryFunctionType = Object*(*)();
using FactoryMapType = std::unordered_map<std::string, ObjectFactoryFunctionType>;
FactoryMapType factory_map;
Object* Create(const std::string& type)
@bysreg
bysreg / ray_plane_intersection.cpp
Created June 18, 2017 03:28
ray plane intersection test
struct IntersectResult
{
float t;
bool hit;
IntersectResult() = default;
IntersectResult(bool a_hit, float a_t) : hit(a_hit), t(a_t) {}
};
IntersectResult Intersect(const Ray& ray, const Plane& plane)
@bysreg
bysreg / nextPow2.cpp
Created February 10, 2016 00:34
next power of 2 for integer
static inline int nextPow2(int n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
@bysreg
bysreg / MiniJSON.cs
Last active August 29, 2015 14:14 — forked from darktable/MiniJSON.cs
/*
* Copyright (c) 2013 Calvin Rien
*
* Based on the JSON parser by Patrick van Bergen
* http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
*
* Simplified it so that it doesn't throw exceptions
* and can be used in Unity iPhone with maximum code stripping.
*
* Permission is hereby granted, free of charge, to any person obtaining