-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.h
156 lines (138 loc) · 5.65 KB
/
template.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Template, major revision 6, update for INFOMOV
// IGAD/NHTV/UU - Jacco Bikker - 2006-2016
#pragma once
#include "math.h"
#include "stdlib.h"
#include "emmintrin.h"
#include "stdio.h"
#include "windows.h"
#include "surface.h"
namespace Tmpl8 {
// vectors
class float2 // adapted from https://github.com/dcow/RayTracer
{
public:
union { struct { float x, y; }; float cell[2]; };
float2() {}
float2( float v ) : x( v ), y( v ) {}
float2( float x, float y ) : x( x ), y( y ) {}
float2 operator - () const { return float2( -x, -y ); }
float2 operator + ( const float2& addOperand ) const { return float2( x + addOperand.x, y + addOperand.y ); }
float2 operator - ( const float2& operand ) const { return float2( x - operand.x, y - operand.y ); }
float2 operator * ( const float2& operand ) const { return float2( x * operand.x, y * operand.y ); }
float2 operator * ( float operand ) const { return float2( x * operand, y * operand ); }
void operator -= ( const float2& a ) { x -= a.x; y -= a.y; }
void operator += ( const float2& a ) { x += a.x; y += a.y; }
void operator *= ( const float2& a ) { x *= a.x; y *= a.y; }
void operator *= ( float a ) { x *= a; y *= a; }
float& operator [] ( const int idx ) { return cell[idx]; }
float length() { return sqrtf( x * x + y * y ); }
float sqlength() { return x * x + y * y; }
float2 normalized() { float r = 1.0f / length(); return float2( x * r, y * r ); }
void normalize() { float r = 1.0f / length(); x *= r; y *= r; }
static float2 normalize( float2 v ) { return v.normalized(); }
float dot( const float2& operand ) const { return x * operand.x + y * operand.y; }
};
class float3
{
public:
union { struct { float x, y, z; }; float cell[3]; };
float3() {}
float3( float v ) : x( v ), y( v ), z( v ) {}
float3( float x, float y, float z ) : x( x ), y( y ), z( z ) {}
float3 operator - () const { return float3( -x, -y, -z ); }
float3 operator + ( const float3& addOperand ) const { return float3( x + addOperand.x, y + addOperand.y, z + addOperand.z ); }
float3 operator - ( const float3& operand ) const { return float3( x - operand.x, y - operand.y, z - operand.z ); }
float3 operator * ( const float3& operand ) const { return float3( x * operand.x, y * operand.y, z * operand.z ); }
void operator -= ( const float3& a ) { x -= a.x; y -= a.y; z -= a.z; }
void operator += ( const float3& a ) { x += a.x; y += a.y; z += a.z; }
void operator *= ( const float3& a ) { x *= a.x; y *= a.y; z *= a.z; }
void operator *= ( const float a ) { x *= a; y *= a; z *= a; }
float operator [] ( const unsigned int& idx ) const { return cell[idx]; }
float& operator [] ( const unsigned int& idx ) { return cell[idx]; }
float length() const { return sqrtf( x * x + y * y + z * z ); }
float sqrLentgh() const { return x * x + y * y + z * z; }
float3 normalized() const { float r = 1.0f / length(); return float3( x * r, y * r, z * r ); }
void normalize() { float r = 1.0f / length(); x *= r; y *= r; z *= r; }
static float3 normalize( const float3 v ) { return v.normalized(); }
float3 cross( const float3& operand ) const
{
return float3( y * operand.z - z * operand.y, z * operand.x - x * operand.z, x * operand.y - y * operand.x );
}
float dot( const float3& operand ) const { return x * operand.x + y * operand.y + z * operand.z; }
};
float3 normalize( const float3& v );
float2 normalize( const float2& v );
float3 cross( const float3& a, const float3& b );
float dot( const float3& a, const float3& b );
float dot( const float2& a, const float2& b );
float3 operator * ( const float& s, const float3& v );
// float2 operator * ( const float& s, const float2& v );
float3 operator * ( const float3& v, const float& s );
float2 operator * ( float2& v, float& s );
float length( const float2& v );
float sqlength(const float2& v);
unsigned int morton(const unsigned int x, const unsigned int y);
unsigned int splitBitwise(const unsigned int x);
};
#include "game.h"
#include <vector>
#include "freeimage.h"
#include "threads.h"
extern "C"
{
#include "glew.h"
}
#include "gl.h"
#include "io.h"
#include <ios>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "SDL.h"
#include "SDL_syswm.h"
#include "wglext.h"
#include "fcntl.h"
using namespace Tmpl8; // to use template classes
using namespace std; // to use stl vectors
inline float Rand( float range ) { return ((float)rand() / RAND_MAX) * range; }
inline int IRand( int range ) { return rand() % range; }
int filesize( FILE* f );
namespace Tmpl8 {
#define PI 3.14159265358979323846264338327950f
#define INVPI 0.31830988618379067153776752674503f
#define MALLOC64(x) _aligned_malloc(x,64)
#define FREE64(x) _aligned_free(x)
#define PREFETCH(x) _mm_prefetch((const char*)(x),_MM_HINT_T0)
#define PREFETCH_ONCE(x) _mm_prefetch((const char*)(x),_MM_HINT_NTA)
#define PREFETCH_WRITE(x) _m_prefetchw((const char*)(x))
#define loadss(mem) _mm_load_ss((const float*const)(mem))
#define broadcastps(ps) _mm_shuffle_ps((ps),(ps), 0)
#define broadcastss(ss) broadcastps(loadss((ss)))
struct Timer
{
typedef long long value_type;
static double inv_freq;
value_type start;
Timer() : start( get() ) { init(); }
float elapsed() const { return (float)((get() - start) * inv_freq); }
static value_type get()
{
LARGE_INTEGER c;
QueryPerformanceCounter( &c );
return c.QuadPart;
}
static double to_time(const value_type vt) { return double(vt) * inv_freq; }
void reset() { start = get(); }
static void init()
{
LARGE_INTEGER f;
QueryPerformanceFrequency( &f );
inv_freq = 1000./double(f.QuadPart);
}
};
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned char byte;
#define BADFLOAT(x) ((*(uint*)&x & 0x7f000000) == 0x7f000000)
}; // namespace Tmpl8