-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility_methods.cpp
67 lines (53 loc) · 1.65 KB
/
utility_methods.cpp
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
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#endif
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h> // This is located in the “GLUT” directory on MacOSX
#endif
#include <ctime>
#include <cstdlib>
#include "utility_methods.h"
#define TWO_PI 6.2831853071795864769252866
//Returns vector with random entries
Vec3Df RandomVector() {
int r1 = rand() % 100000 - 50000;
int r2 = rand() % 100000 - 50000;
int r3 = rand() % 100000 - 50000;
float f1 = (((float) (r1)) / 50000);
float f2 = (((float) (r1)) / 50000);
float f3 = (((float) (r1)) / 50000);
return Vec3Df(f1, f2, f3);
}
//Generate random Gaussian vector
Vec3Df GaussianVector() {
return Vec3Df(generateGaussianNoise(1), generateGaussianNoise(1), generateGaussianNoise(1));
}
//Generates random gaussian numbers
//Based on http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
float generateGaussianNoise(const float &variance) {
static bool hasSpare = false;
static float rand1, rand2;
if (hasSpare) {
hasSpare = false;
return sqrt(variance * rand1) * sin(rand2);
}
hasSpare = true;
rand1 = rand() / ((float) RAND_MAX);
if (rand1 < 1e-100)
rand1 = 1e-100;
rand1 = -2 * log(rand1);
rand2 = (rand() / ((float) RAND_MAX)) * TWO_PI;
return sqrt(variance * rand1) * cos(rand2);
}
//Returns reflection vector for a given point on mesh from a certain starting point
Vec3Df getReflectionVector(const Vec3Df & normal, const Vec3Df & cameraPos, const Vec3Df & vertexPos) {
Vec3Df view = cameraPos - vertexPos;
Vec3Df norm = normal;
view.normalize();
norm.normalize();
float innerDotProduct = Vec3Df::dotProduct(view, norm);
return norm * (2 * innerDotProduct) - view;
}