Skip to content

Instantly share code, notes, and snippets.

@borisbat
borisbat / gist:5811081
Created June 19, 2013 01:43
class name
template <typename ObjectType>
string TYPE_NAME()
{
// determine type name from typeid
// note - if this ever failes, we can always go constexpr route, i.e
// constexpr static const char * TYPE_NAME() { return "ClassNameHere"; };
// and maybe even try to parse __FUNCTION__ inside of it somehow
regex REG_class_name ( "N\\d*war\\d*(.+)E" );
smatch what;
if ( !regex_match( string(typeid(ObjectType).name()), what, REG_class_name) )
@borisbat
borisbat / gist:5836110
Last active December 18, 2015 19:49
C++11 STL
#include <iostream>
#include <vector>
#include <functional>
#include <initializer_list>
#include <string>
using namespace std;
template <typename TT>
class RArray : protected vector<TT>
class Report
{
public:
Report() { cout << "Report();" << endl; }
Report(const Report & ) { cout << "Report(const & Report);" << endl; };
Report(Report &&) { cout << "Report(&&);" << endl; }
Report & operator = (const Report &) { cout << "Report::=(const Report &);" << endl; return *this; };
};
template <typename Tc, typename T>
class Report
{
public:
Report() : i_am_dead(false) { cout << " Report();" << endl; }
Report(const Report & ) { cout << " Report(const Report&);" << endl; };
Report(Report && h) { cout << " Report(&&);" << endl; h.i_am_dead = true; }
Report & operator = (const Report&) { cout << " Report::=(const Report&);" << endl; return *this; };
~Report() { cout << " ~Report() i_am_dead=" << i_am_dead << endl; }
bool i_am_dead;
};
@borisbat
borisbat / gist:5890068
Last active December 19, 2015 03:28
Simple storage.
#include <iostream>
#include <vector>
#include <functional>
#include <initializer_list>
#define USE_EMPLACE 1
#define USE_UREF 1
using namespace std;
vec3 finalColor = vec3( 0.0 );
float steps = 0.0;
int initialSteps = max ( 4, g_Steps / 3 );
int maxEstSteps = 32;
float factor = max ( 1.0, length(g_light_color.rgb)/length(vec3(1.0)) );
float tolerance = 1.0 / ( (g_Steps * 4.0 + initialSteps) * factor );
// do initial steps
for ( int i=0; i<initialSteps; ++i )
vec3 finalColor = vec3( 0.0 );
float steps = 0.0;
int initialSteps = max ( 4, g_Steps / 3 );
int maxEstSteps = 32;
float kFactor = 2.0 / g_Steps;
// do initial steps
for ( int i=0; i<initialSteps; ++i )
@borisbat
borisbat / gist:7314207
Created November 5, 2013 05:10
instanceNodes
///////////////////////////////////////////
// parse library_visual_scenes (nodes) here
void importDaeNodeArray ( const pugi::xpath_node_set & nodes, vector<NodePtr> & outNodes, const WeakNodePtr & parent )
{
using namespace pugi;
if ( nodes.size()==0 )
return;
outNodes.resize(nodes.size());
for ( unsigned int ni=0; ni<nodes.size(); ni++ )
@borisbat
borisbat / gist:8308442
Created January 7, 2014 22:54
Tangent space
mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );
// solve the linear system
vec3 dp2perp = cross( dp2, N );
// this will split the work between all available threads
// if chunk_count is -1, default number of chunks will be computed
void JobQue::parallel_for ( int from, int to, const JobChunk & chunk, int chunk_count, int step )
{
if ( from >= to )
return;
// so the idea here is that if say 1 thread is busy
// we don't pay 2x time. smaller chunks are more evenly distributed
if ( chunk_count==-1 )
chunk_count = mThreadCount * 4;