AGL
A graphics library
aglm.h
Go to the documentation of this file.
1 // Copyright 2020, Savvy Sine, alinen
2 
3 #ifndef AGL_AGLM_H_
4 #define AGL_AGLM_H_
5 
6 #include <stdio.h>
7 #include <iostream>
8 #include <limits>
9 #include <memory>
10 #include <random>
11 #include <cmath>
12 #include <glm/gtc/matrix_transform.hpp>
13 #include <glm/gtx/norm.hpp>
14 #include <glm/gtx/quaternion.hpp>
15 #include <glm/gtc/epsilon.hpp>
16 
31 extern std::ostream& operator<<(std::ostream& o, const glm::mat4& m);
32 
40 extern std::ostream& operator<<(std::ostream& o, const glm::mat3& m);
41 
49 extern std::ostream& operator<<(std::ostream& o, const glm::vec3& v);
50 
58 extern std::ostream& operator<<(std::ostream& o, const glm::vec4& v);
59 
67 extern std::ostream& operator<<(std::ostream& o, const glm::vec2& v);
68 
77 extern std::ostream& operator<<(std::ostream& o, const glm::quat& v);
78 
79 namespace glm {
80  using point3 = glm::vec3;
81  using color = glm::vec3;
82 }
83 
84 const float kPI = glm::pi<float>();
85 const float kINFINITY = std::numeric_limits<float>::infinity();
86 
87 namespace agl {
88 
92 inline float random() {
93  static std::uniform_real_distribution<float> distribution(0.0f, 1.0f);
94  static std::mt19937 generator;
95  return distribution(generator);
96 }
97 
101 inline float random(float min, float max) {
102  static std::uniform_real_distribution<float> distribution(min, max);
103  static std::mt19937 generator;
104  return distribution(generator);
105 }
106 
110 inline glm::vec3 randomUnitCube() {
111  float x = random(-0.5, 0.5);
112  float y = random(-0.5, 0.5);
113  float z = random(-0.5, 0.5);
114  return glm::vec3(x, y, z);
115 }
116 
120 inline glm::vec3 randomUnitSquare() {
121  float x = random(-0.5, 0.5);
122  float y = random(-0.5, 0.5);
123  return glm::vec3(x, y, 0);
124 }
125 
126 
130 inline glm::vec3 randomUnitSphere() {
131  glm::vec3 p = randomUnitCube();
132  while (glm::length(p) >= 1.0f) {
133  p = randomUnitCube();
134  }
135  return p;
136 }
137 
141 inline glm::vec3 randomUnitDisk() {
142  glm::vec3 p = randomUnitSquare();
143  while (glm::length(p) >= 1.0f) {
144  p = randomUnitSquare();
145  }
146  return p;
147 }
148 
154 inline glm::vec3 randomHemisphere(const glm::vec3& normal) {
155  glm::vec3 in_unit_sphere = randomUnitSphere();
156 
157  // In the same hemisphere as the normal
158  if (glm::dot(in_unit_sphere, normal) > 0.0f) {
159  return in_unit_sphere;
160  } else {
161  return -in_unit_sphere;
162  }
163 }
164 
170 inline glm::vec3 randomUnitVector() {
171  return glm::normalize(randomUnitSphere());
172 }
173 
174 } // end namespace agl
175 #endif // AGL_AGLM_H_
agl::randomUnitSquare
glm::vec3 randomUnitSquare()
Return a random direction in a unit square in the XY plane.
Definition: aglm.h:120
agl::randomHemisphere
glm::vec3 randomHemisphere(const glm::vec3 &normal)
Generate random direction in hemisphere around normal.
Definition: aglm.h:154
agl::randomUnitDisk
glm::vec3 randomUnitDisk()
Return a random direction in a unit disk in the XY plane.
Definition: aglm.h:141
agl::randomUnitVector
glm::vec3 randomUnitVector()
Generate random unit vector.
Definition: aglm.h:170
operator<<
std::ostream & operator<<(std::ostream &o, const glm::mat4 &m)
Print a matrix.
Definition: aglm.cpp:9
agl::randomUnitCube
glm::vec3 randomUnitCube()
Return a random direction in a unit cube.
Definition: aglm.h:110
agl::random
float random()
Return a random number between 0 and 1 [0, 1)
Definition: aglm.h:92
agl::randomUnitSphere
glm::vec3 randomUnitSphere()
Return a random direction in a unit sphere.
Definition: aglm.h:130