Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <sstream>
  6.  
  7. #define STB_IMAGE_IMPLEMENTATION
  8. #include "stb_image.h"
  9.  
  10. #define STBI_MSC_SECURE_CRT
  11. #define STB_IMAGE_WRITE_IMPLEMENTATION
  12. #include "stb_image_write.h"
  13. using namespace std;
  14. struct Vector3
  15. {
  16. float x, y, z;
  17. Vector3() { x = y = z = 0; }
  18. Vector3(float x, float y, float z) : x(x), y(y), z(z) {}
  19.  
  20. inline Vector3& operator=(const Vector3& v)
  21. {
  22. x = v.x;
  23. y = v.y;
  24. z = v.z;
  25. return *this;
  26. }
  27.  
  28. inline bool operator==(const Vector3& v)
  29. {
  30. return (x == v.x && y == v.y && z == v.z);
  31. }
  32.  
  33. inline Vector3 operator+(const Vector3& v)
  34. {
  35. return Vector3(x + v.x, y + v.y, z + v.z);
  36. }
  37.  
  38. inline Vector3 operator-(const Vector3& v)
  39. {
  40. return Vector3(x - v.x, y - v.y, z - v.z);
  41. }
  42.  
  43. inline float dot(const Vector3& v)
  44. {
  45. return (x * v.x) + (y * v.y) + (z * v.z);
  46. }
  47.  
  48. inline Vector3 scalarMultiply(float scalar)
  49. {
  50. return Vector3(x * scalar, y * scalar, z * scalar);
  51. }
  52.  
  53. inline Vector3 crossProduct(const Vector3& v)
  54. {
  55. return Vector3(y*v.z - z * v.y, z*v.x - x * v.z, x*v.y - y * v.x);
  56. }
  57.  
  58. inline float magnitude()
  59. {
  60. return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
  61. }
  62.  
  63. inline Vector3 normalize()
  64. {
  65. Vector3 n = *this;
  66. return n.scalarMultiply(1 / n.magnitude());
  67. }
  68.  
  69. inline string intToString()
  70. {
  71. return "<" + to_string((int)x) + " " + to_string((int)y) + " " + to_string((int)z) + ">" + " ";
  72. }
  73.  
  74. inline string floatToString()
  75. {
  76. return "<" + to_string((float)x) + " " + to_string((float)y) + " " + to_string((float)z) + ">" + " ";
  77. }
  78. };
  79. struct Color
  80. {
  81. uint8_t r, g, b;
  82. Color() { r = g = b = 0; }
  83. Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {}
  84. };
  85. struct Ray
  86. {
  87. Vector3 origin, direction;
  88.  
  89. Ray() : origin(Vector3()), direction(Vector3(0, 0, 1)) {}
  90. Ray(Vector3 origin, Vector3 direction) : origin(origin), direction(direction) {}
  91. };
  92.  
  93. struct Plane
  94. {
  95. Vector3 normal;
  96. float distance;
  97. Vector3 point;
  98. Color color;
  99.  
  100. Plane(Vector3 normal, float distance, Color color) :normal(normal), distance(distance), color(color) {}
  101. bool intersected(Ray &ray, float &t)
  102. {
  103. Vector3 n = normal.normalize();
  104. t = -(ray.origin.dot(n) + distance) / (ray.direction.normalize().dot(n));
  105.  
  106. if (t <= 0)
  107. {
  108. return false;
  109. }
  110. else
  111. {
  112. return true;
  113. }
  114. }
  115. };
  116.  
  117. struct OBB
  118. {
  119.  
  120. Vector3 u;
  121. Vector3 v;
  122. Vector3 w;
  123.  
  124. Vector3 center;
  125.  
  126. Color color;
  127.  
  128. Vector3 sideDirecitons[3] = { u, v, w };
  129. float halfLengths[3];
  130. OBB(Vector3 u, Vector3 v, Vector3 w, Vector3 center, Color color) :u(u), v(v), w(w), center(center), color(color)
  131. {
  132. for(int i = 0; i < 3; i++)
  133. {
  134. sideDirecitons[i] = sideDirecitons[i].normalize();
  135. cout << sideDirecitons[i].intToString();
  136. }
  137.  
  138. for(int i = 0; i < 3; i++)
  139. {
  140. halfLengths[i] = (center - sideDirecitons[i]).magnitude();
  141. }
  142. };
  143. bool intersected(Ray &ray, float &t)
  144. {
  145. float tMin = -INFINITY;
  146. float tMax = INFINITY;
  147.  
  148. Vector3 p = center - ray.origin;
  149.  
  150. for (int i = 0; i < 3; i++)
  151. {
  152. float e = sideDirecitons[i].dot(p);
  153. float f = sideDirecitons[i].dot(ray.direction);
  154. if(abs(f) > FLT_EPSILON)
  155. {
  156. float t1 = (e + halfLengths[i]) / f;
  157. float t2 = (e - halfLengths[i]) / f;
  158. if(t1 > t2)
  159. {
  160. swap(t1, t2);
  161. }
  162. if (t1 > tMin)
  163. {
  164. tMin = t1;
  165. }
  166. if (t2 < tMax)
  167. {
  168. tMax = t2;
  169. }
  170. if (tMin > tMax)
  171. {
  172. return false;
  173. }
  174. if (tMax < 0)
  175. {
  176. return false;
  177. }
  178. }
  179. else if (-e - halfLengths[i] > 0 || -e + halfLengths[i] < 0)
  180. {
  181. return false;
  182. }
  183. }
  184. if (tMin > 0)
  185. {
  186. return true;
  187. }
  188. else
  189. {
  190. return true;
  191. }
  192. }
  193. };
  194.  
  195. struct Triangle
  196. {
  197. Vector3 a, b, c;
  198. Color color;
  199.  
  200. Triangle(Vector3 a, Vector3 b, Vector3 c, Color color) : a(a), b(b), c(c), color(color) {};
  201.  
  202. bool intersected(Ray &ray, float &t, float &u, float &v)
  203. {
  204. Vector3 ba = b - a;
  205. Vector3 ca = c - a;
  206. Vector3 q = ray.direction.crossProduct(ca);
  207. float det = ba.dot(q);
  208. if (det > -FLT_EPSILON && det < FLT_EPSILON)
  209. {
  210. return false;
  211. }
  212. else
  213. {
  214. float f = 1 / det;
  215. Vector3 s = ray.origin - a;
  216. float u = f * (s.dot(q));
  217. if (u < 0.0f)
  218. {
  219. return false;
  220. }
  221. Vector3 r = s.crossProduct(ba);
  222. float v = f * (ray.direction.dot(r));
  223. if (v < 0.0f || u + v > 1.0f)
  224. {
  225. return false;
  226. }
  227. t = f * (ca.dot(r));
  228. return true;
  229. }
  230. }
  231. };
  232.  
  233. struct Sphere
  234. {
  235. Vector3 center;
  236. float radius;
  237. Color color;
  238.  
  239. Sphere(Vector3 center, float radius, Color color) : center(center), radius(radius), color(color) {}
  240.  
  241. bool intersected(Ray &ray, float &t)
  242. {
  243. Vector3 d = ray.direction.normalize();
  244. Vector3 oc = ray.origin - center;
  245. float b = d.dot(oc);
  246. float c = (oc).dot(oc) - pow(radius, 2);
  247. float discriminant = pow(b, 2) - c;
  248. if (discriminant < 0)
  249. {
  250. return false;
  251. }
  252. else
  253. {
  254. discriminant = sqrt(discriminant);
  255. float t1 = -b - discriminant;
  256. float t2 = -b + discriminant;
  257. t = (t1 < t2) ? t1 : t2;
  258. return true;
  259. }
  260. }
  261. };
  262.  
  263. void setPixel(uint8_t* imgPtr, int width, int nrOfChannels, int x, int y, Color &color)
  264. {
  265. int index = (y * width + x) * nrOfChannels;
  266. imgPtr[index] = color.r;
  267. imgPtr[index + 1] = color.g;
  268. imgPtr[index + 2] = color.b;
  269. }
  270.  
  271. int main()
  272. {
  273. const int width = 256;
  274. const int height = 256;
  275. const int nrOfChannels = 3;
  276.  
  277. Sphere sphere(Vector3(width / 2, height / 2, 50), 50, Color(0, 255, 255));
  278.  
  279. Plane plane(Vector3(0, 0, -1), 1, Color(0, 0, 255));
  280.  
  281. Triangle triangle(Vector3(20, 20, 0), Vector3(100, 100, 0), Vector3(150, 50, 0), Color(255, 0, 0));
  282.  
  283. OBB obb(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1), Vector3(20,20,5), Color(0, 255, 0));
  284.  
  285. uint8_t* imageData = new uint8_t[width*height * nrOfChannels];
  286. int index = 0;
  287.  
  288. for (int j = 0; j < height; j++)
  289. {
  290. for (int i = 0; i < width; i++)
  291. {
  292. Color gray(100, 100, 100);
  293. setPixel(imageData, width, nrOfChannels, i, j, gray);
  294. }
  295. }
  296.  
  297. for (int j = 0; j < height; j++)
  298. {
  299. for (int i = 0; i < width; i++)
  300. {
  301. float t, u, v = 0;
  302. float nearest;
  303. Ray ray(Vector3(i, j, 0), Vector3(0, 0, 1));
  304. if (plane.intersected(ray, t))
  305. {
  306. setPixel(imageData, width, nrOfChannels, i, j, plane.color);
  307. }
  308. if (sphere.intersected(ray, t))
  309. {
  310. setPixel(imageData, width, nrOfChannels, i, j, sphere.color);
  311. }
  312. if (triangle.intersected(ray, t, u, v))
  313. {
  314. setPixel(imageData, width, nrOfChannels, i, j, triangle.color);
  315. }
  316. if(obb.intersected(ray,t))
  317. {
  318. setPixel(imageData, width, nrOfChannels, i, j, obb.color);
  319. }
  320. }
  321. }
  322.  
  323. cout << "done" << endl;
  324.  
  325. stbi_write_png("bruh.png", width, height, nrOfChannels, imageData, width * nrOfChannels);
  326.  
  327. delete[] imageData;
  328. getchar();
  329. return 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement