Skip to content

Instantly share code, notes, and snippets.

@Novum
Novum / gist:1272928
Created October 8, 2011 21:46
world position from depth buffer value
// Reconstruct worldspace depth value from z/w depth buffer
float depth = -(z_near * z_far) / (zbuffer_depth * (z_far - z_near) - z_far);
// Compute screenspace coordinate of pixel
float2 screenspace = (float2(pixel.xy) / float2(viewport_size.xy)) * 2.0f - 1.0f;
// Get direction of ray from camera through pixel
float3 ray_direction = normalize(camera_forward + camera_right * screenspace.x - camera_up * screenspace.y);
// Reconstruct world position from depth: depth in z buffer is distance to picture plane, not camera
@Novum
Novum / gist:1200562
Created September 7, 2011 13:31
Fast SSE pow for range [0, 1]
// Fast SSE pow for range [0, 1]
// Adapted from C. Schlick with one more iteration each for exp(x) and ln(x)
// 8 muls, 5 adds, 1 rcp
inline __m128 _mm_fastpow_0_1_ps(__m128 x, __m128 y)
{
static const __m128 fourOne = _mm_set1_ps(1.0f);
static const __m128 fourHalf = _mm_set1_ps(0.5f);
__m128 a = _mm_sub_ps(fourOne, y);
__m128 b = _mm_sub_ps(x, fourOne);
@Novum
Novum / Matrix.hpp
Created July 31, 2011 17:17
Template Matrix class
// Default type traits for class Matrix
template <class type>
class DefaultMatrixTraits
{
public:
static const type zero() { return (type)0; }
static const type one() { return (type)1; }
static const type two() { return (type)2; }
};
// Tests each segment for a hit. All calculations are done in screen space pixel coordinates
bool CollisionPath::hitTest(const ViewportPosition &selection_position) const
{
const Vector2 &mouse_pixel_pos = selection_position.getPixelPosition();
const Viewport &viewport = selection_position.getViewport();
const Camera &camera = viewport.getCamera();
const WorldLayer &layer = *getLayer();
for(auto iter = anchors.begin(); iter != anchors.end()-1; ++iter) {
const Vector2 &segment_begin =