Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5.  
  6. #define WIDTH 119
  7. #define HEIGHT 29
  8. #define PIXELASPECT 2.0f
  9.  
  10. struct vec3 {
  11.     float x;
  12.     float y;
  13.     float z;
  14.  
  15.     vec3 operator+(const vec3& other) const {
  16.         return { x + other.x, y + other.y, z + other.z };
  17.     }
  18.  
  19.     vec3 operator-(const vec3& other) const {
  20.         return { x - other.x, y - other.y, z - other.z };
  21.     }
  22.  
  23.     vec3 operator-() const {
  24.         return { -x, -y, -z };
  25.     }
  26.  
  27.     vec3 operator*(const float& scalar) const {
  28.         return { x * scalar, y * scalar, z * scalar };
  29.     }
  30.  
  31.     vec3 operator/(const float& scalar) const {
  32.         return { x / scalar, y / scalar, z / scalar };
  33.     }
  34.  
  35.     float dot(const vec3& other) const {
  36.         return x * other.x + y * other.y + z * other.z;
  37.     }
  38.  
  39.     float len() const {
  40.         return sqrtf(this->dot(*this));
  41.     }
  42.  
  43.     void normalize() {
  44.         float len = this->len();
  45.         x /= len;
  46.         y /= len;
  47.         z /= len;
  48.     }
  49. };
  50.  
  51. // Sphere data
  52. vec3 center{ 0.0f, 0.0f, 0.0f };
  53. float radius = 22.0f;
  54.  
  55. // Light data
  56. vec3 lightpos{ 0.0f, 0.0f, -500.0f };
  57. const char colors[] = { '.', '-', ':', '=', '+', '#', '@' };
  58.  
  59. // Each frame, we move the light around in a circle
  60. void update(unsigned int frame) {
  61.     lightpos.x = sinf((float)frame / 10.0f) * 500.0f;
  62.     lightpos.y = cosf((float)frame / 10.0f) * 500.0f;
  63. }
  64.  
  65. float intersectSphere(vec3 origin, vec3 dir, vec3 center, float radius) {
  66.     vec3 oc = origin - center;
  67.     float a = 1.0f;
  68.     float b = 2.0f * oc.dot(dir);
  69.     float c = oc.dot(oc) - radius * radius;
  70.     float discriminant = b * b - 4 * a * c;
  71.     if (discriminant < 0.0f) {
  72.         return -1.0f;
  73.     }
  74.     return (-b - sqrtf(discriminant)) / (2.0f * a);
  75. }
  76.  
  77. void draw() {
  78.     // Ray direction - all parallel, down Z axis.
  79.     const vec3 dir{ 0.0f, 0.0f, 1.0f };
  80.  
  81.     // For each pixel...
  82.     for (int y = 0; y < HEIGHT; y++) {
  83.         for (int x = 0; x < WIDTH; x++) {
  84.             float xWorld = x - (WIDTH / 2) + 0.5f;
  85.             float yWorld = (y - (HEIGHT / 2) + 0.5f) * PIXELASPECT;
  86.             vec3 origin = vec3{ xWorld, yWorld, -30.0f };
  87.  
  88.             float t = intersectSphere(origin, dir, center, radius);
  89.  
  90.             if (t > 0.0f) { // Ray hit.
  91.                 vec3 intersection = origin + (dir * t);
  92.  
  93.                 vec3 normal = intersection - center;
  94.                 normal.normalize();
  95.  
  96.                 vec3 lightDir = lightpos - intersection;
  97.                 lightDir.normalize();
  98.  
  99.                 float lum = normal.dot(lightDir);
  100.  
  101.                 if (lum < 0) {
  102.                     // Give some brightness even if pointing
  103.                     // completely away from the light.
  104.                     std::cout << colors[0];
  105.                 }
  106.                 else {
  107.                     // Pick character based on light calculation.
  108.                     std::cout << colors[(int)(sizeof(colors) * lum)];
  109.                 }
  110.             }
  111.             else { // Ray miss
  112.                 std::cout << " ";
  113.             }
  114.         }
  115.         std::cout << "\n";
  116.     }
  117. }
  118.  
  119. int main() {
  120.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  121.  
  122.     // Hide cursor
  123.     CONSOLE_CURSOR_INFO info;
  124.     info.dwSize = 100;
  125.     info.bVisible = FALSE;
  126.     SetConsoleCursorInfo(console, &info);
  127.  
  128.     unsigned int frame = 0;
  129.  
  130.     while (true) {
  131.         SetConsoleCursorPosition(console, { 0, 0 });
  132.         update(frame++);
  133.         draw();
  134.         Sleep(16);
  135.     }
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement